1
|
1 |
|
from __future__ import absolute_import |
|
|
|
|
2
|
1 |
|
from __future__ import print_function |
3
|
|
|
|
4
|
1 |
|
import codecs |
5
|
1 |
|
import yaml |
6
|
1 |
|
import sys |
|
|
|
|
7
|
|
|
|
8
|
1 |
|
from .jinja import extract_substitutions_dict_from_template, process_file |
9
|
1 |
|
from .constants import (PKG_MANAGER_TO_SYSTEM, |
10
|
|
|
JINJA_MACROS_BASE_DEFINITIONS, JINJA_MACROS_HIGHLEVEL_DEFINITIONS) |
11
|
1 |
|
from .constants import DEFAULT_UID_MIN |
12
|
|
|
|
13
|
1 |
|
try: |
14
|
1 |
|
from yaml import CSafeLoader as yaml_SafeLoader |
15
|
1 |
|
except ImportError: |
16
|
1 |
|
from yaml import SafeLoader as yaml_SafeLoader |
17
|
|
|
|
18
|
|
|
|
19
|
1 |
|
def _bool_constructor(self, node): |
20
|
|
|
return self.construct_scalar(node) |
21
|
|
|
|
22
|
|
|
|
23
|
|
|
# Don't follow python bool case |
24
|
1 |
|
yaml_SafeLoader.add_constructor(u'tag:yaml.org,2002:bool', _bool_constructor) |
25
|
|
|
|
26
|
|
|
|
27
|
1 |
|
def _save_rename(result, stem, prefix): |
28
|
|
|
result["{0}_{1}".format(prefix, stem)] = stem |
29
|
|
|
|
30
|
|
|
|
31
|
1 |
|
def _open_yaml(stream, original_file=None, substitutions_dict={}): |
|
|
|
|
32
|
|
|
""" |
33
|
|
|
Open given file-like object and parse it as YAML. |
34
|
|
|
|
35
|
|
|
Optionally, pass the path to the original_file for better error handling |
36
|
|
|
when the file contents are passed. |
37
|
|
|
|
38
|
|
|
Return None if it contains "documentation_complete" key set to "false". |
39
|
|
|
""" |
40
|
1 |
|
try: |
41
|
1 |
|
yaml_contents = yaml.load(stream, Loader=yaml_SafeLoader) |
42
|
|
|
|
43
|
1 |
|
if yaml_contents.pop("documentation_complete", "true") == "false" and \ |
44
|
|
|
substitutions_dict.get("cmake_build_type") != "Debug": |
45
|
|
|
return None |
46
|
|
|
|
47
|
1 |
|
return yaml_contents |
48
|
|
|
except Exception as e: |
|
|
|
|
49
|
|
|
_file = original_file |
50
|
|
|
if not _file: |
51
|
|
|
_file = stream |
52
|
|
|
print("Exception while handling file: %s" % _file, file=sys.stderr) |
53
|
|
|
raise e |
54
|
|
|
|
55
|
|
|
|
56
|
1 |
|
def _get_implied_properties(existing_properties): |
57
|
|
|
result = dict() |
58
|
|
|
if ("pkg_manager" in existing_properties and |
59
|
|
|
"pkg_system" not in existing_properties): |
60
|
|
|
pkg_manager = existing_properties["pkg_manager"] |
61
|
|
|
result["pkg_system"] = PKG_MANAGER_TO_SYSTEM[pkg_manager] |
62
|
|
|
|
63
|
|
|
if "uid_min" not in existing_properties: |
64
|
|
|
result["uid_min"] = DEFAULT_UID_MIN |
65
|
|
|
|
66
|
|
|
if "auid" not in existing_properties: |
67
|
|
|
result["auid"] = existing_properties.get("uid_min", DEFAULT_UID_MIN) |
68
|
|
|
|
69
|
|
|
return result |
70
|
|
|
|
71
|
|
|
|
72
|
1 |
|
def open_and_expand(yaml_file, substitutions_dict=None): |
73
|
|
|
""" |
74
|
|
|
Process the file as a template, using substitutions_dict to perform |
75
|
|
|
expansion. Then, process the expansion result as a YAML content. |
76
|
|
|
|
77
|
|
|
See also: _open_yaml |
78
|
|
|
""" |
79
|
|
|
if substitutions_dict is None: |
80
|
|
|
substitutions_dict = dict() |
81
|
|
|
|
82
|
|
|
expanded_template = process_file(yaml_file, substitutions_dict) |
83
|
|
|
yaml_contents = _open_yaml(expanded_template, yaml_file, substitutions_dict) |
84
|
|
|
return yaml_contents |
85
|
|
|
|
86
|
|
|
|
87
|
1 |
|
def open_and_macro_expand(yaml_file, substitutions_dict=None): |
88
|
|
|
""" |
89
|
|
|
Do the same as open_and_expand, but load definitions of macros |
90
|
|
|
so they can be expanded in the template. |
91
|
|
|
""" |
92
|
|
|
if substitutions_dict is None: |
93
|
|
|
substitutions_dict = dict() |
94
|
|
|
|
95
|
|
|
try: |
96
|
|
|
macro_definitions = extract_substitutions_dict_from_template( |
97
|
|
|
JINJA_MACROS_BASE_DEFINITIONS, substitutions_dict) |
98
|
|
|
macro_definitions.update(extract_substitutions_dict_from_template( |
99
|
|
|
JINJA_MACROS_HIGHLEVEL_DEFINITIONS, substitutions_dict)) |
100
|
|
|
except Exception as exc: |
101
|
|
|
msg = ("Error extracting macro definitions: {0}" |
102
|
|
|
.format(str(exc))) |
103
|
|
|
raise RuntimeError(msg) |
104
|
|
|
substitutions_dict.update(macro_definitions) |
105
|
|
|
return open_and_expand(yaml_file, substitutions_dict) |
106
|
|
|
|
107
|
|
|
|
108
|
1 |
|
def open_raw(yaml_file): |
109
|
|
|
""" |
110
|
|
|
Open given file-like object and parse it as YAML |
111
|
|
|
without performing any kind of template processing |
112
|
|
|
|
113
|
|
|
See also: _open_yaml |
114
|
|
|
""" |
115
|
1 |
|
with codecs.open(yaml_file, "r", "utf8") as stream: |
116
|
1 |
|
yaml_contents = _open_yaml(stream, original_file=yaml_file) |
117
|
1 |
|
return yaml_contents |
118
|
|
|
|
119
|
|
|
|
120
|
1 |
|
def open_environment(build_config_yaml, product_yaml): |
|
|
|
|
121
|
|
|
contents = open_raw(build_config_yaml) |
122
|
|
|
contents.update(open_raw(product_yaml)) |
123
|
|
|
contents.update(_get_implied_properties(contents)) |
124
|
|
|
return contents |
125
|
|
|
|
The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:
If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.