Compare commits
22 Commits
remove-eol
...
main
Author | SHA1 | Date | |
---|---|---|---|
278386c3d5 | |||
1bd66e42de | |||
04fa347d28 | |||
b76826a873 | |||
583cd2b0bb | |||
f9c462b94a | |||
b59e908d84 | |||
3059b36908 | |||
29df64c07b | |||
c1dd243035 | |||
0bb2277e26 | |||
6fe0869e8b | |||
16fb7ca849 | |||
bcf65ce10f | |||
4eead212cf | |||
b9600cb631 | |||
7ecbf2c5cd | |||
35b07836e8 | |||
7380fa99ec | |||
bb0b82ab72 | |||
564a120bfe | |||
e58f1fd7b1 |
@ -72,6 +72,9 @@ def test_step(docker_tag, python_cmd="python"):
|
|||||||
"{} -V".format(python_cmd),
|
"{} -V".format(python_cmd),
|
||||||
"make clean-all test"
|
"make clean-all test"
|
||||||
],
|
],
|
||||||
|
"environment": {
|
||||||
|
"PIP_CACHE_DIR": ".pip-cache",
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -9,7 +9,6 @@ from dataclasses import dataclass
|
|||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from shutil import copy
|
from shutil import copy
|
||||||
from shutil import copytree
|
from shutil import copytree
|
||||||
from shutil import move
|
|
||||||
|
|
||||||
import toml
|
import toml
|
||||||
from wheel.wheelfile import WheelFile
|
from wheel.wheelfile import WheelFile
|
||||||
@ -17,11 +16,10 @@ from wheel.wheelfile import WheelFile
|
|||||||
import release_gitter as rg
|
import release_gitter as rg
|
||||||
from release_gitter import removeprefix
|
from release_gitter import removeprefix
|
||||||
|
|
||||||
PACKAGE_NAME = "pseudo"
|
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
class Config:
|
class Config:
|
||||||
|
name: str
|
||||||
format: str
|
format: str
|
||||||
git_url: str
|
git_url: str
|
||||||
hostname: str
|
hostname: str
|
||||||
@ -39,56 +37,52 @@ class Config:
|
|||||||
include_extra_files: list[str] | None = None
|
include_extra_files: list[str] | None = None
|
||||||
|
|
||||||
|
|
||||||
def download(config: Config) -> list[Path]:
|
def download(config: Config, wheel_scripts: Path) -> list[Path]:
|
||||||
release = rg.fetch_release(
|
"""Download and extract files to the wheel_scripts directory"""
|
||||||
rg.GitRemoteInfo(config.hostname, config.owner, config.repo), config.version
|
return rg.download_release(
|
||||||
)
|
rg.GitRemoteInfo(config.hostname, config.owner, config.repo),
|
||||||
asset = rg.match_asset(
|
wheel_scripts,
|
||||||
release,
|
|
||||||
config.format,
|
config.format,
|
||||||
version=config.version,
|
version=config.version,
|
||||||
system_mapping=config.map_system,
|
system_mapping=config.map_system,
|
||||||
arch_mapping=config.map_arch,
|
arch_mapping=config.map_arch,
|
||||||
|
extract_files=config.extract_files,
|
||||||
|
pre_release=config.pre_release,
|
||||||
|
exec=config.exec,
|
||||||
)
|
)
|
||||||
|
|
||||||
files = rg.download_asset(asset, extract_files=config.extract_files)
|
|
||||||
|
|
||||||
# Optionally execute post command
|
|
||||||
if config.exec:
|
|
||||||
rg.check_call(config.exec, shell=True)
|
|
||||||
|
|
||||||
return files
|
|
||||||
|
|
||||||
|
|
||||||
def read_metadata() -> Config:
|
def read_metadata() -> Config:
|
||||||
config = toml.load("pyproject.toml").get("tool", {}).get("release-gitter")
|
"""Read configuration from pyproject.toml"""
|
||||||
if not config:
|
pyproject = toml.load("pyproject.toml").get("tool", {}).get("release-gitter")
|
||||||
|
if not pyproject:
|
||||||
raise ValueError("Must have configuration in [tool.release-gitter]")
|
raise ValueError("Must have configuration in [tool.release-gitter]")
|
||||||
|
|
||||||
git_url = config.pop("git-url", None)
|
git_url = pyproject.pop("git-url", None)
|
||||||
remote_info = rg.parse_git_remote(git_url)
|
remote_info = rg.parse_git_url(git_url)
|
||||||
|
|
||||||
args = Config(
|
config = Config(
|
||||||
format=config.pop("format"),
|
name=pyproject.pop("name", remote_info.repo),
|
||||||
|
format=pyproject.pop("format"),
|
||||||
git_url=git_url,
|
git_url=git_url,
|
||||||
hostname=config.pop("hostname", remote_info.hostname),
|
hostname=pyproject.pop("hostname", remote_info.hostname),
|
||||||
owner=config.pop("owner", remote_info.owner),
|
owner=pyproject.pop("owner", remote_info.owner),
|
||||||
repo=config.pop("repo", remote_info.repo),
|
repo=pyproject.pop("repo", remote_info.repo),
|
||||||
)
|
)
|
||||||
|
|
||||||
for key, value in config.items():
|
for key, value in pyproject.items():
|
||||||
setattr(args, str(key).replace("-", "_"), value)
|
setattr(config, str(key).replace("-", "_"), value)
|
||||||
|
|
||||||
if args.version is None:
|
if config.version is None:
|
||||||
args.version = rg.read_version(
|
config.version = rg.read_version(
|
||||||
args.version_git_tag,
|
config.version_git_tag,
|
||||||
not args.version_git_no_fetch,
|
not config.version_git_no_fetch,
|
||||||
)
|
)
|
||||||
|
|
||||||
if args.extract_all:
|
if config.extract_all:
|
||||||
args.extract_files = []
|
config.extract_files = []
|
||||||
|
|
||||||
return args
|
return config
|
||||||
|
|
||||||
|
|
||||||
class _PseudoBuildBackend:
|
class _PseudoBuildBackend:
|
||||||
@ -105,7 +99,7 @@ class _PseudoBuildBackend:
|
|||||||
version = removeprefix(metadata.version, "v") if metadata.version else "0.0.0"
|
version = removeprefix(metadata.version, "v") if metadata.version else "0.0.0"
|
||||||
|
|
||||||
# Returns distinfo dir?
|
# Returns distinfo dir?
|
||||||
dist_info = Path(metadata_directory) / f"{PACKAGE_NAME}-{version}.dist-info"
|
dist_info = Path(metadata_directory) / f"{metadata.name}-{version}.dist-info"
|
||||||
dist_info.mkdir()
|
dist_info.mkdir()
|
||||||
|
|
||||||
# Write metadata
|
# Write metadata
|
||||||
@ -114,7 +108,7 @@ class _PseudoBuildBackend:
|
|||||||
"\n".join(
|
"\n".join(
|
||||||
[
|
[
|
||||||
"Metadata-Version: 2.1",
|
"Metadata-Version: 2.1",
|
||||||
f"Name: {PACKAGE_NAME}",
|
f"Name: {metadata.name}",
|
||||||
f"Version: {version}",
|
f"Version: {version}",
|
||||||
]
|
]
|
||||||
)
|
)
|
||||||
@ -146,6 +140,8 @@ class _PseudoBuildBackend:
|
|||||||
def build_wheel(
|
def build_wheel(
|
||||||
self, wheel_directory, config_settings=None, metadata_directory=None
|
self, wheel_directory, config_settings=None, metadata_directory=None
|
||||||
):
|
):
|
||||||
|
if metadata_directory is None:
|
||||||
|
raise ValueError("Cannot build wheel without metadata_directory")
|
||||||
metadata_directory = Path(metadata_directory)
|
metadata_directory = Path(metadata_directory)
|
||||||
|
|
||||||
metadata = read_metadata()
|
metadata = read_metadata()
|
||||||
@ -154,15 +150,13 @@ class _PseudoBuildBackend:
|
|||||||
wheel_directory = Path(wheel_directory)
|
wheel_directory = Path(wheel_directory)
|
||||||
wheel_directory.mkdir(exist_ok=True)
|
wheel_directory.mkdir(exist_ok=True)
|
||||||
|
|
||||||
wheel_scripts = wheel_directory / f"{PACKAGE_NAME}-{version}.data/scripts"
|
wheel_scripts = wheel_directory / f"{metadata.name}-{version}.data/scripts"
|
||||||
wheel_scripts.mkdir(parents=True, exist_ok=True)
|
wheel_scripts.mkdir(parents=True, exist_ok=True)
|
||||||
|
|
||||||
copytree(metadata_directory, wheel_directory / metadata_directory.name)
|
copytree(metadata_directory, wheel_directory / metadata_directory.name)
|
||||||
|
|
||||||
metadata = read_metadata()
|
metadata = read_metadata()
|
||||||
files = download(metadata)
|
download(metadata, wheel_scripts)
|
||||||
for file in files:
|
|
||||||
move(file, wheel_scripts / file.name)
|
|
||||||
|
|
||||||
for file_name in metadata.include_extra_files or []:
|
for file_name in metadata.include_extra_files or []:
|
||||||
file = Path(file_name)
|
file = Path(file_name)
|
||||||
@ -175,11 +169,11 @@ class _PseudoBuildBackend:
|
|||||||
|
|
||||||
print(f"ls {wheel_directory}: {list(wheel_directory.rglob('*'))}")
|
print(f"ls {wheel_directory}: {list(wheel_directory.rglob('*'))}")
|
||||||
|
|
||||||
wheel_filename = f"{PACKAGE_NAME}-{version}-py2.py3-none-any.whl"
|
wheel_filename = f"{metadata.name}-{version}-py2.py3-none-any.whl"
|
||||||
with WheelFile(wheel_directory / wheel_filename, "w") as wf:
|
with WheelFile(wheel_directory / wheel_filename, "w") as wf:
|
||||||
print("Repacking wheel as {}...".format(wheel_filename), end="")
|
print("Repacking wheel as {}...".format(wheel_filename), end="")
|
||||||
# sys.stdout.flush()
|
# sys.stdout.flush()
|
||||||
wf.write_files(wheel_directory)
|
wf.write_files(str(wheel_directory))
|
||||||
|
|
||||||
return wheel_filename
|
return wheel_filename
|
||||||
|
|
||||||
|
46
pseudo_builder_test.py
Normal file
46
pseudo_builder_test.py
Normal file
@ -0,0 +1,46 @@
|
|||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import shutil
|
||||||
|
import subprocess
|
||||||
|
import venv
|
||||||
|
from pathlib import Path
|
||||||
|
from unittest import TestCase
|
||||||
|
|
||||||
|
ITEST_VENV_PATH = Path("venv-itest")
|
||||||
|
|
||||||
|
|
||||||
|
class TestPseudoBuilder(TestCase):
|
||||||
|
def setUp(self):
|
||||||
|
venv.create(
|
||||||
|
ITEST_VENV_PATH,
|
||||||
|
system_site_packages=False,
|
||||||
|
clear=True,
|
||||||
|
with_pip=True,
|
||||||
|
)
|
||||||
|
self.pip_install("-e", ".[builder]")
|
||||||
|
|
||||||
|
def tearDown(self):
|
||||||
|
shutil.rmtree(ITEST_VENV_PATH)
|
||||||
|
|
||||||
|
def pip_install(self, *args: str):
|
||||||
|
subprocess.run(
|
||||||
|
[str(ITEST_VENV_PATH.joinpath("bin", "pip")), "install", *args],
|
||||||
|
check=True,
|
||||||
|
)
|
||||||
|
|
||||||
|
def test_install_remote_package(self):
|
||||||
|
self.assertTrue(ITEST_VENV_PATH.exists())
|
||||||
|
self.assertTrue(ITEST_VENV_PATH.joinpath("bin", "python").exists())
|
||||||
|
self.assertTrue(ITEST_VENV_PATH.joinpath("bin", "pip").exists())
|
||||||
|
|
||||||
|
itest_packages = {
|
||||||
|
"stylua": "git+https://github.com/JohnnyMorganz/StyLua",
|
||||||
|
"selene": "git+https://github.com/amitds1997/selene",
|
||||||
|
}
|
||||||
|
|
||||||
|
for package, source in itest_packages.items():
|
||||||
|
self.pip_install("--no-index", "--no-build-isolation", source)
|
||||||
|
# Check if the package is installed
|
||||||
|
assert ITEST_VENV_PATH.joinpath("bin", package).exists()
|
||||||
|
# Check if the package has executable permissions
|
||||||
|
assert ITEST_VENV_PATH.joinpath("bin", package).stat().st_mode & 0o111
|
@ -48,7 +48,7 @@ dependencies = [
|
|||||||
[tool.hatch.envs.test.scripts]
|
[tool.hatch.envs.test.scripts]
|
||||||
run = [
|
run = [
|
||||||
"coverage erase",
|
"coverage erase",
|
||||||
"coverage run --source=release_gitter -m unittest discover . *_test.py",
|
"coverage run --source=release_gitter,pseudo_builder -m unittest discover -p '*_test.py'",
|
||||||
"coverage report -m # --fail-under 70",
|
"coverage report -m # --fail-under 70",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
@ -2,7 +2,9 @@
|
|||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
import argparse
|
import argparse
|
||||||
|
import logging
|
||||||
import platform
|
import platform
|
||||||
|
import tempfile
|
||||||
from collections.abc import Sequence
|
from collections.abc import Sequence
|
||||||
from dataclasses import dataclass
|
from dataclasses import dataclass
|
||||||
from io import BytesIO
|
from io import BytesIO
|
||||||
@ -14,12 +16,16 @@ from subprocess import check_output
|
|||||||
from tarfile import TarFile
|
from tarfile import TarFile
|
||||||
from tarfile import TarInfo
|
from tarfile import TarInfo
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
from typing import NamedTuple
|
||||||
from urllib.parse import urlparse
|
from urllib.parse import urlparse
|
||||||
from zipfile import ZipFile
|
from zipfile import ZipFile
|
||||||
|
|
||||||
import requests
|
import requests
|
||||||
|
|
||||||
__version__ = "2.3.0"
|
__version__ = "3.0.3"
|
||||||
|
|
||||||
|
|
||||||
|
logging.basicConfig(level=logging.WARNING)
|
||||||
|
|
||||||
|
|
||||||
class UnsupportedContentTypeError(ValueError):
|
class UnsupportedContentTypeError(ValueError):
|
||||||
@ -73,6 +79,12 @@ def get_synonyms(value: str, thesaurus: list[list[str]]) -> list[str]:
|
|||||||
return results
|
return results
|
||||||
|
|
||||||
|
|
||||||
|
class MatchedValues(NamedTuple):
|
||||||
|
version: str
|
||||||
|
system: str
|
||||||
|
arch: str
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
class GitRemoteInfo:
|
class GitRemoteInfo:
|
||||||
"""Extracts information about a repository"""
|
"""Extracts information about a repository"""
|
||||||
@ -114,13 +126,13 @@ class GitRemoteInfo:
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def parse_git_remote(git_url: str | None = None) -> GitRemoteInfo:
|
def read_git_remote() -> str:
|
||||||
"""Extract Github repo info from a git remote url"""
|
"""Reads the git remote url from the origin"""
|
||||||
if not git_url:
|
return check_output(["git", "remote", "get-url", "origin"]).decode("UTF-8").strip()
|
||||||
git_url = (
|
|
||||||
check_output(["git", "remote", "get-url", "origin"]).decode("UTF-8").strip()
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
|
def parse_git_url(git_url: str) -> GitRemoteInfo:
|
||||||
|
"""Extract Github repo info from a git remote url"""
|
||||||
# Normalize Github ssh url as a proper URL
|
# Normalize Github ssh url as a proper URL
|
||||||
if git_url.startswith("git@github.com:"):
|
if git_url.startswith("git@github.com:"):
|
||||||
git_ssh_parts = git_url.partition(":")
|
git_ssh_parts = git_url.partition(":")
|
||||||
@ -167,6 +179,7 @@ def read_git_tag(fetch: bool = True) -> str | None:
|
|||||||
def read_version(from_tags: bool = False, fetch: bool = False) -> str | None:
|
def read_version(from_tags: bool = False, fetch: bool = False) -> str | None:
|
||||||
"""Read version information from file or from git"""
|
"""Read version information from file or from git"""
|
||||||
if from_tags:
|
if from_tags:
|
||||||
|
logging.debug("Reading version from git tag")
|
||||||
return read_git_tag(fetch)
|
return read_git_tag(fetch)
|
||||||
|
|
||||||
matchers = {
|
matchers = {
|
||||||
@ -176,10 +189,13 @@ def read_version(from_tags: bool = False, fetch: bool = False) -> str | None:
|
|||||||
for name, extractor in matchers.items():
|
for name, extractor in matchers.items():
|
||||||
p = Path(name)
|
p = Path(name)
|
||||||
if p.exists():
|
if p.exists():
|
||||||
|
logging.debug(f"Reading version from {p}")
|
||||||
return extractor(p)
|
return extractor(p)
|
||||||
|
|
||||||
# TODO: Log this out to stderr
|
logging.warning(
|
||||||
# raise ValueError(f"Unknown project type. Didn't find any of {matchers.keys()}")
|
"Unknown local project version. Didn't find any of %s", set(matchers.keys())
|
||||||
|
)
|
||||||
|
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
@ -202,6 +218,8 @@ def fetch_release(
|
|||||||
|
|
||||||
# Return the latest if requested
|
# Return the latest if requested
|
||||||
if version is None or version == "latest":
|
if version is None or version == "latest":
|
||||||
|
logging.debug("Looking for latest release")
|
||||||
|
|
||||||
for release in result.json():
|
for release in result.json():
|
||||||
if release["prerelease"] and not pre_release:
|
if release["prerelease"] and not pre_release:
|
||||||
continue
|
continue
|
||||||
@ -211,6 +229,8 @@ def fetch_release(
|
|||||||
# Return matching version
|
# Return matching version
|
||||||
for release in result.json():
|
for release in result.json():
|
||||||
if release["tag_name"].endswith(version):
|
if release["tag_name"].endswith(version):
|
||||||
|
logging.debug(f"Found release {release['name']} matching version {version}")
|
||||||
|
|
||||||
return release
|
return release
|
||||||
|
|
||||||
raise ValueError(
|
raise ValueError(
|
||||||
@ -225,7 +245,7 @@ def match_asset(
|
|||||||
version: str | None = None,
|
version: str | None = None,
|
||||||
system_mapping: dict[str, str] | None = None,
|
system_mapping: dict[str, str] | None = None,
|
||||||
arch_mapping: dict[str, str] | None = None,
|
arch_mapping: dict[str, str] | None = None,
|
||||||
) -> dict[Any, Any]:
|
) -> tuple[dict[Any, Any], MatchedValues]:
|
||||||
"""Accepts a release and searches for an appropriate asset attached using
|
"""Accepts a release and searches for an appropriate asset attached using
|
||||||
a provided template and some alternative mappings for version, system, and machine info
|
a provided template and some alternative mappings for version, system, and machine info
|
||||||
|
|
||||||
@ -261,13 +281,7 @@ def match_asset(
|
|||||||
|
|
||||||
# This should never really happen
|
# This should never really happen
|
||||||
if version is None:
|
if version is None:
|
||||||
if "{version}" in format:
|
raise ValueError("No version provided or found in release name.")
|
||||||
raise ValueError(
|
|
||||||
"No version provided or found in release name but is in format"
|
|
||||||
)
|
|
||||||
else:
|
|
||||||
# This should never happen, but since version isn't used anywhere, we can make it an empty string
|
|
||||||
version = ""
|
|
||||||
|
|
||||||
system = platform.system()
|
system = platform.system()
|
||||||
if system_mapping:
|
if system_mapping:
|
||||||
@ -286,7 +300,7 @@ def match_asset(
|
|||||||
version=version_opt,
|
version=version_opt,
|
||||||
system=system_opt,
|
system=system_opt,
|
||||||
arch=arch_opt,
|
arch=arch_opt,
|
||||||
)
|
): MatchedValues(version=version_opt, system=system_opt, arch=arch_opt)
|
||||||
for version_opt, system_opt, arch_opt in product(
|
for version_opt, system_opt, arch_opt in product(
|
||||||
(
|
(
|
||||||
version.lstrip("v"),
|
version.lstrip("v"),
|
||||||
@ -299,7 +313,7 @@ def match_asset(
|
|||||||
|
|
||||||
for asset in release["assets"]:
|
for asset in release["assets"]:
|
||||||
if asset["name"] in expected_names:
|
if asset["name"] in expected_names:
|
||||||
return asset
|
return (asset, expected_names[asset["name"]])
|
||||||
|
|
||||||
raise ValueError(
|
raise ValueError(
|
||||||
f"Could not find asset named {expected_names} on release {release['name']}"
|
f"Could not find asset named {expected_names} on release {release['name']}"
|
||||||
@ -315,8 +329,12 @@ class PackageAdapter:
|
|||||||
"application/zip",
|
"application/zip",
|
||||||
"application/x-zip-compressed",
|
"application/x-zip-compressed",
|
||||||
):
|
):
|
||||||
|
logging.debug("Opening zip file from response content")
|
||||||
|
|
||||||
self._package = ZipFile(BytesIO(response.content))
|
self._package = ZipFile(BytesIO(response.content))
|
||||||
elif content_type == "application/x-tar":
|
elif content_type == "application/x-tar":
|
||||||
|
logging.debug("Opening tar file from response content")
|
||||||
|
|
||||||
self._package = TarFile(fileobj=response.raw)
|
self._package = TarFile(fileobj=response.raw)
|
||||||
elif content_type in (
|
elif content_type in (
|
||||||
"application/gzip",
|
"application/gzip",
|
||||||
@ -324,6 +342,8 @@ class PackageAdapter:
|
|||||||
"application/x-tar+xz",
|
"application/x-tar+xz",
|
||||||
"application/x-compressed-tar",
|
"application/x-compressed-tar",
|
||||||
):
|
):
|
||||||
|
logging.debug("Opening compressed tar file from response content")
|
||||||
|
|
||||||
self._package = TarFile.open(fileobj=BytesIO(response.content), mode="r:*")
|
self._package = TarFile.open(fileobj=BytesIO(response.content), mode="r:*")
|
||||||
else:
|
else:
|
||||||
raise UnsupportedContentTypeError(
|
raise UnsupportedContentTypeError(
|
||||||
@ -334,6 +354,7 @@ class PackageAdapter:
|
|||||||
"""Get list of all file names in package"""
|
"""Get list of all file names in package"""
|
||||||
if isinstance(self._package, ZipFile):
|
if isinstance(self._package, ZipFile):
|
||||||
return self._package.namelist()
|
return self._package.namelist()
|
||||||
|
|
||||||
if isinstance(self._package, TarFile):
|
if isinstance(self._package, TarFile):
|
||||||
return self._package.getnames()
|
return self._package.getnames()
|
||||||
|
|
||||||
@ -351,19 +372,26 @@ class PackageAdapter:
|
|||||||
If the `file_names` list is empty, all files will be extracted"""
|
If the `file_names` list is empty, all files will be extracted"""
|
||||||
if path is None:
|
if path is None:
|
||||||
path = Path.cwd()
|
path = Path.cwd()
|
||||||
|
|
||||||
if not members:
|
if not members:
|
||||||
|
logging.debug("Extracting all members to %s", path)
|
||||||
|
|
||||||
self._package.extractall(path=path)
|
self._package.extractall(path=path)
|
||||||
|
|
||||||
return self.get_names()
|
return self.get_names()
|
||||||
|
|
||||||
|
# TODO: Use walrus operator when dropping 3.7 support
|
||||||
missing_members = set(members) - set(self.get_names())
|
missing_members = set(members) - set(self.get_names())
|
||||||
if missing_members:
|
if missing_members:
|
||||||
raise ValueError(f"Missing members: {missing_members}")
|
raise ValueError(f"Missing members: {missing_members}")
|
||||||
|
|
||||||
|
logging.debug("Extracting members %s to %s", members, path)
|
||||||
|
|
||||||
if isinstance(self._package, ZipFile):
|
if isinstance(self._package, ZipFile):
|
||||||
self._package.extractall(path=path, members=members)
|
self._package.extractall(path=path, members=members)
|
||||||
if isinstance(self._package, TarFile):
|
if isinstance(self._package, TarFile):
|
||||||
self._package.extractall(
|
self._package.extractall(
|
||||||
path=path, members=(TarInfo(name) for name in members)
|
path=path, members=(self._package.getmember(name) for name in members)
|
||||||
)
|
)
|
||||||
|
|
||||||
return members
|
return members
|
||||||
@ -386,7 +414,7 @@ def get_asset_package(
|
|||||||
continue
|
continue
|
||||||
else:
|
else:
|
||||||
raise UnsupportedContentTypeError(
|
raise UnsupportedContentTypeError(
|
||||||
"Cannot extract files from archive because we don't recognize the content type"
|
f"Cannot extract files from archive because we don't recognize the content types {possible_content_types}"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@ -413,8 +441,10 @@ def download_asset(
|
|||||||
result = requests.get(asset["browser_download_url"])
|
result = requests.get(asset["browser_download_url"])
|
||||||
|
|
||||||
if extract_files is not None:
|
if extract_files is not None:
|
||||||
|
logging.info("Extracting package %s", asset["name"])
|
||||||
package = get_asset_package(asset, result)
|
package = get_asset_package(asset, result)
|
||||||
extract_files = package.extractall(path=destination, members=extract_files)
|
extract_files = package.extractall(path=destination, members=extract_files)
|
||||||
|
|
||||||
return [destination / name for name in extract_files]
|
return [destination / name for name in extract_files]
|
||||||
|
|
||||||
file_name = destination / asset["name"]
|
file_name = destination / asset["name"]
|
||||||
@ -461,6 +491,7 @@ class MapAddAction(argparse.Action):
|
|||||||
|
|
||||||
|
|
||||||
def _parse_args(args: list[str] | None = None) -> argparse.Namespace:
|
def _parse_args(args: list[str] | None = None) -> argparse.Namespace:
|
||||||
|
logging.debug("Parsing arguments: %s", args)
|
||||||
parser = argparse.ArgumentParser()
|
parser = argparse.ArgumentParser()
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
"format",
|
"format",
|
||||||
@ -474,7 +505,9 @@ def _parse_args(args: list[str] | None = None) -> argparse.Namespace:
|
|||||||
default=Path.cwd(),
|
default=Path.cwd(),
|
||||||
help="Destination directory. Defaults to current directory",
|
help="Destination directory. Defaults to current directory",
|
||||||
)
|
)
|
||||||
parser.add_argument("-v", action="store_true", help="verbose logging")
|
parser.add_argument(
|
||||||
|
"-v", action="count", help="verbose or debug logging", default=0
|
||||||
|
)
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
"--hostname",
|
"--hostname",
|
||||||
help="Git repository hostname",
|
help="Git repository hostname",
|
||||||
@ -545,12 +578,29 @@ def _parse_args(args: list[str] | None = None) -> argparse.Namespace:
|
|||||||
action="store_true",
|
action="store_true",
|
||||||
help="Only print the URL and do not download",
|
help="Only print the URL and do not download",
|
||||||
)
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
"--use-temp-dir",
|
||||||
|
action="store_true",
|
||||||
|
help="Use a temporary directory as the destination",
|
||||||
|
)
|
||||||
|
|
||||||
parsed_args = parser.parse_args(args)
|
parsed_args = parser.parse_args(args)
|
||||||
|
|
||||||
# Merge in fields from args and git remote
|
# Merge in fields from args and git remote
|
||||||
if not all((parsed_args.owner, parsed_args.repo, parsed_args.hostname)):
|
if not all((parsed_args.owner, parsed_args.repo, parsed_args.hostname)):
|
||||||
remote_info = parse_git_remote(parsed_args.git_url)
|
# Check to see if a git url was provided. If not, we use local directory git remote
|
||||||
|
if parsed_args.git_url is None:
|
||||||
|
parsed_args.git_url = read_git_remote()
|
||||||
|
|
||||||
|
# If using a local repo, try to determine version from project files
|
||||||
|
if parsed_args.version is None:
|
||||||
|
parsed_args.version = read_version(
|
||||||
|
parsed_args.version_git_tag,
|
||||||
|
not parsed_args.version_git_no_fetch,
|
||||||
|
)
|
||||||
|
|
||||||
|
# Get parts from git url
|
||||||
|
remote_info = parse_git_url(parsed_args.git_url)
|
||||||
|
|
||||||
def merge_field(a, b, field):
|
def merge_field(a, b, field):
|
||||||
value = getattr(a, field)
|
value = getattr(a, field)
|
||||||
@ -560,15 +610,12 @@ def _parse_args(args: list[str] | None = None) -> argparse.Namespace:
|
|||||||
for field in ("owner", "repo", "hostname"):
|
for field in ("owner", "repo", "hostname"):
|
||||||
merge_field(parsed_args, remote_info, field)
|
merge_field(parsed_args, remote_info, field)
|
||||||
|
|
||||||
if parsed_args.version is None:
|
|
||||||
parsed_args.version = read_version(
|
|
||||||
parsed_args.version_git_tag,
|
|
||||||
not parsed_args.version_git_no_fetch,
|
|
||||||
)
|
|
||||||
|
|
||||||
if parsed_args.extract_all:
|
if parsed_args.extract_all:
|
||||||
parsed_args.extract_files = []
|
parsed_args.extract_files = []
|
||||||
|
|
||||||
|
if parsed_args.use_temp_dir:
|
||||||
|
parsed_args.destination = Path(tempfile.mkdtemp())
|
||||||
|
|
||||||
return parsed_args
|
return parsed_args
|
||||||
|
|
||||||
|
|
||||||
@ -581,55 +628,99 @@ def download_release(
|
|||||||
arch_mapping: dict[str, str] | None = None,
|
arch_mapping: dict[str, str] | None = None,
|
||||||
extract_files: list[str] | None = None,
|
extract_files: list[str] | None = None,
|
||||||
pre_release=False,
|
pre_release=False,
|
||||||
|
exec: str | None = None,
|
||||||
) -> list[Path]:
|
) -> list[Path]:
|
||||||
"""Convenience method for fetching, downloading and extracting a release"""
|
"""Convenience method for fetching, downloading, and extracting a release
|
||||||
|
|
||||||
|
This is slightly different than running off the commandline, it will execute the shell script
|
||||||
|
from the destination directory, not the current working directory.
|
||||||
|
"""
|
||||||
release = fetch_release(
|
release = fetch_release(
|
||||||
remote_info,
|
remote_info,
|
||||||
version=version,
|
version=version,
|
||||||
pre_release=pre_release,
|
pre_release=pre_release,
|
||||||
)
|
)
|
||||||
asset = match_asset(
|
asset, matched_values = match_asset(
|
||||||
release,
|
release,
|
||||||
format,
|
format,
|
||||||
version=version,
|
version=version,
|
||||||
system_mapping=system_mapping,
|
system_mapping=system_mapping,
|
||||||
arch_mapping=arch_mapping,
|
arch_mapping=arch_mapping,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
format_fields = dict(
|
||||||
|
asset_name=asset["name"],
|
||||||
|
**matched_values._asdict(),
|
||||||
|
)
|
||||||
|
|
||||||
|
formatted_files = (
|
||||||
|
[file.format(**format_fields) for file in extract_files]
|
||||||
|
if extract_files is not None
|
||||||
|
else None
|
||||||
|
)
|
||||||
|
|
||||||
files = download_asset(
|
files = download_asset(
|
||||||
asset,
|
asset,
|
||||||
extract_files=extract_files,
|
extract_files=formatted_files,
|
||||||
destination=destination,
|
destination=destination,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
if exec:
|
||||||
|
check_call(
|
||||||
|
exec.format(asset["name"], **format_fields), shell=True, cwd=destination
|
||||||
|
)
|
||||||
|
|
||||||
return files
|
return files
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
args = _parse_args()
|
args = _parse_args()
|
||||||
|
|
||||||
|
logging.getLogger().setLevel(30 - 10 * args.v)
|
||||||
|
|
||||||
|
# Fetch the release
|
||||||
release = fetch_release(
|
release = fetch_release(
|
||||||
GitRemoteInfo(args.hostname, args.owner, args.repo),
|
GitRemoteInfo(args.hostname, args.owner, args.repo),
|
||||||
version=args.version,
|
version=args.version,
|
||||||
pre_release=args.prerelease,
|
pre_release=args.prerelease,
|
||||||
)
|
)
|
||||||
asset = match_asset(
|
|
||||||
|
logging.debug("Found release: %s", release["name"])
|
||||||
|
|
||||||
|
version = args.version or release["tag_name"]
|
||||||
|
|
||||||
|
logging.debug("Release version: %s", version)
|
||||||
|
|
||||||
|
# Find the asset to download using mapping rules
|
||||||
|
asset, matched_values = match_asset(
|
||||||
release,
|
release,
|
||||||
args.format,
|
args.format,
|
||||||
version=args.version,
|
version=version,
|
||||||
system_mapping=args.map_system,
|
system_mapping=args.map_system,
|
||||||
arch_mapping=args.map_arch,
|
arch_mapping=args.map_arch,
|
||||||
)
|
)
|
||||||
|
|
||||||
if args.v:
|
logging.info(f"Downloading {asset['name']} from release {release['name']}")
|
||||||
print(f"Downloading {asset['name']} from release {release['name']}")
|
|
||||||
|
|
||||||
if args.url_only:
|
if args.url_only:
|
||||||
print(asset["browser_download_url"])
|
print(asset["browser_download_url"])
|
||||||
return
|
return
|
||||||
|
|
||||||
|
format_fields = dict(
|
||||||
|
asset_name=asset["name"],
|
||||||
|
**matched_values._asdict(),
|
||||||
|
)
|
||||||
|
|
||||||
|
# Format files to extract with version info, as this is sometimes included
|
||||||
|
formatted_files = (
|
||||||
|
[file.format(**format_fields) for file in args.extract_files]
|
||||||
|
if args.extract_files is not None
|
||||||
|
else None
|
||||||
|
)
|
||||||
|
|
||||||
files = download_asset(
|
files = download_asset(
|
||||||
asset,
|
asset,
|
||||||
extract_files=args.extract_files,
|
extract_files=formatted_files,
|
||||||
destination=args.destination,
|
destination=args.destination,
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -637,7 +728,11 @@ def main():
|
|||||||
|
|
||||||
# Optionally execute post command
|
# Optionally execute post command
|
||||||
if args.exec:
|
if args.exec:
|
||||||
check_call(args.exec.format(asset["name"]), shell=True)
|
check_call(
|
||||||
|
args.exec.format(asset["name"], **format_fields),
|
||||||
|
shell=True,
|
||||||
|
cwd=args.destination,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
@ -82,7 +82,7 @@ class TestRemoteInfo(unittest.TestCase):
|
|||||||
release_gitter.InvalidRemoteError,
|
release_gitter.InvalidRemoteError,
|
||||||
),
|
),
|
||||||
):
|
):
|
||||||
test_case.run(release_gitter.parse_git_remote)
|
test_case.run(release_gitter.parse_git_url)
|
||||||
|
|
||||||
def test_generate_release_url(self):
|
def test_generate_release_url(self):
|
||||||
for subtest in (
|
for subtest in (
|
||||||
@ -199,6 +199,13 @@ class TestContentTypeDetection(unittest.TestCase):
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def first_result(f):
|
||||||
|
def wrapper(*args, **kwargs):
|
||||||
|
return f(*args, **kwargs)[0]
|
||||||
|
|
||||||
|
return wrapper
|
||||||
|
|
||||||
|
|
||||||
class TestMatchAsset(unittest.TestCase):
|
class TestMatchAsset(unittest.TestCase):
|
||||||
def test_match_asset_versions(self, *_):
|
def test_match_asset_versions(self, *_):
|
||||||
# Input variations:
|
# Input variations:
|
||||||
@ -233,7 +240,7 @@ class TestMatchAsset(unittest.TestCase):
|
|||||||
)
|
)
|
||||||
]
|
]
|
||||||
for test_case in happy_cases:
|
for test_case in happy_cases:
|
||||||
test_case.run(release_gitter.match_asset)
|
test_case.run(first_result(release_gitter.match_asset))
|
||||||
|
|
||||||
def test_match_asset_systems(self, *_):
|
def test_match_asset_systems(self, *_):
|
||||||
# Input variations:
|
# Input variations:
|
||||||
@ -347,7 +354,7 @@ class TestMatchAsset(unittest.TestCase):
|
|||||||
),
|
),
|
||||||
)
|
)
|
||||||
for test_case in test_cases:
|
for test_case in test_cases:
|
||||||
test_case.run(run_with_context)
|
test_case.run(first_result(run_with_context))
|
||||||
|
|
||||||
def test_match_asset_archs(self, *_):
|
def test_match_asset_archs(self, *_):
|
||||||
# Input variations:
|
# Input variations:
|
||||||
@ -468,7 +475,7 @@ class TestMatchAsset(unittest.TestCase):
|
|||||||
),
|
),
|
||||||
)
|
)
|
||||||
for test_case in test_cases:
|
for test_case in test_cases:
|
||||||
test_case.run(run_with_context)
|
test_case.run(first_result(run_with_context))
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
Loading…
Reference in New Issue
Block a user