diff options
author | William Deegan <bill@baddogconsulting.com> | 2017-10-11 23:41:20 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-10-11 23:41:20 (GMT) |
commit | 645e552e21cbb5513e686cf7dc665eccc1f46826 (patch) | |
tree | c4b72a1578ff3e846a908aa65776d21e5a3c907c | |
parent | 81c02354dfd7634708eb79547e9448150cc016a6 (diff) | |
parent | 4f7b5abd7c9fe05a6f894005fdd3586c87d02462 (diff) | |
download | SCons-645e552e21cbb5513e686cf7dc665eccc1f46826.zip SCons-645e552e21cbb5513e686cf7dc665eccc1f46826.tar.gz SCons-645e552e21cbb5513e686cf7dc665eccc1f46826.tar.bz2 |
Merge pull request #12 from bdbaddog/add_cmp_to_compat
Add cmp to SCons.Util as py3 no longer provides it and fix GenerateHelpText()'s sort parameter functionality
-rw-r--r-- | doc/man/scons.xml | 4 | ||||
-rw-r--r-- | src/CHANGES.txt | 3 | ||||
-rw-r--r-- | src/engine/SCons/Util.py | 11 | ||||
-rw-r--r-- | src/engine/SCons/Variables/VariablesTests.py | 27 | ||||
-rw-r--r-- | src/engine/SCons/Variables/__init__.py | 7 |
5 files changed, 44 insertions, 8 deletions
diff --git a/doc/man/scons.xml b/doc/man/scons.xml index 3268860..1f99753 100644 --- a/doc/man/scons.xml +++ b/doc/man/scons.xml @@ -4056,7 +4056,9 @@ and return -1, 0 or 1 (like the standard Python <emphasis>cmp</emphasis> -function).</para> +function). + +Optionally a Boolean value of True for <emphasis>sort</emphasis> will cause a standard alphabetical sort to be performed</para> <literallayout class="monospaced"> Help(vars.GenerateHelpText(env)) diff --git a/src/CHANGES.txt b/src/CHANGES.txt index a7ad6f2..7d0f8d8 100644 --- a/src/CHANGES.txt +++ b/src/CHANGES.txt @@ -15,6 +15,9 @@ RELEASE VERSION/DATE TO BE FILLED IN LATER - Fix issue where code in utility routine to_String_for_subst() had code whose result was never properly returned. (Found by: James Rinkevich https://pairlist4.pair.net/pipermail/scons-users/2017-October/006358.html ) + - Fixed Variables.GenerateHelpText() to now use the sort parameter. Due to incorrect 2to3 fixer changes + 8 years ago it was being used as a boolean parameter. Now you can specify sort to be a callable, or boolean + value. (True = normal sort). Manpage also updated. From Thomas Berg: - Fixed a regression in scons-3.0.0 where "from __future__ import print_function" was imposed diff --git a/src/engine/SCons/Util.py b/src/engine/SCons/Util.py index 11bcf2e..7ed9706 100644 --- a/src/engine/SCons/Util.py +++ b/src/engine/SCons/Util.py @@ -1619,6 +1619,17 @@ def to_str (s): return s return str (s, 'utf-8') + + +# No cmp in py3, so we'll define it. +def cmp(a, b): + """ + Define cmp because it's no longer available in python3 + Works under python 2 as well + """ + return (a > b) - (a < b) + + # Local Variables: # tab-width:4 # indent-tabs-mode:nil diff --git a/src/engine/SCons/Variables/VariablesTests.py b/src/engine/SCons/Variables/VariablesTests.py index 7ee66ca..175a14b 100644 --- a/src/engine/SCons/Variables/VariablesTests.py +++ b/src/engine/SCons/Variables/VariablesTests.py @@ -32,6 +32,7 @@ import TestUnit import SCons.Variables import SCons.Subst import SCons.Warnings +from SCons.Util import cmp class Environment(object): @@ -49,12 +50,6 @@ class Environment(object): return key in self.dict -def cmp(a, b): - """ - Define cmp because it's no longer available in python3 - Works under python 2 as well - """ - return (a > b) - (a < b) def check(key, value, env): @@ -492,9 +487,29 @@ B: b - alpha test default: 42 actual: 54 """ + + expectBackwards = """ +B: b - alpha test + default: 42 + actual: 54 + +ANSWER: THE answer to THE question + default: 42 + actual: 54 + +A: a - alpha test + default: 42 + actual: 54 +""" text = opts.GenerateHelpText(env, sort=cmp) assert text == expectAlpha, text + textBool = opts.GenerateHelpText(env, sort=True) + assert text == expectAlpha, text + + textBackwards = opts.GenerateHelpText(env, sort=lambda x, y: cmp(y, x)) + assert textBackwards == expectBackwards, "Expected:\n%s\nGot:\n%s\n"%(textBackwards, expectBackwards) + def test_FormatVariableHelpText(self): """Test generating custom format help text""" opts = SCons.Variables.Variables() diff --git a/src/engine/SCons/Variables/__init__.py b/src/engine/SCons/Variables/__init__.py index 5b20b30..6ad40ed 100644 --- a/src/engine/SCons/Variables/__init__.py +++ b/src/engine/SCons/Variables/__init__.py @@ -30,6 +30,7 @@ __revision__ = "__FILE__ __REVISION__ __DATE__ __DEVELOPER__" import os.path import sys +from functools import cmp_to_key import SCons.Environment import SCons.Errors @@ -287,9 +288,13 @@ class Variables(object): env - an environment that is used to get the current values of the options. + cmp - Either a function as follows: The specific sort function should take two arguments and return -1, 0 or 1 + or a boolean to indicate if it should be sorted. """ - if sort: + if callable(sort): + options = sorted(self.options, key=cmp_to_key(lambda x,y: sort(x.key,y.key))) + elif sort is True: options = sorted(self.options, key=lambda x: x.key) else: options = self.options |