Completed
Push — master ( 219e97...2114cf )
by Dieter
01:21
created

buildtimetrend.travis.load_build_matrix_env_vars()   C

Complexity

Conditions 8

Size

Total Lines 65

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 8
dl 0
loc 65
rs 5.4815

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-2015 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
from builtins import object
0 ignored issues
show
Unused Code introduced by
Unused object imported from builtins
Loading history...
25
import os
26
from buildtimetrend.settings import Settings
27
from buildtimetrend.collection import Collection
28
from buildtimetrend.travis.tools import convert_build_result
29
30
31
def load_all(settings=None):
32
    """
33
    Load all Travis CI environment variables.
34
35
    Load Travis CI environment variables and assign their values to
36
    the corresponding setting value :
37
    - general
38
    - build matrix
39
    - pull request
40
    """
41
    if not isinstance(settings, Settings):
42
        settings = Settings()
43
44
    load_general_env_vars(settings)
45
    load_build_matrix_env_vars(settings)
46
    load_travis_pr_env_vars(settings)
47
48
49
def load_general_env_vars(settings):
50
    """
51
    Load Travis CI environment variables.
52
53
    Load Travis CI environment variables and assign their values to
54
    the corresponding setting value.
55
    """
56
    if "TRAVIS" in os.environ and os.environ["TRAVIS"] == "true":
57
        # set ci_platform setting to "travis"
58
        settings.add_setting("ci_platform", "travis")
59
60
        # assign TRAVIS environment variable values to setting value
61
        settings.env_var_to_settings("TRAVIS_BUILD_NUMBER", "build")
62
        settings.env_var_to_settings("TRAVIS_JOB_NUMBER", "job")
63
        settings.env_var_to_settings("TRAVIS_BRANCH", "branch")
64
        settings.env_var_to_settings("TRAVIS_REPO_SLUG", "project_name")
65
66
        # convert and set Travis build result
67
        if "TRAVIS_TEST_RESULT" in os.environ:
68
            # map $TRAVIS_TEST_RESULT to a more readable value
69
            settings.add_setting(
70
                "result",
71
                convert_build_result(os.environ["TRAVIS_TEST_RESULT"])
72
            )
73
74
75
def load_build_matrix_env_vars(settings):
76
    """
77
    Retrieve build matrix data from environment variables.
78
79
    Load Travis CI build matrix environment variables
80
    and assign their values to the corresponding setting value.
81
82
    Properties :
83
    - language
84
    - language version (if applicable)
85
    - compiler (if applicable)
86
    - operating system
87
    - environment parameters
88
89
    Parameters:
90
    - settings: Settings instance
91
    """
92
    if "TRAVIS" in os.environ and os.environ["TRAVIS"] == "true":
93
        build_matrix = Collection()
94
95
        if "TRAVIS_OS_NAME" in os.environ:
96
            build_matrix.add_item("os", os.environ["TRAVIS_OS_NAME"])
97
98
        # set language and language version
99
        language_env_vars = {
100
            'TRAVIS_DART_VERSION': 'dart',
101
            'TRAVIS_GO_VERSION': 'go',
102
            'TRAVIS_HAXE_VERSION': 'haxe',
103
            'TRAVIS_JDK_VERSION': 'java',
104
            'TRAVIS_JULIA_VERSION': 'julia',
105
            'TRAVIS_NODE_VERSION': 'javascript',
106
            'TRAVIS_OTP_RELEASE': 'erlang',
107
            'TRAVIS_PERL_VERSION': 'perl',
108
            'TRAVIS_PHP_VERSION': 'php',
109
            'TRAVIS_PYTHON_VERSION': 'python',
110
            'TRAVIS_R_VERSION': 'r',
111
            'TRAVIS_RUBY_VERSION': 'ruby',
112
            'TRAVIS_RUST_VERSION': 'rust',
113
            'TRAVIS_SCALA_VERSION': 'scala'
114
        }
115
        for env_var, language in language_env_vars.items():
116
            if env_var in os.environ:
117
                build_matrix.add_item("language", language)
118
                build_matrix.add_item(
119
                    "language_version",
120
                    str(os.environ[env_var])
121
                )
122
123
        # language specific build matrix parameters
124
        parameters = {
125
            'TRAVIS_XCODE_SDK': 'xcode_sdk',  # Objective-C
126
            'TRAVIS_XCODE_SCHEME': 'xcode_scheme',  # Objective-C
127
            'TRAVIS_XCODE_PROJECT': 'xcode_project',  # Objective-C
128
            'TRAVIS_XCODE_WORKSPACE': 'xcode_workspace',  # Objective-C
129
            'CC': 'compiler',  # C, C++
130
            'ENV': 'parameters'
131
        }
132
133
        for parameter, name in parameters.items():
134
            if parameter in os.environ:
135
                build_matrix.add_item(name, str(os.environ[parameter]))
136
137
        settings.add_setting(
138
            "build_matrix",
139
            build_matrix.get_items_with_summary()
140
        )
141
142
143
def load_travis_pr_env_vars(settings):
144
    """
145
    Load Travis CI pull request environment variables.
146
147
    Load Travis CI pull request environment variables
148
    and assign their values to the corresponding setting value.
149
    """
150
    if "TRAVIS" in os.environ and os.environ["TRAVIS"] == "true" and \
151
            "TRAVIS_PULL_REQUEST" in os.environ and \
152
            not os.environ["TRAVIS_PULL_REQUEST"] == "false":
153
        settings.add_setting("build_trigger", "pull_request")
154
        settings.add_setting(
155
            "pull_request",
156
            {
157
                'is_pull_request': True,
158
                'title': "unknown",
159
                'number': os.environ["TRAVIS_PULL_REQUEST"]
160
            }
161
        )
162
    else:
163
        settings.add_setting("build_trigger", "push")
164
        settings.add_setting(
165
            "pull_request",
166
            {
167
                'is_pull_request': False,
168
                'title': None,
169
                'number': None
170
            }
171
        )
172