def __init__(self, script=None): venv_parent_dir = working_directory() / 'replay_virtualenvs' self.datastore = Memory() self.context = context.Context( self.datastore, venv_parent_dir, working_directory() / 'temp', self._local_pypi_url) if script: self.plugin = list(self.context.load_plugins(script))[0]
def _file_pairs(self, copy_spec): datastore = self.context.datastore working_directory = externals.working_directory() for spec in copy_spec: for local_file, ds_file in spec.iteritems(): yield (working_directory / local_file), (datastore / ds_file)
def parse_args(args): parser = argparse.ArgumentParser() # optional parameters, with defaults parser.add_argument( "--datastore", "--ds", default=externals.working_directory().path, help="Persistent place for data (default: %(default)s)", ) parser.add_argument( "--script-working-directory", "--dir", default=TEMPORARY_DIRECTORY, help="Run script[s] under this directory - somewhere" " (default: NEW TEMPORARY DIRECTORY)", ) parser.add_argument( "--virtualenv-parent-directory", "--venvs", default=get_virtualenv_parent_dir(), help="Use this directory to cache python virtual environments" " (default: %(default)s)", ) # mandatory parameter parser.add_argument("script_path", help="Script to run") args = parser.parse_args(args) # fix script_path args.script_path = externals.File(args.script_path).path return args
def test_run_with_explicit_working_directory(self): getcwd_script = pkg_resources.resource_filename( 'replay', 'tests/fixtures/scripts/getcwd.script') ds = working_directory() / 'datastore' wd = working_directory() / 'script_working_directory' command = [ 'replay', '--script-working-directory=' + wd.path, '--datastore=' + ds.path, getcwd_script] with mock.patch('sys.argv', command): m.main() self.assertEqual( wd.path.encode('utf8'), (ds / 'working_directory').content)
def test_run_with_defaults(self): to_roman_script = pkg_resources.resource_filename( 'replay', 'tests/fixtures/scripts/to_roman.script') ds = working_directory() (ds / 'arab').content = b'23' with mock.patch('sys.argv', ['replay', to_roman_script]): m.main() self.assertEqual(b'XXIII', (ds / 'roman').content)
def test_defaults(self): args = m.parse_args(['dir/scriptname.py']) self.assertEqual(working_directory().path, args.datastore) self.assertEqual( m.TEMPORARY_DIRECTORY, args.script_working_directory) self.assertEqual( m.get_virtualenv_parent_dir(), args.virtualenv_parent_directory)
def test_outputs_are_uploaded_to_datastore(self): f = fixtures.PluginContext( """\ Outputs: - an output file: /output/datastore/path """ ) with f.plugin: (externals.working_directory() / "an output file").content = b"data" self.assertEqual(b"data", (f.datastore / "output/datastore/path").content)
def test_inputs_are_downloaded_from_datastore(self): f = fixtures.PluginContext( """\ Inputs: - an input file: input/datastore/path """ ) (f.datastore / "input/datastore/path").content = b"hello" with f.plugin: self.assertEqual(b"hello", (externals.working_directory() / "an input file").content)
def test_short_inline_shell_script_executed(self): wd = externals.working_directory() (wd / "script").content = b"echo hello from /bin/sh > output" f = fixtures.PluginContext( u"""\ Execute: chmod +x script; ./script """ ) with f.plugin: pass self.assertEqual(b"hello from /bin/sh", (wd / "output").content.rstrip())
def test_python_script_executed(self): wd = externals.working_directory() (wd / "script.py").content = b"""open('output', 'w').write('hello from python')""" f = fixtures.PluginContext( u"""\ Execute: python script.py """ ) with f.plugin: pass self.assertEqual(b"hello from python", (wd / "output").content.rstrip())
def test_relative_script_path(self): args = m.parse_args(['relative/script/path/scriptname.py']) self.assertEqual( (working_directory() / 'relative/script/path/scriptname.py').path, args.script_path)