Skip to content

Commit e8676f2

Browse files
committed
Implement GetSConsVersion static method
1 parent 5dd1d9c commit e8676f2

File tree

9 files changed

+93
-5
lines changed

9 files changed

+93
-5
lines changed

CHANGES.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ NOTE: 4.3.0 now requires Python 3.6.0 and above. Python 3.5.x is no longer suppo
99

1010
RELEASE VERSION/DATE TO BE FILLED IN LATER
1111

12+
From Thaddeus Crews:
13+
- GetSConsVersion() to grab the latest SCons version without needing to
14+
dig into private functions.
15+
1216
From Raymond Li:
1317
- Fix issue #3935: OSErrors are now no longer hidden during execution of
1418
Actions. All exceptions during the execution of an Action are now

RELEASE.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ Here is a summary of the changes since 4.7.0:
1616
NEW FUNCTIONALITY
1717
-----------------
1818

19-
- List new features (presumably why a checkpoint is being released)
19+
- GetSConsVersion() to grab the current SCons version from an exposed api.
2020

2121
DEPRECATED FUNCTIONALITY
2222
------------------------

SCons/Script/SConscript.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
import sys
4646
import traceback
4747
import time
48+
from typing import Tuple
4849

4950
class SConscriptReturn(Exception):
5051
pass
@@ -385,7 +386,7 @@ class SConsEnvironment(SCons.Environment.Base):
385386
# Private methods of an SConsEnvironment.
386387
#
387388
@staticmethod
388-
def _get_major_minor_revision(version_string):
389+
def _get_major_minor_revision(version_string: str) -> Tuple[int, int, int]:
389390
"""Split a version string into major, minor and (optionally)
390391
revision parts.
391392
@@ -484,15 +485,22 @@ def Default(self, *targets) -> None:
484485
SCons.Script._Set_Default_Targets(self, targets)
485486

486487
@staticmethod
487-
def EnsureSConsVersion(major, minor, revision: int=0) -> None:
488+
def GetSConsVersion() -> Tuple[int, int, int]:
489+
"""Return the current SCons version.
490+
491+
.. versionadded:: 4.8.0
492+
"""
493+
return SConsEnvironment._get_major_minor_revision(SCons.__version__)
494+
495+
@staticmethod
496+
def EnsureSConsVersion(major: int, minor: int, revision: int = 0) -> None:
488497
"""Exit abnormally if the SCons version is not late enough."""
489498
# split string to avoid replacement during build process
490499
if SCons.__version__ == '__' + 'VERSION__':
491500
SCons.Warnings.warn(SCons.Warnings.DevelopmentVersionWarning,
492501
"EnsureSConsVersion is ignored for development version")
493502
return
494-
scons_ver = SConsEnvironment._get_major_minor_revision(SCons.__version__)
495-
if scons_ver < (major, minor, revision):
503+
if SConsEnvironment.GetSConsVersion() < (major, minor, revision):
496504
if revision:
497505
scons_ver_string = '%d.%d.%d' % (major, minor, revision)
498506
else:

SCons/Script/SConscript.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,18 @@ EnsureSConsVersion(0,96,90)
136136
</summary>
137137
</scons_function>
138138

139+
<scons_function name="GetSConsVersion">
140+
<arguments signature="global">
141+
()
142+
</arguments>
143+
<summary>
144+
<para>
145+
Returns the current SCons version in the form of a Tuple[int, int, int],
146+
representing the major, minor, and revision values respectively.
147+
</para>
148+
</summary>
149+
</scons_function>
150+
139151
<scons_function name="Exit">
140152
<arguments signature="global">
141153
([value])

SCons/Script/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,7 @@ def Variables(files=None, args=ARGUMENTS):
297297
#
298298
# Static functions that do not trigger initialization of
299299
# DefaultEnvironment() and don't use its state.
300+
GetSConsVersion = _SConscript.SConsEnvironment.GetSConsVersion
300301
EnsureSConsVersion = _SConscript.SConsEnvironment.EnsureSConsVersion
301302
EnsurePythonVersion = _SConscript.SConsEnvironment.EnsurePythonVersion
302303
Exit = _SConscript.SConsEnvironment.Exit

doc/generated/functions.mod

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ THIS IS AN AUTOMATICALLY-GENERATED FILE. DO NOT EDIT.
5050
<!ENTITY f-GetBuildPath "<function xmlns='http://www.scons.org/dbxsd/v1.0'>GetBuildPath</function>">
5151
<!ENTITY f-GetLaunchDir "<function xmlns='http://www.scons.org/dbxsd/v1.0'>GetLaunchDir</function>">
5252
<!ENTITY f-GetOption "<function xmlns='http://www.scons.org/dbxsd/v1.0'>GetOption</function>">
53+
<!ENTITY f-GetSConsVersion "<function xmlns='http://www.scons.org/dbxsd/v1.0'>GetSConsVersion</function>">
5354
<!ENTITY f-Glob "<function xmlns='http://www.scons.org/dbxsd/v1.0'>Glob</function>">
5455
<!ENTITY f-Help "<function xmlns='http://www.scons.org/dbxsd/v1.0'>Help</function>">
5556
<!ENTITY f-Ignore "<function xmlns='http://www.scons.org/dbxsd/v1.0'>Ignore</function>">
@@ -132,6 +133,7 @@ THIS IS AN AUTOMATICALLY-GENERATED FILE. DO NOT EDIT.
132133
<!ENTITY f-env-GetBuildPath "<function xmlns='http://www.scons.org/dbxsd/v1.0'>env.GetBuildPath</function>">
133134
<!ENTITY f-env-GetLaunchDir "<function xmlns='http://www.scons.org/dbxsd/v1.0'>env.GetLaunchDir</function>">
134135
<!ENTITY f-env-GetOption "<function xmlns='http://www.scons.org/dbxsd/v1.0'>env.GetOption</function>">
136+
<!ENTITY f-env-GetSConsVersion "<function xmlns='http://www.scons.org/dbxsd/v1.0'>env.GetSConsVersion</function>">
135137
<!ENTITY f-env-Glob "<function xmlns='http://www.scons.org/dbxsd/v1.0'>env.Glob</function>">
136138
<!ENTITY f-env-Help "<function xmlns='http://www.scons.org/dbxsd/v1.0'>env.Help</function>">
137139
<!ENTITY f-env-Ignore "<function xmlns='http://www.scons.org/dbxsd/v1.0'>env.Ignore</function>">
@@ -220,6 +222,7 @@ THIS IS AN AUTOMATICALLY-GENERATED FILE. DO NOT EDIT.
220222
<!ENTITY f-link-GetBuildPath "<link linkend='f-GetBuildPath' xmlns='http://www.scons.org/dbxsd/v1.0'><function>GetBuildPath</function></link>">
221223
<!ENTITY f-link-GetLaunchDir "<link linkend='f-GetLaunchDir' xmlns='http://www.scons.org/dbxsd/v1.0'><function>GetLaunchDir</function></link>">
222224
<!ENTITY f-link-GetOption "<link linkend='f-GetOption' xmlns='http://www.scons.org/dbxsd/v1.0'><function>GetOption</function></link>">
225+
<!ENTITY f-link-GetSConsVersion "<link linkend='f-GetSConsVersion' xmlns='http://www.scons.org/dbxsd/v1.0'><function>GetSConsVersion</function></link>">
223226
<!ENTITY f-link-Glob "<link linkend='f-Glob' xmlns='http://www.scons.org/dbxsd/v1.0'><function>Glob</function></link>">
224227
<!ENTITY f-link-Help "<link linkend='f-Help' xmlns='http://www.scons.org/dbxsd/v1.0'><function>Help</function></link>">
225228
<!ENTITY f-link-Ignore "<link linkend='f-Ignore' xmlns='http://www.scons.org/dbxsd/v1.0'><function>Ignore</function></link>">
@@ -302,6 +305,7 @@ THIS IS AN AUTOMATICALLY-GENERATED FILE. DO NOT EDIT.
302305
<!ENTITY f-link-env-GetBuildPath "<link linkend='f-GetBuildPath' xmlns='http://www.scons.org/dbxsd/v1.0'><function>env.GetBuildPath</function></link>">
303306
<!ENTITY f-link-env-GetLaunchDir "<link linkend='f-GetLaunchDir' xmlns='http://www.scons.org/dbxsd/v1.0'><function>env.GetLaunchDir</function></link>">
304307
<!ENTITY f-link-env-GetOption "<link linkend='f-GetOption' xmlns='http://www.scons.org/dbxsd/v1.0'><function>env.GetOption</function></link>">
308+
<!ENTITY f-link-env-GetSConsVersion "<link linkend='f-GetSConsVersion' xmlns='http://www.scons.org/dbxsd/v1.0'><function>env.GetSConsVersion</function></link>">
305309
<!ENTITY f-link-env-Glob "<link linkend='f-Glob' xmlns='http://www.scons.org/dbxsd/v1.0'><function>env.Glob</function></link>">
306310
<!ENTITY f-link-env-Help "<link linkend='f-Help' xmlns='http://www.scons.org/dbxsd/v1.0'><function>env.Help</function></link>">
307311
<!ENTITY f-link-env-Ignore "<link linkend='f-Ignore' xmlns='http://www.scons.org/dbxsd/v1.0'><function>env.Ignore</function></link>">

doc/scons.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,7 @@
214214
<!ENTITY EnumVariable "<function xmlns='http://www.scons.org/dbxsd/v1.0'>EnumVariable</function>">
215215
<!ENTITY EnsurePythonVersion "<function xmlns='http://www.scons.org/dbxsd/v1.0'>EnsurePythonVersion</function>">
216216
<!ENTITY EnsureSConsVersion "<function xmlns='http://www.scons.org/dbxsd/v1.0'>EnsureSConsVersion</function>">
217+
<!ENTITY GetSConsVersion "<function xmlns='http://www.scons.org/dbxsd/v1.0'>GetSConsVersion</function>">
217218
<!ENTITY Environment "<function xmlns='http://www.scons.org/dbxsd/v1.0'>Environment</function>">
218219
<!ENTITY Execute "<function xmlns='http://www.scons.org/dbxsd/v1.0'>Execute</function>">
219220
<!ENTITY Exit "<function xmlns='http://www.scons.org/dbxsd/v1.0'>Exit</function>">

doc/user/misc.xml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,33 @@ SCons 1.0 or greater required, but you have SCons 0.98.5
177177

178178
</section>
179179

180+
<section>
181+
<title>Grabbing the current SCons Version: the &GetSConsVersion; Function</title>
182+
183+
<para>
184+
185+
While &EnsureSConsVersion; is acceptable for most cases, there
186+
are times where the user will want to support multiple SCons versions
187+
simultaneously. In this scenario, it's beneficial to retrieve version
188+
information of the currently executing SCons directly. This was previously
189+
only possible by accessing private class methods, which made the
190+
process cumbersome and unintuitive. From 4.8 onwards, it's now possible
191+
to instead call &GetSConsVersion; to recieve a tuple containing the
192+
major, minor, and revision values of the current version.
193+
194+
</para>
195+
196+
<screen>
197+
if GetSConsVersion() &gt;= (4, 9):
198+
# Some function got a new argument in 4.9 that we want to take advantage of
199+
SomeFunc(arg1, arg2, arg3)
200+
else:
201+
# Can't use the extended syntax, but it doesn't warrant exiting prematurely
202+
SomeFunc(arg1, arg2)
203+
</screen>
204+
205+
</section>
206+
180207
<section>
181208
<title>Explicitly Terminating &SCons; While Reading &SConscript; Files: the &Exit; Function</title>
182209

test/EnsureSConsVersion.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,23 @@
6363

6464
test.run(status=2)
6565

66+
test.write('SConstruct', """\
67+
env = Environment()
68+
env.EnsureSConsVersion(*env.GetSConsVersion())
69+
Exit(0)
70+
""")
71+
72+
test.run()
73+
74+
test.write('SConstruct', """\
75+
env = Environment()
76+
ver = env.GetSConsVersion()
77+
env.EnsureSConsVersion(ver[0], ver[1], ver[2])
78+
Exit(0)
79+
""")
80+
81+
test.run()
82+
6683

6784

6885
test.write('SConstruct', """\
@@ -121,6 +138,20 @@
121138

122139
test.run(status=2)
123140

141+
test.write('SConstruct', """\
142+
import SCons
143+
EnsureSConsVersion(*GetSConsVersion())
144+
""")
145+
146+
test.run()
147+
148+
test.write('SConstruct', """\
149+
import SCons
150+
ver = GetSConsVersion()
151+
EnsureSConsVersion(ver[0], ver[1], ver[2])
152+
""")
153+
154+
test.run()
124155

125156

126157
test.pass_test()

0 commit comments

Comments
 (0)