Skip to content

Commit af1fd98

Browse files
committed
parse dependencies and add to header as #r entries
1 parent a9046dc commit af1fd98

2 files changed

Lines changed: 38 additions & 0 deletions

File tree

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: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,40 @@ def _get_package_name(toml_file: str) -> str:
8383
return name
8484

8585

86+
def _sanitize_dependency(dep: str) -> str:
87+
# Remove upper bound constraints (e.g., ", <3" or ",<3") as Rhino doesn't support them
88+
sanitized = re.split(r"\s*,\s*[<>=]", dep)[0]
89+
sanitized = sanitized.split("#")[0].strip() # Remove inline comments
90+
return sanitized
91+
92+
93+
def _get_deps_from_requirements(req_filepath: str) -> str:
94+
with open(req_filepath, "r") as req_file:
95+
dependencies = []
96+
for line in req_file:
97+
line = line.strip()
98+
if line and not line.startswith("#"):
99+
dependencies.append(_sanitize_dependency(line))
100+
return dependencies
101+
102+
103+
def _get_dependencies(base_folder: str) -> str:
104+
toml_filepath = os.path.join(base_folder, "pyproject.toml")
105+
with open(toml_filepath, "r") as f:
106+
toml = tomlkit.load(f)
107+
108+
dependencies = toml.get("project").get("dependencies")
109+
if dependencies:
110+
return [_sanitize_dependency(dep) for dep in dependencies]
111+
112+
dynamic_deps = toml.get("tool").get("setuptools").get("dynamic").get("dependencies")
113+
if dynamic_deps and "file" in dynamic_deps:
114+
req_filepath = os.path.join(base_folder, dynamic_deps["file"])
115+
return _get_deps_from_requirements(req_filepath)
116+
117+
return []
118+
119+
86120
def _get_user_object_path(context):
87121
if hasattr(context, "ghuser_cpython"):
88122
print("checking ghuser_cpython")
@@ -239,6 +273,8 @@ def update_gh_header(ctx, version: str = None, venv: str = None, dev: bool = Fal
239273
for env in envs.split(";"):
240274
new_header.append(f"# env: {env.strip()}\n")
241275
if dev:
276+
dependencies = _get_dependencies(ctx.base_folder)
277+
new_header.append(f"# r: {', '.join(dependencies)}\n")
242278
new_header.append(f"# env: {os.path.join(ctx.base_folder, 'src')}\n")
243279

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

0 commit comments

Comments
 (0)