Skip to content

Commit

Permalink
add combine
Browse files Browse the repository at this point in the history
  • Loading branch information
CagtayFabry committed Mar 9, 2024
1 parent 1fcffc2 commit 8a76b07
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 11 deletions.
8 changes: 4 additions & 4 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.1.0
rev: v4.5.0
hooks:
- id: trailing-whitespace
args: [--markdown-linebreak-ext=md]
- id: end-of-file-fixer
- id: check-yaml
# ----- Python formatting -----
- repo: https://github.com/charliermarsh/ruff-pre-commit
rev: v0.1.13
rev: v0.3.2
hooks:
# Run ruff linter.
- id: ruff
Expand All @@ -24,10 +24,10 @@ repos:
- id: pretty-format-yaml
args: [--autofix, --indent, '2']
- repo: https://github.com/tox-dev/pyproject-fmt
rev: 1.6.0
rev: 1.7.0
hooks:
- id: pyproject-fmt
- repo: https://github.com/abravalheri/validate-pyproject
rev: v0.15
rev: v0.16
hooks:
- id: validate-pyproject
8 changes: 8 additions & 0 deletions pydeps2env/environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,3 +150,11 @@ def export(
)
with open(p, "w") as outfile:
yaml.dump(conda_env, outfile, default_flow_style=False)

def combine(self, other: Environment):
"""Merge other Environment requirements into this Environment."""
self.requirements = combine_requirements(self.requirements, other.requirements)
self.build_system = combine_requirements(self.build_system, other.build_system)
self.pip_packages = list(set(self.pip_packages) | set(other.pip_packages))
self.pip_packages.sort(key=str.lower)

17 changes: 10 additions & 7 deletions pydeps2env/generate_environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@


def create_environment_file(
filename: str,
filename: list[str],
output_file: str,
channels: list[str],
extras: list[str],
Expand All @@ -16,7 +16,9 @@ def create_environment_file(
except ModuleNotFoundError: # try local file if not installed
from environment import Environment

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

_include = include_build_system == "include"
env.export(output_file, include_build_system=_include)
Expand All @@ -27,9 +29,9 @@ def main():

parser = argparse.ArgumentParser()
parser.add_argument(
"setup", type=str, default="pyproject.toml", help="dependency file"
"setup", type=str, nargs="*", default="pyproject.toml", help="dependency file"
)
parser.add_argument("env", type=str, default="environment.yml", help="output file")
parser.add_argument("-o", "--output", type=str, default="environment.yml", help="output file")
parser.add_argument("-c", "--channels", type=str, nargs="*", default=["defaults"])
parser.add_argument("-e", "--extras", type=str, nargs="*", default=[])
parser.add_argument(
Expand All @@ -42,12 +44,13 @@ def main():
parser.add_argument("-p", "--pip", type=str, nargs="*", default=[])
args = parser.parse_args()

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

create_environment_file(
filename=args.setup,
output_file=args.env,
output_file=args.output,
channels=args.channels,
extras=args.extras,
pip=args.pip,
Expand Down
1 change: 1 addition & 0 deletions test/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ dependencies = [
"IPython",
"numpy>=1.20",
"pandas>=1",
"both>=1",
]
[project.optional-dependencies]
pip_only = [
Expand Down
2 changes: 2 additions & 0 deletions test/setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ setup_requires =
install_requires =
numpy >=1.20
pandas >=1.0
both <=3.0
setup_only >=1.0

[options.extras_require]
test =
Expand Down

0 comments on commit 8a76b07

Please sign in to comment.