Skip to content

Commit

Permalink
fix parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
CagtayFabry committed Mar 10, 2024
1 parent 4679513 commit 45fc6fe
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 11 deletions.
14 changes: 11 additions & 3 deletions pydeps2env/environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,15 @@
import warnings


def split_extras(filename: str) -> tuple[str, set]:
"""Split extras requirements indicated in []."""
if "[" in filename:
filename, extras = filename.split("[",1)
extras = set(extras.split("]",1)[0].split(","))
else:
extras = {}
return filename, extras

def add_requirement(
req: Requirement | str,
requirements: dict[str, Requirement],
Expand Down Expand Up @@ -53,9 +62,8 @@ class Environment:
def __post_init__(self):
self.extras = set(self.extras)

if isinstance(self.filename, str) and "[" in self.filename:
self.filename, extras = self.filename.split("[",1)
extras = extras.split("]",1)[0].split(",")
if isinstance(self.filename, str):
self.filename, extras = split_extras(self.filename)
self.extras |= set(extras)

if Path(self.filename).suffix == ".toml":
Expand Down
17 changes: 9 additions & 8 deletions pydeps2env/generate_environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

from pathlib import Path

try:
from pydeps2env.environment import Environment, split_extras
except ModuleNotFoundError: # try local file if not installed
from environment import Environment, split_extras


def create_environment_file(
filename: list[str],
Expand All @@ -11,14 +16,9 @@ def create_environment_file(
pip: list[str],
include_build_system: bool = False,
):
try:
from pydeps2env.environment import Environment
except ModuleNotFoundError: # try local file if not installed
from environment import Environment

env = Environment(filename[0], pip_packages=pip, extras=extras, channels=channels)
for f in filename[1:]:
env.combine(Environment(f))
env.combine(Environment(f, pip_packages=pip, extras=extras, channels=channels))

_include = include_build_system == "include"
env.export(output_file, include_build_system=_include)
Expand Down Expand Up @@ -47,8 +47,9 @@ def main():
args = parser.parse_args()

for file in args.setup:
if not Path(file).is_file():
raise FileNotFoundError(f"Could not find file {args.setup}")
filename, _ = split_extras(file)
if not Path(filename).is_file():
raise FileNotFoundError(f"Could not find file {filename}")

create_environment_file(
filename=args.setup,
Expand Down

0 comments on commit 45fc6fe

Please sign in to comment.