From 46662590f29ddb27f8938fa6593cd39a402a235e Mon Sep 17 00:00:00 2001 From: ines Date: Sun, 20 May 2018 15:55:07 +0200 Subject: [PATCH] Pass additional options to pip when installing model (resolves #1456) Treat all additional arguments passed to the download command as pip options to allow user to customise the command. For example: python -m spacy download en --user --- spacy/cli/download.py | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/spacy/cli/download.py b/spacy/cli/download.py index 68e09d45f..ec5043562 100644 --- a/spacy/cli/download.py +++ b/spacy/cli/download.py @@ -16,22 +16,24 @@ from .. import about @plac.annotations( model=("model to download, shortcut or name)", "positional", None, str), direct=("force direct download. Needs model name with version and won't " - "perform compatibility check", "flag", "d", bool)) -def download(model, direct=False): + "perform compatibility check", "flag", "d", bool), + pip_args=("additional arguments to be passed to `pip install` when " + "installing the model")) +def download(model, direct=False, *pip_args): """ Download compatible model from default download path using pip. Model can be shortcut, model name or, if --direct flag is set, full model name with version. """ if direct: - dl = download_model('{m}/{m}.tar.gz#egg={m}'.format(m=model)) + dl = download_model('{m}/{m}.tar.gz#egg={m}'.format(m=model), pip_args) else: shortcuts = get_json(about.__shortcuts__, "available shortcuts") model_name = shortcuts.get(model, model) compatibility = get_compatibility() version = get_version(model_name, compatibility) dl = download_model('{m}-{v}/{m}-{v}.tar.gz#egg={m}=={v}' - .format(m=model_name, v=version)) + .format(m=model_name, v=version), pip_args) if dl != 0: # if download subprocess doesn't return 0, exit sys.exit(dl) try: @@ -75,8 +77,10 @@ def get_version(model, comp): return comp[model][0] -def download_model(filename): +def download_model(filename, user_pip_args=None): download_url = about.__download_url__ + '/' + filename - return subprocess.call( - [sys.executable, '-m', 'pip', 'install', '--no-cache-dir', '--no-deps', - download_url], env=os.environ.copy()) + pip_args = ['--no-cache-dir', '--no-deps'] + if user_pip_args: + pip_args.extend(user_pip_args) + cmd = [sys.executable, '-m', 'pip', 'install'] + pip_args + [download_url] + return subprocess.call(cmd, env=os.environ.copy())