Skip to content

Commit 5f07b5d

Browse files
authored
Merge pull request SCons#4514 from Repiteo/GetSConsVersion
Implement `GetSConsVersion` static method
2 parents a97bcd1 + f5f7984 commit 5f07b5d

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+
access SCons internals.
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() added to retrieve the SCons version.
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: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,19 @@ 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+
<emphasis>Added in 4.7.1</emphasis>.
148+
</para>
149+
</summary>
150+
</scons_function>
151+
139152
<scons_function name="Exit">
140153
<arguments signature="global">
141154
([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: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,32 @@ SCons 1.0 or greater required, but you have SCons 0.98.5
177177

178178
</section>
179179

180+
<section>
181+
<title>Accessing 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 SCons internals. From SCons4.8 onwards, it's now possible
190+
to instead call &GetSConsVersion; to recieve a tuple containing the
191+
major, minor, and revision values of the current version.
192+
193+
</para>
194+
195+
<screen>
196+
if GetSConsVersion() &gt;= (4, 9):
197+
# Some function got a new argument in 4.9 that we want to take advantage of
198+
SomeFunc(arg1, arg2, arg3)
199+
else:
200+
# Can't use the extended syntax, but it doesn't warrant exiting prematurely
201+
SomeFunc(arg1, arg2)
202+
</screen>
203+
204+
</section>
205+
180206
<section>
181207
<title>Explicitly Terminating &SCons; While Reading &SConscript; Files: the &Exit; Function</title>
182208

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)