Skip to content

Commit

Permalink
Add packse fetch
Browse files Browse the repository at this point in the history
  • Loading branch information
zanieb committed Feb 24, 2024
1 parent 8e1c550 commit b903c06
Show file tree
Hide file tree
Showing 11 changed files with 451 additions and 381 deletions.
49 changes: 42 additions & 7 deletions src/packse/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
ServeError,
UserError,
)
from packse.fetch import fetch
from packse.index import index_down, index_up
from packse.inspect import inspect
from packse.list import list
Expand Down Expand Up @@ -41,7 +42,7 @@ def entrypoint():
try:
args.call(args)
except DestinationAlreadyExists as exc:
print(f"{exc}. Pass `--rm` to allow removal.", file=sys.stderr)
print(f"{exc}. Pass `--force` to allow removal.", file=sys.stderr)
exit(1)
except UserError as exc:
print(f"{exc}.", file=sys.stderr)
Expand Down Expand Up @@ -73,7 +74,7 @@ def entrypoint():
def _call_build(args):
build(
args.targets,
rm_destination=args.rm,
rm_destination=args.force,
short_names=args.short_names,
no_hash=args.no_hash,
skip_root=args.skip_root,
Expand All @@ -88,7 +89,7 @@ def _call_build_package(args):
args.no_sdist,
args.requires_python,
args.wheel_tag,
args.rm,
args.force,
)


Expand Down Expand Up @@ -137,6 +138,14 @@ def _call_publish(args):
)


def _call_fetch(args):
fetch(
args.dest,
ref=args.ref,
force=args.force,
)


def _call_list(args):
skip_invalid = args.skip_invalid
if not args.targets:
Expand Down Expand Up @@ -195,9 +204,9 @@ def _add_build_parser(subparsers):
help="The scenario to build",
)
parser.add_argument(
"--rm",
"--force",
action="store_true",
help="Allow removal of existing build directory",
help="Overwrite existing builds",
)
parser.add_argument(
"--short-names",
Expand Down Expand Up @@ -254,9 +263,9 @@ def _add_build_package_parser(subparsers):
help="Disable building source distributions",
)
parser.add_argument(
"--rm",
"--force",
action="store_true",
help="Allow removal of existing build directory",
help="Replace existing builds",
)
_add_shared_arguments(parser)

Expand Down Expand Up @@ -498,6 +507,31 @@ def _add_shared_arguments(parser):
)


def _add_fetch_parser(subparsers):
parser = subparsers.add_parser(
"fetch", help="Fetch built-in scenarios from the packse repository"
)
parser.set_defaults(call=_call_fetch)
parser.add_argument(
"--dest",
type=Path,
help="Where to place the fetched scenarios",
)
parser.add_argument(
"--ref",
type=str,
default=None,
help="The reference to fetch",
)
parser.add_argument(
"-f",
"--force",
action="store_true",
help="Allow replacement of existing destination directory",
)
_add_shared_arguments(parser)


def get_parser() -> argparse.ArgumentParser:
parser = _root_parser()
subparsers = parser.add_subparsers(title="commands")
Expand All @@ -508,6 +542,7 @@ def get_parser() -> argparse.ArgumentParser:
_add_list_parser(subparsers)
_add_inspect_parser(subparsers)
_add_serve_parser(subparsers)
_add_fetch_parser(subparsers)
_add_index_parser(subparsers)

return parser
78 changes: 78 additions & 0 deletions src/packse/fetch.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import logging
import shutil
import subprocess
import tempfile
import time
from pathlib import Path

import pkg_resources

from packse.error import DestinationAlreadyExists

logger = logging.getLogger(__name__)


def fetch(
dest: Path | None = None,
ref: str | None = None,
repo_url: str = "https://github.com/zanieb/packse",
repo_subdir: str = "scenarios",
force: bool = False,
):
start_time = time.time()
dest = dest or (Path.cwd() / "scenarios")

if dest.exists() and not force:
raise DestinationAlreadyExists(dest)

if not ref:
ref = pkg_resources.get_distribution("packse").version
if ref == "0.0.0":
ref = "HEAD"

with tempfile.TemporaryDirectory() as tmpdir:
# Perform a sparse checkout where we only grab the `scenarios` folder
logger.info("Cloning repository %s", repo_url)
subprocess.check_call(
[
"git",
"clone",
"-n",
"--depth=1",
"--filter=tree:0",
repo_url,
"repo",
],
stdout=subprocess.DEVNULL,
stderr=subprocess.STDOUT,
cwd=tmpdir,
)

logger.info("Checking out directory '%s' at ref %s", repo_subdir, ref)
subprocess.check_call(
["git", "sparse-checkout", "set", "--no-cone", repo_subdir],
stdout=subprocess.DEVNULL,
stderr=subprocess.STDOUT,
cwd=Path(tmpdir) / "repo",
)
subprocess.check_call(
["git", "checkout", ref],
stdout=subprocess.DEVNULL,
stderr=subprocess.STDOUT,
cwd=Path(tmpdir) / "repo",
)

src = Path(tmpdir) / "repo" / repo_subdir
file_count = 0
for file in src.iterdir():
file_count += 1
logger.info("Found file %s", file.name)

logger.info("Copying files into '%s'", dest)
shutil.copytree(src, dest, dirs_exist_ok=True)

logger.info(
"Fetched %s files in %.2fs",
file_count,
time.time() - start_time,
)
13 changes: 5 additions & 8 deletions tests/__snapshots__/test_build.ambr
Original file line number Diff line number Diff line change
Expand Up @@ -210,20 +210,17 @@
}),
'stderr': '''
Building 'example-8597e52a' in directory '[PWD]/build/example-8597e52a'
Destination directory '[PWD]/build/example-8597e52a' already exists. Pass `--rm` to allow removal.
Destination directory '[PWD]/build/example-8597e52a' already exists. Pass `--force` to allow removal.

''',
'stdout': '',
})
# ---
# name: test_build_example_already_exists_with_rm_flag
dict({
'exit_code': 0,
'exit_code': 2,
'stderr': '<not included>',
'stdout': '''
example-8597e52a

''',
'stdout': '',
})
# ---
# name: test_build_example_short_names
Expand Down Expand Up @@ -632,8 +629,8 @@
dict({
'exit_code': 2,
'stderr': '''
usage: packse build [-h] [--rm] [--short-names] [--no-hash] [--skip-root] [-v]
[-q]
usage: packse build [-h] [--force] [--short-names] [--no-hash] [--skip-root]
[-v] [-q]
targets [targets ...]
packse build: error: the following arguments are required: targets

Expand Down
4 changes: 2 additions & 2 deletions tests/__snapshots__/test_build_pkg.ambr
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
'stderr': '''
usage: packse build-pkg [-h] [-t WHEEL_TAG] [--no-wheel]
[--requires-python REQUIRES_PYTHON] [--no-sdist]
[--rm] [-v] [-q]
[--force] [-v] [-q]
name version
packse build-pkg: error: the following arguments are required: name, version

Expand Down Expand Up @@ -146,7 +146,7 @@
'stderr': '''
usage: packse build-pkg [-h] [-t WHEEL_TAG] [--no-wheel]
[--requires-python REQUIRES_PYTHON] [--no-sdist]
[--rm] [-v] [-q]
[--force] [-v] [-q]
name version
packse build-pkg: error: the following arguments are required: version

Expand Down
Loading

0 comments on commit b903c06

Please sign in to comment.