def generate_scripts_from_multisheller_file(multisheller_file,
                                            generation_directory,
                                            name_without_extension):
    # Parse msh file
    with open(multisheller_file) as fi:
        contents = fi.read()
    contents = contents
    tree = ast.parse(contents)

    ls = locals().copy()
    cmds = []
    for expr in tree.body:
        codeobj = compile(ast.Expression(expr.value), '<string>', mode='eval')
        res = eval(codeobj, globals(), ls)
        if type(expr) == ast.Assign:
            for t in expr.targets:
                ls.update({t.id: res})
        else:
            cmds.append(res)

    # Generate scripts
    write_script(generation_directory, name_without_extension, cmds, "cmdexe")
    write_script(generation_directory, name_without_extension, cmds, "bash")
    write_script(generation_directory, name_without_extension, cmds, "zsh")
    write_script(generation_directory, name_without_extension, cmds, "xonsh")
    write_script(generation_directory, name_without_extension, cmds,
                 "powershell")

    # Conda do not explicitly support different activation scripts for zsh and bash,
    # so we generate them as .bash and .zsh (ignored by conda) and then generate a
    # small .sh script to source the correct one depending on the used shell
    write_bash_zsh_disambiguation_script(generation_directory,
                                         name_without_extension)
def generate_scripts_from_multisheller_file(multisheller_file,
                                            generation_directory,
                                            name_without_extension):
    # Parse msh file
    with open(multisheller_file) as fi:
        contents = fi.read()
    contents = contents
    tree = ast.parse(contents)

    ls = locals().copy()
    cmds = []
    for expr in tree.body:
        codeobj = compile(ast.Expression(expr.value), '<string>', mode='eval')
        res = eval(codeobj, globals(), ls)
        if type(expr) == ast.Assign:
            for t in expr.targets:
                ls.update({t.id: res})
        else:
            cmds.append(res)

    # Generate scripts
    write_script(generation_directory, name_without_extension, cmds, "cmdexe")
    write_script(generation_directory, name_without_extension, cmds, "bash")
    write_script(generation_directory, name_without_extension, cmds, "zsh")
    write_script(generation_directory, name_without_extension, cmds, "xonsh")
    write_script(generation_directory, name_without_extension, cmds,
                 "powershell")
Esempio n. 3
0
def translate_file(f, output):
    with open(f) as fi:
        contents = fi.read()
    contents = contents
    tree = ast.parse(contents)

    ls = locals().copy()
    cmds = []
    for expr in tree.body:
        codeobj = compile(ast.Expression(expr.value), '<string>', mode='eval')
        res = eval(codeobj, globals(), ls)
        if type(expr) == ast.Assign:
            for t in expr.targets:
                ls.update({t.id: res})
        else:
            cmds.append(res)

    # Generate scripts
    generation_directory, name_without_extension = os.path.split(output)

    write_script(generation_directory, cmds, "cmdexe", name_without_extension)
    write_script(generation_directory, cmds, "bash", name_without_extension)
    write_script(generation_directory, cmds, "zsh", name_without_extension)
    write_script(generation_directory, cmds, "xonsh", name_without_extension)
    write_script(generation_directory, cmds, "powershell",
                 name_without_extension)

    # Conda do not explicitly support different activation scripts for zsh and bash,
    # so we generate them as .bash and .zsh (ignored by conda) and then generate a
    # small .sh script to source the correct one depending on the used shell
    # See https://github.com/mamba-org/multisheller/issues/11
    write_bash_zsh_disambiguation_script(generation_directory,
                                         name_without_extension)
Esempio n. 4
0
def call_interpreter(s, tmp_path, interpreter):
    f = write_script(tmp_path, s, interpreter)
    if interpreter not in enable_on_os[running_os]:
        return None, None

    if interpreter == 'cmdexe':
        res = subprocess.run(['cmd.exe', '/C', f],
                             stdout=subprocess.PIPE,
                             stderr=subprocess.PIPE,
                             check=True)
    elif interpreter == 'powershell':
        res = subprocess.run(["powershell.exe", "-File", f],
                             stdout=subprocess.PIPE,
                             stderr=subprocess.PIPE,
                             check=True)
    else:
        res = subprocess.run([interpreter, f],
                             stdout=subprocess.PIPE,
                             stderr=subprocess.PIPE,
                             check=True)
    stdout = res.stdout.decode('utf-8').strip()
    stderr = res.stderr.decode('utf-8').strip()
    return stdout, stderr