|
| 1 | +import pathlib |
| 2 | +import random |
| 3 | +import string |
1 | 4 | import subprocess |
2 | 5 |
|
3 | | -import compas_rhino |
| 6 | +import click |
4 | 7 |
|
| 8 | +rhinocode = pathlib.Path().home() / ".rhinocode" |
| 9 | +rhinopython = rhinocode / "py39-rh8" / "python3.9" |
| 10 | +site_envs = rhinocode / "py39-rh8" / "site-envs" |
5 | 11 |
|
6 | | -def install(args): |
7 | | - subprocess.check_call([compas_rhino._get_default_rhino_cpython_path("8.0"), "-m", "pip", "install"] + args) |
8 | 12 |
|
| 13 | +def random_string(length=8) -> str: |
| 14 | + return "".join(random.choices(string.ascii_letters + string.digits, k=length)) |
| 15 | + |
| 16 | + |
| 17 | +def find_full_env_name(name: str) -> str: |
| 18 | + for filepath in site_envs.iterdir(): |
| 19 | + if filepath.stem.startswith(name): |
| 20 | + return filepath.stem |
| 21 | + raise ValueError(f"No environment with this name exists: {name}") |
| 22 | + |
| 23 | + |
| 24 | +def default_env_name() -> str: |
| 25 | + return find_full_env_name("default") |
| 26 | + |
| 27 | + |
| 28 | +def ensure_site_env(name: str) -> str: |
| 29 | + try: |
| 30 | + fullname = find_full_env_name(name) |
| 31 | + except ValueError: |
| 32 | + fullname = f"{name}-{random_string()}" |
| 33 | + return fullname |
9 | 34 |
|
10 | | -if __name__ == "__main__": |
11 | | - import argparse |
12 | 35 |
|
13 | | - parser = argparse.ArgumentParser() |
| 36 | +@click.command() |
| 37 | +@click.argument("package") |
| 38 | +@click.option("--env", default="default", help="Name of the site env, without the random suffix...") |
| 39 | +@click.option("--upgrade/--no-upgrade", default=False) |
| 40 | +@click.option("--deps/--no-deps", default=True) |
| 41 | +def install_package(package, env, upgrade, deps): |
| 42 | + if package == ".": |
| 43 | + package = pathlib.Path().cwd() |
| 44 | + elif package == "..": |
| 45 | + package = pathlib.Path().cwd().parent |
14 | 46 |
|
15 | | - parser.add_argument("pipargs", help="Arguments to be passed on to pip as a string") |
| 47 | + target = site_envs / ensure_site_env(env or "default") |
| 48 | + target.mkdir(exist_ok=True) |
16 | 49 |
|
17 | | - args = parser.parse_args() |
18 | | - pipargs = args.pipargs.split() |
| 50 | + args = [ |
| 51 | + str(rhinopython), |
| 52 | + "-m", |
| 53 | + "pip", |
| 54 | + "install", |
| 55 | + package, |
| 56 | + "--target", |
| 57 | + target, |
| 58 | + "--no-warn-script-location", |
| 59 | + ] |
19 | 60 |
|
20 | | - install(pipargs) |
| 61 | + if upgrade: |
| 62 | + args.append("--upgrade") |
| 63 | + |
| 64 | + if not deps: |
| 65 | + args.append("--no-deps") |
| 66 | + |
| 67 | + return subprocess.check_call(args) |
| 68 | + |
| 69 | + |
| 70 | +# ============================================================================= |
| 71 | +# Main |
| 72 | +# ============================================================================= |
| 73 | + |
| 74 | +if __name__ == "__main__": |
| 75 | + install_package() |
0 commit comments