Passed
Push — master ( f9ed9c...2417ae )
by Matěj
02:01
created

ssg.products.map_name()   A

Complexity

Conditions 5

Size

Total Lines 21
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 5

Importance

Changes 0
Metric Value
cc 5
eloc 13
nop 1
dl 0
loc 21
rs 9.2833
c 0
b 0
f 0
ccs 10
cts 10
cp 1
crap 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 re
5 1
import os
6 1
from collections import namedtuple
7
8 1
from .constants import product_directories
9 1
from .yaml import open_raw
10
11
12
# SSG Makefile to official product name mapping
13 1
_version_name_map = {
0 ignored issues
show
Coding Style Naming introduced by
The name _version_name_map does not conform to the constant naming conventions ((([A-Z_][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...
14
    'chromium': 'Google Chromium Browser',
15
    'fedora': 'Fedora',
16
    'firefox': 'Mozilla Firefox',
17
    'jre': 'Java Runtime Environment',
18
    'rhel-osp': 'Red Hat OpenStack Platform',
19
    'rhel': 'Red Hat Enterprise Linux',
20
    'debian': 'Debian',
21
    'ubuntu': 'Ubuntu',
22
    'eap': 'JBoss Enterprise Application Platform',
23
    'fuse': 'JBoss Fuse',
24
    'opensuse': 'openSUSE',
25
    'sle': 'SUSE Linux Enterprise',
26
    'wrlinux': 'Wind River Linux',
27
    'ol': 'Oracle Linux',
28
    'ocp': 'Red Hat OpenShift Container Platform',
29
}
30
31 1
multi_list = ["rhel", "fedora", "rhel-osp", "debian", "ubuntu",
0 ignored issues
show
Coding Style Naming introduced by
The name multi_list does not conform to the constant naming conventions ((([A-Z_][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...
32
              "wrlinux", "opensuse", "sle", "ol", "ocp"]
33
34 1
PRODUCT_NAME_PARSER = re.compile(r"([a-zA-Z\-]+)([0-9]+)")
35
36
37 1
def parse_name(product):
38
    """
39
    Returns a namedtuple of (name, version) from parsing a given product;
40
    e.g., "rhel7" -> ("rhel", "7")
41
    """
42
43 1
    prod_tuple = namedtuple('product', ['name', 'version'])
44
45 1
    _product = product
46 1
    _product_version = None
47 1
    match = PRODUCT_NAME_PARSER.match(product)
48
49 1
    if match:
50 1
        _product = match.group(1)
51 1
        _product_version = match.group(2)
52
53 1
    return prod_tuple(_product, _product_version)
54
55
56 1
def get_all(ssg_root):
57
    """
58
    Analyzes all products in the SSG root and sorts them into two categories:
59
    those which use linux_os and those which use their own directory. Returns
60
    a namedtuple of sets, (linux, other).
61
    """
62
63 1
    linux_products = set()
64 1
    other_products = set()
65
66 1
    for product in product_directories:
67 1
        product_dir = os.path.join(ssg_root, product)
68 1
        product_yaml_path = os.path.join(product_dir, "product.yml")
69 1
        product_yaml = open_raw(product_yaml_path)
70
71 1
        guide_dir = os.path.join(product_dir, product_yaml['benchmark_root'])
72 1
        guide_dir = os.path.abspath(guide_dir)
73
74 1
        if 'linux_os' in guide_dir:
75 1
            linux_products.add(product)
76
        else:
77 1
            other_products.add(product)
78
79 1
    products = namedtuple('products', ['linux', 'other'])
80 1
    return products(linux_products, other_products)
81
82
83 1
def map_name(version):
84
    """Maps SSG Makefile internal product name to official product name"""
85
86 1
    if version.startswith("multi_platform_"):
87 1
        trimmed_version = version[len("multi_platform_"):]
88 1
        if trimmed_version not in multi_list:
89 1
            raise RuntimeError(
90
                "%s is an invalid product version. If it's multi_platform the "
91
                "suffix has to be from (%s)."
92
                % (version, ", ".join(multi_list))
93
            )
94 1
        return map_name(trimmed_version)
95
96
    # By sorting in reversed order, keys which are a longer version of other keys are
97
    # visited first (e.g., rhel-osp vs. rhel)
98 1
    for key in sorted(_version_name_map, reverse=True):
99 1
        if version.startswith(key):
100 1
            return _version_name_map[key]
101
102 1
    raise RuntimeError("Can't map version '%s' to any known product!"
103
                       % (version))
104