-
Notifications
You must be signed in to change notification settings - Fork 11
/
ypkg-gen-history
executable file
·187 lines (148 loc) · 4.54 KB
/
ypkg-gen-history
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
182
183
184
185
186
187
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# This file is part of ypkg2
#
# Copyright 2015-2020 Solus Project
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
from ypkg2.ypkgspec import YpkgSpec, PackageHistory
from ypkg2.main import show_version
import subprocess
import sys
import os
import pisi.specfile
import re
import argparse
from yaml import load as yaml_load
try:
from yaml import CLoader as Loader
except Exception as e:
from yaml import Loader
MAX_HISTORY_LEN = 10
cve_hit = re.compile(r".*(CVE\-[0-9]+\-[0-9]+).*")
class CommiterInfo:
name = None
email = None
date = None
subject = None
body = None
def get_git_tags(wdir):
cmd = "git -C \"{}\" tag --sort=-refname".format(wdir)
ret = set()
try:
out = subprocess.check_output(cmd, shell=True)
except Exception as e:
return None
for i in out.split("\n"):
i = i.strip()
ret.add(i)
return sorted(list(ret))
def get_yml_at_tag(wdir, tag):
cmd = "git -C \"{}\" show {}:package.yml".format(wdir, tag)
ret = None
try:
out = subprocess.check_output(cmd, shell=True)
except Exception as e:
return None
yml = YpkgSpec()
try:
yaml_data = yaml_load(out, Loader=Loader)
except Exception as e:
return False
if not yml.load_from_data(yaml_data):
return None
return yml
def get_commiter_infos(wdir, tag):
fmt = "%an\n%ae\n%ad\n%s\n%b"
cmd = "git -C \"{}\" log --pretty=format:\"{}\" {} -1 --date=iso". \
format(wdir, fmt, tag)
out = None
try:
out = subprocess.check_output(cmd, shell=True)
except Exception as e:
return None
splits = out.split("\n")
if len(splits) < 4:
return None
com = CommiterInfo()
com.name = splits[0].strip()
com.email = splits[1].strip()
com.date = splits[2].split(" ")[0].strip()
com.subject = splits[3].strip()
if len(splits) > 4:
com.body = "\n".join(splits[4:])
return com
def main():
parser = argparse.ArgumentParser(description="Ypkg History Generator")
parser.add_argument("-v", "--version", action="store_true",
help="Show version information and exit")
parser.add_argument("-D", "--output-dir", type=str,
help="Set the output directory for resulting files")
# Main file
parser.add_argument("filename", help="Path to the ypkg YAML file",
nargs='?')
args = parser.parse_args()
if args.version:
show_version()
if not args.filename:
print("Fatal: No filename provided")
sys.exit(1)
yml = os.path.abspath(args.filename)
wdir = os.path.dirname(yml)
# check git exists
fp = os.path.join(wdir, ".git")
if not os.path.exists(fp):
print(("Debug: Skipping non git tree: {}".format(wdir)))
sys.exit(0)
outputDir = wdir
if args.output_dir:
od = args.output_dir
if not os.path.exists(args.output_dir):
print(("{} does not exist".format(od)))
sys.exit(1)
outputDir = od
outputDir = os.path.abspath(outputDir)
tags = get_git_tags(wdir)
history = list()
for tag in tags:
tag = tag.strip()
if tag == "":
continue
spec = get_yml_at_tag(wdir, tag)
if not spec:
continue
info = get_commiter_infos(wdir, tag)
if not info:
continue
history.append((spec, info))
history = sorted(history, key=lambda x: x[0].pkg_release, reverse=True)
if len(history) > MAX_HISTORY_LEN:
history = history[0:MAX_HISTORY_LEN]
hist = os.path.join(outputDir, "history.xml")
hist_obj = PackageHistory()
for i in history:
com = i[1]
spec = i[0]
update = pisi.specfile.Update()
update.name = str(com.name)
update.email = com.email
update.version = spec.pkg_version
update.release = str(spec.pkg_release)
update.date = com.date
comment = com.subject
if com.body:
comment += "\n" + com.body
update.comment = comment
for word in comment.split():
if cve_hit.match(word):
update.type = "security"
break
hist_obj.history.append(update)
hist_obj.write(hist)
if __name__ == "__main__":
main()