Total Complexity | 6 |
Total Lines | 47 |
Duplicated Lines | 0 % |
Changes | 0 |
1 | """ |
||
2 | Anaconda environments. |
||
3 | |||
4 | Original source: https://github.com/dmyersturnbull/tyrannosaurus |
||
5 | Copyright 2020–2021 Douglas Myers-Turnbull |
||
6 | Licensed under the Apache License, Version 2.0 (the "License"); |
||
7 | you may not use this file except in compliance with the License. |
||
8 | You may obtain a copy of the License at https://www.apache.org/licenses/LICENSE-2.0 |
||
9 | """ |
||
10 | import logging |
||
11 | from pathlib import Path |
||
12 | from typing import Sequence |
||
13 | |||
14 | from tyrannosaurus.context import Context |
||
15 | from tyrannosaurus.helpers import EnvHelper |
||
16 | |||
17 | |||
18 | logger = logging.getLogger(__package__) |
||
19 | |||
20 | |||
21 | class CondaEnv: |
||
22 | def __init__(self, name: str, dev: bool, extras: bool): |
||
23 | self.name = name |
||
24 | self.dev = dev |
||
25 | self.extras = extras |
||
26 | |||
27 | def create(self, context: Context, path: Path) -> Sequence[str]: |
||
28 | deps = self._get_deps(context) |
||
29 | logger.info(f"Writing environment with {len(deps)} dependencies to {path} ...") |
||
30 | lines = EnvHelper().process(self.name, deps, self.extras) |
||
31 | if not context.dry_run: |
||
32 | path.parent.mkdir(parents=True, exist_ok=True) |
||
33 | path.write_text("\n".join(lines), encoding="utf8") |
||
34 | return lines |
||
35 | |||
36 | def _get_deps(self, context: Context) -> Sequence[str]: |
||
37 | path = Path(self.name + ".yml") |
||
38 | if path.exists(): |
||
39 | context.back_up(path) |
||
40 | deps = dict(context.deps) |
||
41 | if self.dev: |
||
42 | deps.update(context.dev_deps) |
||
43 | return deps |
||
44 | |||
45 | |||
46 | __all__ = ["CondaEnv"] |
||
47 |