From e08257d4012b6df624c88b45ba28e8f9829ce2ad Mon Sep 17 00:00:00 2001 From: Matthew Honnibal Date: Sun, 28 Jun 2020 14:07:32 +0200 Subject: [PATCH] Add example of how to do sparse-checkout --- spacy/cli/_git_sparse_checkout_example.py | 62 +++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 spacy/cli/_git_sparse_checkout_example.py diff --git a/spacy/cli/_git_sparse_checkout_example.py b/spacy/cli/_git_sparse_checkout_example.py new file mode 100644 index 000000000..2bb3d6734 --- /dev/null +++ b/spacy/cli/_git_sparse_checkout_example.py @@ -0,0 +1,62 @@ +import tempfile +import typer +from pathlib import Path +import subprocess +import shlex +import shutil +from contextlib import contextmanager + + +@contextmanager +def make_tempdir(): + d = Path(tempfile.mkdtemp()) + yield d + shutil.rmtree(str(d)) + + + +def clone_repo(repo, temp_dir): + subprocess.check_call([ + "git", + "clone", + repo, + temp_dir, + "--no-checkout", + "--depth", "1", + "--config", "core.sparseCheckout=true" + ]) + + +def checkout_and_fetch(temp_dir): + subprocess.check_call([ + "git", + "-C", temp_dir, + "fetch" + ]) + subprocess.check_call([ + "git", + "-C", temp_dir, + "checkout" + ]) + + +def set_sparse_checkout_dir(temp_dir, subpath): + with (temp_dir / ".git" / "info" / "sparse-checkout").open("w") as file_: + file_.write(subpath) + + +def main(repo: str, subpath: str, dest: Path): + with make_tempdir() as temp_dir: + clone_repo(repo, temp_dir) + print("After clone", list(temp_dir.iterdir())) + set_sparse_checkout_dir(temp_dir, subpath) + checkout_and_fetch(temp_dir) + print("After checkout", list(temp_dir.iterdir())) + assert (temp_dir / subpath) in list(temp_dir.iterdir()) + shutil.copytree(temp_dir / subpath, dest / subpath, dirs_exist_ok=True) + print("Exists after cleanup?", temp_dir.exists()) + print("Destination", list(dest.iterdir())) + + +if __name__ == "__main__": + typer.run(main)