19
from pathlib import Path
20
from typing import List, Tuple
22
if __name__ not in ("__main__", "__mp_main__"):
24
"This file is intended to be executed as an executable program. You cannot use "
25
"it as a module.To run this script, run the ./{__file__} command"
29
def is_relative_to(path: Path, *other):
30
"""Return True if the path is relative to another path or False.
32
This function is backported from Python 3.9 - Path.relativeto.
35
path.relative_to(*other)
42
prog = Path(__file__).name
46
usage: {prog} [-h] SUBDIRECTORY ARGS [ARGS ...]
48
Runs the program in a subdirectory and fix paths in arguments.
52
When this program is executed with the following command:
53
{prog} frontend/ yarn eslint frontend/src/index.ts
54
Then the command will be executed:
55
yarn eslint src/index.ts
56
and the current working directory will be set to frontend/
59
SUBDIRECTORY subdirectory within which the subprocess will be executed
60
ARGS sequence of program arguments
63
-h, --help show this help message and exit\
69
def parse_args() -> Tuple[str, List[str]]:
70
if len(sys.argv) == 2 and sys.argv[1] in ("-h", "--help"):
74
print("Missing arguments")
79
return sys.argv[1], sys.argv[2:]
82
def fix_arg(subdirectory: str, arg: str) -> str:
84
if not (arg_path.exists() and is_relative_to(arg_path, subdirectory)):
86
return str(arg_path.relative_to(subdirectory))
89
def try_as_shell(fixed_args: List[str], subdirectory: str):
97
print("Direct call failed, trying as shell command:")
98
shell_cmd = shlex.join(fixed_args)
101
subprocess.run(shell_cmd, cwd=subdirectory, check=True, shell=True)
102
except subprocess.CalledProcessError as ex:
103
sys.exit(ex.returncode)
107
subdirectory, subprocess_args = parse_args()
109
fixed_args = [fix_arg(subdirectory, arg) for arg in subprocess_args]
111
subprocess.run(fixed_args, cwd=subdirectory, check=True)
112
except subprocess.CalledProcessError as ex:
113
sys.exit(ex.returncode)
114
except FileNotFoundError:
115
if "win32" in sys.platform:
116
try_as_shell(fixed_args, subdirectory)