Update and use unified --build option

This commit is contained in:
Ines Montani 2021-01-30 13:11:36 +11:00
parent 2609ba4e89
commit 2332c4280b
3 changed files with 46 additions and 34 deletions

View File

@ -1,11 +1,11 @@
from typing import Optional, Union, Any, Dict, List from typing import Optional, Union, Any, Dict, List, Tuple
import shutil import shutil
from pathlib import Path from pathlib import Path
from wasabi import Printer, get_raw_input from wasabi import Printer, get_raw_input
import srsly import srsly
import sys import sys
from ._util import app, Arg, Opt from ._util import app, Arg, Opt, string_to_list
from ..schemas import validate, ModelMetaSchema from ..schemas import validate, ModelMetaSchema
from .. import util from .. import util
from .. import about from .. import about
@ -16,13 +16,12 @@ def package_cli(
# fmt: off # fmt: off
input_dir: Path = Arg(..., help="Directory with pipeline data", exists=True, file_okay=False), input_dir: Path = Arg(..., help="Directory with pipeline data", exists=True, file_okay=False),
output_dir: Path = Arg(..., help="Output parent directory", exists=True, file_okay=False), output_dir: Path = Arg(..., help="Output parent directory", exists=True, file_okay=False),
code_paths: Optional[str] = Opt(None, "--code", "-c", help="Comma-separated paths to Python file with additional code (registered functions) to be included in the package"), code_paths: str = Opt("", "--code", "-c", help="Comma-separated paths to Python file with additional code (registered functions) to be included in the package"),
meta_path: Optional[Path] = Opt(None, "--meta-path", "--meta", "-m", help="Path to meta.json", exists=True, dir_okay=False), meta_path: Optional[Path] = Opt(None, "--meta-path", "--meta", "-m", help="Path to meta.json", exists=True, dir_okay=False),
create_meta: bool = Opt(False, "--create-meta", "-c", "-C", help="Create meta.json, even if one exists"), create_meta: bool = Opt(False, "--create-meta", "-c", "-C", help="Create meta.json, even if one exists"),
name: Optional[str] = Opt(None, "--name", "-n", help="Package name to override meta"), 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"), 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"), build: str = Opt("sdist", "--build", "-b", help="Comma-separated formats to build: sdist and/or wheel, or none."),
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"), force: bool = Opt(False, "--force", "-f", "-F", help="Force overwriting existing data in output directory"),
# fmt: on # fmt: on
): ):
@ -41,11 +40,8 @@ def package_cli(
DOCS: https://nightly.spacy.io/api/cli#package DOCS: https://nightly.spacy.io/api/cli#package
""" """
code_paths = ( create_sdist, create_wheel = get_build_formats(string_to_list(build))
[Path(p.strip()) for p in code_paths.split(",")] code_paths = [Path(p.strip()) for p in string_to_list(code_paths)]
if code_paths is not None
else []
)
package( package(
input_dir, input_dir,
output_dir, output_dir,
@ -54,8 +50,8 @@ def package_cli(
name=name, name=name,
version=version, version=version,
create_meta=create_meta, create_meta=create_meta,
create_sdist=not no_sdist and not wheel, create_sdist=create_sdist,
create_wheel=wheel, create_wheel=create_wheel,
force=force, force=force,
silent=False, silent=False,
) )
@ -85,6 +81,9 @@ def package(
msg.fail("Can't locate pipeline data", input_path, exits=1) msg.fail("Can't locate pipeline data", input_path, exits=1)
if not output_path or not output_path.exists(): if not output_path or not output_path.exists():
msg.fail("Output directory not found", output_path, exits=1) msg.fail("Output directory not found", output_path, exits=1)
if create_sdist or create_wheel:
opts = ["sdist" if create_sdist else "", "wheel" if create_wheel else ""]
msg.info(f"Building package artifacts: {', '.join(opt for opt in opts if opt)}")
for code_path in code_paths: for code_path in code_paths:
if not code_path.exists(): if not code_path.exists():
msg.fail("Can't find code file", code_path, exits=1) msg.fail("Can't find code file", code_path, exits=1)
@ -165,6 +164,18 @@ def has_wheel() -> bool:
return False return False
def get_build_formats(formats: List[str]) -> Tuple[bool, bool]:
supported = ["sdist", "wheel", "none"]
for form in formats:
if form not in supported:
msg = Printer()
err = f"Unknown build format: {form}. Supported: {', '.join(supported)}"
msg.fail(err, exits=1)
if not formats or "none" in formats:
return (False, False)
return ("sdist" in formats, "wheel" in formats)
def create_file(file_path: Path, contents: str) -> None: def create_file(file_path: Path, contents: str) -> None:
file_path.touch() file_path.touch()
file_path.open("w", encoding="utf-8").write(contents) file_path.open("w", encoding="utf-8").write(contents)

View File

@ -900,20 +900,21 @@ registered functions like
copied into the package and imported in the `__init__.py`. If the path to a 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 [`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 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 directly from the command line. spaCy will then create a build artifact that you
that you can distribute and install with `pip install`. Alternatively, you can can distribute and install with `pip install`.
also set `--wheel` to build a binary `.whl` file instead.
<Infobox title="New in v3.0" variant="warning"> <Infobox title="New in v3.0" variant="warning">
The `spacy package` command now also builds the `.tar.gz` archive automatically, The `spacy package` command now also builds the `.tar.gz` archive automatically,
so you don't have to run `python setup.py sdist` separately anymore. To disable so you don't have to run `python setup.py sdist` separately anymore. To disable
this, you can set the `--no-sdist` flag. this, you can set `--build none`. You can also choose to build a binary wheel
(which installs more efficiently) by setting `--build wheel`, or to build both
the sdist and wheel by setting `--build sdist,wheel`.
</Infobox> </Infobox>
```cli ```cli
$ python -m spacy package [input_dir] [output_dir] [--code] [--meta-path] [--create-meta] [--no-sdist] [--name] [--version] [--force] $ python -m spacy package [input_dir] [output_dir] [--code] [--meta-path] [--create-meta] [--build] [--name] [--version] [--force]
``` ```
> #### Example > #### Example
@ -924,20 +925,19 @@ $ python -m spacy package [input_dir] [output_dir] [--code] [--meta-path] [--cre
> $ pip install dist/en_pipeline-0.0.0.tar.gz > $ pip install dist/en_pipeline-0.0.0.tar.gz
> ``` > ```
| Name | Description | | Name | Description |
| ------------------------------------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | | ------------------------------------------------ | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `input_dir` | Path to directory containing pipeline data. ~~Path (positional)~~ | | `input_dir` | Path to directory containing pipeline data. ~~Path (positional)~~ |
| `output_dir` | Directory to create package folder in. ~~Path (positional)~~ | | `output_dir` | Directory to create package folder in. ~~Path (positional)~~ |
| `--code`, `-c` <Tag variant="new">3</Tag> | 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)~~ | | `--code`, `-c` <Tag variant="new">3</Tag> | 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). ~~str (option)~~ |
| `--meta-path`, `-m` <Tag variant="new">2</Tag> | Path to [`meta.json`](/api/data-formats#meta) file (optional). ~~Optional[Path] \(option)~~ | | `--meta-path`, `-m` <Tag variant="new">2</Tag> | Path to [`meta.json`](/api/data-formats#meta) file (optional). ~~Optional[Path] \(option)~~ |
| `--create-meta`, `-C` <Tag variant="new">2</Tag> | 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)~~ | | `--create-meta`, `-C` <Tag variant="new">2</Tag> | 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` <Tag variant="new">3</Tag> | Don't build the `.tar.gz` sdist automatically. Can be set if you want to run this step manually. ~~bool (flag)~~ | | `--build`, `-b` <Tag variant="new">3</Tag> | Comma-separated artifact formats to build. Can be `sdist` (for a `.tar.gz` archive) and/or `wheel` (for a binary `.whl` file), or `none` if you want to run this step manually. The generated artifacts can be installed by `pip install`. Defaults to `sdist`. ~~str (option)~~ |
| `--wheel`, `-W` <Tag variant="new">3</Tag> | 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` <Tag variant="new">3</Tag> | Package name to override in meta. ~~Optional[str] \(option)~~ |
| `--name`, `-n` <Tag variant="new">3</Tag> | Package name to override in meta. ~~Optional[str] \(option)~~ | | `--version`, `-v` <Tag variant="new">3</Tag> | Package version to override in meta. Useful when training new versions, as it doesn't require editing the meta template. ~~Optional[str] \(option)~~ |
| `--version`, `-v` <Tag variant="new">3</Tag> | 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)~~ |
| `--force`, `-f` | Force overwriting of existing folder in output directory. ~~bool (flag)~~ | | `--help`, `-h` | Show help message and available arguments. ~~bool (flag)~~ |
| `--help`, `-h` | Show help message and available arguments. ~~bool (flag)~~ | | **CREATES** | A Python package containing the spaCy pipeline. |
| **CREATES** | A Python package containing the spaCy pipeline. |
## project {#project new="3"} ## project {#project new="3"}

View File

@ -1064,9 +1064,10 @@ setting up the label scheme.
The [`spacy package`](/api/cli#package) command now automatically builds the 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 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` step manually anymore. To disable the behavior, you can set `--build none`. You
flag. You can now also specify `--wheel` to build a binary `.whl` file instead, can also choose to build a binary wheel (which installs more efficiently) by
which will install more efficiently. setting `--build wheel`, or to build both the sdist and wheel by setting
`--build sdist,wheel`.
```diff ```diff
python -m spacy package ./output ./packages python -m spacy package ./output ./packages