utils.py 8.02 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# Copyright (C) 2013 GRNET S.A. All rights reserved.
#
# Redistribution and use in source and binary forms, with or
# without modification, are permitted provided that the following
# conditions are met:
#
#   1. Redistributions of source code must retain the above
#      copyright notice, this list of conditions and the following
#      disclaimer.
#
#   2. Redistributions in binary form must reproduce the above
#      copyright notice, this list of conditions and the following
#      disclaimer in the documentation and/or other materials
#      provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
# The views and conclusions contained in the software and
# documentation are those of the authors and should not be
# interpreted as representing official policies, either expressed
# or implied, of GRNET S.A.

import os
import git
36
import sh
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
37
import re
38
from collections import namedtuple
39
40
from configobj import ConfigObj

41
from devflow import BRANCH_TYPES
42
43


44
def get_repository(path=None):
45
    """Load the repository from the current working dir."""
46
47
    if path is None:
        path = os.getcwd()
48
    try:
49
        return git.Repo(path)
50
    except git.InvalidGitRepositoryError:
Nikos Skalkotos's avatar
Nikos Skalkotos committed
51
        msg = "Cound not retrieve git information. Directory '%s'"\
52
              " is not a git repository!" % path
53
54
55
        raise RuntimeError(msg)


56
57
58
59
60
61
def get_config(path=None):
    """Load configuration file."""
    if path is None:
        toplevel = get_vcs_info().toplevel
        path = os.path.join(toplevel, "devflow.conf")

62
    if not os.path.isfile(path):
Nikos Skalkotos's avatar
Nikos Skalkotos committed
63
        raise RuntimeError("Config file: '%s' does not exist!" % path)
64

65
66
67
68
    config = ConfigObj(path)
    return config


69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
def get_vcs_info():
    """Return current git HEAD commit information.

    Returns a tuple containing
        - branch name
        - commit id
        - commit count
        - git describe output
        - path of git toplevel directory

    """

    repo = get_repository()
    branch = repo.head.reference
    revid = get_commit_id(branch.commit, branch)
    revno = len(list(repo.iter_commits()))
    toplevel = repo.working_dir
86
    config = repo.config_reader()
87
88
89
90
91
92
    try:
        name = config.get_value("user", "name")
        email = config.get_value("user", "email")
    except Exception as e:
        raise ValueError("Can not read name/email from .gitconfig"
                         " file.: %s" % e)
93
94

    info = namedtuple("vcs_info", ["branch", "revid", "revno",
95
                                   "toplevel", "name", "email"])
96
97

    return info(branch=branch.name, revid=revid, revno=revno,
98
                toplevel=toplevel, name=name, email=email)
99
100
101
102
103
104


def get_commit_id(commit, current_branch):
    """Return the commit ID

    If the commit is a 'merge' commit, and one of the parents is a
Nikos Skalkotos's avatar
Nikos Skalkotos committed
105
    debian branch we return a combination of the parents commits.
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122

    """
    def short_id(commit):
        return commit.hexsha[0:7]

    parents = commit.parents
    cur_br_name = current_branch.name
    if len(parents) == 1:
        return short_id(commit)
    elif len(parents) == 2:
        if cur_br_name.startswith("debian-") or cur_br_name == "debian":
            pr1, pr2 = parents
            return short_id(pr1) + "_" + short_id(pr2)
        else:
            return short_id(commit)
    else:
        raise RuntimeError("Commit %s has more than 2 parents!" % commit)
123
124
125
126


def get_debian_branch(branch):
    """Find the corresponding debian- branch"""
127
128
    distribution = get_distribution_codename()
    repo = get_repository()
129
    if branch == "master":
130
131
132
        deb_branch = "debian-" + distribution
    else:
        deb_branch = "-".join(["debian", branch, distribution])
133
    # Check if debian-branch exists (local or origin)
134
135
136
    if _get_branch(deb_branch):
        return deb_branch
    # Check without distribution
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
137
    deb_branch = re.sub("-" + distribution + "$", "", deb_branch)
138
    if _get_branch(deb_branch):
139
140
        return deb_branch
    branch_type = BRANCH_TYPES[get_branch_type(branch)]
141
142
143
144
145
146
147
    # If not try the default debian branch with distribution
    default_branch = branch_type.debian_branch + "-" + distribution
    if _get_branch(default_branch):
        repo.git.branch(deb_branch, default_branch)
        print "Created branch '%s' from '%s'" % (deb_branch, default_branch)
        return deb_branch
    # And without distribution
148
149
150
151
    default_branch = branch_type.debian_branch
    if _get_branch(default_branch):
        repo.git.branch(deb_branch, default_branch)
        print "Created branch '%s' from '%s'" % (deb_branch, default_branch)
152
        return deb_branch
153
154
155
    # If not try the debian branch
    repo.git.branch(deb_branch, default_branch)
    print "Created branch '%s' from 'debian'" % deb_branch
156
157
158
159
160
161
162
163
164
    return "debian"


def _get_branch(branch):
    repo = get_repository()
    if branch in repo.branches:
        return branch
    origin_branch = "origin/" + branch
    if origin_branch in repo.refs:
Christos Stavrakakis's avatar
Christos Stavrakakis committed
165
        print "Creating branch '%s' to track '%s'" % (branch, origin_branch)
166
167
168
169
        repo.git.branch(branch, origin_branch)
        return branch
    else:
        return None
170
171
172
173
174
175
176


def get_build_mode():
    """Determine the build mode"""
    # Get it from environment if exists
    mode = os.environ.get("DEVFLOW_BUILD_MODE", None)
    if mode is None:
Christos Stavrakakis's avatar
Fix bug    
Christos Stavrakakis committed
177
        branch = get_branch_type(get_vcs_info().branch)
178
        try:
Christos Stavrakakis's avatar
Fix bug    
Christos Stavrakakis committed
179
            br_type = BRANCH_TYPES[get_branch_type(branch)]
180
181
182
183
184
185
        except KeyError:
            allowed_branches = ", ".join(x for x in BRANCH_TYPES.keys())
            raise ValueError("Malformed branch name '%s', cannot classify as"
                             " one of %s" % (branch, allowed_branches))
        mode = "snapshot" if br_type.builds_snapshot else "release"
    return mode
Christos Stavrakakis's avatar
Fix bug    
Christos Stavrakakis committed
186
187
188
189
190


def normalize_branch_name(branch_name):
    """Normalize branch name by removing debian- if exists"""
    brnorm = branch_name
191
    codename = get_distribution_codename()
Christos Stavrakakis's avatar
Fix bug    
Christos Stavrakakis committed
192
    if brnorm == "debian":
193
        return "master"
194
    elif brnorm == codename:
195
        return "master"
196
197
198
199
200
201
    elif brnorm == "debian-%s" % codename:
        return "master"
    elif brnorm.startswith("debian-%s-" % codename):
        return brnorm.replace("debian-%s-" % codename, "", 1)
    elif brnorm.startswith("debian-"):
        return brnorm.replace("debian-", "", 1)
Christos Stavrakakis's avatar
Fix bug    
Christos Stavrakakis committed
202
203
204
205
206
207
208
209
210
211
212
    return brnorm


def get_branch_type(branch_name):
    """Extract the type from a branch name"""
    branch_name = normalize_branch_name(branch_name)
    if "-" in branch_name:
        btypestr = branch_name.split("-")[0]
    else:
        btypestr = branch_name
    return btypestr
213
214
215
216


def version_to_tag(version):
    return version.replace("~", "")
217

218

219
def undebianize(branch):
220
    codename = get_distribution_codename()
221
222
    if branch == "debian":
        return "master"
223
224
225
226
227
228
    elif branch == codename:
        return "master"
    elif branch == "debian-%s" % codename:
        return "master"
    elif branch.startswith("debian-%s-" % codename):
        return branch.replace("debian-%s-" % codename, "", 1)
229
230
231
232
    elif branch.startswith("debian-"):
        return branch.replace("debian-", "")
    else:
        return branch
233
234
235


def get_distribution_codename():
236
    codename = sh.uname().lower().strip()
237
238
239
240
241
    if codename == "linux":
        # lets try to be more specific using lsb_release
        try:
            output = sh.lsb_release("-c")  # pylint: disable=E1101
            _, codename = output.split("\t")
Nikos Skalkotos's avatar
Nikos Skalkotos committed
242
        except sh.CommandNotFound:
243
            pass
244
245
    codename = codename.strip()
    return codename