-
Notifications
You must be signed in to change notification settings - Fork 294
feat: requires-python #536
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
fa6a1a7
feat: requires-python
henryiii e83bb4d
refactor: pull out config file reading
henryiii ce663b1
fix: always assume highest Python patch version
henryiii 531f93e
refactor: try new design
henryiii bee2eb6
refactor: function and bump MyPy to 0.800
henryiii 11ee70e
fix: tighten AST to call only, add docs
henryiii 7dec6f9
fix: address review points from @joerick
henryiii File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -105,3 +105,5 @@ env3?/ | |
|
|
||
| # MyPy cache | ||
| .mypy_cache/ | ||
|
|
||
| all_known_setup.yaml | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,110 @@ | ||
| #!/usr/bin/env python3 | ||
| from __future__ import annotations | ||
|
|
||
| import ast | ||
| from pathlib import Path | ||
| from typing import Iterator, Optional | ||
|
|
||
| import click | ||
| import yaml | ||
| from ghapi.core import GhApi, HTTP404NotFoundError # type: ignore | ||
| from rich import print | ||
|
|
||
| from cibuildwheel.projectfiles import Analyzer | ||
|
|
||
| DIR = Path(__file__).parent.resolve() | ||
|
|
||
|
|
||
| def parse(contents: str) -> Optional[str]: | ||
| try: | ||
| tree = ast.parse(contents) | ||
| analyzer = Analyzer() | ||
| analyzer.visit(tree) | ||
| return analyzer.requires_python or "" | ||
| except Exception: | ||
| return None | ||
|
|
||
|
|
||
| def check_repo(name: str, contents: str) -> str: | ||
| s = f" {name}: " | ||
| if name == "setup.py": | ||
| if "python_requires" not in contents: | ||
| s += "❌" | ||
| res = parse(contents) | ||
| if res is None: | ||
| s += "⚠️ " | ||
| elif res: | ||
| s += "✅ " + res | ||
| elif "python_requires" in contents: | ||
| s += "☑️" | ||
|
|
||
| elif name == "setup.cfg": | ||
| s += "✅" if "python_requires" in contents else "❌" | ||
| else: | ||
| s += "✅" if "requires-python" in contents else "❌" | ||
|
|
||
| return s | ||
|
|
||
|
|
||
| class MaybeRemote: | ||
| def __init__(self, cached_file: Path | str, *, online: bool) -> None: | ||
| self.online = online | ||
| if self.online: | ||
| self.contents: dict[str, dict[str, Optional[str]]] = { | ||
| "setup.py": {}, | ||
| "setup.cfg": {}, | ||
| "pyproject.toml": {}, | ||
| } | ||
| else: | ||
| with open(cached_file) as f: | ||
| self.contents = yaml.safe_load(f) | ||
|
|
||
| def get(self, repo: str, filename: str) -> Optional[str]: | ||
| if self.online: | ||
| try: | ||
| self.contents[filename][repo] = ( | ||
| GhApi(*repo.split("/")).get_content(filename).decode() | ||
| ) | ||
| except HTTP404NotFoundError: | ||
| self.contents[filename][repo] = None | ||
| return self.contents[filename][repo] | ||
| elif repo in self.contents[filename]: | ||
| return self.contents[filename][repo] | ||
| else: | ||
| raise RuntimeError( | ||
| f"Trying to access {repo}:{filename} and not in cache, rebuild cache" | ||
| ) | ||
|
|
||
| def save(self, filename: Path | str) -> None: | ||
| with open(filename, "w") as f: | ||
| yaml.safe_dump(self.contents, f, default_flow_style=False) | ||
|
|
||
| def on_each(self, repos: list[str]) -> Iterator[tuple[str, str, Optional[str]]]: | ||
| for repo in repos: | ||
| print(f"[bold]{repo}:") | ||
| for filename in sorted(self.contents, reverse=True): | ||
| yield repo, filename, self.get(repo, filename) | ||
|
|
||
|
|
||
| @click.command() | ||
| @click.option("--online", is_flag=True, help="Remember to set GITHUB_TOKEN") | ||
| def main(online: bool) -> None: | ||
| with open(DIR / "../docs/data/projects.yml") as f: | ||
| known = yaml.safe_load(f) | ||
|
|
||
| repos = [x["gh"] for x in known] | ||
|
|
||
| ghinfo = MaybeRemote("all_known_setup.yaml", online=online) | ||
|
|
||
| for _, filename, contents in ghinfo.on_each(repos): | ||
| if contents is None: | ||
| print(f"[red] {filename}: Not found") | ||
| else: | ||
| print(check_repo(filename, contents)) | ||
|
|
||
| if online: | ||
| ghinfo.save("all_known_setup.yaml") | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| import ast | ||
| import sys | ||
| from configparser import ConfigParser | ||
| from pathlib import Path | ||
| from typing import Any, Optional | ||
|
|
||
| import toml | ||
|
|
||
| if sys.version_info < (3, 8): | ||
| Constant = ast.Str | ||
|
|
||
| def get_constant(x: ast.Str) -> str: | ||
| return x.s | ||
|
|
||
|
|
||
| else: | ||
| Constant = ast.Constant | ||
|
|
||
| def get_constant(x: ast.Constant) -> Any: | ||
| return x.value | ||
|
|
||
|
|
||
| class Analyzer(ast.NodeVisitor): | ||
| def __init__(self) -> None: | ||
| self.requires_python: Optional[str] = None | ||
|
|
||
| def visit(self, content: ast.AST) -> None: | ||
| for node in ast.walk(content): | ||
| for child in ast.iter_child_nodes(node): | ||
| child.parent = node # type: ignore | ||
| super().visit(content) | ||
henryiii marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| def visit_keyword(self, node: ast.keyword) -> None: | ||
| self.generic_visit(node) | ||
| if node.arg == "python_requires": | ||
| # Must not be nested in an if or other structure | ||
| # This will be Module -> Expr -> Call -> keyword | ||
| if ( | ||
| not hasattr(node.parent.parent.parent, "parent") # type: ignore | ||
| and isinstance(node.value, Constant) | ||
| ): | ||
| self.requires_python = get_constant(node.value) | ||
|
|
||
|
|
||
| def setup_py_python_requires(content: str) -> Optional[str]: | ||
| try: | ||
| tree = ast.parse(content) | ||
| analyzer = Analyzer() | ||
henryiii marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| analyzer.visit(tree) | ||
| return analyzer.requires_python or None | ||
| except Exception: | ||
| return None | ||
|
|
||
|
|
||
| def get_requires_python_str(package_dir: Path) -> Optional[str]: | ||
| "Return the python requires string from the most canonical source available, or None" | ||
|
|
||
| # Read in from pyproject.toml:project.requires-python | ||
| try: | ||
| info = toml.load(package_dir / 'pyproject.toml') | ||
| return str(info['project']['requires-python']) | ||
| except (FileNotFoundError, KeyError, IndexError, TypeError): | ||
| pass | ||
|
|
||
| # Read in from setup.cfg:options.python_requires | ||
| try: | ||
| config = ConfigParser() | ||
| config.read(package_dir / 'setup.cfg') | ||
| return str(config['options']['python_requires']) | ||
| except (FileNotFoundError, KeyError, IndexError, TypeError): | ||
| pass | ||
|
|
||
| try: | ||
| with open(package_dir / 'setup.py') as f: | ||
| return setup_py_python_requires(f.read()) | ||
| except FileNotFoundError: | ||
| pass | ||
|
|
||
| return None | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.