-
Notifications
You must be signed in to change notification settings - Fork 18
/
devenv.py
646 lines (522 loc) · 21 KB
/
devenv.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
import argparse
import os
import re
import sys
import shlex
from pathlib import Path
from textwrap import dedent
from .gen_scripts import render_activate_script, render_deactivate_script
_selector_pattern = re.compile(r".*?#\s*\[(.*)\].*")
def preprocess_selector_in_line(line):
x = _selector_pattern.search(line)
if x is None:
return line
expr = x.group(1).strip()
return f"{{% if {expr} %}}{line}{{% endif %}}"
def preprocess_selectors(contents):
contents = contents.split("\n")
lines = [preprocess_selector_in_line(line) for line in contents]
return "\n".join(lines)
def _min_conda_devenv_version(min_version):
"""Checks that the current conda devenv version is at least the given version"""
from distutils.version import LooseVersion
import conda_devenv
if LooseVersion(conda_devenv.__version__) < LooseVersion(min_version):
msg = "This file requires at minimum conda-devenv {}, but you have {} installed.\n"
sys.stderr.write(msg.format(min_version, conda_devenv.__version__))
sys.stderr.write("Please update conda-devenv.\n")
raise SystemExit(1)
return ""
def _get_env(var_name, default=None, valid=None):
""" Get env var value or default value and check against allowed values.
:param str var_name:
Name of the environment variable.
:param Optional[str] default:
Default value for the variable. If not specified, the method raises
an error when the variable is not set.
:param Optional[List[str]] valid:
List of allowed values of the variable.
:return: str
Value of the environment variable or default
"""
value = os.environ.get(var_name, default)
if value is None:
raise ValueError(f"Environment variable {var_name} is not set.")
if valid is not None and value not in valid:
raise ValueError(
f"Allowed values for environment variable {var_name} are {valid}, "
f"got {value}"
)
return value
def render_jinja(contents, filename, is_included):
import jinja2
import sys
import platform
iswin = sys.platform.startswith("win")
islinux = sys.platform.startswith("linux")
isosx = sys.platform.startswith("darwin")
is32bit = "32bit" == platform.architecture()[0]
is64bit = not is32bit
jinja_dict = {
"is_included": is_included,
"os": os,
"platform": platform,
"root": os.path.dirname(os.path.abspath(filename)),
"sys": sys,
"x86": "x86" == platform.machine(),
"x86_64": "x86_64" == platform.machine(),
"linux": islinux,
"linux32": islinux and is32bit,
"linux64": islinux and is64bit,
"osx": isosx,
"unix": islinux or isosx,
"win": iswin,
"win32": iswin and is32bit,
"win64": iswin and is64bit,
"min_conda_devenv_version": _min_conda_devenv_version,
"get_env": _get_env,
}
contents = preprocess_selectors(contents)
return jinja2.Template(contents).render(**jinja_dict)
def handle_includes(root_filename, root_yaml):
# This is a depth-first search
import yaml
import collections
queue = collections.OrderedDict({Path(root_filename).resolve(): root_yaml})
visited = collections.OrderedDict()
if root_yaml is None:
raise ValueError(
"The root file '{root_filename}' is empty.".format(
root_filename=root_filename
)
)
while queue:
filename, yaml_dict = queue.popitem()
if filename in visited:
continue
for included_filename in yaml_dict.get("includes") or []:
if Path(included_filename).is_absolute():
included_filename = Path(included_filename)
else:
included_filename = Path(filename).parent / included_filename
if not included_filename.is_file():
raise ValueError(
"Couldn't find the file '{included_filename}' "
"while processing the file '{filename}'.".format(
included_filename=included_filename, filename=filename
)
)
jinja_contents = render_jinja(
included_filename.read_text(encoding="UTF-8"),
included_filename,
is_included=True,
)
included_yaml_dict = yaml.safe_load(jinja_contents)
if included_yaml_dict is None:
raise ValueError(
"The file '{included_filename}' which was"
" included by '{filename}' is empty.".format(
included_filename=included_filename, filename=filename
)
)
queue[included_filename.resolve()] = included_yaml_dict
if "includes" in yaml_dict:
del yaml_dict["includes"]
visited[filename] = yaml_dict
return visited
def separate_strings_from_dicts(elements):
"""
Receive a list of strings and dicts an returns 2 lists, one solely with string and the other
with dicts.
:param List[Union[str, Dict[str, str]]] elements:
:rtype: Tuple[List[str], List[Dict[str, str]]]
"""
all_strs = []
all_dicts = []
for item in elements:
if isinstance(item, str):
all_strs.append(item)
elif isinstance(item, dict):
all_dicts.append(item)
else:
raise RuntimeError(f"Only strings and dicts are supported, got: {item!r}")
return all_strs, all_dicts
def merge(dicts, keys_to_skip=("name",)):
final_dict = {}
for d in dicts:
if not isinstance(d, dict):
raise ValueError(
"Found '{!r}' when a dict is expected, check if our's '*.devenv.yml'"
" files are properly formatted.".format(d)
)
for key, value in d.items():
if key in keys_to_skip:
continue
if key in final_dict:
if isinstance(value, dict):
final_dict[key] = merge([final_dict[key], value])
elif isinstance(value, list):
# The can be dicts inside lists `'dependencies': [{'pip': ['foo', 'bar']}]`.
target_strings, target_dicts = separate_strings_from_dicts(
final_dict[key]
)
new_strings, new_dicts = separate_strings_from_dicts(value)
s = set()
s.update(target_strings)
s.update(new_strings)
merged_list = sorted(list(s))
merged_dict = merge(target_dicts + new_dicts)
if len(merged_dict) > 0:
merged_list.append(merged_dict)
final_dict[key] = merged_list
elif value is None:
continue
else:
message = " ".join(
[
"Can't merge the key: '{key}' because it will override the previous value.",
"Only lists and dicts can be merged. The type obtained was: {type}",
]
).format(key=key, type=type(value))
raise ValueError(message)
elif value is not None:
final_dict[key] = value
merge_dependencies_version_specifications(final_dict, key_to_merge="dependencies")
return final_dict
def merge_dependencies_version_specifications(yaml_dict, key_to_merge, pip=False):
import collections
import re
value_to_merge = yaml_dict.get(key_to_merge, None)
if value_to_merge is None:
return
package_pattern = (
r"^(?P<channel>[a-z0-9_\-/.]+::)?"
# package regex based on https://conda.io/docs/building/pkg-name-conv.html#package-naming-conventions
r"(?P<package>[a-z0-9_\-.]+)"
r"\s*(?P<version>.*)$"
)
new_dependencies = {}
new_dict_dependencies = []
for dep in value_to_merge:
if isinstance(dep, dict):
for key in dep:
merge_dependencies_version_specifications(
dep, key_to_merge=key, pip=(key == "pip")
)
new_dict_dependencies.append(dep)
elif isinstance(dep, str):
if pip and ("+" in dep or ":" in dep):
# Look for dependencies in the pip section that are
# managed by version control. For example:
# hg+ssh://hg@bitbucket.org/mforbes/mmfutils-fork@0.4.12
# Skip processing these and just pass them through
package_name = dep
package_version = ""
else:
m = re.match(package_pattern, dep, flags=re.IGNORECASE)
if m is None:
raise RuntimeError(
'The package version specification "{}" do not follow the'
" expected format.".format(dep)
)
# Consider the channel name as part of the package name.
# If multiple channels are specified, the package will be repeated.
package_name = m.group("package")
if m.group("channel"):
package_name = m.group("channel") + package_name
package_version = m.group("version")
# OrderedDict is used as an ordered set, the value is ignored.
version_matchers = new_dependencies.setdefault(
package_name, collections.OrderedDict()
)
if len(package_version) > 0:
version_matchers[package_version] = True
else:
raise RuntimeError(f"Only strings and dicts are supported, got: {dep!r}")
result = set()
for dep_name, dep_version_matchers in new_dependencies.items():
if len(dep_version_matchers) > 0:
result.add(dep_name + " " + ",".join(dep_version_matchers))
else:
result.add(dep_name)
new_dict_dependencies = sorted(new_dict_dependencies, key=lambda x: list(x.keys()))
yaml_dict[key_to_merge] = sorted(result) + new_dict_dependencies
def load_yaml_dict(filename):
with open(filename, "r") as f:
contents = f.read()
rendered_contents = render_jinja(contents, filename, is_included=False)
import yaml
root_yaml = yaml.safe_load(rendered_contents)
all_yaml_dicts = handle_includes(filename, root_yaml)
for filename, yaml_dict in all_yaml_dicts.items():
environment_key_value = yaml_dict.get("environment", {})
if not isinstance(environment_key_value, dict):
raise ValueError(
"The 'environment' key is supposed to be a dictionary, but you have the type "
"'{type}' at '{filename}'.".format(
type=type(environment_key_value), filename=filename
)
)
merged_dict = merge(all_yaml_dicts.values())
# Force the "name" because we want to keep the name of the root yaml
merged_dict["name"] = root_yaml["name"]
environment = merged_dict.pop("environment", {})
return merged_dict, environment
DEFAULT_HEADER = "# generated by conda-devenv, do not modify and do not commit to VCS\n"
def render_for_conda_env(yaml_dict, header=DEFAULT_HEADER):
import yaml
contents = header
contents += yaml.dump(yaml_dict, default_flow_style=False)
return contents
def __write_conda_environment_file(args, filename, rendered_contents):
if args.output_file:
output_filename = args.output_file
else:
output_filename, yaml_ext = os.path.splitext(filename)
output_filename, devenv_ext = os.path.splitext(output_filename)
if yaml_ext == "" or devenv_ext == "":
# File has no extension or has a single extension, if we proceed we
# will override the input file
raise ValueError(
"Can't guess the output filename, please provide "
"the output filename with the --output-filename "
"flag"
)
output_filename += yaml_ext
with open(output_filename, "w") as f:
f.write(rendered_contents)
return output_filename
def truncate_history_file(env_directory):
"""
Since conda version 4.4 the "--prune" option does not prune the environment to match just the
supplied specs but take in account the previous environment history we truncate the history
file so only the package and version specs from the environment description file are used.
This is based on the comments:
- https://github.com/conda/conda/issues/6809#issuecomment-367877250
- https://github.com/conda/conda/issues/7279#issuecomment-389359679
If the behavior of the "--prune" option changes again or something in the lines
"--ignore-history" or "--prune-hard" ar implemented we should revisit this function and
update the "conda-env" arguments.
"""
if env_directory is None:
return # Environment does not exists, no history to truncate
from os.path import isfile, join
from time import time
from shutil import copyfile
history_filename = join(env_directory, "conda-meta", "history")
history_backup_filename = "{}.{}".format(history_filename, time())
if isfile(history_filename):
copyfile(history_filename, history_backup_filename)
with open(history_filename, "w") as history:
history.truncate()
def __call_conda_env_update(args, output_filename):
import sys
command = [
"conda",
"env",
"update",
"--file",
output_filename,
]
if not args.no_prune:
command.append("--prune")
if args.name:
command.extend(["--name", args.name])
if args.quiet:
command.extend(["--quiet"])
if args.verbose:
command.extend(["-" + "v" * args.verbose])
if not args.quiet:
print("> Executing: %s" % " ".join(command))
old_argv = sys.argv[:]
try:
del command[0]
sys.argv = command
try:
return _call_conda()
except SystemExit as e:
return e.code
finally:
sys.argv = old_argv
def _call_conda():
"""
Calls conda-env directly using its internal API. ``sys.argv`` must already be configured at this point.
We have this indirection here so we can mock this function during testing.
"""
from conda_env.cli.main import main
return main()
def write_activate_deactivate_scripts(
args, conda_yaml_dict, environment, env_directory
):
if env_directory is None:
env_name = args.name or conda_yaml_dict["name"]
env_directory = get_env_directory(env_name)
if env_directory is None:
raise ValueError("Couldn't find directory of environment '%s'" % env_name)
from os.path import join
activate_directory = join(env_directory, "etc", "conda", "activate.d")
deactivate_directory = join(env_directory, "etc", "conda", "deactivate.d")
if not os.path.exists(activate_directory):
os.makedirs(activate_directory)
if not os.path.exists(deactivate_directory):
os.makedirs(deactivate_directory)
if sys.platform == "win32":
files = [("devenv-vars.bat", "cmd")]
else:
# Linux and Mac should create a .sh
files = [("devenv-vars.sh", "bash"), ("devenv-vars.fish", "fish")]
for filename, shell in files:
activate_script = render_activate_script(environment, shell)
deactivate_script = render_deactivate_script(environment, shell)
with open(join(activate_directory, filename), "w") as f:
f.write(activate_script)
with open(join(deactivate_directory, filename), "w") as f:
f.write(deactivate_script)
def get_env_name(args, output_filename, conda_yaml_dict=None):
"""
:param argparse.Namespace args:
When the user supplies the name option in the command line this namespace have a "name"
defined with a not `None` value and this value is returned.
:param str output_filename:
No jinja rendering is performed on this file if it is used.
:param Optional[Dict[str,Any]] conda_yaml_dict:
If supplied and not `None` then `output_filename` is ignored.
"""
if args.name:
return args.name
if conda_yaml_dict is None:
import yaml
with open(output_filename, "r") as stream:
conda_yaml_dict = yaml.safe_load(stream)
return conda_yaml_dict["name"]
def _get_envs_dirs_from_conda():
from conda.base.context import context
return context.envs_dirs
def get_env_directory(env_name):
"""
:rtype: Optional[str]
:return: The environment path if the enviromment exists.
"""
envs_dirs = _get_envs_dirs_from_conda()
for directory in envs_dirs:
env = os.path.join(directory, env_name)
conda_meta_dir = os.path.join(env, "conda-meta")
if os.path.isdir(conda_meta_dir):
return os.path.normpath(env)
return None
def parse_env_var_args(env_var_args):
"""
:param List[str] env_var_args:
List of arguments in the form "VAR_NAME" or "VAR_NAME=VALUE"
:return: Dict[str,str]
Mapping from "VAR_NAME" to "VALUE" or empty str.
"""
env_vars = {}
if env_var_args is not None:
for arg in env_var_args:
split_arg = arg.split("=", 1)
if len(split_arg) == 1:
env_vars[split_arg[0]] = ""
elif len(split_arg) == 2:
env_vars[split_arg[0]] = split_arg[1]
return env_vars
def main(args=None):
if args is None:
args = sys.argv[1:]
parser = argparse.ArgumentParser(
description="Work with multiple conda-environment-like yaml files in dev mode."
)
parser.add_argument(
"--file",
"-f",
nargs="?",
help="The environment.devenv.yml file to process. "
"The default value is '%(default)s'.",
default="environment.devenv.yml",
)
parser.add_argument("--name", "-n", nargs="?", help="Name of environment.")
parser.add_argument(
"--print",
help="Prints the rendered file as will be sent to conda-"
"env to stdout and exits.",
action="store_true",
)
parser.add_argument(
"--print-full",
help="Similar to --print, but also " "includes the 'environment' section.",
action="store_true",
)
parser.add_argument(
"--no-prune", help="Don't pass --prune flag to conda-env.", action="store_true"
)
parser.add_argument("--output-file", nargs="?", help="Output filename.")
parser.add_argument(
"--quiet", action="store_true", default=False, help="Do not show progress"
)
parser.add_argument(
"--env-var",
"-e",
action="append",
help="Define or override environment variables in the form VAR_NAME or "
"VAR_NAME=VALUE.",
)
parser.add_argument(
"--verbose",
"-v",
action="count",
default=0,
help="Use once for info, twice for debug, three times for trace.",
)
parser.add_argument(
"--version", action="store_true", default=False, help="Show version and exit"
)
args = parser.parse_args(args)
if args.version:
from ._version import version
print(f"conda-devenv version {version}")
return 0
filename = args.file
filename = os.path.abspath(filename)
if not os.path.isfile(filename):
print(f'File "{filename}" does not exist.', file=sys.stderr)
return 1
is_devenv_input_file = filename.endswith(".devenv.yml")
if is_devenv_input_file:
# update environment variables
os.environ.update(parse_env_var_args(args.env_var))
# render conda-devenv file
conda_yaml_dict, environment = load_yaml_dict(filename)
rendered_contents = render_for_conda_env(conda_yaml_dict)
if args.print or args.print_full:
print(rendered_contents)
if args.print_full:
print(render_for_conda_env({"environment": environment}, header=""))
return 0
# Write to the output file
output_filename = __write_conda_environment_file(
args, filename, rendered_contents
)
else:
conda_yaml_dict = environment = None
# Just call conda-env directly in plain environment.yml files
output_filename = filename
if args.print:
with open(filename) as f:
print(f.read())
return 0
env_name = get_env_name(args, output_filename, conda_yaml_dict)
env_directory = get_env_directory(env_name)
if not args.no_prune:
# Truncate the history file
truncate_history_file(env_directory)
# Call conda-env update
retcode = __call_conda_env_update(args, output_filename)
if retcode:
return retcode
if is_devenv_input_file:
write_activate_deactivate_scripts(
args, conda_yaml_dict, environment, env_directory
)
return 0
if __name__ == "__main__":
sys.exit(main())