root / devflow / utils.py @ 671d8708
History | View | Annotate | Download (7.1 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 |
import re |
38 |
from collections import namedtuple |
39 |
from configobj import ConfigObj |
40 |
|
41 |
from devflow import BRANCH_TYPES |
42 |
|
43 |
|
44 |
def get_repository(path=None): |
45 |
"""Load the repository from the current working dir."""
|
46 |
if path is None: |
47 |
path = os.getcwd() |
48 |
try:
|
49 |
return git.Repo(path)
|
50 |
except git.InvalidGitRepositoryError:
|
51 |
msg = "Cound not retrivie git information. Directory '%s'"\
|
52 |
" is not a git repository!" % path
|
53 |
raise RuntimeError(msg) |
54 |
|
55 |
|
56 |
def get_config(path=None): |
57 |
"""Load configuration file."""
|
58 |
if path is None: |
59 |
toplevel = get_vcs_info().toplevel |
60 |
path = os.path.join(toplevel, "devflow.conf")
|
61 |
|
62 |
config = ConfigObj(path) |
63 |
return config
|
64 |
|
65 |
|
66 |
def get_vcs_info(): |
67 |
"""Return current git HEAD commit information.
|
68 |
|
69 |
Returns a tuple containing
|
70 |
- branch name
|
71 |
- commit id
|
72 |
- commit count
|
73 |
- git describe output
|
74 |
- path of git toplevel directory
|
75 |
|
76 |
"""
|
77 |
|
78 |
repo = get_repository() |
79 |
branch = repo.head.reference |
80 |
revid = get_commit_id(branch.commit, branch) |
81 |
revno = len(list(repo.iter_commits())) |
82 |
toplevel = repo.working_dir |
83 |
|
84 |
info = namedtuple("vcs_info", ["branch", "revid", "revno", |
85 |
"toplevel"])
|
86 |
|
87 |
return info(branch=branch.name, revid=revid, revno=revno,
|
88 |
toplevel=toplevel) |
89 |
|
90 |
|
91 |
def get_commit_id(commit, current_branch): |
92 |
"""Return the commit ID
|
93 |
|
94 |
If the commit is a 'merge' commit, and one of the parents is a
|
95 |
debian branch we return a compination of the parents commits.
|
96 |
|
97 |
"""
|
98 |
def short_id(commit): |
99 |
return commit.hexsha[0:7] |
100 |
|
101 |
parents = commit.parents |
102 |
cur_br_name = current_branch.name |
103 |
if len(parents) == 1: |
104 |
return short_id(commit)
|
105 |
elif len(parents) == 2: |
106 |
if cur_br_name.startswith("debian-") or cur_br_name == "debian": |
107 |
pr1, pr2 = parents |
108 |
return short_id(pr1) + "_" + short_id(pr2) |
109 |
else:
|
110 |
return short_id(commit)
|
111 |
else:
|
112 |
raise RuntimeError("Commit %s has more than 2 parents!" % commit) |
113 |
|
114 |
|
115 |
def get_debian_branch(branch): |
116 |
"""Find the corresponding debian- branch"""
|
117 |
distribution = get_distribution_codename() |
118 |
repo = get_repository() |
119 |
if branch == "master": |
120 |
deb_branch = "debian-" + distribution
|
121 |
else:
|
122 |
deb_branch = "-".join(["debian", branch, distribution]) |
123 |
# Check if debian-branch exists (local or origin)
|
124 |
if _get_branch(deb_branch):
|
125 |
return deb_branch
|
126 |
# Check without distribution
|
127 |
deb_branch = re.sub("-" + distribution + "$", "", deb_branch) |
128 |
if _get_branch(deb_branch):
|
129 |
return deb_branch
|
130 |
branch_type = BRANCH_TYPES[get_branch_type(branch)] |
131 |
# If not try the default debian branch with distribution
|
132 |
default_branch = branch_type.debian_branch + "-" + distribution
|
133 |
if _get_branch(default_branch):
|
134 |
repo.git.branch(deb_branch, default_branch) |
135 |
print "Created branch '%s' from '%s'" % (deb_branch, default_branch) |
136 |
return deb_branch
|
137 |
# And without distribution
|
138 |
default_branch = branch_type.debian_branch |
139 |
if _get_branch(default_branch):
|
140 |
repo.git.branch(deb_branch, default_branch) |
141 |
print "Created branch '%s' from '%s'" % (deb_branch, default_branch) |
142 |
return deb_branch
|
143 |
# If not try the debian branch
|
144 |
repo.git.branch(deb_branch, default_branch) |
145 |
print "Created branch '%s' from 'debian'" % deb_branch |
146 |
return "debian" |
147 |
|
148 |
|
149 |
def _get_branch(branch): |
150 |
repo = get_repository() |
151 |
if branch in repo.branches: |
152 |
return branch
|
153 |
origin_branch = "origin/" + branch
|
154 |
if origin_branch in repo.refs: |
155 |
print "Creating branch '%s' to track '%s'" % (branch, origin_branch) |
156 |
repo.git.branch(branch, origin_branch) |
157 |
return branch
|
158 |
else:
|
159 |
return None |
160 |
|
161 |
|
162 |
def get_build_mode(): |
163 |
"""Determine the build mode"""
|
164 |
# Get it from environment if exists
|
165 |
mode = os.environ.get("DEVFLOW_BUILD_MODE", None) |
166 |
if mode is None: |
167 |
branch = get_branch_type(get_vcs_info().branch) |
168 |
try:
|
169 |
br_type = BRANCH_TYPES[get_branch_type(branch)] |
170 |
except KeyError: |
171 |
allowed_branches = ", ".join(x for x in BRANCH_TYPES.keys()) |
172 |
raise ValueError("Malformed branch name '%s', cannot classify as" |
173 |
" one of %s" % (branch, allowed_branches))
|
174 |
mode = "snapshot" if br_type.builds_snapshot else "release" |
175 |
return mode
|
176 |
|
177 |
|
178 |
def normalize_branch_name(branch_name): |
179 |
"""Normalize branch name by removing debian- if exists"""
|
180 |
brnorm = branch_name |
181 |
codename = get_distribution_codename() |
182 |
if brnorm == "debian": |
183 |
return "master" |
184 |
elif brnorm == "codename": |
185 |
return master
|
186 |
elif brnorm == "debian-%s" % codename: |
187 |
return "master" |
188 |
elif brnorm.startswith("debian-%s-" % codename): |
189 |
return brnorm.replace("debian-%s-" % codename, "", 1) |
190 |
elif brnorm.startswith("debian-"): |
191 |
return brnorm.replace("debian-", "", 1) |
192 |
return brnorm
|
193 |
|
194 |
|
195 |
def get_branch_type(branch_name): |
196 |
"""Extract the type from a branch name"""
|
197 |
branch_name = normalize_branch_name(branch_name) |
198 |
if "-" in branch_name: |
199 |
btypestr = branch_name.split("-")[0] |
200 |
else:
|
201 |
btypestr = branch_name |
202 |
return btypestr
|
203 |
|
204 |
|
205 |
def version_to_tag(version): |
206 |
return version.replace("~", "") |
207 |
|
208 |
|
209 |
def undebianize(branch): |
210 |
if branch == "debian": |
211 |
return "master" |
212 |
elif branch.startswith("debian-"): |
213 |
return branch.replace("debian-", "") |
214 |
else:
|
215 |
return branch
|
216 |
|
217 |
|
218 |
def get_distribution_codename(): |
219 |
output = sh.lsb_release("-c")
|
220 |
_, codename = output.split("\t")
|
221 |
codename = codename.strip() |
222 |
return codename
|