From e5443493bf348993378e1dc59cafde315f851d9b Mon Sep 17 00:00:00 2001 From: Jussi Pakkanen Date: Sun, 30 Jun 2013 22:30:11 +0300 Subject: [PATCH] Can run local script files as well as commands. --- interpreter.py | 23 ++++++++++++++----- test cases/common/38 run program/meson.build | 17 ++++++++++++++ .../common/38 run program/scripts/hello.sh | 3 +++ 3 files changed, 37 insertions(+), 6 deletions(-) create mode 100755 test cases/common/38 run program/scripts/hello.sh diff --git a/interpreter.py b/interpreter.py index f5c04e821..0be909a01 100644 --- a/interpreter.py +++ b/interpreter.py @@ -39,12 +39,9 @@ class InterpreterObject(): class RunProcess(InterpreterObject): - def __init__(self, command_array): + def __init__(self, command_array, curdir): super().__init__() - try: - pc = subprocess.Popen(command_array, stdout=subprocess.PIPE, stderr=subprocess.PIPE) - except FileNotFoundError: - raise InterpreterException('Command "%s" not found.' % command_array[0]) + pc = self.run_command(command_array, curdir) (stdout, stderr) = pc.communicate() self.returncode = pc.returncode self.stdout = stdout.decode() @@ -53,6 +50,20 @@ class RunProcess(InterpreterObject): 'stdout' : self.stdout_method, 'stderr' : self.stderr_method, }) + + def run_command(self, command_array, curdir): + cmd_name = command_array[0] + try: + return subprocess.Popen(command_array, stdout=subprocess.PIPE, stderr=subprocess.PIPE) + except FileNotFoundError: + pass + # Was not a command, try to run as a script relative to current dir. + fullpath = os.path.join(curdir, command_array[0]) + command_array = [fullpath] + command_array[1:] + try: + return subprocess.Popen(command_array, stdout=subprocess.PIPE, stderr=subprocess.PIPE) + except FileNotFoundError: + raise InterpreterException('Could not execute command "%s".' % cmd_name) def returncode_method(self, args, kwargs): return self.returncode @@ -799,7 +810,7 @@ class Interpreter(): for i in args: if not isinstance(i, str): raise InterpreterObject('Run_command arguments must be strings.') - return RunProcess(args) + return RunProcess(args, os.path.join(self.environment.source_dir, self.subdir)) def func_configuration_data(self, node, args, kwargs): if len(args) != 0: diff --git a/test cases/common/38 run program/meson.build b/test cases/common/38 run program/meson.build index 2c23985ea..e03564378 100644 --- a/test cases/common/38 run program/meson.build +++ b/test cases/common/38 run program/meson.build @@ -16,3 +16,20 @@ endif if c.stderr() != '' error('Extra text in stderr.') endif + +# Now the same with a script. + +cs = run_command('scripts/hello.sh') +correct = 'hello' + +if cs.returncode() != 0 + error('Executing script failed.') +endif + +if cs.stdout().strip() != correct + error('Getting stdout failed (script).') +endif + +if cs.stderr() != '' + error('Extra text in stderr (script).') +endif diff --git a/test cases/common/38 run program/scripts/hello.sh b/test cases/common/38 run program/scripts/hello.sh new file mode 100755 index 000000000..2a22daa88 --- /dev/null +++ b/test cases/common/38 run program/scripts/hello.sh @@ -0,0 +1,3 @@ +#!/bin/sh + +echo hello