Skip to content

Commit

Permalink
feat: set field as dynamic if variable exists in req
Browse files Browse the repository at this point in the history
  • Loading branch information
frostming committed Oct 21, 2022
1 parent 09a97e5 commit 7877f73
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 21 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ curl -sSL https://raw.githubusercontent.com/pdm-project/pdm/main/install-pdm.py
```

For security reasons, you should verify the checksum of `install-pdm.py`.
The sha256 checksum is: `6222f16660490dc2bb67ed2c9ae5f641d0283dcc4e91692f64319835529172ed`
The sha256 checksum is: `ed83f61b7ad3c3fcace57fda31175ad861c4283aeea02ba13b6351a66c2cca60`

The installer will install PDM into the user site and the location depends on the system:

Expand Down
2 changes: 1 addition & 1 deletion README_zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ curl -sSL https://raw.githubusercontent.com/pdm-project/pdm/main/install-pdm.py
```

为安全起见,你应该检查 `install-pdm.py` 文件的正确性。
SHA256 校验和: `6222f16660490dc2bb67ed2c9ae5f641d0283dcc4e91692f64319835529172ed`
SHA256 校验和: `ed83f61b7ad3c3fcace57fda31175ad861c4283aeea02ba13b6351a66c2cca60`

默认情况下,此脚本会将 PDM 安装在 Python 的用户目录下,具体位置取决于当前系统:

Expand Down
2 changes: 1 addition & 1 deletion docs/docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ Like Pip, PDM provides an installation script that will install PDM into an isol
```

For security reasons, you should verify the checksum of `install-pdm.py`.
The sha256 checksum is: `6222f16660490dc2bb67ed2c9ae5f641d0283dcc4e91692f64319835529172ed`
The sha256 checksum is: `ed83f61b7ad3c3fcace57fda31175ad861c4283aeea02ba13b6351a66c2cca60`

The installer will install PDM into the user site and the location depends on the system:

Expand Down
17 changes: 11 additions & 6 deletions install-pdm.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ class Installer:
prerelease: bool = False
additional_deps: Sequence[str] = ()
skip_add_to_path: bool = False
output_path: str | None = None

def __post_init__(self):
self._path = self._decide_path()
Expand Down Expand Up @@ -360,21 +361,21 @@ def _post_install(self, venv_path: Path, bin_path: Path) -> None:
)
if not self.skip_add_to_path:
_add_to_path(bin_path)
self._set_github_env(venv_path, script)
self._write_output(venv_path, script)

def _set_github_env(self, venv_path: Path, script: Path) -> None:
if not os.getenv("GITHUB_ENV"):
def _write_output(self, venv_path: Path, script: Path) -> None:
if not self.output_path:
return

print("Writing output to", colored("green", self.output_path))
output = {
"pdm_version": self.version,
"pdm_bin": str(script),
"install_python_version": f"{sys.version_info.major}."
f"{sys.version_info.minor}.{sys.version_info.micro}",
"install_location": str(venv_path),
}
with open(os.getenv("GITHUB_ENV"), "a") as f:
f.write(f"PDM_INSTALL_SCRIPT_OUTPUT<<EOF\n{json.dumps(output)}\nEOF")
with open(self.output_path, "w") as f:
json.dump(output, f, indent=2)

def install(self) -> None:
venv = self._make_env()
Expand Down Expand Up @@ -450,6 +451,9 @@ def main():
help="Do not add binary to the PATH.",
default=os.getenv("PDM_SKIP_ADD_TO_PATH"),
)
parser.add_argument(
"-o", "--output", help="Output file to write the installation info to"
)

options = parser.parse_args()
installer = Installer(
Expand All @@ -458,6 +462,7 @@ def main():
prerelease=options.prerelease,
additional_deps=options.dep,
skip_add_to_path=options.skip_add_to_path,
output_path=options.output,
)
if options.remove:
installer.uninstall()
Expand Down
2 changes: 1 addition & 1 deletion src/pdm/cli/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,7 @@ def do_remove(
if group not in list(project.iter_groups()):
raise ProjectError(f"No-exist group {group}")

deps = project.get_pyproject_dependencies(group, dev)
deps, _ = project.get_pyproject_dependencies(group, dev)
project.core.ui.echo(
f"Removing packages from [primary]{group}[/] "
f"{'dev-' if dev else ''}dependencies: "
Expand Down
31 changes: 21 additions & 10 deletions src/pdm/project/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -570,18 +570,23 @@ def is_lockfile_compatible(self) -> bool:
accepted = get_specifier(f"~={lockfile_version}")
return accepted.contains(self.LOCKFILE_VERSION)

def get_pyproject_dependencies(self, group: str, dev: bool = False) -> list[str]:
"""Get the dependencies array in the pyproject.toml"""
def get_pyproject_dependencies(
self, group: str, dev: bool = False
) -> tuple[list[str], bool]:
"""Get the dependencies array in the pyproject.toml
Return a tuple of two elements, the first is the dependencies array,
and the second tells whether it is a dev-dependencies group.
"""
if group == "default":
return self.meta.setdefault("dependencies", [])
return self.meta.setdefault("dependencies", []), False
deps_dict = {
False: self.meta.setdefault("optional-dependencies", {}),
True: self.tool_settings.setdefault("dev-dependencies", {}),
}
for deps in deps_dict.values():
for is_dev, deps in deps_dict.items():
if group in deps:
return deps[group]
return deps_dict[dev].setdefault(group, [])
return deps[group], is_dev
return deps_dict[dev].setdefault(group, []), dev

def add_dependencies(
self,
Expand All @@ -590,18 +595,24 @@ def add_dependencies(
dev: bool = False,
show_message: bool = True,
) -> None:
deps = cast(Array, self.get_pyproject_dependencies(to_group, dev))
deps.multiline(True)
deps, is_dev = self.get_pyproject_dependencies(to_group, dev)
cast(Array, deps).multiline(True)
has_variable = False
for _, dep in requirements.items():
matched_index = next(
(i for i, r in enumerate(deps) if dep.matches(r)),
None,
)
req = dep.as_line()
if "${" in req:
has_variable = True
if matched_index is None:
deps.append(dep.as_line())
deps.append(req)
else:
req = dep.as_line()
deps[matched_index] = req
if not is_dev and has_variable:
field = "dependencies" if to_group == "default" else "optional-dependencies"
self.meta["dynamic"] = sorted(set(self.meta.get("dynamic", []) + [field]))
self.write_pyproject(show_message)

def write_pyproject(self, show_message: bool = True) -> None:
Expand Down
2 changes: 1 addition & 1 deletion tests/cli/test_add.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ def test_add_editable_package_with_extras(project, working_set):
)
assert (
f"-e {path_to_url(dep_path)}#egg=demo[security]"
in project.get_pyproject_dependencies("dev", True)
in project.get_pyproject_dependencies("dev", True)[0]
)
assert "demo" in working_set
assert "requests" in working_set
Expand Down

0 comments on commit 7877f73

Please sign in to comment.