Commit 653766d2 authored by Kai Lautaportti's avatar Kai Lautaportti

Restore the original working directory after the recipe finishes.

This change makes sure that the working directory is restored to the location
it was before running the recipe, regardless whether the execution of the
recipe is successful or not.

Thanks to Jonathan Ballet for pointing out this problem.
parent 3f728307
...@@ -112,47 +112,51 @@ class Recipe(object): ...@@ -112,47 +112,51 @@ class Recipe(object):
log.info('Using local source directory: %s' % self.options['path']) log.info('Using local source directory: %s' % self.options['path'])
compile_dir = self.options['path'] compile_dir = self.options['path']
current_dir = os.getcwd()
os.mkdir(self.options['location']) os.mkdir(self.options['location'])
os.chdir(compile_dir) os.chdir(compile_dir)
try: try:
if not self.is_build_dir(): try:
contents = os.listdir(compile_dir) if not self.is_build_dir():
if len(contents) == 1: contents = os.listdir(compile_dir)
os.chdir(contents[0]) if len(contents) == 1:
if not self.is_build_dir(): os.chdir(contents[0])
if not self.is_build_dir():
log.error('Unable to find the configure script')
raise zc.buildout.UserError('Invalid package contents')
else:
log.error('Unable to find the configure script') log.error('Unable to find the configure script')
raise zc.buildout.UserError('Invalid package contents') raise zc.buildout.UserError('Invalid package contents')
else:
log.error('Unable to find the configure script')
raise zc.buildout.UserError('Invalid package contents')
if patches: if patches:
log.info('Applying patches') log.info('Applying patches')
for patch in patches: for patch in patches:
self.run('%s %s < %s' % (patch_cmd, patch_options, patch)) self.run('%s %s < %s' % (patch_cmd, patch_options, patch))
if 'pre-configure-hook' in self.options and len(self.options['pre-configure-hook'].strip()) > 0: if 'pre-configure-hook' in self.options and len(self.options['pre-configure-hook'].strip()) > 0:
log.info('Executing pre-configure-hook') log.info('Executing pre-configure-hook')
self.call_script(self.options['pre-configure-hook']) self.call_script(self.options['pre-configure-hook'])
self.run('%s %s' % (configure_cmd, ' '.join(configure_options))) self.run('%s %s' % (configure_cmd, ' '.join(configure_options)))
if 'pre-make-hook' in self.options and len(self.options['pre-make-hook'].strip()) > 0: if 'pre-make-hook' in self.options and len(self.options['pre-make-hook'].strip()) > 0:
log.info('Executing pre-make-hook') log.info('Executing pre-make-hook')
self.call_script(self.options['pre-make-hook']) self.call_script(self.options['pre-make-hook'])
self.run(make_cmd) self.run(make_cmd)
self.run('%s %s' % (make_cmd, make_targets)) self.run('%s %s' % (make_cmd, make_targets))
if 'post-make-hook' in self.options and len(self.options['post-make-hook'].strip()) > 0: if 'post-make-hook' in self.options and len(self.options['post-make-hook'].strip()) > 0:
log.info('Executing post-make-hook') log.info('Executing post-make-hook')
self.call_script(self.options['post-make-hook']) self.call_script(self.options['post-make-hook'])
except: except:
log.error('Compilation error. The package is left as is at %s where ' log.error('Compilation error. The package is left as is at %s where '
'you can inspect what went wrong' % os.getcwd()) 'you can inspect what went wrong' % os.getcwd())
raise raise
finally:
os.chdir(current_dir)
if self.options['url']: if self.options['url']:
if self.options.get('keep-compile-dir', '').lower() in ('true', 'yes', '1', 'on'): if self.options.get('keep-compile-dir', '').lower() in ('true', 'yes', '1', 'on'):
......
...@@ -6,6 +6,7 @@ import re ...@@ -6,6 +6,7 @@ import re
import shutil import shutil
import tempfile import tempfile
import unittest import unittest
import zc.buildout
import zc.buildout.testing import zc.buildout.testing
import zc.buildout.tests import zc.buildout.tests
...@@ -21,7 +22,7 @@ def setUp(test): ...@@ -21,7 +22,7 @@ def setUp(test):
class NonInformativeTests(unittest.TestCase): class NonInformativeTests(unittest.TestCase):
def setUp(self): def setUp(self):
self.dir = tempfile.mkdtemp() self.dir = os.path.realpath(tempfile.mkdtemp())
def tearDown(self): def tearDown(self):
shutil.rmtree(self.dir) shutil.rmtree(self.dir)
...@@ -35,9 +36,11 @@ class NonInformativeTests(unittest.TestCase): ...@@ -35,9 +36,11 @@ class NonInformativeTests(unittest.TestCase):
def make_recipe(self, buildout, name, options): def make_recipe(self, buildout, name, options):
from hexagonit.recipe.cmmi import Recipe from hexagonit.recipe.cmmi import Recipe
parts_directory_path = os.path.join(self.dir, 'test_parts')
os.mkdir(parts_directory_path)
bo = { bo = {
'buildout' : { 'buildout' : {
'parts-directory' : '', 'parts-directory' : parts_directory_path,
} }
} }
bo.update(buildout) bo.update(buildout)
...@@ -61,6 +64,24 @@ class NonInformativeTests(unittest.TestCase): ...@@ -61,6 +64,24 @@ class NonInformativeTests(unittest.TestCase):
self.failUnless(os.path.exists(makefile)) self.failUnless(os.path.exists(makefile))
self.failUnless(recipe.is_build_dir()) self.failUnless(recipe.is_build_dir())
def test_working_directory_restored_after_failure(self):
compile_directory = os.path.join(self.dir, 'compile_directory')
os.makedirs(compile_directory)
recipe = self.make_recipe({}, 'test', {'path' : compile_directory})
os.chdir(self.dir)
self.assertRaises(zc.buildout.UserError, recipe.install)
self.assertEquals(self.dir, os.getcwd())
def test_working_directory_restored_after_success(self):
compile_directory = os.path.join(self.dir, 'compile_directory')
os.makedirs(compile_directory)
self.write_file(os.path.join(compile_directory, 'configure'), 'Dummy configure')
recipe = self.make_recipe({}, 'test', {'path' : compile_directory})
os.chdir(self.dir)
self.assertEquals(self.dir, os.getcwd())
def test_suite(): def test_suite():
suite = unittest.TestSuite(( suite = unittest.TestSuite((
......
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