prefix-dev / shell

The ultimate cross-platform, bash-like shell
MIT License
53 stars 8 forks source link

Make `subprocess.run(cmd, shell=True)` call `shell` #180

Open certik opened 4 days ago

certik commented 4 days ago

I want to replace this code:

import subprocess as sp

def run_cmd(cmd, cwd=None):
    print(f"+ {cmd}")
    process = sp.run(cmd, shell=True, cwd=cwd)
    if process.returncode != 0:
        print("Command failed.")
        exit(1)

with an equivalent code that however calls shell to execute the above command, not cmd.exe. Currently I get:

~/repos/lfortran/integration_tests$ python run_tests.py
+ rm -rf C:\Users\ondrejcertik\repos\lfortran\integration_tests/test-llvm
'rm' is not recognized as an internal or external command,
operable program or batch file.
Command failed.

What's the best way to do that?

Hofer-Julian commented 4 days ago

Maybe this?

import subprocess as sp

def run_cmd(cmd, cwd=None):
    print(f"+ {cmd}")
    process = sp.run(["shell", "-c", cmd], cwd=cwd)
    if process.returncode != 0:
        print("Command failed.")
        exit(1)
certik commented 4 days ago

Yes, that's what I would like --- but we need to implement -c first.