Passed
Pull Request — master (#3198)
by Alexander
03:27 queued 01:20
created

ssg.yaml   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 125
Duplicated Lines 0 %

Test Coverage

Coverage 43.08%

Importance

Changes 0
Metric Value
eloc 72
dl 0
loc 125
rs 10
c 0
b 0
f 0
ccs 28
cts 65
cp 0.4308
wmc 20

8 Functions

Rating   Name   Duplication   Size   Complexity  
A open_and_macro_expand() 0 19 3
A _bool_constructor() 0 2 1
A open_environment() 0 5 1
A open_and_expand() 0 13 2
A _save_rename() 0 2 1
A _get_implied_properties() 0 14 5
A open_raw() 0 10 2
A _open_yaml() 0 23 5
1 1
from __future__ import absolute_import
0 ignored issues
show
Coding Style introduced by
This module should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
2 1
from __future__ import print_function
3
4 1
import codecs
5 1
import yaml
6 1
import sys
0 ignored issues
show
introduced by
standard import "import sys" should be placed before "import yaml"
Loading history...
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={}):
0 ignored issues
show
Bug Best Practice introduced by
The default value {} might cause unintended side-effects.

Objects as default values are only created once in Python and not on each invocation of the function. If the default object is modified, this modification is carried over to the next invocation of the method.

# Bad:
# If array_param is modified inside the function, the next invocation will
# receive the modified object.
def some_function(array_param=[]):
    # ...

# Better: Create an array on each invocation
def some_function(array_param=None):
    array_param = array_param or []
    # ...
Loading history...
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:
0 ignored issues
show
Coding Style Naming introduced by
The name e does not conform to the variable naming conventions ((([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
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):
0 ignored issues
show
Coding Style introduced by
This function should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
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