Commit fbd5b6b1 authored by Jim Fulton's avatar Jim Fulton

Merge pull request #89 from reinout/sys-exit-when-restarting-fix

Fix for #73: proper sys.exit code when restarting buildout
parents 4becdee2 63fb5214
...@@ -1899,7 +1899,10 @@ def main(args=None): ...@@ -1899,7 +1899,10 @@ def main(args=None):
command, args) command, args)
getattr(buildout, command)(args) getattr(buildout, command)(args)
except SystemExit: except SystemExit:
pass logging.shutdown()
# Make sure we properly propagate an exit code from a restarted
# buildout process.
raise
except Exception: except Exception:
v = sys.exc_info()[1] v = sys.exc_info()[1]
_doing() _doing()
...@@ -1920,10 +1923,10 @@ def main(args=None): ...@@ -1920,10 +1923,10 @@ def main(args=None):
traceback.print_exception(*exc_info) traceback.print_exception(*exc_info)
sys.exit(1) sys.exit(1)
finally: finally:
logging.shutdown() logging.shutdown()
if sys.version_info[:2] < (2, 4): if sys.version_info[:2] < (2, 4):
def reversed(iterable): def reversed(iterable):
result = list(iterable); result = list(iterable);
......
...@@ -107,7 +107,7 @@ def clean_up_pyc(*path): ...@@ -107,7 +107,7 @@ def clean_up_pyc(*path):
## FIXME - check for other platforms ## FIXME - check for other platforms
MUST_CLOSE_FDS = not sys.platform.startswith('win') MUST_CLOSE_FDS = not sys.platform.startswith('win')
def system(command, input=''): def system(command, input='', with_exit_code=False):
p = subprocess.Popen(command, p = subprocess.Popen(command,
shell=True, shell=True,
stdin=subprocess.PIPE, stdin=subprocess.PIPE,
...@@ -121,7 +121,12 @@ def system(command, input=''): ...@@ -121,7 +121,12 @@ def system(command, input=''):
result = o.read() + e.read() result = o.read() + e.read()
o.close() o.close()
e.close() e.close()
return result.decode() output = result.decode()
if with_exit_code:
# Use the with_exit_code=True parameter when you want to test the exit
# code of the command you're running.
output += 'EXIT CODE: %s' % p.wait()
return output
def get(url): def get(url):
return str(urlopen(url).read().decode()) return str(urlopen(url).read().decode())
......
...@@ -234,3 +234,68 @@ directory: ...@@ -234,3 +234,68 @@ directory:
<BLANKLINE> <BLANKLINE>
if __name__ == '__main__': if __name__ == '__main__':
sys.exit(zc.buildout.buildout.main()) sys.exit(zc.buildout.buildout.main())
When buildout restarts and the restarted buildout exits with an error code,
the original buildout that called the second buildout also exits with that
error code. Otherwise build scripts can erroneously detect a succesful
buildout run even if it failed.
Make a recipe that fails:
>>> mkdir(sample_buildout, 'failrecipe')
>>> write(sample_buildout, 'failrecipe', 'failrecipe.py',
... """
... import pkg_resources
... import sys
... print_ = lambda *a: sys.stdout.write(' '.join(map(str, a))+'\\n')
...
... class Recipe:
...
... def __init__(self, buildout, name, options):
... sys.exit('recipe sys-exits')
...
... def install(self):
... pass
...
... update = install
... """)
>>> write(sample_buildout, 'failrecipe', 'setup.py',
... """
... from setuptools import setup
...
... setup(
... name = "failrecipe",
... entry_points = {'zc.buildout': ['default = failrecipe:Recipe']},
... )
... """)
Let's downgrade again, triggering a restart. And use the failing recipe that
gives us a sys.exit:
>>> write(sample_buildout, 'buildout.cfg',
... """
... [buildout]
... find-links = %(new_releases)s
... index = %(new_releases)s
... parts = fail
... develop = failrecipe
...
... [versions]
... zc.buildout = < 99
... distribute = < 99
...
... [fail]
... recipe = failrecipe
... """ % dict(new_releases=new_releases))
Run the buildout:
>>> print_(system(buildout, with_exit_code=True), end='')
Upgraded:
zc.buildout version 1.4.4;
distribute version 0.6;
restarting.
Generated script '/sample-buildout/bin/buildout'.
Develop: '/sample-buildout/failrecipe'
recipe sys-exits
EXIT CODE: 1
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment