Skip to content

Commit

Permalink
Merge pull request #4514 from Repiteo/GetSConsVersion
Browse files Browse the repository at this point in the history
Implement `GetSConsVersion` static method
  • Loading branch information
bdbaddog authored May 4, 2024
2 parents a97bcd1 + f5f7984 commit 5f07b5d
Show file tree
Hide file tree
Showing 9 changed files with 93 additions and 5 deletions.
4 changes: 4 additions & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ NOTE: 4.3.0 now requires Python 3.6.0 and above. Python 3.5.x is no longer suppo

RELEASE VERSION/DATE TO BE FILLED IN LATER

From Thaddeus Crews:
- GetSConsVersion() to grab the latest SCons version without needing to
access SCons internals.

From Raymond Li:
- Fix issue #3935: OSErrors are now no longer hidden during execution of
Actions. All exceptions during the execution of an Action are now
Expand Down
2 changes: 1 addition & 1 deletion RELEASE.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Here is a summary of the changes since 4.7.0:
NEW FUNCTIONALITY
-----------------

- List new features (presumably why a checkpoint is being released)
- GetSConsVersion() added to retrieve the SCons version.

DEPRECATED FUNCTIONALITY
------------------------
Expand Down
16 changes: 12 additions & 4 deletions SCons/Script/SConscript.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import sys
import traceback
import time
from typing import Tuple

class SConscriptReturn(Exception):
pass
Expand Down Expand Up @@ -385,7 +386,7 @@ class SConsEnvironment(SCons.Environment.Base):
# Private methods of an SConsEnvironment.
#
@staticmethod
def _get_major_minor_revision(version_string):
def _get_major_minor_revision(version_string: str) -> Tuple[int, int, int]:
"""Split a version string into major, minor and (optionally)
revision parts.
Expand Down Expand Up @@ -484,15 +485,22 @@ def Default(self, *targets) -> None:
SCons.Script._Set_Default_Targets(self, targets)

@staticmethod
def EnsureSConsVersion(major, minor, revision: int=0) -> None:
def GetSConsVersion() -> Tuple[int, int, int]:
"""Return the current SCons version.
.. versionadded:: 4.8.0
"""
return SConsEnvironment._get_major_minor_revision(SCons.__version__)

@staticmethod
def EnsureSConsVersion(major: int, minor: int, revision: int = 0) -> None:
"""Exit abnormally if the SCons version is not late enough."""
# split string to avoid replacement during build process
if SCons.__version__ == '__' + 'VERSION__':
SCons.Warnings.warn(SCons.Warnings.DevelopmentVersionWarning,
"EnsureSConsVersion is ignored for development version")
return
scons_ver = SConsEnvironment._get_major_minor_revision(SCons.__version__)
if scons_ver < (major, minor, revision):
if SConsEnvironment.GetSConsVersion() < (major, minor, revision):
if revision:
scons_ver_string = '%d.%d.%d' % (major, minor, revision)
else:
Expand Down
13 changes: 13 additions & 0 deletions SCons/Script/SConscript.xml
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,19 @@ EnsureSConsVersion(0,96,90)
</summary>
</scons_function>

<scons_function name="GetSConsVersion">
<arguments signature="global">
()
</arguments>
<summary>
<para>
Returns the current SCons version in the form of a Tuple[int, int, int],
representing the major, minor, and revision values respectively.
<emphasis>Added in 4.7.1</emphasis>.
</para>
</summary>
</scons_function>

<scons_function name="Exit">
<arguments signature="global">
([value])
Expand Down
1 change: 1 addition & 0 deletions SCons/Script/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,7 @@ def Variables(files=None, args=ARGUMENTS):
#
# Static functions that do not trigger initialization of
# DefaultEnvironment() and don't use its state.
GetSConsVersion = _SConscript.SConsEnvironment.GetSConsVersion
EnsureSConsVersion = _SConscript.SConsEnvironment.EnsureSConsVersion
EnsurePythonVersion = _SConscript.SConsEnvironment.EnsurePythonVersion
Exit = _SConscript.SConsEnvironment.Exit
Expand Down
4 changes: 4 additions & 0 deletions doc/generated/functions.mod
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ THIS IS AN AUTOMATICALLY-GENERATED FILE. DO NOT EDIT.
<!ENTITY f-GetBuildPath "<function xmlns='http://www.scons.org/dbxsd/v1.0'>GetBuildPath</function>">
<!ENTITY f-GetLaunchDir "<function xmlns='http://www.scons.org/dbxsd/v1.0'>GetLaunchDir</function>">
<!ENTITY f-GetOption "<function xmlns='http://www.scons.org/dbxsd/v1.0'>GetOption</function>">
<!ENTITY f-GetSConsVersion "<function xmlns='http://www.scons.org/dbxsd/v1.0'>GetSConsVersion</function>">
<!ENTITY f-Glob "<function xmlns='http://www.scons.org/dbxsd/v1.0'>Glob</function>">
<!ENTITY f-Help "<function xmlns='http://www.scons.org/dbxsd/v1.0'>Help</function>">
<!ENTITY f-Ignore "<function xmlns='http://www.scons.org/dbxsd/v1.0'>Ignore</function>">
Expand Down Expand Up @@ -132,6 +133,7 @@ THIS IS AN AUTOMATICALLY-GENERATED FILE. DO NOT EDIT.
<!ENTITY f-env-GetBuildPath "<function xmlns='http://www.scons.org/dbxsd/v1.0'>env.GetBuildPath</function>">
<!ENTITY f-env-GetLaunchDir "<function xmlns='http://www.scons.org/dbxsd/v1.0'>env.GetLaunchDir</function>">
<!ENTITY f-env-GetOption "<function xmlns='http://www.scons.org/dbxsd/v1.0'>env.GetOption</function>">
<!ENTITY f-env-GetSConsVersion "<function xmlns='http://www.scons.org/dbxsd/v1.0'>env.GetSConsVersion</function>">
<!ENTITY f-env-Glob "<function xmlns='http://www.scons.org/dbxsd/v1.0'>env.Glob</function>">
<!ENTITY f-env-Help "<function xmlns='http://www.scons.org/dbxsd/v1.0'>env.Help</function>">
<!ENTITY f-env-Ignore "<function xmlns='http://www.scons.org/dbxsd/v1.0'>env.Ignore</function>">
Expand Down Expand Up @@ -220,6 +222,7 @@ THIS IS AN AUTOMATICALLY-GENERATED FILE. DO NOT EDIT.
<!ENTITY f-link-GetBuildPath "<link linkend='f-GetBuildPath' xmlns='http://www.scons.org/dbxsd/v1.0'><function>GetBuildPath</function></link>">
<!ENTITY f-link-GetLaunchDir "<link linkend='f-GetLaunchDir' xmlns='http://www.scons.org/dbxsd/v1.0'><function>GetLaunchDir</function></link>">
<!ENTITY f-link-GetOption "<link linkend='f-GetOption' xmlns='http://www.scons.org/dbxsd/v1.0'><function>GetOption</function></link>">
<!ENTITY f-link-GetSConsVersion "<link linkend='f-GetSConsVersion' xmlns='http://www.scons.org/dbxsd/v1.0'><function>GetSConsVersion</function></link>">
<!ENTITY f-link-Glob "<link linkend='f-Glob' xmlns='http://www.scons.org/dbxsd/v1.0'><function>Glob</function></link>">
<!ENTITY f-link-Help "<link linkend='f-Help' xmlns='http://www.scons.org/dbxsd/v1.0'><function>Help</function></link>">
<!ENTITY f-link-Ignore "<link linkend='f-Ignore' xmlns='http://www.scons.org/dbxsd/v1.0'><function>Ignore</function></link>">
Expand Down Expand Up @@ -302,6 +305,7 @@ THIS IS AN AUTOMATICALLY-GENERATED FILE. DO NOT EDIT.
<!ENTITY f-link-env-GetBuildPath "<link linkend='f-GetBuildPath' xmlns='http://www.scons.org/dbxsd/v1.0'><function>env.GetBuildPath</function></link>">
<!ENTITY f-link-env-GetLaunchDir "<link linkend='f-GetLaunchDir' xmlns='http://www.scons.org/dbxsd/v1.0'><function>env.GetLaunchDir</function></link>">
<!ENTITY f-link-env-GetOption "<link linkend='f-GetOption' xmlns='http://www.scons.org/dbxsd/v1.0'><function>env.GetOption</function></link>">
<!ENTITY f-link-env-GetSConsVersion "<link linkend='f-GetSConsVersion' xmlns='http://www.scons.org/dbxsd/v1.0'><function>env.GetSConsVersion</function></link>">
<!ENTITY f-link-env-Glob "<link linkend='f-Glob' xmlns='http://www.scons.org/dbxsd/v1.0'><function>env.Glob</function></link>">
<!ENTITY f-link-env-Help "<link linkend='f-Help' xmlns='http://www.scons.org/dbxsd/v1.0'><function>env.Help</function></link>">
<!ENTITY f-link-env-Ignore "<link linkend='f-Ignore' xmlns='http://www.scons.org/dbxsd/v1.0'><function>env.Ignore</function></link>">
Expand Down
1 change: 1 addition & 0 deletions doc/scons.mod
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@
<!ENTITY EnumVariable "<function xmlns='http://www.scons.org/dbxsd/v1.0'>EnumVariable</function>">
<!ENTITY EnsurePythonVersion "<function xmlns='http://www.scons.org/dbxsd/v1.0'>EnsurePythonVersion</function>">
<!ENTITY EnsureSConsVersion "<function xmlns='http://www.scons.org/dbxsd/v1.0'>EnsureSConsVersion</function>">
<!ENTITY GetSConsVersion "<function xmlns='http://www.scons.org/dbxsd/v1.0'>GetSConsVersion</function>">
<!ENTITY Environment "<function xmlns='http://www.scons.org/dbxsd/v1.0'>Environment</function>">
<!ENTITY Execute "<function xmlns='http://www.scons.org/dbxsd/v1.0'>Execute</function>">
<!ENTITY Exit "<function xmlns='http://www.scons.org/dbxsd/v1.0'>Exit</function>">
Expand Down
26 changes: 26 additions & 0 deletions doc/user/misc.xml
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,32 @@ SCons 1.0 or greater required, but you have SCons 0.98.5

</section>

<section>
<title>Accessing SCons Version: the &GetSConsVersion; Function</title>

<para>

While &EnsureSConsVersion; is acceptable for most cases, there
are times where the user will want to support multiple SCons versions
simultaneously. In this scenario, it's beneficial to retrieve version
information of the currently executing SCons directly. This was previously
only possible by accessing SCons internals. From SCons4.8 onwards, it's now possible
to instead call &GetSConsVersion; to recieve a tuple containing the
major, minor, and revision values of the current version.

</para>

<screen>
if GetSConsVersion() &gt;= (4, 9):
# Some function got a new argument in 4.9 that we want to take advantage of
SomeFunc(arg1, arg2, arg3)
else:
# Can't use the extended syntax, but it doesn't warrant exiting prematurely
SomeFunc(arg1, arg2)
</screen>

</section>

<section>
<title>Explicitly Terminating &SCons; While Reading &SConscript; Files: the &Exit; Function</title>

Expand Down
31 changes: 31 additions & 0 deletions test/EnsureSConsVersion.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,23 @@

test.run(status=2)

test.write('SConstruct', """\
env = Environment()
env.EnsureSConsVersion(*env.GetSConsVersion())
Exit(0)
""")

test.run()

test.write('SConstruct', """\
env = Environment()
ver = env.GetSConsVersion()
env.EnsureSConsVersion(ver[0], ver[1], ver[2])
Exit(0)
""")

test.run()



test.write('SConstruct', """\
Expand Down Expand Up @@ -121,6 +138,20 @@

test.run(status=2)

test.write('SConstruct', """\
import SCons
EnsureSConsVersion(*GetSConsVersion())
""")

test.run()

test.write('SConstruct', """\
import SCons
ver = GetSConsVersion()
EnsureSConsVersion(ver[0], ver[1], ver[2])
""")

test.run()


test.pass_test()
Expand Down

0 comments on commit 5f07b5d

Please sign in to comment.