Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup script file implementation #177

Merged
merged 3 commits into from
May 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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
2 changes: 1 addition & 1 deletion poetry/core/masonry/builders/sdist.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ def build_setup(self) -> bytes:
script_files = self.convert_script_files()
if script_files:
rel_paths = [str(p.relative_to(self._path)) for p in script_files]
before.append('scripts = \\\n["{}"]\n'.format('", "'.join(rel_paths)))
before.append("scripts = \\\n{}\n".format(pformat(rel_paths)))
extra.append("'scripts': scripts,")

if self._package.python_versions != "*":
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.

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.

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