Skip to content

Commit

Permalink
cleanup script file implementation
Browse files Browse the repository at this point in the history
This change refactors implementation and tests relating to script file
specifications.

Relates-to: #40
Relates-to: #241
  • Loading branch information
abn committed May 7, 2021
1 parent 8b1cc5e commit 76057b2
Show file tree
Hide file tree
Showing 35 changed files with 265 additions and 93 deletions.
69 changes: 30 additions & 39 deletions poetry/core/masonry/builders/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,50 +285,36 @@ def convert_entry_points(self) -> Dict[str, List[str]]:
result = defaultdict(list)

# Scripts -> Entry points
for name, ep in self._poetry.local_config.get("scripts", {}).items():
extras: str = ""
module_path: str = ""
for name, specification in self._poetry.local_config.get("scripts", {}).items():
if isinstance(specification, str):
# TODO: deprecate this in favour or reference
specification = {"reference": specification, "type": "console"}

# Currently we support 2 legacy and 1 new format:
# (legacy) my_script = 'my_package.main:entry'
# (legacy) my_script = { callable = 'my_package.main:entry' }
# (supported) my_script = { reference = 'my_package.main:entry', type = "console" }

if isinstance(ep, str):
if "callable" in specification:
warnings.warn(
"This way of declaring console scripts is deprecated and will be removed in a future version. "
'Use reference = "{}", type = "console" instead.'.format(ep),
f"Use of callable in script specification ({name}) is deprecated. Use reference instead.",
DeprecationWarning,
)
extras = ""
module_path = ep
elif isinstance(ep, dict) and (
ep.get("type") == "console"
or "callable" in ep # Supporting both new and legacy format for now
):
if "callable" in ep:
warnings.warn(
"Using the keyword callable is deprecated and will be removed in a future version. "
'Use reference = "{}", type = "console" instead.'.format(
ep["callable"]
),
DeprecationWarning,
)
specification = {
"reference": specification["callable"],
"type": "console",
}

extras = "[{}]".format(", ".join(ep["extras"]))
module_path = ep.get("reference", ep.get("callable"))
else:
if specification.get("type") != "console":
continue

result["console_scripts"].append(
"{} = {}{}".format(name, module_path, extras)
)
extras = specification.get("extras", [])
extras = f"[{', '.join(extras)}]" if extras else ""
reference = specification.get("reference")

if reference:
result["console_scripts"].append(f"{name} = {reference}{extras}")

# Plugins -> entry points
plugins = self._poetry.local_config.get("plugins", {})
for groupname, group in plugins.items():
for name, ep in sorted(group.items()):
result[groupname].append("{} = {}".format(name, ep))
for name, specification in sorted(group.items()):
result[groupname].append(f"{name} = {specification}")

for groupname in result:
result[groupname] = sorted(result[groupname])
Expand All @@ -338,21 +324,26 @@ def convert_entry_points(self) -> Dict[str, List[str]]:
def convert_script_files(self) -> List[Path]:
script_files: List[Path] = []

for _, ep in self._poetry.local_config.get("scripts", {}).items():
if isinstance(ep, dict) and ep.get("type") == "file":
source = ep["reference"]
for name, specification in self._poetry.local_config.get("scripts", {}).items():
if isinstance(specification, dict) and specification.get("type") == "file":
source = specification["reference"]

if Path(source).is_absolute():
raise RuntimeError(
"{} is an absolute path. Expected relative path.".format(source)
f"{source} in {name} is an absolute path. Expected relative path."
)

abs_path = Path.joinpath(self._path, source)

if not abs_path.exists():
raise RuntimeError("{} file-script is not found.".format(abs_path))
raise RuntimeError(
f"{abs_path} in script specification ({name}) is not found."
)

if not abs_path.is_file():
raise RuntimeError("{} file-script is not a file.".format(abs_path))
raise RuntimeError(
f"{abs_path} in script specification ({name}) is not a file."
)

script_files.append(abs_path)

Expand Down
7 changes: 3 additions & 4 deletions tests/masonry/builders/fixtures/complete/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ cachy = { version = "^0.2.0", extras = ["msgpack"] }

[tool.poetry.dependencies.pendulum]
version = "^1.4"
markers= 'python_version ~= "2.7" and sys_platform == "win32" or python_version in "3.4 3.5"'
markers = 'python_version ~= "2.7" and sys_platform == "win32" or python_version in "3.4 3.5"'
optional = true

[tool.poetry.dev-dependencies]
Expand All @@ -48,9 +48,8 @@ time = ["pendulum"]
[tool.poetry.scripts]
my-script = "my_package:main"
my-2nd-script = "my_package:main2"
extra-script-legacy = {callable = "my_package.extra_legacy:main", extras = ["time"]}
extra-script = {reference = "my_package.extra:main", extras = ["time"], type = "console"}
sh-script = {reference = "bin/script1.sh", type = "file"}
file-script = { reference = "bin/script.sh", type = "file" }
extra-script = { reference = "my_package.extra:main", extras = ["time"], type = "console" }


[tool.poetry.urls]
Expand Down

This file was deleted.

Empty file.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
My Package
==========
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
__version__ = "1.2.3"
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
[tool.poetry]
name = "my-package"
version = "1.2.3"
description = "Some description."
authors = [
"Poetry Maintainers <[email protected]>"
]
license = "MIT"
readme = "README.rst"

[tool.poetry.dependencies]
python = "^3.6"

[tool.poetry.dev-dependencies]

[tool.poetry.extras]

[tool.poetry.scripts]
script-legacy = "my_package:main"
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
My Package
==========
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
__version__ = "1.2.3"
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
[tool.poetry]
name = "my-package"
version = "1.2.3"
description = "Some description."
authors = [
"Poetry Maintainers <[email protected]>"
]
license = "MIT"
readme = "README.rst"

[tool.poetry.dependencies]
python = "^3.6"

[tool.poetry.dev-dependencies]

[tool.poetry.extras]
time = []

[tool.poetry.scripts]
script-legacy = { callable = "my_package.extra_legacy:main" }
extra-script-legacy = { callable = "my_package.extra_legacy:main", extras = ["time"] }

This file was deleted.

This file was deleted.

This file was deleted.

Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
My Package
==========
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
__version__ = "1.2.3"
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
[tool.poetry]
name = "my-package"
version = "1.2.3"
description = "Some description."
authors = [
"Poetry Maintainers <[email protected]>"
]
license = "MIT"
readme = "README.rst"

[tool.poetry.dependencies]
python = "^3.6"

[tool.poetry.dev-dependencies]

[tool.poetry.extras]
time = []

[tool.poetry.scripts]
script = { reference = "my_package.extra:main", type = "console" }
extra-script = { reference = "my_package.extra:main", extras = ["time"], type = "console" }
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
My Package
==========
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env bash

echo "Hello World!"
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
__version__ = "1.2.3"
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
[tool.poetry]
name = "my-package"
version = "1.2.3"
description = "Some description."
authors = [
"Poetry Maintainers <[email protected]>"
]
license = "MIT"
readme = "README.rst"

[tool.poetry.dependencies]
python = "^3.6"

[tool.poetry.dev-dependencies]

[tool.poetry.extras]

[tool.poetry.scripts]
sh-script = { reference = "bin/script.sh", type = "file" }
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
My Package
==========
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env bash

echo "Hello World!"
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
__version__ = "1.2.3"
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
[tool.poetry]
name = "my-package"
version = "1.2.3"
description = "Some description."
authors = [
"Poetry Maintainers <[email protected]>"
]
license = "MIT"
readme = "README.rst"

[tool.poetry.dependencies]
python = "^3.6"

[tool.poetry.dev-dependencies]

[tool.poetry.extras]

[tool.poetry.scripts]
invalid_definition = { reference = "bin/script.sh", type = "ffiillee" }
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
My Package
==========
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
__version__ = "1.2.3"
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
[tool.poetry]
name = "my-package"
version = "1.2.3"
description = "Some description."
authors = [
"Poetry Maintainers <[email protected]>"
]
license = "MIT"
readme = "README.rst"

[tool.poetry.dependencies]
python = "^3.6"

[tool.poetry.dev-dependencies]

[tool.poetry.extras]

[tool.poetry.scripts]
sh-script = { reference = "bin/script.sh", type = "file" }
Loading

0 comments on commit 76057b2

Please sign in to comment.