forked from rpm-software-management/libcomps
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild_prep.py
executable file
·164 lines (144 loc) · 4.83 KB
/
build_prep.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
#!/bin/env python
import subprocess
import sys
import pprint
import string
import json
from datetime import date
def is_commit_tag(commit):
p = subprocess.Popen(['git', 'describe',
'--tags', "--exact-match", "%s"%commit],
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
p.wait()
if p.returncode is 0:
return p.stdout.readline().strip()
else:
return None
def tag_to_commit(tag):
p = subprocess.Popen(['git', 'rev-parse', "%s^{commit}"%tag],
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
p.wait()
if p.returncode:
return None
x = p.stdout.readline().strip()
return x
def commits_date(commits):
dates = []
for c in commits:
p = subprocess.Popen(['git', 'log', '-1', "--format=%at", "%s"%c],
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
p.wait()
dates.append(int(p.stdout.read()))
return dates
def git_tags_chrono():
p = subprocess.Popen(['git', 'tag'], stdout=subprocess.PIPE)
p.wait()
if p.returncode:
return None
tags = [x.strip() for x in p.stdout]
dates = [commits_date([t]) for t in tags]
return [z[0] for z in sorted(zip(tags, dates), key=lambda x: x[1])]
def git_tags():
p = subprocess.Popen(['git', 'tag'], stdout=subprocess.PIPE)
if p.returncode:
return None
tags = [x.strip() for x in p.stdout]
return tags
def commits_for_tag(tags, tag):
index = tags.index(tag)
#print index
if index == 0:
prev = None
else:
prev = tags[index-1]
prev = tag_to_commit(prev)
tag = tag_to_commit(tag)
#print prev
commits = []
p = subprocess.Popen(['git', 'rev-list', '--all'], stdout=subprocess.PIPE)
start = False
for x in p.stdout:
x = x.strip()
#print x,tag
if not start and x == tag:
start = True
#print "start true"
if start:
commits.append(x)
if x == prev:
break
return commits
def log_for_commits(commits, _format=None):
log = []
if not _format:
_args = ['git', 'log', '-1']
else:
_args = ['git', 'log', '-1', '--format=%s'%_format]
for x in commits:
#print x
p = subprocess.Popen(_args + [x], stdout=subprocess.PIPE)
log.append([x.rstrip("\n") for x in p.stdout])
return log
def format_chlog_msg(msg):
msg = filter(lambda x: x != "", msg)
for x in range(0, len(msg)):
if not msg[x].startswith("- "):
msg[x] = "- "+msg[x]
return msg
def build_chlog(tags ,top='HEAD'):
f = open("chcommits", "r")
chcommits = set([x.strip() for x in f])
f.close()
log = []
for tag in tags:
head = log_for_commits([tag], _format="%ct%n%cn <%ce>%n%B")
head = ["*"]+head[0]
head_body = head[3:]
head = head[:3]
head[1] = date.fromtimestamp(int(head[1])).strftime("%a %b %d %Y")
head.append("-".join(tag.split("-")[1:]))
#print head
commits = commits_for_tag(tags, tag)
loc_chcommits = list(chcommits & set(commits))
loc_log = log_for_commits(loc_chcommits, _format="%B")
_log = [" ".join(head)]
_log.append("\n".join(format_chlog_msg(head_body)))
for x in loc_log:
_log.append("\n".join(format_chlog_msg(x)))
_log.append("")
#print _log
log.append("\n".join(_log))
return reversed(log)
def prepare(ref='HEAD'):
vfp = open("version.json", "r")
version = json.load(vfp)
vfp.close()
subs = {}
top_commit = subs["GITREVLONG"] = tag_to_commit(ref)
subs.update(version)
tags = git_tags_chrono()
subs["CHANGELOG"] = "\n".join(build_chlog(tags, top_commit))
tag = is_commit_tag(top_commit)
if not tag:
subs["SOURCE_URL_PATH"] = "archive/%{commit}/libcomps-%{commit}.tar.gz"
archive_name = "libcomps-%s.tar.gz"%(top_commit,)
else:
subs["SOURCE_URL_PATH"] = tags[-1]+".tar.gz"
archive_name = "%s.tar.gz"%(tag,)
subs["VERSION"] = "%s.%s.%s" % (subs["libcomps_VERSION_MAJOR"],
subs["libcomps_VERSION_MINOR"],
subs["libcomps_VERSION_PATCH"])
spec = open("libcomps.spec.in", "r")
specstr_in = spec.read()
spec.close()
specstr_out = string.Template(specstr_in).safe_substitute(subs)
spec = open("libcomps.spec", "w")
spec.write(specstr_out)
spec.close()
p = subprocess.Popen(['git', 'archive', top_commit, "--format=tar.gz",
"--prefix=libcomps-%s/"%(top_commit,), "-o",
archive_name])#,
#stdout=subprocess.PIPE, stderr=subprocess.PIPE)
p.wait()
if __name__ == "__main__":
prepare(next(iter(sys.argv[1:]), 'HEAD'))