From aeefea2d7f66b819bbaf6dff8a8afef63b05722e Mon Sep 17 00:00:00 2001 From: Steven Knight Date: Sat, 9 Feb 2002 22:26:59 +0000 Subject: Make writing a .sconsign more robust by writing to a temporary file first and renaming it. --- src/CHANGES.txt | 3 ++ src/engine/SCons/Sig/__init__.py | 32 ++++++++++++++--- test/nonwritable-sconsign.py | 60 -------------------------------- test/sconsign.py | 75 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 105 insertions(+), 65 deletions(-) delete mode 100644 test/nonwritable-sconsign.py create mode 100644 test/sconsign.py diff --git a/src/CHANGES.txt b/src/CHANGES.txt index 7ac9979..d533be9 100644 --- a/src/CHANGES.txt +++ b/src/CHANGES.txt @@ -36,6 +36,9 @@ RELEASE 0.05 - - Look up implicit (scanned) dependencies relative to the directory of file being scanned. + - Make writing .sconsign files more robust by first trying to write + to a temp file that we rename. + From Anthony Roach: - Make the scons script return an error code on failures. diff --git a/src/engine/SCons/Sig/__init__.py b/src/engine/SCons/Sig/__init__.py index 52d8b33..d1e546f 100644 --- a/src/engine/SCons/Sig/__init__.py +++ b/src/engine/SCons/Sig/__init__.py @@ -29,6 +29,7 @@ The Signature package for the scons software construction utility. __revision__ = "__FILE__ __REVISION__ __DATE__ __DEVELOPER__" +import os import os.path import string import SCons.Node @@ -114,17 +115,38 @@ class SConsignFile: def write(self): """ Write the .sconsign file to disk. + + Try to write to a temporary file first, and rename it if we + succeed. If we can't write to the temporary file, it's + probably because the directory isn't writable (and if so, + how did we build anything in this directory, anyway?), so + try to write directly to the .sconsign file as a backup. + If we can't rename, try to copy the temporary contents back + to the .sconsign file. Either way, always try to remove + the temporary file at the end. """ if self.dirty: + temp = os.path.join(self.dir.path, '.scons%d' % os.getpid()) try: + file = open(temp, 'wt') + fname = temp + except: file = open(self.sconsign, 'wt') + fname = self.sconsign + keys = self.entries.keys() + keys.sort() + for name in keys: + file.write("%s: %s\n" % (name, self.entries[name])) + file.close + if fname != self.sconsign: + try: + os.rename(fname, self.sconsign) + except: + open(self.sconsign, 'wb').write(open(fname, 'rb').read()) + try: + os.unlink(temp) except: pass - else: - keys = self.entries.keys() - keys.sort() - for name in keys: - file.write("%s: %s\n" % (name, self.entries[name])) class Calculator: diff --git a/test/nonwritable-sconsign.py b/test/nonwritable-sconsign.py deleted file mode 100644 index 6955438..0000000 --- a/test/nonwritable-sconsign.py +++ /dev/null @@ -1,60 +0,0 @@ -#!/usr/bin/env python -# -# Copyright (c) 2001, 2002 Steven Knight -# -# 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. -# - -__revision__ = "test/nonwritable-sconsign.py __REVISION__ __DATE__ __DEVELOPER__" - -import TestSCons -import os - -test = TestSCons.TestSCons() - -test.write('SConstruct', """ -env = Environment() -env.Program(target = 'foo1', source = 'f1.c') -""") - -test.write('f1.c', r""" -int -main(int argc, char *argv[]) -{ - argv[argc++] = "--"; - printf("f1.c\n"); - exit (0); -} -""") - -test.write('.sconsign', "") - -# For *NIX, systems, make .sconsign not writable. -# For Win32 systems, open it to lock it. -os.chmod(test.workpath('.sconsign'), 0444) -f = open(test.workpath('.sconsign'), 'r') - -test.run(arguments = ".") -test.run(program = test.workpath('foo1'), stdout = "f1.c\n") - -os.chmod(test.workpath('.sconsign'), 0666) -f.close() - -test.pass_test() diff --git a/test/sconsign.py b/test/sconsign.py new file mode 100644 index 0000000..ac6bf2f --- /dev/null +++ b/test/sconsign.py @@ -0,0 +1,75 @@ +#!/usr/bin/env python +# +# Copyright (c) 2001, 2002 Steven Knight +# +# 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. +# + +__revision__ = "__FILE__ __REVISION__ __DATE__ __DEVELOPER__" + +import os +import TestSCons + +test = TestSCons.TestSCons() + +test.subdir('sub1', 'sub2') + +test.write('SConstruct', """ +def build1(target, source, env): + open(str(target), 'wb').write(open(str(source[0]), 'rb').read()) + return None + +def build2(target, source, env): + import os + import os.path + open(str(target), 'wb').write(open(str(source[0]), 'rb').read()) + dir, file = os.path.split(target) + os.chmod(dir, 0555) + return None + +B1 = Builder(name = "B1", action = build1) +B2 = Builder(name = "B2", action = build2) +env = Environment(BUILDERS = [B1, B2]) +env.B1(target = 'sub1/foo.out', source = 'foo.in') +env.B2(target = 'sub2/foo.out', source = 'foo.in') +""") + +test.write('foo.in', "foo.in\n") + +sub1__sconsign = test.workpath('sub1', '.sconsign') +sub2__sconsign = test.workpath('sub2', '.sconsign') + +test.write(sub1__sconsign, "") +test.write(sub2__sconsign, "") + +# For *NIX systems, make .sconsign not writable. +# For Win32 systems, open it to lock it. +os.chmod(sub1__sconsign, 0444) +f = open(sub1__sconsign, 'r') + +test.run(arguments = '.') + +test.fail_test(test.read(sub1__sconsign) == "") +test.fail_test(test.read(sub2__sconsign) == "") + +os.chmod(sub1__sconsign, 0666) +f.close() + +test.pass_test() -- cgit v0.12