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

Feature: setup.py import #1137

Merged
merged 4 commits into from
Jun 14, 2022
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
13 changes: 7 additions & 6 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ on:
branches:
- main
- dev
- '1.x'
- "1.x"
paths-ignore:
- "docs/**"
- "news/**"
Expand Down Expand Up @@ -51,11 +51,6 @@ jobs:
- uses: actions/checkout@v2
with:
submodules: true
- name: Set Python 2.7
uses: actions/setup-python@v2
with:
python-version: 2.7
architecture: ${{ matrix.arch }}
- name: Set Python 3.6
uses: actions/setup-python@v2
with:
Expand All @@ -79,6 +74,12 @@ jobs:
with:
python-version: 3.9
architecture: ${{ matrix.arch }}
- name: Set Python 3.10
uses: actions/setup-python@v2
if: matrix.python-version != '3.10'
with:
python-version: "3.10"
architecture: ${{ matrix.arch }}
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v2
with:
Expand Down
9 changes: 6 additions & 3 deletions docs/docs/usage/project.md
Original file line number Diff line number Diff line change
Expand Up @@ -215,10 +215,13 @@ PDM provides `import` command so that you don't have to initialize the project m
1. Pipenv's `Pipfile`
2. Poetry's section in `pyproject.toml`
3. Flit's section in `pyproject.toml`
4. `requirements.txt` format used by Pip
4. `requirements.txt` format used by pip
5. setuptools `setup.py`

Also, when you are executing `pdm init` or `pdm install`, PDM can auto-detect possible files to import
if your PDM project has not been initialized yet.
Also, when you are executing `pdm init` or `pdm install`, PDM can auto-detect possible files to import if your PDM project has not been initialized yet.

!!! attention "CAUTION"
Converting a `setup.py` will execute the file with the project interpreter. Make sure `setuptools` is installed with the interpreter and the `setup.py` is trusted.

## Export locked packages to alternative formats

Expand Down
1 change: 1 addition & 0 deletions news/1062.feature.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add support for importing from a `setup.py` project.
23 changes: 16 additions & 7 deletions pdm.lock

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

1 change: 1 addition & 0 deletions pdm/cli/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,7 @@ def find_importable_files(project: Project) -> Iterable[tuple[str, Path]]:
"pyproject.toml",
"requirements.in",
"requirements.txt",
"setup.py",
):
project_file = project.root / filename
if not project_file.exists():
Expand Down
53 changes: 50 additions & 3 deletions pdm/formats/setup_py.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,63 @@
import os
from pathlib import Path
from typing import Any, List, Optional
from typing import Any, Dict, List, Mapping, Optional, Tuple

from pdm.formats.base import array_of_inline_tables, make_array, make_inline_table
from pdm.project import Project


def check_fingerprint(project: Project, filename: Path) -> bool:
return os.path.basename(filename) == "setup.py"


def convert(project: Project, filename: Path, options: Optional[Any]) -> None:
raise NotImplementedError()
def convert(
project: Project, filename: Path, options: Optional[Any]
) -> Tuple[Mapping[str, Any], Mapping[str, Any]]:
from pdm.models.in_process import parse_setup_py

parsed = parse_setup_py(
str(project.environment.interpreter.executable), str(filename)
)
metadata: Dict[str, Any] = {}
settings: Dict[str, Any] = {}
for name in [
"name",
"version",
"description",
"keywords",
"urls",
"readme",
]:
if name in parsed:
metadata[name] = parsed[name]
if "authors" in parsed:
metadata["authors"] = array_of_inline_tables(parsed["authors"])
if "maintainers" in parsed:
metadata["maintainers"] = array_of_inline_tables(parsed["maintainers"])
if "classifiers" in parsed:
metadata["classifiers"] = make_array(sorted(parsed["classifiers"]), True)
if "python_requires" in parsed:
metadata["requires-python"] = parsed["python_requires"]
if "install_requires" in parsed:
metadata["dependencies"] = make_array(sorted(parsed["install_requires"]), True)
if "extras_require" in parsed:
metadata["optional-dependencies"] = {
k: make_array(sorted(v), True) for k, v in parsed["extras_require"].items()
}
if "license" in parsed:
metadata["license"] = make_inline_table({"text": parsed["license"]})
if "package_dir" in parsed:
settings["package-dir"] = parsed["package_dir"]

entry_points = parsed.get("entry_points", {})
if "console_scripts" in entry_points:
metadata["scripts"] = entry_points.pop("console_scripts")
if "gui_scripts" in entry_points:
metadata["gui-scripts"] = entry_points.pop("gui_scripts")
if entry_points:
metadata["entry-points"] = entry_points

return metadata, settings


def export(project: Project, candidates: List, options: Optional[Any]) -> str:
Expand Down
8 changes: 7 additions & 1 deletion pdm/models/in_process/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import os
import subprocess
from pathlib import Path
from typing import Dict, Optional
from typing import Any, Dict, Optional

FOLDER_PATH = Path(__file__).parent

Expand Down Expand Up @@ -35,3 +35,9 @@ def get_pep508_environment(executable: str) -> Dict[str, str]:
script = str(FOLDER_PATH / "pep508.py")
args = [executable, "-Es", script]
return json.loads(subprocess.check_output(args))


def parse_setup_py(executable: str, path: str) -> Dict[str, Any]:
"""Parse setup.py and return the kwargs"""
cmd = [executable, "-Es", str(FOLDER_PATH / "parse_setup.py"), path]
return json.loads(subprocess.check_output(cmd))
Loading