Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
CagtayFabry committed Apr 8, 2024
1 parent 21ad9b6 commit f8428c7
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 18 deletions.
12 changes: 6 additions & 6 deletions pydeps2env/environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,12 +223,12 @@ def _get_dependencies(
if _python:
deps = [str(_python)] + deps

pip = [
str(r)
for r in reqs.values()
if (not isinstance(r, Requirement) or r.name in _pip_packages)
and r.name not in remove
]
pip = []
for r in reqs.values():
if (
not isinstance(r, Requirement) or r.name in _pip_packages
) and r.name not in remove:
pip.append(str(r) if not r.url else r.url)
pip.sort(key=str.lower)

return deps, pip
Expand Down
57 changes: 45 additions & 12 deletions pydeps2env/generate_environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,54 @@

from pathlib import Path

import yaml

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],
output_file: str,
channels: list[str],
extras: list[str],
pip: set[str],
remove: set[str],
sources: list[str],
output: str = "environment.yml",
*,
channels: list[str] = None,
extras: list[str] = None,
pip: set[str] = None,
remove: set[str] = None,
additional_requirements: list[str] = None,
include_build_system: str = "omit",
):
if channels is None:
channels = ["conda-forge"]
if extras is None:
extras = []
if pip is None:
pip = []
if remove is None:
remove = {}
if additional_requirements is None:
additional_requirements = []
pip = set(pip)
env = Environment(filename[0], pip_packages=pip, extras=extras, channels=channels)
for f in filename[1:]:
env = Environment(
sources[0],
pip_packages=pip,
extras=extras,
channels=channels,
extra_requirements=additional_requirements,
)
for f in sources[1:]:
env.combine(Environment(f, pip_packages=pip, extras=extras, channels=channels))

_include = include_build_system == "include"
env.export(output_file, include_build_system=_include, remove=remove)
env.export(output, include_build_system=_include, remove=remove)


def create_from_definition(env_def: str):
with open(env_def, "r") as f:
config = yaml.load(f.read(), yaml.SafeLoader)
create_environment_file(**config)


def create_environment(
Expand Down Expand Up @@ -59,7 +84,11 @@ def main():

parser = argparse.ArgumentParser()
parser.add_argument(
"setup", type=str, nargs="*", default="pyproject.toml", help="dependency file"
"sources",
type=str,
nargs="*",
default="pyproject.toml",
help="dependency files and sources",
)
parser.add_argument(
"-o", "--output", type=str, default="environment.yml", help="output file"
Expand All @@ -75,6 +104,9 @@ def main():
)
parser.add_argument("-p", "--pip", type=str, nargs="*", default=[])
parser.add_argument("-r", "--remove", type=str, nargs="*", default=[])
parser.add_argument(
"-a", "--additional_requirements", type=str, nargs="*", default=[]
)
args = parser.parse_args()

for file in args.setup:
Expand All @@ -83,13 +115,14 @@ def main():
raise FileNotFoundError(f"Could not find file {filename}")

create_environment_file(
filename=args.setup,
filename=args.sources,
output_file=args.output,
channels=args.channels,
extras=args.extras,
pip=args.pip,
include_build_system=args.build_system,
remove=args.remove,
additional_requirements=args.additional_requirements,
include_build_system=args.build_system,
)


Expand Down

0 comments on commit f8428c7

Please sign in to comment.