From 7144cf56430cff650a34ef102ad64ce5feb1d9cd Mon Sep 17 00:00:00 2001 From: William Deegan Date: Wed, 11 Oct 2017 14:34:14 -0400 Subject: Add cmp to SCons.Util as py3 no longer provides it --- src/engine/SCons/Util.py | 11 +++++++++++ src/engine/SCons/Variables/VariablesTests.py | 7 +------ 2 files changed, 12 insertions(+), 6 deletions(-) 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..b260d54 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): -- cgit v0.12 From 4f7b5abd7c9fe05a6f894005fdd3586c87d02462 Mon Sep 17 00:00:00 2001 From: William Deegan Date: Wed, 11 Oct 2017 18:43:46 -0400 Subject: Fix logic which was broken by automatic fixers. the sort parameter to GenerateHelpText() is now actually called. --- doc/man/scons.xml | 4 +++- src/CHANGES.txt | 3 +++ src/engine/SCons/Variables/VariablesTests.py | 20 ++++++++++++++++++++ src/engine/SCons/Variables/__init__.py | 7 ++++++- 4 files changed, 32 insertions(+), 2 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 cmp -function). +function). + +Optionally a Boolean value of True for sort will cause a standard alphabetical sort to be performed 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/Variables/VariablesTests.py b/src/engine/SCons/Variables/VariablesTests.py index b260d54..175a14b 100644 --- a/src/engine/SCons/Variables/VariablesTests.py +++ b/src/engine/SCons/Variables/VariablesTests.py @@ -487,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 -- cgit v0.12