Adds a CLI tool to copy files or folders from a git repository into the current project based on a registry. The tool provides commands to: - `add`: Copies files/folders based on registry entries. - `list`: Lists available registry entries. Registry entries are defined in `jeAddons/registry.py`. The tool supports overriding the registry's ref and destination.
36 lines
1.2 KiB
Python
36 lines
1.2 KiB
Python
import subprocess
|
|
import tempfile
|
|
import shutil
|
|
from pathlib import Path
|
|
|
|
|
|
def fetch_path(repo_url: str, repo_path: str, dest: Path, ref: str = "HEAD", force: bool = False):
|
|
with tempfile.TemporaryDirectory(prefix="jeaddons-") as tmp:
|
|
tmp_path = Path(tmp)
|
|
subprocess.check_call(
|
|
["git", "clone", "--filter=blob:none", "--no-checkout", "--depth", "1", repo_url, str(tmp_path)]
|
|
)
|
|
subprocess.check_call(["git", "-C", str(tmp_path), "sparse-checkout", "init", "--cone"])
|
|
subprocess.check_call(["git", "-C", str(tmp_path), "sparse-checkout", "set", repo_path])
|
|
subprocess.check_call(["git", "-C", str(tmp_path), "checkout", ref])
|
|
|
|
src = tmp_path / repo_path
|
|
if not src.exists():
|
|
raise FileNotFoundError(repo_path)
|
|
|
|
target = dest / src.name
|
|
if target.exists():
|
|
if not force:
|
|
raise FileExistsError(target)
|
|
if target.is_dir():
|
|
shutil.rmtree(target)
|
|
else:
|
|
target.unlink()
|
|
|
|
if src.is_dir():
|
|
shutil.copytree(src, target)
|
|
return target
|
|
|
|
shutil.copy2(src, target)
|
|
return target
|