Commit dad75f6c by Sébastien Eustace

Add support for optional scripts

parent 48ed586b
......@@ -9,6 +9,7 @@
- Added a cache version system.
- Added a `--lock` option to `update` to only update the lock file without executing operations.
- Added support for the `Project-URL` metadata.
- Added support for optional scripts.
### Changed
......
......@@ -20,6 +20,9 @@ class RunCommand(EnvCommand):
return self.env.execute(*args)
def run_script(self, script, args):
if isinstance(script, dict):
script = script["callable"]
module, callable_ = script.split(":")
src_in_sys_path = "sys.path.append('src'); " if self._module.is_in_src() else ""
......
......@@ -371,6 +371,41 @@
]
}
},
"scripts": {
"type": "object",
"patternProperties": {
"^[a-zA-Z-_.0-9]+$": {
"oneOf": [
{
"$ref": "#/definitions/script"
},
{
"$ref": "#/definitions/extra-script"
}
]
}
}
},
"script": {
"type": "string",
"description": "A simple script pointing to a callable object."
},
"extra-script": {
"type": "object",
"description": "A script that should be installed only if extras are activated.",
"properties": {
"callable": {
"$ref": "#/definitions/script"
},
"extras": {
"type": "array",
"description": "The required extras for this script.",
"items": {
"type": "string"
}
}
}
},
"repository": {
"type": "object",
"properties": {
......
......@@ -138,7 +138,12 @@ class Builder(object):
# Scripts -> Entry points
for name, ep in self._poetry.local_config.get("scripts", {}).items():
result["console_scripts"].append("{} = {}".format(name, ep))
extras = ""
if isinstance(ep, dict):
extras = "[{}]".format(", ".join(ep["extras"]))
ep = ep["callable"]
result["console_scripts"].append("{} = {}{}".format(name, ep, extras))
# Plugins -> entry points
plugins = self._poetry.local_config.get("plugins", {})
......
......@@ -215,4 +215,20 @@ class Poetry:
"Consider specifying a more explicit one."
)
# Checking for scripts with extras
if "scripts" in config:
scripts = config["scripts"]
for name, script in scripts.items():
if not isinstance(script, dict):
continue
extras = script["extras"]
for extra in extras:
if extra not in config["extras"]:
result["errors"].append(
'Script "{}" requires extra "{}" which is not defined.'.format(
name, extra
)
)
return result
......@@ -37,3 +37,4 @@ time = ["pendulum"]
[tool.poetry.scripts]
my-script = "my_package:main"
my-2nd-script = "my_package:main2"
extra-script = {callable = "my_package.extra:main", extras = ["time"]}
......@@ -107,6 +107,7 @@ def test_complete():
decode(entry_points.decode())
== """\
[console_scripts]
extra-script=my_package.extra:main[time]
my-2nd-script=my_package:main2
my-script=my_package:main
......
......@@ -121,6 +121,7 @@ def test_make_setup():
assert ns["install_requires"] == ["cachy[msgpack]>=0.2.0,<0.3.0", "cleo>=0.6,<0.7"]
assert ns["entry_points"] == {
"console_scripts": [
"extra-script = my_package.extra:main[time]",
"my-2nd-script = my_package:main2",
"my-script = my_package:main",
]
......@@ -222,26 +223,6 @@ def test_make_pkg_info_multi_constraints_dependency():
]
def test_find_files_to_add():
poetry = Poetry.create(project("complete"))
builder = SdistBuilder(poetry, NullEnv(), NullIO())
result = builder.find_files_to_add()
assert sorted(result) == sorted(
[
Path("LICENSE"),
Path("README.rst"),
Path("my_package/__init__.py"),
Path("my_package/data1/test.json"),
Path("my_package/sub_pkg1/__init__.py"),
Path("my_package/sub_pkg2/__init__.py"),
Path("my_package/sub_pkg2/data2/data.json"),
Path("pyproject.toml"),
]
)
def test_find_packages():
poetry = Poetry.create(project("complete"))
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment