implement delete_existing for AppendUnique and PrependUnique. Finishes #2091.
authorgaryo <garyo@fdb21ef1-2011-0410-befe-b5e4ea1792b1>
Thu, 2 Oct 2008 02:17:58 +0000 (02:17 +0000)
committergaryo <garyo@fdb21ef1-2011-0410-befe-b5e4ea1792b1>
Thu, 2 Oct 2008 02:17:58 +0000 (02:17 +0000)
git-svn-id: http://scons.tigris.org/svn/scons/trunk@3529 fdb21ef1-2011-0410-befe-b5e4ea1792b1

doc/man/scons.1
src/engine/SCons/Environment.py
src/engine/SCons/EnvironmentTests.py

index f080bd734276a1c7492498cd0d7ff2cacefcfa4f..2d2cc95696e1bfacd1e3513c8dc1362ecaff05ad 100644 (file)
@@ -2732,7 +2732,7 @@ after: /biz:/foo/bar:/foo
 
 '\"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
 .TP
-.RI env.AppendUnique( key = val ", [...])"
+.RI env.AppendUnique( key = val ", [...], delete_existing=0)"
 Appends the specified keyword arguments
 to the end of construction variables in the environment.
 If the Environment does not have
@@ -2743,6 +2743,9 @@ then any value(s) that already exist in the
 construction variable will
 .I not
 be added again to the list.
+However, if delete_existing is 1, 
+existing matching values are removed first, so
+existing values in the arg list move to the end of the list.
 
 Example:
 
@@ -4940,7 +4943,7 @@ after: /foo/bar:/foo:/biz
 
 '\"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
 .TP
-.RI env.PrependUnique( key = val ", [...])"
+.RI env.PrependUnique( key = val ", delete_existing=0, [...])"
 Appends the specified keyword arguments
 to the beginning of construction variables in the environment.
 If the Environment does not have
@@ -4951,6 +4954,9 @@ then any value(s) that already exist in the
 construction variable will
 .I not
 be added again to the list.
+However, if delete_existing is 1, 
+existing matching values are removed first, so
+existing values in the arg list move to the front of the list.
 
 Example:
 
index affe91e7c8879ca03111d5df33df95232364d9ec..5ac10ac124c303fe46f32ca23bb5bff44698ea55 100644 (file)
@@ -1181,9 +1181,11 @@ class Base(SubstitutionEnvironment):
 
         self._dict[envname][name] = nv
 
-    def AppendUnique(self, **kw):
+    def AppendUnique(self, delete_existing=0, **kw):
         """Append values to existing construction variables
         in an Environment, if they're not already there.
+        If delete_existing is 1, removes existing values first, so
+        values move to end.
         """
         kw = copy_non_reserved_keywords(kw)
         for key, val in kw.items():
@@ -1196,17 +1198,26 @@ class Base(SubstitutionEnvironment):
                 dk = self._dict[key]
                 if not SCons.Util.is_List(dk):
                     dk = [dk]
-                val = filter(lambda x, dk=dk: x not in dk, val)
+                if delete_existing:
+                    dk = filter(lambda x, val=val: x not in val, dk)
+                else:
+                    val = filter(lambda x, dk=dk: x not in dk, val)
                 self._dict[key] = dk + val
             else:
                 dk = self._dict[key]
                 if SCons.Util.is_List(dk):
                     # By elimination, val is not a list.  Since dk is a
                     # list, wrap val in a list first.
-                    if not val in dk:
+                    if delete_existing:
+                        dk = filter(lambda x, val=val: x not in val, dk)
                         self._dict[key] = dk + [val]
+                    else:
+                        if not val in dk:
+                            self._dict[key] = dk + [val]
                 else:
-                    self._dict[key] = self._dict[key] + val
+                    if delete_existing:
+                        dk = filter(lambda x, val=val: x not in val, dk)
+                    self._dict[key] = dk + val
         self.scanner_map_delete(kw)
 
     def Clone(self, tools=[], toolpath=None, parse_flags = None, **kw):
@@ -1523,9 +1534,11 @@ class Base(SubstitutionEnvironment):
 
         self._dict[envname][name] = nv
 
-    def PrependUnique(self, **kw):
-        """Append values to existing construction variables
+    def PrependUnique(self, delete_existing=0, **kw):
+        """Prepend values to existing construction variables
         in an Environment, if they're not already there.
+        If delete_existing is 1, removes existing values first, so
+        values move to front.
         """
         kw = copy_non_reserved_keywords(kw)
         for key, val in kw.items():
@@ -1538,16 +1551,25 @@ class Base(SubstitutionEnvironment):
                 dk = self._dict[key]
                 if not SCons.Util.is_List(dk):
                     dk = [dk]
-                val = filter(lambda x, dk=dk: x not in dk, val)
+                if delete_existing:
+                    dk = filter(lambda x, val=val: x not in val, dk)
+                else:
+                    val = filter(lambda x, dk=dk: x not in dk, val)
                 self._dict[key] = val + dk
             else:
                 dk = self._dict[key]
                 if SCons.Util.is_List(dk):
                     # By elimination, val is not a list.  Since dk is a
                     # list, wrap val in a list first.
-                    if not val in dk:
+                    if delete_existing:
+                        dk = filter(lambda x, val=val: x not in val, dk)
                         self._dict[key] = [val] + dk
+                    else:
+                        if not val in dk:
+                            self._dict[key] = [val] + dk
                 else:
+                    if delete_existing:
+                        dk = filter(lambda x, val=val: x not in val, dk)
                     self._dict[key] = val + dk
         self.scanner_map_delete(kw)
 
index 950dd38d21ba8cd67e445b54de4b99e7cddebb40..568f14d1f35d5f2d999e0ad83e4524aaf0db51a4 100644 (file)
@@ -1576,7 +1576,8 @@ def exists(env):
                           BBB4 = ['b4'],
                           BBB5 = ['b5'],
                           CCC1 = '',
-                          CCC2 = '')
+                          CCC2 = '',
+                          DDD1 = ['a', 'b', 'c'])
         env.AppendUnique(AAA1 = 'a1',
                          AAA2 = ['a2'],
                          AAA3 = ['a3', 'b', 'c', 'a3'],
@@ -1588,7 +1589,8 @@ def exists(env):
                          BBB4 = 'b4.new',
                          BBB5 = ['b5.new'],
                          CCC1 = 'c1',
-                         CCC2 = ['c2'])
+                         CCC2 = ['c2'],
+                         DDD1 = 'b')
 
         assert env['AAA1'] == 'a1a1', env['AAA1']
         assert env['AAA2'] == ['a2'], env['AAA2']
@@ -1602,7 +1604,13 @@ def exists(env):
         assert env['BBB5'] == ['b5', 'b5.new'], env['BBB5']
         assert env['CCC1'] == 'c1', env['CCC1']
         assert env['CCC2'] == ['c2'], env['CCC2']
+        assert env['DDD1'] == ['a', 'b', 'c'], env['DDD1']
 
+        env.AppendUnique(DDD1 = 'b', delete_existing=1)
+        assert env['DDD1'] == ['a', 'c', 'b'], env['DDD1'] # b moves to end
+        env.AppendUnique(DDD1 = ['a','b'], delete_existing=1)
+        assert env['DDD1'] == ['c', 'a', 'b'], env['DDD1'] # a & b move to end
+        
         env['CLVar'] = CLVar([])
         env.AppendUnique(CLVar = 'bar')
         result = env['CLVar']
@@ -2224,7 +2232,8 @@ f5: \
                           BBB4 = ['b4'],
                           BBB5 = ['b5'],
                           CCC1 = '',
-                          CCC2 = '')
+                          CCC2 = '',
+                          DDD1 = ['a', 'b', 'c'])
         env.PrependUnique(AAA1 = 'a1',
                           AAA2 = ['a2'],
                           AAA3 = ['a3', 'b', 'c', 'a3'],
@@ -2236,7 +2245,8 @@ f5: \
                           BBB4 = 'b4.new',
                           BBB5 = ['b5.new'],
                           CCC1 = 'c1',
-                          CCC2 = ['c2'])
+                          CCC2 = ['c2'],
+                          DDD1 = 'b')
         assert env['AAA1'] == 'a1a1', env['AAA1']
         assert env['AAA2'] == ['a2'], env['AAA2']
         assert env['AAA3'] == ['b', 'c', 'a3'], env['AAA3']
@@ -2249,6 +2259,13 @@ f5: \
         assert env['BBB5'] == ['b5.new', 'b5'], env['BBB5']
         assert env['CCC1'] == 'c1', env['CCC1']
         assert env['CCC2'] == ['c2'], env['CCC2']
+        assert env['DDD1'] == ['a', 'b', 'c'], env['DDD1']
+
+        env.PrependUnique(DDD1 = 'b', delete_existing=1)
+        assert env['DDD1'] == ['b', 'a', 'c'], env['DDD1'] # b moves to front
+        env.PrependUnique(DDD1 = ['a','c'], delete_existing=1)
+        assert env['DDD1'] == ['a', 'c', 'b'], env['DDD1'] # a & c move to front
+
 
         env['CLVar'] = CLVar([])
         env.PrependUnique(CLVar = 'bar')