-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add cli command 'find-build-deps'
- Loading branch information
Showing
9 changed files
with
220 additions
and
14 deletions.
There are no files selected for viewing
This file contains 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,6 @@ | ||
"""pytest configuration.""" | ||
|
||
|
||
def pytest_configure(config): | ||
"""Configure pytst session.""" | ||
config.addinivalue_line("markers", "e2e: end to end tests.") |
This file contains 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 |
---|---|---|
@@ -1,7 +1,7 @@ | ||
# Usage | ||
|
||
```{eval-rst} | ||
.. click:: pybuild_deps.__main__:main | ||
.. click:: pybuild_deps.__main__:cli | ||
:prog: pybuild-deps | ||
:nested: full | ||
``` |
This file contains 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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 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 |
---|---|---|
@@ -1,12 +1,28 @@ | ||
"""Command-line interface.""" | ||
import logging | ||
|
||
import click | ||
|
||
|
||
@click.command() | ||
@click.group() | ||
@click.version_option() | ||
def main() -> None: | ||
@click.option("--log-level", default="ERROR") | ||
def cli(log_level) -> None: | ||
"""Entrypoint for PyBuild Deps.""" | ||
logging.basicConfig(level=log_level) | ||
|
||
|
||
@cli.command() | ||
@click.argument("package-name") | ||
@click.argument("version") | ||
def find_build_deps(package_name, version): | ||
"""Find build dependencies for given package.""" | ||
from pybuild_deps.find_build_dependencies import find_build_dependencies | ||
|
||
deps = find_build_dependencies(package_name=package_name, version=version) | ||
for dep in deps: | ||
click.echo(dep) | ||
|
||
|
||
if __name__ == "__main__": | ||
main(prog_name="pybuild-deps") # pragma: no cover | ||
cli(prog_name="pybuild-deps") # pragma: no cover |
This file contains 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,39 @@ | ||
"""Find build dependencies of a python package.""" | ||
|
||
import logging | ||
import tarfile | ||
|
||
from pybuild_deps.get_package_source import get_package_source | ||
from pybuild_deps.parsers import parse_pyproject_toml, parse_setup_cfg, parse_setup_py | ||
|
||
|
||
def find_build_dependencies(package_name, version): | ||
"""Find build dependencies for a given package.""" | ||
file_parser_map = { | ||
"pyproject.toml": parse_pyproject_toml, | ||
"setup.cfg": parse_setup_cfg, | ||
"setup.py": parse_setup_py, | ||
} | ||
logging.info("retrieving source for package %s==%s", package_name, version) | ||
source_path = get_package_source(package_name, version) | ||
build_dependencies = [] | ||
with tarfile.open(fileobj=source_path.open("rb")) as tarball: | ||
for file_name, parser in file_parser_map.items(): | ||
try: | ||
file = tarball.extractfile(f"{package_name}-{version}/{file_name}") | ||
except KeyError: | ||
logging.info( | ||
"%s file not found for package %s==%s", | ||
file_name, | ||
package_name, | ||
version, | ||
) | ||
continue | ||
logging.info( | ||
"parsing file %s for package %s==%s", | ||
file_name, | ||
package_name, | ||
version, | ||
) | ||
build_dependencies += parser(file.read().decode()) | ||
return build_dependencies |
This file contains 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,55 @@ | ||
"""Get source code for a given package.""" | ||
|
||
import logging | ||
from pathlib import Path | ||
|
||
import requests | ||
from xdg import xdg_cache_home | ||
|
||
CACHE_PATH = xdg_cache_home() / "pybuild-deps" | ||
|
||
|
||
def get_package_source(package_name: str, version: str) -> Path: | ||
"""Get ource code for a given package.""" | ||
cached_path = CACHE_PATH / package_name / version | ||
tarball_path = cached_path / "source.tar.gz" | ||
error_path = cached_path / "error.json" | ||
if tarball_path.exists(): | ||
logging.info("using cached version for package %s==%s", package_name, version) | ||
return tarball_path | ||
|
||
elif error_path.exists(): | ||
raise NotImplementedError() | ||
|
||
return retrieve_and_save_source_from_pypi( | ||
package_name, version, tarball_path=tarball_path, error_path=error_path | ||
) | ||
|
||
|
||
def retrieve_and_save_source_from_pypi( | ||
package_name: str, | ||
version: str, | ||
*, | ||
tarball_path: Path, | ||
error_path: Path, | ||
): | ||
"""Retrieve package source from pypi and store it in a cache.""" | ||
source_url = get_source_url(package_name, version) | ||
response = requests.get(source_url, timeout=10) | ||
if not response.ok: | ||
raise NotImplementedError() | ||
tarball_path.parent.mkdir(parents=True, exist_ok=True) | ||
tarball_path.write_bytes(response.content) | ||
return tarball_path | ||
|
||
|
||
def get_source_url(package_name, version): | ||
"""Get url for source code.""" | ||
response = requests.get( | ||
f"https://pypi.org/pypi/{package_name}/{version}/json", timeout=10 | ||
) | ||
if not response.ok: | ||
raise NotImplementedError() | ||
for url in response.json()["urls"]: # pragma: no branch | ||
if url["python_version"] == "source": | ||
return url["url"] |
This file contains 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