From 2609ba4e896e932b1dc80b91f135568f80f5b492 Mon Sep 17 00:00:00 2001 From: Ines Montani Date: Sat, 30 Jan 2021 11:54:02 +1100 Subject: [PATCH] Support building wheel in spacy package --- spacy/cli/package.py | 22 +++++++++++++++++++++- website/docs/api/cli.md | 6 ++++-- website/docs/usage/v3.md | 3 ++- 3 files changed, 27 insertions(+), 4 deletions(-) diff --git a/spacy/cli/package.py b/spacy/cli/package.py index 04b2f1c9e..706134b79 100644 --- a/spacy/cli/package.py +++ b/spacy/cli/package.py @@ -22,6 +22,7 @@ def package_cli( name: Optional[str] = Opt(None, "--name", "-n", help="Package name to override meta"), version: Optional[str] = Opt(None, "--version", "-v", help="Package version to override meta"), no_sdist: bool = Opt(False, "--no-sdist", "-NS", help="Don't build .tar.gz sdist, can be set if you want to run this step manually"), + wheel: bool = Opt(False, "--wheel", "-W", help="Build a binary .whl instead of a .tar.gz archive for more efficient installation (requires wheel to be installed)"), force: bool = Opt(False, "--force", "-f", "-F", help="Force overwriting existing data in output directory"), # fmt: on ): @@ -53,7 +54,8 @@ def package_cli( name=name, version=version, create_meta=create_meta, - create_sdist=not no_sdist, + create_sdist=not no_sdist and not wheel, + create_wheel=wheel, force=force, silent=False, ) @@ -68,6 +70,7 @@ def package( version: Optional[str] = None, create_meta: bool = False, create_sdist: bool = True, + create_wheel: bool = False, force: bool = False, silent: bool = True, ) -> None: @@ -75,6 +78,9 @@ def package( input_path = util.ensure_path(input_dir) output_path = util.ensure_path(output_dir) meta_path = util.ensure_path(meta_path) + if create_wheel and not has_wheel(): + err = "Generating a binary .whl file requires wheel to be installed" + msg.fail(err, "pip install wheel", exits=1) if not input_path or not input_path.exists(): msg.fail("Can't locate pipeline data", input_path, exits=1) if not output_path or not output_path.exists(): @@ -143,6 +149,20 @@ def package( util.run_command([sys.executable, "setup.py", "sdist"], capture=False) zip_file = main_path / "dist" / f"{model_name_v}.tar.gz" msg.good(f"Successfully created zipped Python package", zip_file) + if create_wheel: + with util.working_dir(main_path): + util.run_command([sys.executable, "setup.py", "bdist_wheel"], capture=False) + wheel = main_path / "dist" / f"{model_name_v}-py3-none-any.whl" + msg.good(f"Successfully created binary wheel", wheel) + + +def has_wheel() -> bool: + try: + import wheel # noqa: F401 + + return True + except ImportError: + return False def create_file(file_path: Path, contents: str) -> None: diff --git a/website/docs/api/cli.md b/website/docs/api/cli.md index 747afb5be..d2397f564 100644 --- a/website/docs/api/cli.md +++ b/website/docs/api/cli.md @@ -901,7 +901,8 @@ copied into the package and imported in the `__init__.py`. If the path to a [`meta.json`](/api/data-formats#meta) is supplied, or a `meta.json` is found in the input directory, this file is used. Otherwise, the data can be entered directly from the command line. spaCy will then create a `.tar.gz` archive file -that you can distribute and install with `pip install`. +that you can distribute and install with `pip install`. Alternatively, you can +also set `--wheel` to build a binary `.whl` file instead. @@ -930,7 +931,8 @@ $ python -m spacy package [input_dir] [output_dir] [--code] [--meta-path] [--cre | `--code`, `-c` 3 | Comma-separated paths to Python files to be included in the package and imported in its `__init__.py`. This allows including [registering functions](/usage/training#custom-functions) and [custom components](/usage/processing-pipelines#custom-components). ~~Optional[str] \(option)~~ | | `--meta-path`, `-m` 2 | Path to [`meta.json`](/api/data-formats#meta) file (optional). ~~Optional[Path] \(option)~~ | | `--create-meta`, `-C` 2 | Create a `meta.json` file on the command line, even if one already exists in the directory. If an existing file is found, its entries will be shown as the defaults in the command line prompt. ~~bool (flag)~~ | -| `--no-sdist`, `-NS`, | Don't build the `.tar.gz` sdist automatically. Can be set if you want to run this step manually. ~~bool (flag)~~ | +| `--no-sdist`, `-NS` 3 | Don't build the `.tar.gz` sdist automatically. Can be set if you want to run this step manually. ~~bool (flag)~~ | +| `--wheel`, `-W` 3 | Build a binary wheel instead of the sdist for more efficient installation. Requires the [`wheel`](https://pypi.org/project/wheel/) extension for `setuptools` to be installed. ~~bool (flag)~~ | | `--name`, `-n` 3 | Package name to override in meta. ~~Optional[str] \(option)~~ | | `--version`, `-v` 3 | Package version to override in meta. Useful when training new versions, as it doesn't require editing the meta template. ~~Optional[str] \(option)~~ | | `--force`, `-f` | Force overwriting of existing folder in output directory. ~~bool (flag)~~ | diff --git a/website/docs/usage/v3.md b/website/docs/usage/v3.md index 5aa5507f7..28d170d14 100644 --- a/website/docs/usage/v3.md +++ b/website/docs/usage/v3.md @@ -1065,7 +1065,8 @@ setting up the label scheme. The [`spacy package`](/api/cli#package) command now automatically builds the installable `.tar.gz` sdist of the Python package, so you don't have to run this step manually anymore. You can disable the behavior by setting the `--no-sdist` -flag. +flag. You can now also specify `--wheel` to build a binary `.whl` file instead, +which will install more efficiently. ```diff python -m spacy package ./output ./packages