Completed
Push — master ( 889440...e965d2 )
by Dieter
01:16
created

load_build_matrix_env_vars()   C

Complexity

Conditions 8

Size

Total Lines 65

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 8
c 2
b 0
f 0
dl 0
loc 65
rs 5.4814

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
# vim: set expandtab sw=4 ts=4:
2
"""
3
Travis CI environment variable loading functions.
4
5
Copyright (C) 2014-2016 Dieter Adriaenssens <[email protected]>
6
7
This file is part of buildtimetrend/python-lib
8
<https://github.com/buildtimetrend/python-lib/>
9
10
This program is free software: you can redistribute it and/or modify
11
it under the terms of the GNU Affero General Public License as published by
12
the Free Software Foundation, either version 3 of the License, or
13
any later version.
14
15
This program is distributed in the hope that it will be useful,
16
but WITHOUT ANY WARRANTY; without even the implied warranty of
17
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18
GNU Affero General Public License for more details.
19
20
You should have received a copy of the GNU Affero General Public License
21
along with this program. If not, see <http://www.gnu.org/licenses/>.
22
"""
23
from builtins import str
24
import os
25
from buildtimetrend.settings import Settings
26
from buildtimetrend.collection import Collection
27
from buildtimetrend.travis.tools import convert_build_result
28
29
30
def load_all(settings=None):
31
    """
32
    Load all Travis CI environment variables.
33
34
    Load Travis CI environment variables and assign their values to
35
    the corresponding setting value :
36
    - general
37
    - build matrix
38
    - pull request
39
    """
40
    if not isinstance(settings, Settings):
41
        settings = Settings()
42
43
    load_general_env_vars(settings)
44
    load_build_matrix_env_vars(settings)
45
    load_travis_pr_env_vars(settings)
46
47
48
def load_general_env_vars(settings):
49
    """
50
    Load Travis CI environment variables.
51
52
    Load Travis CI environment variables and assign their values to
53
    the corresponding setting value.
54
    """
55
    if "TRAVIS" in os.environ and os.environ["TRAVIS"] == "true":
56
        # set ci_platform setting to "travis"
57
        settings.add_setting("ci_platform", "travis")
58
59
        # assign TRAVIS environment variable values to setting value
60
        settings.env_var_to_settings("TRAVIS_BUILD_NUMBER", "build")
61
        settings.env_var_to_settings("TRAVIS_JOB_NUMBER", "job")
62
        settings.env_var_to_settings("TRAVIS_BRANCH", "branch")
63
        settings.env_var_to_settings("TRAVIS_REPO_SLUG", "project_name")
64
65
        # convert and set Travis build result
66
        if "TRAVIS_TEST_RESULT" in os.environ:
67
            # map $TRAVIS_TEST_RESULT to a more readable value
68
            settings.add_setting(
69
                "result",
70
                convert_build_result(os.environ["TRAVIS_TEST_RESULT"])
71
            )
72
73
74
def load_build_matrix_env_vars(settings):
75
    """
76
    Retrieve build matrix data from environment variables.
77
78
    Load Travis CI build matrix environment variables
79
    and assign their values to the corresponding setting value.
80
81
    Properties :
82
    - language
83
    - language version (if applicable)
84
    - compiler (if applicable)
85
    - operating system
86
    - environment parameters
87
88
    Parameters:
89
    - settings: Settings instance
90
    """
91
    if "TRAVIS" in os.environ and os.environ["TRAVIS"] == "true":
92
        build_matrix = Collection()
93
94
        if "TRAVIS_OS_NAME" in os.environ:
95
            build_matrix.add_item("os", os.environ["TRAVIS_OS_NAME"])
96
97
        # set language and language version
98
        language_env_vars = {
99
            'TRAVIS_DART_VERSION': 'dart',
100
            'TRAVIS_GO_VERSION': 'go',
101
            'TRAVIS_HAXE_VERSION': 'haxe',
102
            'TRAVIS_JDK_VERSION': 'java',
103
            'TRAVIS_JULIA_VERSION': 'julia',
104
            'TRAVIS_NODE_VERSION': 'javascript',
105
            'TRAVIS_OTP_RELEASE': 'erlang',
106
            'TRAVIS_PERL_VERSION': 'perl',
107
            'TRAVIS_PHP_VERSION': 'php',
108
            'TRAVIS_PYTHON_VERSION': 'python',
109
            'TRAVIS_R_VERSION': 'r',
110
            'TRAVIS_RUBY_VERSION': 'ruby',
111
            'TRAVIS_RUST_VERSION': 'rust',
112
            'TRAVIS_SCALA_VERSION': 'scala'
113
        }
114
        for env_var, language in language_env_vars.items():
115
            if env_var in os.environ:
116
                build_matrix.add_item("language", language)
117
                build_matrix.add_item(
118
                    "language_version",
119
                    str(os.environ[env_var])
120
                )
121
122
        # language specific build matrix parameters
123
        parameters = {
124
            'TRAVIS_XCODE_SDK': 'xcode_sdk',  # Objective-C
125
            'TRAVIS_XCODE_SCHEME': 'xcode_scheme',  # Objective-C
126
            'TRAVIS_XCODE_PROJECT': 'xcode_project',  # Objective-C
127
            'TRAVIS_XCODE_WORKSPACE': 'xcode_workspace',  # Objective-C
128
            'CC': 'compiler',  # C, C++
129
            'ENV': 'parameters'
130
        }
131
132
        for parameter, name in parameters.items():
133
            if parameter in os.environ:
134
                build_matrix.add_item(name, str(os.environ[parameter]))
135
136
        settings.add_setting(
137
            "build_matrix",
138
            build_matrix.get_items_with_summary()
139
        )
140
141
142
def load_travis_pr_env_vars(settings):
143
    """
144
    Load Travis CI pull request environment variables.
145
146
    Load Travis CI pull request environment variables
147
    and assign their values to the corresponding setting value.
148
    """
149
    if "TRAVIS" in os.environ and os.environ["TRAVIS"] == "true" and \
150
            "TRAVIS_PULL_REQUEST" in os.environ and \
151
            not os.environ["TRAVIS_PULL_REQUEST"] == "false":
152
        settings.add_setting("build_trigger", "pull_request")
153
        settings.add_setting(
154
            "pull_request",
155
            {
156
                'is_pull_request': True,
157
                'title': "unknown",
158
                'number': os.environ["TRAVIS_PULL_REQUEST"]
159
            }
160
        )
161
    else:
162
        settings.add_setting("build_trigger", "push")
163
        settings.add_setting(
164
            "pull_request",
165
            {
166
                'is_pull_request': False,
167
                'title': None,
168
                'number': None
169
            }
170
        )
171