diff options
author | Richard Maw <richard.maw@codethink.co.uk> | 2012-02-21 15:25:17 +0000 |
---|---|---|
committer | Richard Maw <richard.maw@codethink.co.uk> | 2012-02-21 17:01:02 +0000 |
commit | 913e365bfa29cf1769a2923211f29215b20badf4 (patch) | |
tree | 54d0a5fdf21e7f170134b6cf1856ff7817ebcf4d /morphlib/builder.py | |
parent | 647f3134ab4d9335bca10b10b6cfbefadf4c0677 (diff) | |
download | morph-913e365bfa29cf1769a2923211f29215b20badf4.tar.gz |
builder.py: use sh -c instead of writing a script
Putting the commands in a file made it more difficult to tell
what happened in scripts, until the self.msg was added.
After then it made the commands more difficult to reproduce
So if it can safely be done in a single command it should be more
readable.
This approach uses sh -c to cd then evaluate the remaining options
The command is 'cd "$1" && shift && eval "$@"', then passes the command
as arguments to the shell script, so the program does not need to do any
escaping of strings.
If the commands were a list of strings instead of a quoted string then
it would be a bit safer, but less flexible for use.
Diffstat (limited to 'morphlib/builder.py')
-rw-r--r-- | morphlib/builder.py | 13 |
1 files changed, 2 insertions, 11 deletions
diff --git a/morphlib/builder.py b/morphlib/builder.py index 9063bf3e..ad0b14f9 100644 --- a/morphlib/builder.py +++ b/morphlib/builder.py @@ -386,23 +386,14 @@ class ChunkBuilder(BlobBuilder): builddir = self.builddir[len(self.staging):] destdir = self.destdir[len(self.staging):] for cmd in commands: - self.msg('about to execute in chroot: %s' % cmd) - script = os.path.join(self.staging, 'temp.sh') - with open(script, 'w') as f: - f.write('#!/bin/sh\n') - f.write('set -ex\n') - f.write('cd %s\n' % builddir) - f.write('%s\n' % cmd) - os.chmod(script, 0555) - chroot_cmd = '/usr/sbin/chroot %s ./temp.sh' % self.staging old_destdir = ex.env.get('DESTDIR', None) ex.env['DESTDIR'] = destdir - ex.run([chroot_cmd]) + ex.runv(['/usr/sbin/chroot', self.staging, 'sh', '-c', + 'cd "$1" && shift && eval "$@"', '--', builddir, cmd]) if old_destdir is None: del ex.env['DESTDIR'] else: ex.env['DESTDIR'] = old_destdir - os.remove(script) else: self.ex.run(commands) |