forked from gofed/gofed
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bumpSpec.py
182 lines (143 loc) · 4.39 KB
/
bumpSpec.py
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import optparse
from modules.Utils import runCommand
from modules.SpecParser import SpecParser
from modules.RepositoryInfo import RepositoryInfo
def getSpec():
so, se, rc = runCommand("ls *.spec")
if rc != 0:
return ""
else:
return so.strip().split(" ")[0]
def getMacros(spec):
err = ""
macros = {}
obj = SpecParser(spec)
if not obj.parse():
err = obj.getError()
return err, {}, -1
macros["project"] = obj.getMacro("project")
macros["repo"] = obj.getMacro("repo")
macros["provider"] = obj.getMacro("provider")
macros["commit"] = obj.getMacro("commit")
last_bug_id = obj.getBugIdFromLastChangelog()
if macros["project"] == "":
err = "Unable to detect project macro"
return err, {}, -1
if macros["repo"] == "":
err = "Unable to detect repo macro"
return err, {}, -1
if macros["provider"] == "":
err = "unable to detect provider macro"
return err, {}, -1
if macros["commit"] == "":
err = "unable to detect commit macro"
return err, {}, -1
macros["ip"] = obj.getMacro("provider_prefix")
if macros["ip"] == "":
macros["ip"] = obj.getMacro("import_path")
if macros["ip"] == "":
err = "Unable to detect provider URL"
return err, {}, -1
return "", macros, last_bug_id
def downloadTarball(archive_url):
so, se, rc = runCommand("wget -nv %s --no-check-certificate" % archive_url)
if rc != 0:
print "%sUnable to download tarball:\n%s%s" % (RED, se, ENDC)
exit(1)
def updateSpec(spec, commit):
so, se, rc = runCommand("sed -i -e \"s/%%global commit\([[:space:]]\+\)[[:xdigit:]]\{40\}/%%global commit\\1%s/\" %s" % (commit, spec))
if rc != 0:
return False
else:
return True
def bumpSpec(spec, commit, last_bug_id):
if last_bug_id != -1:
cmd = "rpmdev-bumpspec --comment=\"$(echo \"Bump to upstream %s\n related: #%s\")\" %s" % (commit, last_bug_id, spec)
else:
cmd = "rpmdev-bumpspec --comment=\"Bump to upstream %s\" %s" % (commit, spec)
so, se, rc = runCommand(cmd)
if rc != 0:
return False
else:
return True
if __name__ == "__main__":
parser = optparse.OptionParser("%prog [-c COMMIT] ")
parser.add_option(
"", "-c", "--commit", dest="commit", default = "",
help = "Bump spec file to commit."
)
parser.add_option(
"", "-s", "--skip-master", dest="skipmaster", action="store_true", default = False,
help = "Skip master branch test."
)
parser.add_option(
"", "", "--skip-checks", dest="skipchecks", action="store_true", default = False,
help = "Skip checks for tags and releases."
)
options, args = parser.parse_args()
# must be on master branch
if not options.skipmaster:
so, se, rc = runCommand("git branch | grep '*' | sed 's/*//'")
if rc != 0:
print "Not in a git repository"
exit(1)
branch = so.split('\n')[0].strip()
if branch != "master":
print "Not on branch master"
exit(1)
# get spec file
print "Searching for spec file"
spec = getSpec()
if spec == "":
print "Unable to find spec file"
exit(1)
# get macros
print "Reading macros from %s" % spec
err, macros, last_bug_id = getMacros(spec)
if err != "":
print err
exit(2)
provider = macros["provider"]
project = macros["project"]
repo = macros["repo"]
current_commit = macros["commit"]
# only github so far
if provider != "github" and provider != "bitbucket":
print "Only githum.com and bitbucket.org are supported"
exit(2)
commit = options.commit
if commit == "":
# get latest commit
print "Getting the latest commit from %s" % macros["ip"]
ri_obj = RepositoryInfo(macros["ip"], commit)
if not ri_obj.retrieve():
print ri_obj.getError()
exit(1)
commit = ri_obj.getCommit()
# don't bump if the commit is equal to the latest
if commit == current_commit:
print "The latest commit equals the current commit"
exit(1)
if not options.skipchecks:
if provider == "github":
tags = ri_obj.getGithubTags(project, repo)
releases = ri_obj.getGithubReleases(project, repo)
print "Tags: " + ", ".join(tags[:5])
print "Releases: " + ", ".join(releases[:5])
# download tarball
print "Downloading tarball"
ar_info = ri_obj.getArchiveInfo()
shortcommit = ar_info.shortcommit
archive = ar_info.archive
archive_url = ar_info.archive_url
downloadTarball(archive_url)
# update spec file
print "Updating spec file"
if not updateSpec(spec, commit):
print "Unable to update spec file"
exit(5)
# bump spec file
print "Bumping spec file"
if not bumpSpec(spec, commit, last_bug_id):
print "Unable to bump spec file"
exit(6)