mirror of
https://github.com/explosion/spaCy.git
synced 2024-11-11 04:08:09 +03:00
Add example of how to do sparse-checkout
This commit is contained in:
parent
f385344286
commit
e08257d401
62
spacy/cli/_git_sparse_checkout_example.py
Normal file
62
spacy/cli/_git_sparse_checkout_example.py
Normal file
|
@ -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)
|
Loading…
Reference in New Issue
Block a user