-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
451 additions
and
381 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
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,78 @@ | ||
import subprocess | ||
import tempfile | ||
from pathlib import Path | ||
from packse.error import DestinationAlreadyExists | ||
import shutil | ||
import logging | ||
|
||
import pkg_resources | ||
import time | ||
|
||
|
||
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, | ||
) |
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
Oops, something went wrong.