Skip to content

Commit b9b1572

Browse files
authored
Merge pull request #10 from compas-dev/deps_to_r
parse dependencies and add to header as #r entries
2 parents a9046dc + 49a8ea0 commit b9b1572

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1313

1414
### Changed
1515

16+
* `--dev` flag now also adds the package requirements to the header as `#r` entries.
17+
1618
### Removed
1719

1820

src/compas_invocations2/grasshopper.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import shutil
1212
import tempfile
1313
from pathlib import Path
14+
from typing import List
1415

1516
import invoke
1617
import requests
@@ -83,6 +84,41 @@ def _get_package_name(toml_file: str) -> str:
8384
return name
8485

8586

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+
86122
def _get_user_object_path(context):
87123
if hasattr(context, "ghuser_cpython"):
88124
print("checking ghuser_cpython")
@@ -240,6 +276,9 @@ def update_gh_header(ctx, version: str = None, venv: str = None, dev: bool = Fal
240276
new_header.append(f"# env: {env.strip()}\n")
241277
if dev:
242278
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")
243282

244283
for file in Path(ctx.ghuser_cpython.source_dir).glob("**/code.py"):
245284
try:

0 commit comments

Comments
 (0)