Statistics
| Branch: | Tag: | Revision:

root / devflow / utils.py @ 42868817

History | View | Annotate | Download (6.3 kB)

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

    
34
import os
35
import git
36
import sh
37
from collections import namedtuple
38
from configobj import ConfigObj
39

    
40
from devflow import BRANCH_TYPES
41

    
42

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

    
54

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

    
61
    config = ConfigObj(path)
62
    return config
63

    
64

    
65
def get_vcs_info():
66
    """Return current git HEAD commit information.
67

68
    Returns a tuple containing
69
        - branch name
70
        - commit id
71
        - commit count
72
        - git describe output
73
        - path of git toplevel directory
74

75
    """
76

    
77
    repo = get_repository()
78
    branch = repo.head.reference
79
    revid = get_commit_id(branch.commit, branch)
80
    revno = len(list(repo.iter_commits()))
81
    toplevel = repo.working_dir
82

    
83
    info = namedtuple("vcs_info", ["branch", "revid", "revno",
84
                                   "toplevel"])
85

    
86
    return info(branch=branch.name, revid=revid, revno=revno,
87
                toplevel=toplevel)
88

    
89

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

93
    If the commit is a 'merge' commit, and one of the parents is a
94
    debian branch we return a compination of the parents commits.
95

96
    """
97
    def short_id(commit):
98
        return commit.hexsha[0:7]
99

    
100
    parents = commit.parents
101
    cur_br_name = current_branch.name
102
    if len(parents) == 1:
103
        return short_id(commit)
104
    elif len(parents) == 2:
105
        if cur_br_name.startswith("debian-") or cur_br_name == "debian":
106
            pr1, pr2 = parents
107
            return short_id(pr1) + "_" + short_id(pr2)
108
        else:
109
            return short_id(commit)
110
    else:
111
        raise RuntimeError("Commit %s has more than 2 parents!" % commit)
112

    
113

    
114
def get_debian_branch(branch):
115
    """Find the corresponding debian- branch"""
116
    if branch == "master":
117
        return "debian"
118
    deb_branch = "debian-" + branch
119
    # Check if debian-branch exists (local or origin)
120
    if _get_branch(deb_branch):
121
        return deb_branch
122
    branch_type = BRANCH_TYPES[get_branch_type(branch)]
123
    # If not try the default debian branch
124
    default_branch = branch_type.debian_branch
125
    if _get_branch(default_branch):
126
        repo = get_repository()
127
        repo.git.branch(deb_branch, default_branch)
128
        print "Created branch '%s' from '%s'" % (deb_branch, default_branch)
129
        return deb_branch
130
    # If not try the debian branch
131
    repo.git.branch(deb_branch, default_branch)
132
    print "Created branch '%s' from 'debian'" % deb_branch
133
    return "debian"
134

    
135

    
136
def _get_branch(branch):
137
    repo = get_repository()
138
    if branch in repo.branches:
139
        return branch
140
    origin_branch = "origin/" + branch
141
    if origin_branch in repo.refs:
142
        print "Creating branch '%s' to track '%s'" % (branch, origin_branch)
143
        repo.git.branch(branch, origin_branch)
144
        return branch
145
    else:
146
        return None
147

    
148

    
149
def get_build_mode():
150
    """Determine the build mode"""
151
    # Get it from environment if exists
152
    mode = os.environ.get("DEVFLOW_BUILD_MODE", None)
153
    if mode is None:
154
        branch = get_branch_type(get_vcs_info().branch)
155
        try:
156
            br_type = BRANCH_TYPES[get_branch_type(branch)]
157
        except KeyError:
158
            allowed_branches = ", ".join(x for x in BRANCH_TYPES.keys())
159
            raise ValueError("Malformed branch name '%s', cannot classify as"
160
                             " one of %s" % (branch, allowed_branches))
161
        mode = "snapshot" if br_type.builds_snapshot else "release"
162
    return mode
163

    
164

    
165
def normalize_branch_name(branch_name):
166
    """Normalize branch name by removing debian- if exists"""
167
    brnorm = branch_name
168
    if brnorm == "debian":
169
        brnorm = "debian-master"
170
    # If it's a debian branch, ignore starting "debian-"
171
    if brnorm.startswith("debian-"):
172
        brnorm = brnorm.replace("debian-", "", 1)
173
    return brnorm
174

    
175

    
176
def get_branch_type(branch_name):
177
    """Extract the type from a branch name"""
178
    branch_name = normalize_branch_name(branch_name)
179
    if "-" in branch_name:
180
        btypestr = branch_name.split("-")[0]
181
    else:
182
        btypestr = branch_name
183
    return btypestr
184

    
185

    
186
def version_to_tag(version):
187
    return version.replace("~", "")
188

    
189

    
190
def undebianize(branch):
191
    if branch == "debian":
192
        return "master"
193
    elif branch.startswith("debian-"):
194
        return branch.replace("debian-", "")
195
    else:
196
        return branch
197

    
198

    
199
def get_distribution_codename():
200
    output = sh.lsb_release("-c")
201
    _, codename = output.split("\t")
202
    codename = codename.strip()
203
    return codename