-
-
Notifications
You must be signed in to change notification settings - Fork 104
[feature] Support Python projects using pyproject.toml #706 #707
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
2d35d60
fadd47a
9b080a6
9759f4d
7a11cd7
35b3000
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -4,6 +4,14 @@ | |||||||||||||||||||||||||
| import re | ||||||||||||||||||||||||||
| import subprocess | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| try: | ||||||||||||||||||||||||||
| import tomllib # pragma: no cover | ||||||||||||||||||||||||||
| except ImportError: # pragma: no cover | ||||||||||||||||||||||||||
| try: | ||||||||||||||||||||||||||
| import tomli as tomllib # pragma: no cover | ||||||||||||||||||||||||||
| except ImportError: # pragma: no cover | ||||||||||||||||||||||||||
| tomllib = None # pragma: no cover | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| def get_package_name_from_setup(): | ||||||||||||||||||||||||||
| """Parses setup.py to find the package name without raising an error.""" | ||||||||||||||||||||||||||
|
|
@@ -22,6 +30,7 @@ def get_package_type_from_setup(): | |||||||||||||||||||||||||
| """Detects package type based on config files present in the project.""" | ||||||||||||||||||||||||||
| package_type_files = { | ||||||||||||||||||||||||||
| "setup.py": "python", | ||||||||||||||||||||||||||
| "pyproject.toml": "python", | ||||||||||||||||||||||||||
| "package.json": "npm", | ||||||||||||||||||||||||||
| "docker-compose.yml": "docker", | ||||||||||||||||||||||||||
| ".ansible-lint": "ansible", | ||||||||||||||||||||||||||
|
|
@@ -58,27 +67,54 @@ def _handle_python_version(config): | |||||||||||||||||||||||||
| netjsonconfig, netdiff). | ||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||
| project_name = get_package_name_from_setup() | ||||||||||||||||||||||||||
| if not project_name: | ||||||||||||||||||||||||||
| if project_name: | ||||||||||||||||||||||||||
| package_directory = project_name.replace("-", "_") | ||||||||||||||||||||||||||
| candidate_files = [ | ||||||||||||||||||||||||||
| os.path.join(package_directory, "__init__.py"), | ||||||||||||||||||||||||||
| os.path.join(package_directory, "version.py"), | ||||||||||||||||||||||||||
| ] | ||||||||||||||||||||||||||
|
Comment on lines
+72
to
+75
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🗄️ Data Integrity & Integration | 🟡 Minor | ⚡ Quick win 🔎 Supported by static analysis🏁 Script executed: #!/bin/bash
set -e
printf '%s\n' '--- repository conventions ---'
find /tmp/coderabbit-repo-knowledge/openwisp-openwisp-utils-2837d08b -type f -name '*.md' -print
printf '%s\n' '--- target file ---'
cat -n openwisp_utils/releaser/config.py | sed -n '1,150p'
printf '%s\n' '--- related tests and version handlers ---'
rg -n -C 4 'VERSION|version.py|pyproject|candidate_files|PACKAGE_VERSION_HANDLERS|setup.py' openwisp_utils tests setup.py pyproject.toml 2>/dev/null || trueRepository: openwisp/openwisp-utils Length of output: 50380 🏁 Script executed: #!/bin/bash
set -e
printf '%s\n' '--- repository conventions ---'
cat /tmp/coderabbit-repo-knowledge/openwisp-openwisp-utils-2837d08b/conventions/repo-wide.md
printf '%s\n' '--- releaser test coverage around fallback ---'
cat -n openwisp_utils/releaser/tests/test_config.py | sed -n '180,330p'
printf '%s\n' '--- version bump dispatch ---'
cat -n openwisp_utils/releaser/version.py | sed -n '45,90p;150,195p'
printf '%s\n' '--- current change summary ---'
git diff --stat
git diff -- openwisp_utils/releaser/config.py openwisp_utils/releaser/tests/test_config.py | sed -n '1,220p'Repository: openwisp/openwisp-utils Length of output: 15731 Fix the mixed-project fallback before using the TOML version. When 🤖 Prompt for AI Agents |
||||||||||||||||||||||||||
| for version_path in candidate_files: | ||||||||||||||||||||||||||
| if not os.path.exists(version_path): | ||||||||||||||||||||||||||
| continue | ||||||||||||||||||||||||||
| with open(version_path, "r") as f: | ||||||||||||||||||||||||||
| content = f.read() | ||||||||||||||||||||||||||
| version_match = re.search(r"^VERSION\s*=\s*\((.*)\)", content, re.M) | ||||||||||||||||||||||||||
| if version_match: | ||||||||||||||||||||||||||
| config["version_path"] = version_path | ||||||||||||||||||||||||||
| try: | ||||||||||||||||||||||||||
| version_tuple = ast.literal_eval(f"({version_match.group(1)})") | ||||||||||||||||||||||||||
| config["CURRENT_VERSION"] = list(version_tuple) | ||||||||||||||||||||||||||
| except (ValueError, SyntaxError, TypeError): | ||||||||||||||||||||||||||
|
Comment on lines
+83
to
+87
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win Continue searching after a VERSION parse failure. If the first candidate contains an unparseable Proposed fix- config["version_path"] = version_path
try:
version_tuple = ast.literal_eval(
f"({version_match.group(1)})"
)
- config["CURRENT_VERSION"] = list(version_tuple)
except (ValueError, SyntaxError, TypeError):
- config["CURRENT_VERSION"] = None
+ continue
+ config["version_path"] = version_path
+ config["CURRENT_VERSION"] = list(version_tuple)
return📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||
| config["CURRENT_VERSION"] = None | ||||||||||||||||||||||||||
| return | ||||||||||||||||||||||||||
| _handle_pyproject_toml_version(config) | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| def _handle_pyproject_toml_version(config): | ||||||||||||||||||||||||||
| """Handles version detection from pyproject.toml.""" | ||||||||||||||||||||||||||
| if not os.path.exists("pyproject.toml"): | ||||||||||||||||||||||||||
| return | ||||||||||||||||||||||||||
| package_directory = project_name.replace("-", "_") | ||||||||||||||||||||||||||
| candidate_files = [ | ||||||||||||||||||||||||||
| os.path.join(package_directory, "__init__.py"), | ||||||||||||||||||||||||||
| os.path.join(package_directory, "version.py"), | ||||||||||||||||||||||||||
| ] | ||||||||||||||||||||||||||
| for version_path in candidate_files: | ||||||||||||||||||||||||||
| if not os.path.exists(version_path): | ||||||||||||||||||||||||||
| continue | ||||||||||||||||||||||||||
| with open(version_path, "r") as f: | ||||||||||||||||||||||||||
| content = f.read() | ||||||||||||||||||||||||||
| version_match = re.search(r"^VERSION\s*=\s*\((.*)\)", content, re.M) | ||||||||||||||||||||||||||
| if version_match: | ||||||||||||||||||||||||||
| config["version_path"] = version_path | ||||||||||||||||||||||||||
| try: | ||||||||||||||||||||||||||
| version_tuple = ast.literal_eval(f"({version_match.group(1)})") | ||||||||||||||||||||||||||
| config["CURRENT_VERSION"] = list(version_tuple) | ||||||||||||||||||||||||||
| except (ValueError, SyntaxError, TypeError): | ||||||||||||||||||||||||||
| config["CURRENT_VERSION"] = None | ||||||||||||||||||||||||||
| return | ||||||||||||||||||||||||||
| if tomllib is None: | ||||||||||||||||||||||||||
| return # pragma: no cover | ||||||||||||||||||||||||||
| with open("pyproject.toml", "rb") as f: | ||||||||||||||||||||||||||
| try: | ||||||||||||||||||||||||||
| data = tomllib.load(f) | ||||||||||||||||||||||||||
| except Exception: | ||||||||||||||||||||||||||
| return | ||||||||||||||||||||||||||
| project = data.get("project", {}) | ||||||||||||||||||||||||||
| version_str = project.get("version") | ||||||||||||||||||||||||||
| if not version_str: | ||||||||||||||||||||||||||
| return | ||||||||||||||||||||||||||
| try: | ||||||||||||||||||||||||||
| parts = version_str.split(".") | ||||||||||||||||||||||||||
| if len(parts) != 3: | ||||||||||||||||||||||||||
| return | ||||||||||||||||||||||||||
| current_version = [int(parts[0]), int(parts[1]), int(parts[2]), "final"] | ||||||||||||||||||||||||||
| except (ValueError, TypeError): | ||||||||||||||||||||||||||
| return | ||||||||||||||||||||||||||
| config["package_type"] = "pyproject" | ||||||||||||||||||||||||||
| config["version_path"] = "pyproject.toml" | ||||||||||||||||||||||||||
| config["CURRENT_VERSION"] = current_version | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| def _handle_npm_version(config): | ||||||||||||||||||||||||||
|
|
@@ -203,6 +239,7 @@ def _handle_generic_version(config): | |||||||||||||||||||||||||
| # Maps package types to their version detection handlers | ||||||||||||||||||||||||||
| PACKAGE_VERSION_HANDLERS = { | ||||||||||||||||||||||||||
| "python": _handle_python_version, | ||||||||||||||||||||||||||
| "pyproject": _handle_pyproject_toml_version, | ||||||||||||||||||||||||||
| "npm": _handle_npm_version, | ||||||||||||||||||||||||||
| "docker": _handle_docker_version, | ||||||||||||||||||||||||||
| "ansible": _handle_ansible_version, | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🔒 Security & Privacy | 🟠 Major | ⚡ Quick win
🔎 Supported by static analysis
🏁 Script executed:
Repository: openwisp/openwisp-utils
Length of output: 50380
🏁 Script executed:
Repository: openwisp/openwisp-utils
Length of output: 50380
🏁 Script executed:
Repository: openwisp/openwisp-utils
Length of output: 50380
Constrain metadata-derived version paths to the project root.
get_package_name_from_setup()accepts../and absolute path components._handle_python_version()then passes the resultingversion_pathtobump_version(), which opens it for writing. Validateproject_nameand reject any candidate whose resolved path is outside the project root.🤖 Prompt for AI Agents
Sources: Path instructions, Linters/SAST tools