Beispiel #1
0
def get_command(global_vals, stage_configs, stage):
    stage_config = stage_configs[stage]
    execution = stage_config['exec']
    if not (execution in ['python', 'python3']):
        raise_exception("Only Python execution "
                        "is currently supported. "
                        "Shell support will be "
                        "added soon.")
    script_name = stage_config['script']
    global_opts = stage_config.get('globals', [])
    arg = stage_config.get('arg', '')
    if not (isinstance(arg, str)):
        raise_exception(
            f"arg should be a string. {artg} in {stage} does not satisfy this."
        )
    arg = [arg] if arg != '' else []
    options = stage_config.get('options', {})
    optkeys = list(options.keys())
    for global_opt in global_opts:
        if global_opt in optkeys:
            pprint(stage_configs[stage])
            pprint(stage)
            raise_exception(
                f"{global_opt} in {stage} config is already a global.")
        options[global_opt] = global_vals[global_opt]
    unparser = argunparse.ArgumentUnparser()
    unparsed = unparser.unparse(*arg, **options)
    if unparsed == '\"\"' or unparsed == '\'\'': unparsed = ''
    #TODO: check for malicious content in unparsed string since it comes
    #from an external library
    return execution, script_name, unparsed
Beispiel #2
0
def run_castxml(input_path: pathlib.Path, output_path: pathlib.Path, gcc: bool = False):
    """Run CastXML with given arguments."""
    args = [input_path]
    kwargs = {}
    if gcc:
        kwargs['castxml-gccxml'] = True
    else:
        kwargs['castxml-output=1'] = True
    kwargs['castxml-cc-gnu'] = 'g++'
    kwargs['o'] = str(output_path)
    return run_tool(CASTXML_PATH, args, kwargs,
                    argunparser=argunparse.ArgumentUnparser(opt_value=' '))
Beispiel #3
0
def call_tool(function,
              args=(),
              kwargs=None,
              cwd: pathlib.Path = None,
              commandline_equivalent: str = None,
              capture_output: bool = True) -> subprocess.CompletedProcess:
    """Call a given function with given arguments and report result as if it was a subprocess.

    Assumption is that the function returns a numeric return code, just as a subprocess would.
    """
    if kwargs is None:
        kwargs = {}
    if not capture_output:
        redirector = fake_context_manager
        stdout = None
        stderr = None
    elif platform.system() == 'Linux':
        redirector = redirect_stdout_and_stderr_via_fd
        stdout = tempfile.NamedTemporaryFile('w+', delete=False)
        stderr = tempfile.NamedTemporaryFile('w+', delete=False)
    else:
        redirector = redirect_stdout_and_stderr
        stdout = io.StringIO()
        stderr = io.StringIO()
    _LOG.debug('calling tool %s(*%s, **%s) (simulating: %s) ...', function,
               args, kwargs, commandline_equivalent)
    with temporarily_change_dir(cwd):
        with redirector(stdout, stderr):
            returncode = function(*args, **kwargs)
    if not capture_output:
        stdout_str = ''
        stderr_str = ''
    elif platform.system() == 'Linux':
        with open(stdout.name) as _:
            stdout_str = _.read()
        with open(stderr.name) as _:
            stderr_str = _.read()
    else:
        stdout_str = stdout.getvalue()
        stderr_str = stderr.getvalue()
    if commandline_equivalent is None:
        argunparser = argunparse.ArgumentUnparser()
        commandline_equivalent = '{} {}'.format(
            function.__name__,
            argunparser.unparse_options_and_args(kwargs, args))
    result = subprocess.CompletedProcess(args=commandline_equivalent,
                                         stdout=stdout_str,
                                         stderr=stderr_str,
                                         returncode=returncode)
    summarize_completed_process(result, actual_call=(function, args, kwargs))
    return result
Beispiel #4
0
def run_castxml(input_path: pathlib.Path, output_path: pathlib.Path, gcc: bool = False):
    """Run CastXML with given arguments."""
    args = ['-std=c++17', '-fcolor-diagnostics', input_path]
    kwargs = {}
    if gcc:
        kwargs['castxml-gccxml'] = True
    else:
        kwargs['castxml-output=1'] = True
    if platform.system() == 'Linux':
        kwargs['castxml-cc-gnu'] = 'g++'
    elif platform.system() == 'Darwin':
        kwargs['castxml-cc-gnu'] = 'clang++'
    kwargs['o'] = str(output_path)
    return run_tool(CASTXML_PATH, args, kwargs,
                    argunparser=argunparse.ArgumentUnparser(opt_value=' '))
Beispiel #5
0
def run_tool(executable: pathlib.Path, args=(), kwargs=None, cwd: pathlib.Path = None,
             argunparser: argunparse.ArgumentUnparser = None) -> subprocess.CompletedProcess:
    """Run a given executable with given arguments."""
    if kwargs is None:
        kwargs = {}
    if argunparser is None:
        argunparser = argunparse.ArgumentUnparser()
    command = [str(executable)] + argunparser.unparse_options_and_args(kwargs, args, to_list=True)
    run_kwargs = {'stdout': subprocess.PIPE, 'stderr': subprocess.PIPE}
    if cwd is not None:
        run_kwargs['cwd'] = str(cwd)
    result = subprocess.run(command, **run_kwargs)
    _LOG.debug('return code of "%s" tool: %s', executable, result)
    _postprocess_result(result)
    if result.returncode != 0:
        _LOG.error('%s', result)
        raise RuntimeError('execution of "{}" failed: {}'.format(executable.name, result))
    return result
Beispiel #6
0
def run_tool(
    executable: pathlib.Path,
    args=(),
    kwargs=None,
    cwd: pathlib.Path = None,
    argunparser: argunparse.ArgumentUnparser = None
) -> subprocess.CompletedProcess:
    """Run a given executable with given arguments."""
    if kwargs is None:
        kwargs = {}
    if argunparser is None:
        argunparser = argunparse.ArgumentUnparser()
    command = [str(executable)] + argunparser.unparse_options_and_args(
        kwargs, args, to_list=True)
    run_kwargs = {'stdout': subprocess.PIPE, 'stderr': subprocess.PIPE}
    if cwd is not None:
        run_kwargs['cwd'] = str(cwd)
    _LOG.debug('running tool %s ...', command)
    result = subprocess.run(command, **run_kwargs)
    _LOG.debug('return code of "%s" tool: %s', executable, result)
    summarize_completed_process(result, executable=executable)
    return result
Beispiel #7
0
def call_tool(function, args=(), kwargs=None, cwd: pathlib.Path = None,
              commandline_equivalent: str = None) -> subprocess.CompletedProcess:
    """Call a given function with given arguments and report result as if it was a subprocess.

    Assumption is that the function returns a numeric return code, just as a subprocess would.
    """
    if kwargs is None:
        kwargs = {}
    stdout = io.StringIO()
    stderr = io.StringIO()
    with temporarily_change_dir(cwd):
        with redirect_stdout_and_stderr(stdout, stderr):
            returncode = function(*args, **kwargs)
    if commandline_equivalent is None:
        argunparser = argunparse.ArgumentUnparser()
        commandline_equivalent = '{} {}'.format(
            function.__name__, argunparser.unparse_options_and_args(kwargs, args))
    result = subprocess.CompletedProcess(
        args=commandline_equivalent, stdout=stdout.getvalue(), stderr=stderr.getvalue(),
        returncode=returncode)
    _postprocess_result(result)
    if result.returncode != 0:
        raise RuntimeError('execution of {}() failed: {}'.format(function.__name__, result))
    return result
Beispiel #8
0
 def __init__(self, language: Language):
     super().__init__()
     self.language = language
     self.argunparser = argunparse.ArgumentUnparser()
 def __init__(self, f_compiler: CompilerInterface = None, *args, **kwargs):
     self.argunparser = argunparse.ArgumentUnparser()
     if f_compiler is None:
         f_compiler = GfortranInterface()
     self.f_compiler = f_compiler
     super().__init__(*args, **kwargs)
Beispiel #10
0
 def __init__(self, f_compiler=None, *args, **kwargs):
     super().__init__(*args, **kwargs)
     self.argunparser = argunparse.ArgumentUnparser()
     self.f2py = F2pyInterface(f_compiler)
Beispiel #11
0
 def __init__(self, *args, **kwargs):
     super().__init__(*args, **kwargs)
     self.argunparser = argunparse.ArgumentUnparser()