|
11 | 11 | import shutil |
12 | 12 | import tempfile |
13 | 13 | from pathlib import Path |
| 14 | +from typing import List |
14 | 15 |
|
15 | 16 | import invoke |
16 | 17 | import requests |
@@ -83,6 +84,41 @@ def _get_package_name(toml_file: str) -> str: |
83 | 84 | return name |
84 | 85 |
|
85 | 86 |
|
| 87 | +def _sanitize_dependency(dep: str) -> str: |
| 88 | + # HACK: Remove upper bound constraints (e.g., ", <3" or ",<3") as Rhino currenly doesn't support them |
| 89 | + # https://discourse.mcneel.com/t/python-dependencies-with-ordered-comparison-syntax/212304 |
| 90 | + sanitized = re.split(r"\s*,\s*[<>=]", dep)[0] |
| 91 | + sanitized = sanitized.split("#")[0].strip() # Remove inline comments |
| 92 | + return sanitized |
| 93 | + |
| 94 | + |
| 95 | +def _get_deps_from_requirements(req_filepath: str) -> str: |
| 96 | + with open(req_filepath, "r") as req_file: |
| 97 | + dependencies = [] |
| 98 | + for line in req_file: |
| 99 | + line = line.strip() |
| 100 | + if line and not line.startswith("#"): |
| 101 | + dependencies.append(_sanitize_dependency(line)) |
| 102 | + return dependencies |
| 103 | + |
| 104 | + |
| 105 | +def _get_dependencies(base_folder: str) -> List[str]: |
| 106 | + toml_filepath = os.path.join(base_folder, "pyproject.toml") |
| 107 | + with open(toml_filepath, "r") as f: |
| 108 | + toml = tomlkit.load(f) |
| 109 | + |
| 110 | + dependencies = toml.get("project", {}).get("dependencies") |
| 111 | + if dependencies: |
| 112 | + return [_sanitize_dependency(dep) for dep in dependencies] |
| 113 | + |
| 114 | + dynamic_deps = toml.get("tool", {}).get("setuptools", {}).get("dynamic", {}).get("dependencies") |
| 115 | + if dynamic_deps and "file" in dynamic_deps: |
| 116 | + req_filepath = os.path.join(base_folder, dynamic_deps["file"]) |
| 117 | + return _get_deps_from_requirements(req_filepath) |
| 118 | + |
| 119 | + return [] |
| 120 | + |
| 121 | + |
86 | 122 | def _get_user_object_path(context): |
87 | 123 | if hasattr(context, "ghuser_cpython"): |
88 | 124 | print("checking ghuser_cpython") |
@@ -240,6 +276,9 @@ def update_gh_header(ctx, version: str = None, venv: str = None, dev: bool = Fal |
240 | 276 | new_header.append(f"# env: {env.strip()}\n") |
241 | 277 | if dev: |
242 | 278 | new_header.append(f"# env: {os.path.join(ctx.base_folder, 'src')}\n") |
| 279 | + dependencies = _get_dependencies(ctx.base_folder) |
| 280 | + if dependencies: |
| 281 | + new_header.append(f"# r: {', '.join(dependencies)}\n") |
243 | 282 |
|
244 | 283 | for file in Path(ctx.ghuser_cpython.source_dir).glob("**/code.py"): |
245 | 284 | try: |
|
0 commit comments