diff options
author | Gary Oberbrunner <garyo@oberbrunner.com> | 2008-10-02 02:17:58 (GMT) |
---|---|---|
committer | Gary Oberbrunner <garyo@oberbrunner.com> | 2008-10-02 02:17:58 (GMT) |
commit | ec7a3415919e0604ca262e0f9fd5df82797f30fe (patch) | |
tree | a58ecf502de371a7ba37b2742f4bc3a40e3bc052 | |
parent | 2339a80cd71559200f18733234af6d5fb93ccbf7 (diff) | |
download | SCons-ec7a3415919e0604ca262e0f9fd5df82797f30fe.zip SCons-ec7a3415919e0604ca262e0f9fd5df82797f30fe.tar.gz SCons-ec7a3415919e0604ca262e0f9fd5df82797f30fe.tar.bz2 |
implement delete_existing for AppendUnique and PrependUnique. Finishes #2091.
-rw-r--r-- | doc/man/scons.1 | 10 | ||||
-rw-r--r-- | src/engine/SCons/Environment.py | 38 | ||||
-rw-r--r-- | src/engine/SCons/EnvironmentTests.py | 25 |
3 files changed, 59 insertions, 14 deletions
diff --git a/doc/man/scons.1 b/doc/man/scons.1 index f080bd7..2d2cc95 100644 --- a/doc/man/scons.1 +++ b/doc/man/scons.1 @@ -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: diff --git a/src/engine/SCons/Environment.py b/src/engine/SCons/Environment.py index affe91e..5ac10ac 100644 --- a/src/engine/SCons/Environment.py +++ b/src/engine/SCons/Environment.py @@ -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) diff --git a/src/engine/SCons/EnvironmentTests.py b/src/engine/SCons/EnvironmentTests.py index 950dd38..568f14d 100644 --- a/src/engine/SCons/EnvironmentTests.py +++ b/src/engine/SCons/EnvironmentTests.py @@ -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') |