/
utils.py
208 lines (159 loc) · 6.17 KB
/
utils.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
# -*- coding: utf-8 -*-
"""Contain utils required by cenv-tool."""
import os
from pathlib import Path
from subprocess import CalledProcessError
from subprocess import check_output
from subprocess import STDOUT
from typing import List
from typing import NoReturn
import jinja2
import six
import yaml
from marshmallow import ValidationError
from cenv_tool.schemata import SMetaYaml
CYAN = '\033[1;36m'
GREEN = '\033[1;32m'
RED = '\033[1;91m'
NCOLOR = '\033[0m'
BOLD = '\033[1;37m'
class CenvProcessError(Exception):
"""Represent a process error during cenv execution."""
def message(
*, text: str, color: str, special: str = None, indent: int = 1
) -> NoReturn:
"""Print passed ``text`` in the passed ``color`` on terminal.
Parameters:
text: the text to print colored on terminal.
color: the color of the text to print.
special: special kind of message to print. Available are ``'row'`` and
``'end'``.
indent: the indent to use for the text.
"""
color_mapping = {
'red': RED,
'green': GREEN,
'cyan': CYAN,
'bold': BOLD,
}
if indent == 1:
indent_prefix = ' ' * indent
else:
indent_prefix = ' ' + '│ ' * (indent - 1)
special_mapping = {
'row': f'{indent_prefix}├── ',
'end': f'{indent_prefix}└── ',
}
if special:
prefix = special_mapping[special]
else:
prefix = ''
print(f'{prefix}{color_mapping[color]}{text}{NCOLOR}')
def run_in_bash(cmd: str) -> str:
"""Run passed ``cmd`` inside bash using :func:`subprocess.check_output`.
Parameters:
cmd: the command to execute.
Returns:
the output of the ran command.
"""
try:
result = check_output([cmd], shell=True, stderr=STDOUT)
except CalledProcessError as err:
error_message = err.output.decode('utf8').split('\n')
message(text='the following error occured:', color='red')
for line in error_message:
message(text=line, color='bold')
raise CenvProcessError(str(err.output))
return result.strip().decode('ascii')
class _NullUndefined(jinja2.Undefined):
"""Handle jinja2-variables with undefined content of ``meta.yaml.``"""
def __unicode__(self):
"""Replace unicode dunder of this class."""
return six.text_type(self._undefined_name)
def __getattr__(self, attribute_name: str):
"""Replace getattr dunder of this class."""
return six.text_type(f'{self}.{attribute_name}')
def __getitem__(self, attribute_name: str):
"""Replace getitem dunder of this class."""
return f'{self}["{attribute_name}"]'
class _StrDict(dict):
"""Handle dictionaries for jinja2-variables of ``meta.yaml``."""
def __getitem__(self, key: str, default: str = '') -> str:
"""Replace getitem dunder of this class."""
return self[key] if key in self else default
def extract_dependencies_from_meta_yaml(meta_yaml_content: dict) -> List[str]:
"""
Extract the dependencies defined in the requirements-run-section.
If additional dev-requirements are defined in the
extra-dev_requirements-section, these dependencies are added to the other
dependencies.
Parameters:
meta_yaml_content: the content from a ``meta.yaml`` as a dict.
Returns:
the collected dependencies.
"""
# extract the dependencies defined the the requirements-run-section
dependencies = meta_yaml_content['requirements']['run']
# remove the python version definition. The version will be extracted
# from the extra-python section
dependencies = list(
filter(
lambda x: x.split(' ')[0] != 'python',
dependencies
)
)
if meta_yaml_content['requirements'].get('run_constrained'):
dependencies.extend(
meta_yaml_content['requirements']['run_constrained']
)
if meta_yaml_content['extra'].get('dev_requirements'):
dependencies.extend(meta_yaml_content['extra']['dev_requirements'])
# append the python version to use in the conda environment
dependencies.append(f'python {meta_yaml_content["extra"]["python"]}')
return dependencies
def read_meta_yaml(path: Path) -> dict:
"""Read the meta.yaml file.
The file is read from relative path ``conda-build/meta.yaml`` inside
the current path, validate the ``meta.yaml`` using the marshmallow-schema,
:class:`SMetaYaml`, extract the project-settings.
Parameters:
path: the current working directory.
Returns:
the ``meta.yaml`` content as a dict.
"""
# load the meta.yaml-content
myaml_content = (path / 'conda-build/meta.yaml').open().read()
jinja2_env = jinja2.Environment(undefined=_NullUndefined)
jinja2_loaded_myaml = jinja2_env.from_string(myaml_content)
render_kwargs = {
'os': os,
'environ': _StrDict(),
'load_setup_py_data': _StrDict,
}
rendered_myaml = jinja2_loaded_myaml.render(**render_kwargs)
loaded_myaml = yaml.safe_load(rendered_myaml)
# validate the content of loaded meta.yaml
try:
dumped = SMetaYaml(strict=True).dumps(loaded_myaml).data
meta_yaml_content = SMetaYaml(strict=True).loads(dumped).data
except ValidationError as err:
message(text='meta.yaml file is not valid!', color='red')
message(text=f'ValidationError in {err.args[0]}', color='red')
raise
# return combined collected project-settings
return meta_yaml_content
def read_config():
"""Read the config file for cenv from the users-home path if it exists.
If there is no user-config-file the default one is used.
Returns:
the content of the read config file.
"""
user_config_path = Path.home() / '.config/cenv/cenv.yml'
default_config_path = Path(__file__).parent / 'cenv.yml'
# Collect settings from config file .cenv.yml
main_config = yaml.safe_load(default_config_path.open().read())
# if a user-config-file exists, read the content and update the main-config
if user_config_path.exists():
user_config = yaml.safe_load(user_config_path.open().read())
main_config.update(user_config)
return main_config