[createiso] Remove chdir()

Explicitly pass workdir to all invoked command. This is easier to follow
and test.

Signed-off-by: Lubomír Sedlář <lsedlar@redhat.com>
This commit is contained in:
Lubomír Sedlář 2016-04-14 10:14:51 +02:00
parent d4effc1610
commit b949bfd615
2 changed files with 32 additions and 40 deletions

View File

@ -2,12 +2,10 @@
import argparse import argparse
import os import os
import contextlib
from kobo import shortcuts from kobo import shortcuts
from .wrappers.iso import IsoWrapper from .wrappers.iso import IsoWrapper
from .wrappers.jigdo import JigdoWrapper from .wrappers.jigdo import JigdoWrapper
from .util import makedirs
def find_templates(fallback): def find_templates(fallback):
@ -20,16 +18,6 @@ def find_templates(fallback):
return output.strip() return output.strip()
@contextlib.contextmanager
def in_dir(dir):
"""Temporarily switch to another directory."""
old_cwd = os.getcwd()
makedirs(dir)
os.chdir(dir)
yield
os.chdir(old_cwd)
def make_image(iso, opts): def make_image(iso, opts):
mkisofs_kwargs = {} mkisofs_kwargs = {}
@ -49,16 +37,17 @@ def make_image(iso, opts):
cmd = iso.get_mkisofs_cmd(opts.iso_name, None, volid=opts.volid, cmd = iso.get_mkisofs_cmd(opts.iso_name, None, volid=opts.volid,
exclude=["./lost+found"], exclude=["./lost+found"],
graft_points=opts.graft_points, **mkisofs_kwargs) graft_points=opts.graft_points, **mkisofs_kwargs)
shortcuts.run(cmd, stdout=True, show_cmd=True) shortcuts.run(cmd, stdout=True, show_cmd=True, workdir=opts.output_dir)
def implant_md5(iso, opts): def implant_md5(iso, opts):
cmd = iso.get_implantisomd5_cmd(opts.iso_name, opts.supported) cmd = iso.get_implantisomd5_cmd(opts.iso_name, opts.supported)
shortcuts.run(cmd, stdout=True, show_cmd=True) shortcuts.run(cmd, stdout=True, show_cmd=True, workdir=opts.output_dir)
def make_manifest(iso, opts): def make_manifest(iso, opts):
shortcuts.run(iso.get_manifest_cmd(opts.iso_name), stdout=True, show_cmd=True) shortcuts.run(iso.get_manifest_cmd(opts.iso_name), stdout=True,
show_cmd=True, workdir=opts.output_dir)
def make_jigdo(opts): def make_jigdo(opts):
@ -73,7 +62,7 @@ def make_jigdo(opts):
cmd = jigdo.get_jigdo_cmd(os.path.join(opts.output_dir, opts.iso_name), cmd = jigdo.get_jigdo_cmd(os.path.join(opts.output_dir, opts.iso_name),
files, output_dir=opts.jigdo_dir, files, output_dir=opts.jigdo_dir,
no_servers=True, report="noprogress") no_servers=True, report="noprogress")
shortcuts.run(cmd, stdout=True, show_cmd=True) shortcuts.run(cmd, stdout=True, show_cmd=True, workdir=opts.output_dir)
def run(opts): def run(opts):
@ -111,5 +100,4 @@ def main(args=None):
if bool(opts.jigdo_dir) != bool(opts.os_tree): if bool(opts.jigdo_dir) != bool(opts.os_tree):
parser.error('--jigdo-dir must be used together with --os-tree') parser.error('--jigdo-dir must be used together with --os-tree')
with in_dir(opts.output_dir):
run(opts) run(opts)

View File

@ -20,10 +20,14 @@ class OstreeScriptTest(helpers.PungiTestCase):
for x, y in zip(actual, expected): for x, y in zip(actual, expected):
self.assertEqual(x, y) self.assertEqual(x, y)
def setUp(self):
super(OstreeScriptTest, self).setUp()
self.outdir = os.path.join(self.topdir, 'isos')
@mock.patch('kobo.shortcuts.run') @mock.patch('kobo.shortcuts.run')
def test_minimal_run(self, run): def test_minimal_run(self, run):
createiso.main([ createiso.main([
'--output-dir={}/isos'.format(self.topdir), '--output-dir={}'.format(self.outdir),
'--iso-name=DP-1.0-20160405.t.3-x86_64.iso', '--iso-name=DP-1.0-20160405.t.3-x86_64.iso',
'--volid=DP-1.0-20160405.t.3', '--volid=DP-1.0-20160405.t.3',
'--graft-points=graft-list', '--graft-points=graft-list',
@ -38,11 +42,11 @@ class OstreeScriptTest(helpers.PungiTestCase):
'-input-charset', 'utf-8', '-x', './lost+found', '-input-charset', 'utf-8', '-x', './lost+found',
'-o', 'DP-1.0-20160405.t.3-x86_64.iso', '-o', 'DP-1.0-20160405.t.3-x86_64.iso',
'-graft-points', '-path-list', 'graft-list'], '-graft-points', '-path-list', 'graft-list'],
show_cmd=True, stdout=True), show_cmd=True, stdout=True, workdir=self.outdir),
mock.call(['/usr/bin/implantisomd5', 'DP-1.0-20160405.t.3-x86_64.iso'], mock.call(['/usr/bin/implantisomd5', 'DP-1.0-20160405.t.3-x86_64.iso'],
show_cmd=True, stdout=True), show_cmd=True, stdout=True, workdir=self.outdir),
mock.call('isoinfo -R -f -i DP-1.0-20160405.t.3-x86_64.iso | grep -v \'/TRANS.TBL$\' | sort >> DP-1.0-20160405.t.3-x86_64.iso.manifest', mock.call('isoinfo -R -f -i DP-1.0-20160405.t.3-x86_64.iso | grep -v \'/TRANS.TBL$\' | sort >> DP-1.0-20160405.t.3-x86_64.iso.manifest',
show_cmd=True, stdout=True)] show_cmd=True, stdout=True, workdir=self.outdir)]
) )
@mock.patch('kobo.shortcuts.run') @mock.patch('kobo.shortcuts.run')
@ -50,7 +54,7 @@ class OstreeScriptTest(helpers.PungiTestCase):
run.return_value = (0, '/usr/share/lorax') run.return_value = (0, '/usr/share/lorax')
createiso.main([ createiso.main([
'--output-dir={}/isos'.format(self.topdir), '--output-dir={}'.format(self.outdir),
'--iso-name=DP-1.0-20160405.t.3-x86_64.iso', '--iso-name=DP-1.0-20160405.t.3-x86_64.iso',
'--volid=DP-1.0-20160405.t.3', '--volid=DP-1.0-20160405.t.3',
'--graft-points=graft-list', '--graft-points=graft-list',
@ -72,13 +76,13 @@ class OstreeScriptTest(helpers.PungiTestCase):
'-no-emul-boot', '-no-emul-boot',
'-o', 'DP-1.0-20160405.t.3-x86_64.iso', '-o', 'DP-1.0-20160405.t.3-x86_64.iso',
'-graft-points', '-path-list', 'graft-list'], '-graft-points', '-path-list', 'graft-list'],
show_cmd=True, stdout=True), show_cmd=True, stdout=True, workdir=self.outdir),
mock.call(['pungi-pylorax-find-templates', '/usr/share/lorax'], mock.call(['pungi-pylorax-find-templates', '/usr/share/lorax'],
show_cmd=True, stdout=True), show_cmd=True, stdout=True),
mock.call(['/usr/bin/implantisomd5', 'DP-1.0-20160405.t.3-x86_64.iso'], mock.call(['/usr/bin/implantisomd5', 'DP-1.0-20160405.t.3-x86_64.iso'],
show_cmd=True, stdout=True), show_cmd=True, stdout=True, workdir=self.outdir),
mock.call('isoinfo -R -f -i DP-1.0-20160405.t.3-x86_64.iso | grep -v \'/TRANS.TBL$\' | sort >> DP-1.0-20160405.t.3-x86_64.iso.manifest', mock.call('isoinfo -R -f -i DP-1.0-20160405.t.3-x86_64.iso | grep -v \'/TRANS.TBL$\' | sort >> DP-1.0-20160405.t.3-x86_64.iso.manifest',
show_cmd=True, stdout=True)] show_cmd=True, stdout=True, workdir=self.outdir)]
) )
@mock.patch('kobo.shortcuts.run') @mock.patch('kobo.shortcuts.run')
@ -86,7 +90,7 @@ class OstreeScriptTest(helpers.PungiTestCase):
run.return_value = (0, '/usr/share/lorax') run.return_value = (0, '/usr/share/lorax')
createiso.main([ createiso.main([
'--output-dir={}/isos'.format(self.topdir), '--output-dir={}'.format(self.outdir),
'--iso-name=DP-1.0-20160405.t.3-ppc64.iso', '--iso-name=DP-1.0-20160405.t.3-ppc64.iso',
'--volid=DP-1.0-20160405.t.3', '--volid=DP-1.0-20160405.t.3',
'--graft-points=graft-list', '--graft-points=graft-list',
@ -106,19 +110,19 @@ class OstreeScriptTest(helpers.PungiTestCase):
'-hfs-bless', '/ppc/mac', '-hfs-bless', '/ppc/mac',
'-o', 'DP-1.0-20160405.t.3-ppc64.iso', '-o', 'DP-1.0-20160405.t.3-ppc64.iso',
'-graft-points', '-path-list', 'graft-list'], '-graft-points', '-path-list', 'graft-list'],
show_cmd=True, stdout=True), show_cmd=True, stdout=True, workdir=self.outdir),
mock.call(['pungi-pylorax-find-templates', '/usr/share/lorax'], mock.call(['pungi-pylorax-find-templates', '/usr/share/lorax'],
show_cmd=True, stdout=True), show_cmd=True, stdout=True),
mock.call(['/usr/bin/implantisomd5', 'DP-1.0-20160405.t.3-ppc64.iso'], mock.call(['/usr/bin/implantisomd5', 'DP-1.0-20160405.t.3-ppc64.iso'],
show_cmd=True, stdout=True), show_cmd=True, stdout=True, workdir=self.outdir),
mock.call('isoinfo -R -f -i DP-1.0-20160405.t.3-ppc64.iso | grep -v \'/TRANS.TBL$\' | sort >> DP-1.0-20160405.t.3-ppc64.iso.manifest', mock.call('isoinfo -R -f -i DP-1.0-20160405.t.3-ppc64.iso | grep -v \'/TRANS.TBL$\' | sort >> DP-1.0-20160405.t.3-ppc64.iso.manifest',
show_cmd=True, stdout=True)] show_cmd=True, stdout=True, workdir=self.outdir)]
) )
@mock.patch('kobo.shortcuts.run') @mock.patch('kobo.shortcuts.run')
def test_bootable_run_buildinstall(self, run): def test_bootable_run_buildinstall(self, run):
createiso.main([ createiso.main([
'--output-dir={}/isos'.format(self.topdir), '--output-dir={}'.format(self.outdir),
'--iso-name=DP-1.0-20160405.t.3-ppc64.iso', '--iso-name=DP-1.0-20160405.t.3-ppc64.iso',
'--volid=DP-1.0-20160405.t.3', '--volid=DP-1.0-20160405.t.3',
'--graft-points=graft-list', '--graft-points=graft-list',
@ -139,11 +143,11 @@ class OstreeScriptTest(helpers.PungiTestCase):
'-hfs-bless', '/ppc/mac', '-hfs-bless', '/ppc/mac',
'-o', 'DP-1.0-20160405.t.3-ppc64.iso', '-o', 'DP-1.0-20160405.t.3-ppc64.iso',
'-graft-points', '-path-list', 'graft-list'], '-graft-points', '-path-list', 'graft-list'],
show_cmd=True, stdout=True), show_cmd=True, stdout=True, workdir=self.outdir),
mock.call(['/usr/bin/implantisomd5', 'DP-1.0-20160405.t.3-ppc64.iso'], mock.call(['/usr/bin/implantisomd5', 'DP-1.0-20160405.t.3-ppc64.iso'],
show_cmd=True, stdout=True), show_cmd=True, stdout=True, workdir=self.outdir),
mock.call('isoinfo -R -f -i DP-1.0-20160405.t.3-ppc64.iso | grep -v \'/TRANS.TBL$\' | sort >> DP-1.0-20160405.t.3-ppc64.iso.manifest', mock.call('isoinfo -R -f -i DP-1.0-20160405.t.3-ppc64.iso | grep -v \'/TRANS.TBL$\' | sort >> DP-1.0-20160405.t.3-ppc64.iso.manifest',
show_cmd=True, stdout=True)] show_cmd=True, stdout=True, workdir=self.outdir)]
) )
@mock.patch('sys.stderr') @mock.patch('sys.stderr')
@ -151,7 +155,7 @@ class OstreeScriptTest(helpers.PungiTestCase):
def test_run_with_jigdo_bad_args(self, run, stderr): def test_run_with_jigdo_bad_args(self, run, stderr):
with self.assertRaises(SystemExit): with self.assertRaises(SystemExit):
createiso.main([ createiso.main([
'--output-dir={}/isos'.format(self.topdir), '--output-dir={}'.format(self.outdir),
'--iso-name=DP-1.0-20160405.t.3-x86_64.iso', '--iso-name=DP-1.0-20160405.t.3-x86_64.iso',
'--volid=DP-1.0-20160405.t.3', '--volid=DP-1.0-20160405.t.3',
'--graft-points=graft-list', '--graft-points=graft-list',
@ -162,7 +166,7 @@ class OstreeScriptTest(helpers.PungiTestCase):
@mock.patch('kobo.shortcuts.run') @mock.patch('kobo.shortcuts.run')
def test_run_with_jigdo(self, run): def test_run_with_jigdo(self, run):
createiso.main([ createiso.main([
'--output-dir={}/isos'.format(self.topdir), '--output-dir={}'.format(self.outdir),
'--iso-name=DP-1.0-20160405.t.3-x86_64.iso', '--iso-name=DP-1.0-20160405.t.3-x86_64.iso',
'--volid=DP-1.0-20160405.t.3', '--volid=DP-1.0-20160405.t.3',
'--graft-points=graft-list', '--graft-points=graft-list',
@ -179,17 +183,17 @@ class OstreeScriptTest(helpers.PungiTestCase):
'-input-charset', 'utf-8', '-x', './lost+found', '-input-charset', 'utf-8', '-x', './lost+found',
'-o', 'DP-1.0-20160405.t.3-x86_64.iso', '-o', 'DP-1.0-20160405.t.3-x86_64.iso',
'-graft-points', '-path-list', 'graft-list'], '-graft-points', '-path-list', 'graft-list'],
show_cmd=True, stdout=True), show_cmd=True, stdout=True, workdir=self.outdir),
mock.call(['/usr/bin/implantisomd5', 'DP-1.0-20160405.t.3-x86_64.iso'], mock.call(['/usr/bin/implantisomd5', 'DP-1.0-20160405.t.3-x86_64.iso'],
show_cmd=True, stdout=True), show_cmd=True, stdout=True, workdir=self.outdir),
mock.call('isoinfo -R -f -i DP-1.0-20160405.t.3-x86_64.iso | grep -v \'/TRANS.TBL$\' | sort >> DP-1.0-20160405.t.3-x86_64.iso.manifest', mock.call('isoinfo -R -f -i DP-1.0-20160405.t.3-x86_64.iso | grep -v \'/TRANS.TBL$\' | sort >> DP-1.0-20160405.t.3-x86_64.iso.manifest',
show_cmd=True, stdout=True), show_cmd=True, stdout=True, workdir=self.outdir),
mock.call(['jigdo-file', 'make-template', '--force', mock.call(['jigdo-file', 'make-template', '--force',
'--image={}/isos/DP-1.0-20160405.t.3-x86_64.iso'.format(self.topdir), '--image={}/isos/DP-1.0-20160405.t.3-x86_64.iso'.format(self.topdir),
'--jigdo={}/jigdo/DP-1.0-20160405.t.3-x86_64.iso.jigdo'.format(self.topdir), '--jigdo={}/jigdo/DP-1.0-20160405.t.3-x86_64.iso.jigdo'.format(self.topdir),
'--template={}/jigdo/DP-1.0-20160405.t.3-x86_64.iso.template'.format(self.topdir), '--template={}/jigdo/DP-1.0-20160405.t.3-x86_64.iso.template'.format(self.topdir),
'--no-servers-section', '--report=noprogress', self.topdir + '/os//'], '--no-servers-section', '--report=noprogress', self.topdir + '/os//'],
show_cmd=True, stdout=True)] show_cmd=True, stdout=True, workdir=self.outdir)]
) )