Skip to content

Commit

Permalink
add setup.cfg parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
CagtayFabry committed Jan 31, 2024
1 parent c3b5e51 commit 49a05d8
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions pydeps2env/environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from packaging.requirements import Requirement
from pathlib import Path
from collections import defaultdict
import configparser
import tomli as tomllib
import yaml
import warnings
Expand Down Expand Up @@ -50,8 +51,13 @@ class Environment:
def __post_init__(self):
if Path(self.filename).suffix == ".toml":
self.load_pyproject()
elif Path(self.filename).suffix == ".cfg":
self.load_config()
else:
raise ValueError(f"Unsupported input {self.filename}")

def load_pyproject(self):
"""Load contents from a toml file (assume pyproject.toml layout)."""
with open(self.filename, "rb") as fh:
cp = defaultdict(dict, tomllib.load(fh))

Expand All @@ -71,6 +77,30 @@ def load_pyproject(self):
for dep in extra_deps:
add_requirement(dep, self.requirements)


def load_config(self):
"""Load contents from a cfg file (assume setup.cfg layout)."""
cp = configparser.ConfigParser(
converters={"list": lambda x: [i.strip() for i in x.split("\n") if i.strip()]}
)
cp.read(self.filename)

if python := cp.get("options", "python_requires"):
add_requirement("python" + python, self.requirements)

for dep in cp.getlist("options", "install_requires"):
add_requirement(dep, self.requirements)

for dep in cp.getlist("options", "setup_requires"):
add_requirement(dep, self.build_system)

for e in self.extras:
extra_deps = cp.getlist("options.extras_require", e)
if not extra_deps:
continue
for dep in extra_deps:
add_requirement(dep, self.requirements)

def _get_dependencies(self, include_build_system: bool = True):
"""Get the default conda environment entries."""

Expand Down

0 comments on commit 49a05d8

Please sign in to comment.