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):
log.info('Using local source directory: %s' % self.options['path'])
compile_dir = self.options['path']
current_dir = os.getcwd()
os.mkdir(self.options['location'])
os.chdir(compile_dir)
try:
if not self.is_build_dir():
contents = os.listdir(compile_dir)
if len(contents) == 1:
os.chdir(contents[0])
if not self.is_build_dir():
try:
if not self.is_build_dir():
contents = os.listdir(compile_dir)
if len(contents) == 1:
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')
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:
log.info('Applying patches')
for patch in patches:
self.run('%s %s < %s' % (patch_cmd, patch_options, patch))
if patches:
log.info('Applying patches')
for patch in patches:
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:
log.info('Executing pre-configure-hook')
self.call_script(self.options['pre-configure-hook'])
if 'pre-configure-hook' in self.options and len(self.options['pre-configure-hook'].strip()) > 0:
log.info('Executing 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:
log.info('Executing pre-make-hook')
self.call_script(self.options['pre-make-hook'])
if 'pre-make-hook' in self.options and len(self.options['pre-make-hook'].strip()) > 0:
log.info('Executing pre-make-hook')
self.call_script(self.options['pre-make-hook'])
self.run(make_cmd)
self.run('%s %s' % (make_cmd, make_targets))
self.run(make_cmd)
self.run('%s %s' % (make_cmd, make_targets))
if 'post-make-hook' in self.options and len(self.options['post-make-hook'].strip()) > 0:
log.info('Executing post-make-hook')
self.call_script(self.options['post-make-hook'])
if 'post-make-hook' in self.options and len(self.options['post-make-hook'].strip()) > 0:
log.info('Executing post-make-hook')
self.call_script(self.options['post-make-hook'])
except:
log.error('Compilation error. The package is left as is at %s where '
'you can inspect what went wrong' % os.getcwd())
raise
except:
log.error('Compilation error. The package is left as is at %s where '
'you can inspect what went wrong' % os.getcwd())
raise
finally:
os.chdir(current_dir)
if self.options['url']:
if self.options.get('keep-compile-dir', '').lower() in ('true', 'yes', '1', 'on'):
......
......@@ -6,6 +6,7 @@ import re
import shutil
import tempfile
import unittest
import zc.buildout
import zc.buildout.testing
import zc.buildout.tests
......@@ -21,7 +22,7 @@ def setUp(test):
class NonInformativeTests(unittest.TestCase):
def setUp(self):
self.dir = tempfile.mkdtemp()
self.dir = os.path.realpath(tempfile.mkdtemp())
def tearDown(self):
shutil.rmtree(self.dir)
......@@ -35,9 +36,11 @@ class NonInformativeTests(unittest.TestCase):
def make_recipe(self, buildout, name, options):
from hexagonit.recipe.cmmi import Recipe
parts_directory_path = os.path.join(self.dir, 'test_parts')
os.mkdir(parts_directory_path)
bo = {
'buildout' : {
'parts-directory' : '',
'parts-directory' : parts_directory_path,
}
}
bo.update(buildout)
......@@ -61,6 +64,24 @@ class NonInformativeTests(unittest.TestCase):
self.failUnless(os.path.exists(makefile))
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():
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