From 3c23953ee555ab9552a2a22cd16e25f0f5dfc408 Mon Sep 17 00:00:00 2001 From: William Deegan Date: Fri, 10 Jul 2020 18:29:27 -0700 Subject: Added TEMPFILEDIR to allow configuring where TEMPFILEMUNGE create's it's temporary files --- CHANGES.txt | 12 +++-- SCons/Platform/__init__.py | 8 +++- SCons/Platform/__init__.xml | 8 +++- test/TempFileMunge/TEMPFILEDIR.py | 56 +++++++++++++++++++++++ test/TempFileMunge/TEMPFILEPREFIX.py | 10 ---- test/TempFileMunge/TEMPFILESUFFIX.py | 10 ---- test/TempFileMunge/fixture/SConstruct.tempfiledir | 11 +++++ test/fixture/echo.py | 2 + 8 files changed, 90 insertions(+), 27 deletions(-) create mode 100644 test/TempFileMunge/TEMPFILEDIR.py create mode 100644 test/TempFileMunge/fixture/SConstruct.tempfiledir create mode 100755 test/fixture/echo.py diff --git a/CHANGES.txt b/CHANGES.txt index 9277f74..354fccd 100755 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -8,15 +8,17 @@ NOTE: The 4.0.0 Release of SCons will drops Python 2.7 Support RELEASE 4.1.0.devyyyymmdd - Mon, 04 Jul 2020 16:06:40 -0700 - From John Doe: - - - Whatever John Doe did. - From Rob Boehne: - Fix subprocess execution of 'lslpp' on AIX to produce text standard i/o. - Re-do the fix for suncxx tool (Oracle Studio compiler) now that only Python 3 is supported, to avoid decoding errors. - + + From William Deegan: + - Added Environment() variable TEMPFILEDIR which allows setting the directory which temp + files createdby TEMPFILEMUNGE are created in. + + + RELEASE 4.0.0 - Sat, 04 Jul 2020 12:00:27 +0000 From Dirk Baechle: diff --git a/SCons/Platform/__init__.py b/SCons/Platform/__init__.py index 4c912d4..5d3541b 100644 --- a/SCons/Platform/__init__.py +++ b/SCons/Platform/__init__.py @@ -205,7 +205,13 @@ class TempFileMunge: else: suffix = '.lnk' - fd, tmp = tempfile.mkstemp(suffix, text=True) + if 'TEMPFILEDIR' in env: + tempfile_dir = env.subst('$TEMPFILEDIR') + os.makedirs(tempfile_dir, exist_ok=True) + else: + tempfile_dir = None + + fd, tmp = tempfile.mkstemp(suffix, dir=tempfile_dir, text=True) native_tmp = SCons.Util.get_native_path(tmp) if env.get('SHELL', None) == 'sh': diff --git a/SCons/Platform/__init__.xml b/SCons/Platform/__init__.xml index e877b1e..6582590 100644 --- a/SCons/Platform/__init__.xml +++ b/SCons/Platform/__init__.xml @@ -267,5 +267,11 @@ Note this value is used literally and not expanded by the subst logic. - + + + +The directory to create the tempfile in. + + + diff --git a/test/TempFileMunge/TEMPFILEDIR.py b/test/TempFileMunge/TEMPFILEDIR.py new file mode 100644 index 0000000..c1ed6f9 --- /dev/null +++ b/test/TempFileMunge/TEMPFILEDIR.py @@ -0,0 +1,56 @@ +#!/usr/bin/env python +# +# Permission is hereby granted, free of charge, to any person obtaining +# a copy of this software and associated documentation files (the +# "Software"), to deal in the Software without restriction, including +# without limitation the rights to use, copy, modify, merge, publish, +# distribute, sublicense, and/or sell copies of the Software, and to +# permit persons to whom the Software is furnished to do so, subject to +# the following conditions: +# +# The above copyright notice and this permission notice shall be included +# in all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY +# KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE +# WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +""" +Verify that setting the $TEMPFILEDIR variable will cause +the generated tempfile used for long command lines to be created in specified directory. +And if not specified in one of TMPDIR, TEMP or TMP (based on os' normal usage of such) +""" +import os.path +import TestSCons + +test = TestSCons.TestSCons(match=TestSCons.match_re) + +test.file_fixture('fixture/SConstruct.tempfiledir', 'SConstruct') + +test.write('foo.in', "foo.in\n") + +test.run(arguments='-n -Q .', + stdout="""\ +Using tempfile \\S+ for command line: +xxx.py foo.out foo.in +xxx.py \\S+ +""") + +try: + tempfile = test.stdout().splitlines()[0].split()[2] +except IndexError: + test.fail_test("Unexpected output couldn't find tempfile") + +dirname = os.path.basename(os.path.dirname(tempfile)) +test.fail_test('my_temp_files' != dirname, message="Temp file not created in \"my_temp_files\" directory") +test.pass_test() + +# Local Variables: +# tab-width:4 +# indent-tabs-mode:nil +# End: +# vim: set expandtab tabstop=4 shiftwidth=4: diff --git a/test/TempFileMunge/TEMPFILEPREFIX.py b/test/TempFileMunge/TEMPFILEPREFIX.py index 9e2f752..ac2ae46 100644 --- a/test/TempFileMunge/TEMPFILEPREFIX.py +++ b/test/TempFileMunge/TEMPFILEPREFIX.py @@ -32,16 +32,6 @@ import TestSCons test = TestSCons.TestSCons(match = TestSCons.match_re) -test.write('echo.py', """\ -import sys -print(sys.argv) -""") - -echo_py = test.workpath('echo.py') - -st = os.stat(echo_py) -os.chmod(echo_py, st[stat.ST_MODE]|0o111) - test.write('SConstruct', """ import os env = Environment( diff --git a/test/TempFileMunge/TEMPFILESUFFIX.py b/test/TempFileMunge/TEMPFILESUFFIX.py index 151338c..a19317d 100644 --- a/test/TempFileMunge/TEMPFILESUFFIX.py +++ b/test/TempFileMunge/TEMPFILESUFFIX.py @@ -32,16 +32,6 @@ import TestSCons test = TestSCons.TestSCons(match=TestSCons.match_re) -test.write('echo.py', """\ -import sys -print(sys.argv) -""") - -echo_py = test.workpath('echo.py') - -st = os.stat(echo_py) -os.chmod(echo_py, st[stat.ST_MODE] | 0o111) - test.write('SConstruct', """ import os env = Environment( diff --git a/test/TempFileMunge/fixture/SConstruct.tempfiledir b/test/TempFileMunge/fixture/SConstruct.tempfiledir new file mode 100644 index 0000000..6b7300d --- /dev/null +++ b/test/TempFileMunge/fixture/SConstruct.tempfiledir @@ -0,0 +1,11 @@ +import os + +my_temp_dir = os.path.join(os.getcwd(), 'my_temp_files') + +env = Environment( + BUILDCOM='${TEMPFILE("xxx.py $TARGET $SOURCES")}', + MAXLINELENGTH=16, + TEMPFILEDIR=my_temp_dir, +) +env.AppendENVPath('PATH', os.curdir) +env.Command('foo.out', 'foo.in', '$BUILDCOM') diff --git a/test/fixture/echo.py b/test/fixture/echo.py new file mode 100755 index 0000000..a13f2ca --- /dev/null +++ b/test/fixture/echo.py @@ -0,0 +1,2 @@ +import sys +print(sys.argv) -- cgit v0.12