Passed
Branch master (2b673d)
by Matěj
03:01
created

ssg.products.get_all()   A

Complexity

Conditions 3

Size

Total Lines 25
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 14
nop 1
dl 0
loc 25
rs 9.7
c 0
b 0
f 0
ccs 14
cts 14
cp 1
crap 3
1 2
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 2
from __future__ import print_function
3
4 2
import re
5 2
import os
6 2
from collections import namedtuple
7
8 2
from .constants import product_directories
9 2
from .yaml import open_raw
10
11
12
# SSG Makefile to official product name mapping
13 2
_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
    'example': 'Example Linux Content',
28
    'ol': 'Oracle Linux',
29
    'ocp': 'Red Hat OpenShift Container Platform',
30
}
31
32 2
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...
33
              "wrlinux", "opensuse", "sle", "ol", "ocp", "example"]
34
35 2
PRODUCT_NAME_PARSER = re.compile(r"([a-zA-Z\-]+)([0-9]+)")
36
37
38 2
def parse_name(product):
39
    """
40
    Returns a namedtuple of (name, version) from parsing a given product;
41
    e.g., "rhel7" -> ("rhel", "7")
42
    """
43
44 2
    prod_tuple = namedtuple('product', ['name', 'version'])
45
46 2
    _product = product
47 2
    _product_version = None
48 2
    match = PRODUCT_NAME_PARSER.match(product)
49
50 2
    if match:
51 2
        _product = match.group(1)
52 2
        _product_version = match.group(2)
53
54 2
    return prod_tuple(_product, _product_version)
55
56
57 2
def get_all(ssg_root):
58
    """
59
    Analyzes all products in the SSG root and sorts them into two categories:
60
    those which use linux_os and those which use their own directory. Returns
61
    a namedtuple of sets, (linux, other).
62
    """
63
64 2
    linux_products = set()
65 2
    other_products = set()
66
67 2
    for product in product_directories:
68 2
        product_dir = os.path.join(ssg_root, product)
69 2
        product_yaml_path = os.path.join(product_dir, "product.yml")
70 2
        product_yaml = open_raw(product_yaml_path)
71
72 2
        guide_dir = os.path.join(product_dir, product_yaml['benchmark_root'])
73 2
        guide_dir = os.path.abspath(guide_dir)
74
75 2
        if 'linux_os' in guide_dir:
76 2
            linux_products.add(product)
77
        else:
78 2
            other_products.add(product)
79
80 2
    products = namedtuple('products', ['linux', 'other'])
81 2
    return products(linux_products, other_products)
82
83
84 2
def map_name(version):
85
    """Maps SSG Makefile internal product name to official product name"""
86
87 2
    if version.startswith("multi_platform_"):
88 2
        trimmed_version = version[len("multi_platform_"):]
89 2
        if trimmed_version not in multi_list:
90 2
            raise RuntimeError(
91
                "%s is an invalid product version. If it's multi_platform the "
92
                "suffix has to be from (%s)."
93
                % (version, ", ".join(multi_list))
94
            )
95 2
        return map_name(trimmed_version)
96
97
    # By sorting in reversed order, keys which are a longer version of other keys are
98
    # visited first (e.g., rhel-osp vs. rhel)
99 2
    for key in sorted(_version_name_map, reverse=True):
100 2
        if version.startswith(key):
101 2
            return _version_name_map[key]
102
103 2
    raise RuntimeError("Can't map version '%s' to any known product!"
104
                       % (version))
105