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 6d2ca1a commit 2bca128
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
5 changes: 4 additions & 1 deletion spacy/cli/_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,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
29 changes: 27 additions & 2 deletions spacy/tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,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 @@ -140,7 +140,32 @@ def test_issue11235():
assert os.path.exists(d / "cfg")
assert os.path.exists(d / f"{lang_var}_model")
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()
Expand Down

0 comments on commit 2bca128

Please sign in to comment.