Skip to content

Commit

Permalink
feat: support converting setup.cfg without existing setup.py
Browse files Browse the repository at this point in the history
Fixes #2222

Signed-off-by: Frost Ming <[email protected]>
  • Loading branch information
frostming committed Sep 1, 2023
1 parent 7a740a6 commit af96cea
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 27 deletions.
1 change: 1 addition & 0 deletions news/2222.feature.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Support convert setup.cfg without existing setup.py.
8 changes: 1 addition & 7 deletions src/pdm/cli/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -602,13 +602,7 @@ def find_importable_files(project: Project) -> Iterable[tuple[str, Path]]:
"""Find all possible files that can be imported"""
from pdm.formats import FORMATS

for filename in (
"Pipfile",
"pyproject.toml",
"requirements.in",
"requirements.txt",
"setup.py",
):
for filename in ("Pipfile", "pyproject.toml", "requirements.in", "requirements.txt", "setup.py", "setup.cfg"):
project_file = project.root / filename
if not project_file.exists():
continue
Expand Down
4 changes: 2 additions & 2 deletions src/pdm/formats/setup_py.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@


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


def convert(project: Project, filename: Path, options: Any | None) -> 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))
parsed = parse_setup_py(str(project.environment.interpreter.executable), os.path.dirname(filename))
metadata: dict[str, Any] = {}
settings: dict[str, Any] = {}
for name in [
Expand Down
37 changes: 19 additions & 18 deletions src/pdm/models/in_process/parse_setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,33 +165,34 @@ def clean_metadata(metadata: Dict[str, Any]) -> None:
def parse_setup(path: str) -> Dict[str, Any]:
import tokenize

import setuptools

setuptools.setup = fake_setup

parsed: Dict[str, Any] = {}
path = os.path.abspath(path)
project_path = os.path.dirname(path)
os.chdir(project_path)
os.chdir(path)
if os.path.exists("setup.cfg"):
parsed.update(_parse_setup_cfg("setup.cfg"))

# Execute setup.py and get the kwargs
__file__ = sys.argv[0] = path
sys.path.insert(0, project_path)
setup_kwargs.clear()
setup_path = os.path.join(path, "setup.py")
if os.path.exists(setup_path):
import setuptools

setuptools.setup = fake_setup

with tokenize.open(path) as f:
code = f.read()
exec(
compile(code, __file__, "exec"),
{"__name__": "__main__", "__file__": __file__, "setup_kwargs": setup_kwargs},
)
parsed.update(setup_kwargs)
# Execute setup.py and get the kwargs
__file__ = sys.argv[0] = setup_path
sys.path.insert(0, setup_path)
setup_kwargs.clear()

with tokenize.open(setup_path) as f:
code = f.read()
exec(
compile(code, __file__, "exec"),
{"__name__": "__main__", "__file__": __file__, "setup_kwargs": setup_kwargs},
)
parsed.update(setup_kwargs)

if "readme" not in parsed:
for readme_file in ("README.md", "README.rst", "README.txt"):
readme_path = os.path.join(project_path, readme_file)
readme_path = os.path.join(path, readme_file)
if os.path.exists(readme_path):
parsed["readme"] = readme_file
break
Expand Down

0 comments on commit af96cea

Please sign in to comment.