Can run local script files as well as commands.

pull/15/head
Jussi Pakkanen 12 years ago
parent a52336b429
commit e5443493bf
  1. 23
      interpreter.py
  2. 17
      test cases/common/38 run program/meson.build
  3. 3
      test cases/common/38 run program/scripts/hello.sh

@ -39,12 +39,9 @@ class InterpreterObject():
class RunProcess(InterpreterObject): class RunProcess(InterpreterObject):
def __init__(self, command_array): def __init__(self, command_array, curdir):
super().__init__() super().__init__()
try: pc = self.run_command(command_array, curdir)
pc = subprocess.Popen(command_array, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
except FileNotFoundError:
raise InterpreterException('Command "%s" not found.' % command_array[0])
(stdout, stderr) = pc.communicate() (stdout, stderr) = pc.communicate()
self.returncode = pc.returncode self.returncode = pc.returncode
self.stdout = stdout.decode() self.stdout = stdout.decode()
@ -54,6 +51,20 @@ class RunProcess(InterpreterObject):
'stderr' : self.stderr_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): def returncode_method(self, args, kwargs):
return self.returncode return self.returncode
@ -799,7 +810,7 @@ class Interpreter():
for i in args: for i in args:
if not isinstance(i, str): if not isinstance(i, str):
raise InterpreterObject('Run_command arguments must be strings.') 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): def func_configuration_data(self, node, args, kwargs):
if len(args) != 0: if len(args) != 0:

@ -16,3 +16,20 @@ endif
if c.stderr() != '' if c.stderr() != ''
error('Extra text in stderr.') error('Extra text in stderr.')
endif 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

Loading…
Cancel
Save