Debian Codegraph @

admin/linux/debian/scripts/git2changelog.py

raw symbols SCIP
1 #!//usr/bin/env python2.7
2
3 # SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors
4 # SPDX-License-Identifier: GPL-2.0-or-later
5
6 from __future__ import print_function
7 import subprocess
8 import re
9 import sys
10 import datetime
11 import os
12 import ConfigParser
13
14 try:
15 long
16 except NameError:
17 long = int
18
19 distribution="yakkety"
20
21 versionTagRE = re.compile("^v([0-9]+((\.[0-9]+)+))(-(.+))?$")
22
23 def processVersionTag(tag):
24 m = versionTagRE.match(tag)
25 if m:
26 return (m.group(1), "release" if m.group(4) is None else "beta")
27 else:
28 return None
29
30 def getCommitVersion(commit):
31 major=None
32 minor=None
33 patch=None
34 try:
35 for line in subprocess.check_output(["git", "show",
36 commit + ":VERSION.cmake"]).splitlines():
37 m = re.match("set\( *MIRALL_VERSION_([A-Z]+) +([0-9]+) *\)", line)
38 if m is not None:
39 kind=m.group(1)
40 version=m.group(2)
41 if kind=="MAJOR":
42 major=version
43 elif kind=="MINOR":
44 minor=version
45 elif kind=="PATCH":
46 patch=version
47 if major and minor and patch:
48 return major + "." + minor + "." + patch
49 else:
50 return None
51 except:
52 return None
53
54 def collectEntries(baseCommit, baseVersion, kind, finalBaseVersion,
55 finalRevDate, config, finalCommit):
56
57 newVersionCommit = None
58 newVersionTag = None
59 newVersionOrigTag = None
60
61 if config is not None and config.has_section("versionhack"):
62 if config.has_option("versionhack", "commit") and \
63 config.has_option("versionhack", "tag"):
64 newVersionCommit = config.get("versionhack", "commit")
65 newVersionTag = config.get("versionhack", "tag")
66
67 entries = []
68
69 args = ["git", "log",
70 "--format=%h%x09%an%x09%ae%x09%aD%x09%ad%x09%s",
71 "--date=unix", "--author-date-order", "--reverse"]
72 try:
73 output = subprocess.check_output(args + [baseCommit + ".."])
74 except:
75 output = subprocess.check_output(args)
76
77
78 lastVersionTag = None
79 lastCMAKEVersion = None
80 for line in output.splitlines():
81 words = line.split("\t")
82 (commit, name, email, date, revdate) = words[0:5]
83 subject = "\t".join(words[5:])
84
85 revdate = datetime.datetime.utcfromtimestamp(long(revdate)).strftime("%Y%m%d.%H%M%S")
86 revdate += "." + commit
87
88 kind = "alpha"
89
90 if commit==newVersionCommit:
91 result = processVersionTag(newVersionTag)
92 if result:
93 newVersionOrigTag = lastVersionTag
94 (baseVersion, _kind) = result
95
96 version=getCommitVersion(commit)
97 if version and version!=lastCMAKEVersion:
98 tag = "v" + version
99 if tag!=newVersionOrigTag:
100 result = processVersionTag(tag)
101 if result:
102 lastVersionTag = tag
103 lastCMAKEVersion = version
104 (baseVersion, _kind) = result
105
106 commitTags = []
107 for tag in subprocess.check_output(["git", "tag",
108 "--points-at",
109 commit]).splitlines():
110 if tag!=newVersionOrigTag:
111 result = processVersionTag(tag)
112 if result:
113 lastVersionTag = tag
114 commitTags.append(tag)
115 (baseVersion, kind1) = result
116 if kind1!=kind and kind!="release":
117 kind = kind1
118
119
120 entries.append((commit, name, email, date, revdate, subject,
121 baseVersion, kind))
122
123 if commit==finalCommit or finalCommit in commitTags:
124 break
125
126 if entries:
127 (commit, name, email, date, revdate, subject, baseVersion, kind) = entries[-1]
128 if finalRevDate is None:
129 revdate = datetime.datetime.now().strftime("%Y%m%d.%H%M%S")+ "." + commit
130 else:
131 revdate = finalRevDate
132 if finalBaseVersion is not None:
133 baseVersion = finalBaseVersion
134 entries[-1] = (commit, name, email, date, revdate, subject, baseVersion, kind)
135
136 entries.reverse()
137
138 return entries
139
140 def genChangeLogEntries(f, entries, distribution):
141 latestBaseVersion = None
142 latestRevDate = None
143 latestKind = None
144 for (commit, name, email, date, revdate, subject, baseVersion, kind) in entries:
145 if latestBaseVersion is None:
146 latestBaseVersion = baseVersion
147 latestRevDate = revdate
148 latestKind = kind
149 upstreamVersion = baseVersion + "-" + revdate
150 if distribution=="stable":
151 version = upstreamVersion
152 else:
153 version = upstreamVersion + "-1.0~" + distribution + "1"
154 print("nextcloud-desktop (%s) %s; urgency=medium" % (version, distribution), file=f)
155 print(file=f)
156 print(" * " + subject, file=f)
157 print(file=f)
158 print(" -- %s <%s> %s" % (name, email, date), file=f)
159 print(file=f)
160 return (latestBaseVersion, latestRevDate, latestKind)
161
162 if __name__ == "__main__":
163 scriptdir = os.path.dirname(__file__)
164 configPath = os.path.join(scriptdir, "git2changelog.cfg")
165
166 baseCommit = "f9b1c724d6ab5431e0cd56b7cd834f2dd48cebb1"
167 baseVersion = "2.4.0"
168
169 config = None
170 if os.path.exists(configPath):
171 config = ConfigParser.SafeConfigParser()
172 config.read(configPath)
173
174 if config.has_section("base"):
175 if config.has_option("base", "commit") and \
176 config.has_option("base", "version"):
177 baseCommit = config.get("base", "commit")
178 baseVersion = config.get("base", "version")
179
180 distribution = sys.argv[2]
181 finalRevDate = sys.argv[3] if len(sys.argv)>3 and sys.argv[3] else None
182 finalBaseVersion = sys.argv[4] if len(sys.argv)>4 and sys.argv[4] else None
183 finalCommit = sys.argv[5] if len(sys.argv)>5 and sys.argv[5] else None
184
185 entries = collectEntries(baseCommit, baseVersion, "alpha",
186 finalBaseVersion, finalRevDate, config,
187 finalCommit)
188
189 with open(sys.argv[1], "wt") as f:
190 (baseVersion, revdate, kind) = genChangeLogEntries(f, entries, distribution)
191 print(baseVersion, revdate, kind)