Skip to content

Commit

Permalink
Support fetching a file from a git repo as an asset
Browse files Browse the repository at this point in the history
Our Git fetcher code assumed that the asset to be copied was a
directory. Support files as well and add tests to check both the file
and directory cases.

Fixes #12168.
  • Loading branch information
danieldk committed Jan 25, 2023
1 parent 57ba37b commit e92f4de
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
5 changes: 4 additions & 1 deletion spacy/cli/_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,10 @@ def git_checkout(
if not is_subpath_of(tmp_dir, source_path):
err = f"'{subpath}' is a path outside of the cloned repository."
msg.fail(err, repo, exits=1)
shutil.copytree(str(source_path), str(dest))
if os.path.isdir(source_path):
shutil.copytree(str(source_path), str(dest))
else:
shutil.copyfile(source_path, dest)
except FileNotFoundError:
err = f"Can't clone {subpath}. Make sure the directory exists in the repo (branch '{branch}')"
msg.fail(err, repo, exits=1)
Expand Down
27 changes: 26 additions & 1 deletion spacy/tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
from spacy.cli._util import parse_config_overrides, string_to_list
from spacy.cli._util import substitute_project_variables
from spacy.cli._util import validate_project_commands
from spacy.cli._util import upload_file, download_file
from spacy.cli._util import upload_file, download_file, git_checkout
from spacy.cli.debug_data import _compile_gold, _get_labels_from_model
from spacy.cli.debug_data import _get_labels_from_spancat
from spacy.cli.debug_data import _get_distribution, _get_kl_divergence
Expand Down Expand Up @@ -145,6 +145,31 @@ def test_issue11235():
assert cfg["commands"][0]["script"][0] == f"hello {lang_var}"


def test_project_git_dir_asset():
with make_tempdir() as d:
# Use a very small repo.
git_checkout(
"https://github.com/explosion/os-signpost.git",
"os_signpost",
d / "signpost",
branch="v0.0.3",
)
assert os.path.isdir(d / "signpost")


@pytest.mark.issue(12168)
def test_project_git_file_asset():
with make_tempdir() as d:
# Use a very small repo.
git_checkout(
"https://github.com/explosion/os-signpost.git",
"README.md",
d / "readme.md",
branch="v0.0.3",
)
assert os.path.isfile(d / "readme.md")


def test_cli_info():
nlp = Dutch()
nlp.add_pipe("textcat")
Expand Down

0 comments on commit e92f4de

Please sign in to comment.