Add support for package update notifications
[aur.git] / git-interface / git-update.py
blobe54e0e685395c006a3d7026890705b5a35529dbd
1 #!/usr/bin/python3
3 import configparser
4 import mysql.connector
5 import os
6 import pygit2
7 import re
8 import subprocess
9 import sys
11 import srcinfo.parse
12 import srcinfo.utils
14 config = configparser.RawConfigParser()
15 config.read(os.path.dirname(os.path.realpath(__file__)) + "/../conf/config")
17 aur_db_host = config.get('database', 'host')
18 aur_db_name = config.get('database', 'name')
19 aur_db_user = config.get('database', 'user')
20 aur_db_pass = config.get('database', 'password')
21 aur_db_socket = config.get('database', 'socket')
23 notify_cmd = config.get('notifications', 'notify-cmd')
25 repo_path = config.get('serve', 'repo-path')
26 repo_regex = config.get('serve', 'repo-regex')
29 def extract_arch_fields(pkginfo, field):
30 values = []
32 if field in pkginfo:
33 for val in pkginfo[field]:
34 values.append({"value": val, "arch": None})
36 for arch in ['i686', 'x86_64']:
37 if field + '_' + arch in pkginfo:
38 for val in pkginfo[field + '_' + arch]:
39 values.append({"value": val, "arch": arch})
41 return values
44 def parse_dep(depstring):
45 dep, _, desc = depstring.partition(': ')
46 depname = re.sub(r'(<|=|>).*', '', dep)
47 depcond = dep[len(depname):]
49 if (desc):
50 return (depname + ': ' + desc, depcond)
51 else:
52 return (depname, depcond)
55 def save_metadata(metadata, db, cur, user):
56 # Obtain package base ID and previous maintainer.
57 pkgbase = metadata['pkgbase']
58 cur.execute("SELECT ID, MaintainerUID FROM PackageBases "
59 "WHERE Name = %s", [pkgbase])
60 (pkgbase_id, maintainer_uid) = cur.fetchone()
61 was_orphan = not maintainer_uid
63 # Obtain the user ID of the new maintainer.
64 cur.execute("SELECT ID FROM Users WHERE Username = %s", [user])
65 user_id = int(cur.fetchone()[0])
67 # Update package base details and delete current packages.
68 cur.execute("UPDATE PackageBases SET ModifiedTS = UNIX_TIMESTAMP(), " +
69 "PackagerUID = %s, OutOfDateTS = NULL WHERE ID = %s",
70 [user_id, pkgbase_id])
71 cur.execute("UPDATE PackageBases SET MaintainerUID = %s " +
72 "WHERE ID = %s AND MaintainerUID IS NULL",
73 [user_id, pkgbase_id])
74 cur.execute("DELETE FROM Packages WHERE PackageBaseID = %s",
75 [pkgbase_id])
77 for pkgname in srcinfo.utils.get_package_names(metadata):
78 pkginfo = srcinfo.utils.get_merged_package(pkgname, metadata)
80 if 'epoch' in pkginfo and int(pkginfo['epoch']) > 0:
81 ver = '{:d}:{:s}-{:s}'.format(int(pkginfo['epoch']),
82 pkginfo['pkgver'],
83 pkginfo['pkgrel'])
84 else:
85 ver = '{:s}-{:s}'.format(pkginfo['pkgver'], pkginfo['pkgrel'])
87 for field in ('pkgdesc', 'url'):
88 if field not in pkginfo:
89 pkginfo[field] = None
91 # Create a new package.
92 cur.execute("INSERT INTO Packages (PackageBaseID, Name, " +
93 "Version, Description, URL) " +
94 "VALUES (%s, %s, %s, %s, %s)",
95 [pkgbase_id, pkginfo['pkgname'], ver,
96 pkginfo['pkgdesc'], pkginfo['url']])
97 db.commit()
98 pkgid = cur.lastrowid
100 # Add package sources.
101 for source_info in extract_arch_fields(pkginfo, 'source'):
102 cur.execute("INSERT INTO PackageSources (PackageID, Source, " +
103 "SourceArch) VALUES (%s, %s, %s)",
104 [pkgid, source_info['value'], source_info['arch']])
106 # Add package dependencies.
107 for deptype in ('depends', 'makedepends',
108 'checkdepends', 'optdepends'):
109 cur.execute("SELECT ID FROM DependencyTypes WHERE Name = %s",
110 [deptype])
111 deptypeid = cur.fetchone()[0]
112 for dep_info in extract_arch_fields(pkginfo, deptype):
113 depname, depcond = parse_dep(dep_info['value'])
114 deparch = dep_info['arch']
115 cur.execute("INSERT INTO PackageDepends (PackageID, " +
116 "DepTypeID, DepName, DepCondition, DepArch) " +
117 "VALUES (%s, %s, %s, %s, %s)",
118 [pkgid, deptypeid, depname, depcond, deparch])
120 # Add package relations (conflicts, provides, replaces).
121 for reltype in ('conflicts', 'provides', 'replaces'):
122 cur.execute("SELECT ID FROM RelationTypes WHERE Name = %s",
123 [reltype])
124 reltypeid = cur.fetchone()[0]
125 for rel_info in extract_arch_fields(pkginfo, reltype):
126 relname, relcond = parse_dep(rel_info['value'])
127 relarch = rel_info['arch']
128 cur.execute("INSERT INTO PackageRelations (PackageID, " +
129 "RelTypeID, RelName, RelCondition, RelArch) " +
130 "VALUES (%s, %s, %s, %s, %s)",
131 [pkgid, reltypeid, relname, relcond, relarch])
133 # Add package licenses.
134 if 'license' in pkginfo:
135 for license in pkginfo['license']:
136 cur.execute("SELECT ID FROM Licenses WHERE Name = %s",
137 [license])
138 if cur.rowcount == 1:
139 licenseid = cur.fetchone()[0]
140 else:
141 cur.execute("INSERT INTO Licenses (Name) VALUES (%s)",
142 [license])
143 db.commit()
144 licenseid = cur.lastrowid
145 cur.execute("INSERT INTO PackageLicenses (PackageID, " +
146 "LicenseID) VALUES (%s, %s)",
147 [pkgid, licenseid])
149 # Add package groups.
150 if 'groups' in pkginfo:
151 for group in pkginfo['groups']:
152 cur.execute("SELECT ID FROM Groups WHERE Name = %s",
153 [group])
154 if cur.rowcount == 1:
155 groupid = cur.fetchone()[0]
156 else:
157 cur.execute("INSERT INTO Groups (Name) VALUES (%s)",
158 [group])
159 db.commit()
160 groupid = cur.lastrowid
161 cur.execute("INSERT INTO PackageGroups (PackageID, "
162 "GroupID) VALUES (%s, %s)", [pkgid, groupid])
164 # Add user to notification list on adoption.
165 if was_orphan:
166 cur.execute("SELECT COUNT(*) FROM PackageNotifications WHERE " +
167 "PackageBaseID = %s AND UserID = %s",
168 [pkgbase_id, user_id])
169 if cur.fetchone()[0] == 0:
170 cur.execute("INSERT INTO PackageNotifications (PackageBaseID, UserID) " +
171 "VALUES (%s, %s)", [pkgbase_id, user_id])
173 db.commit()
175 def update_notify(db, cur, user, pkgbase_id):
176 # Obtain the user ID of the new maintainer.
177 cur.execute("SELECT ID FROM Users WHERE Username = %s", [user])
178 user_id = int(cur.fetchone()[0])
180 # Execute the notification script.
181 subprocess.Popen((notify_cmd, 'update', str(user_id), str(pkgbase_id)))
183 def die(msg):
184 sys.stderr.write("error: {:s}\n".format(msg))
185 exit(1)
188 def warn(msg):
189 sys.stderr.write("warning: {:s}\n".format(msg))
192 def die_commit(msg, commit):
193 sys.stderr.write("error: The following error " +
194 "occurred when parsing commit\n")
195 sys.stderr.write("error: {:s}:\n".format(commit))
196 sys.stderr.write("error: {:s}\n".format(msg))
197 exit(1)
200 repo = pygit2.Repository(repo_path)
202 user = os.environ.get("AUR_USER")
203 pkgbase = os.environ.get("AUR_PKGBASE")
204 privileged = (os.environ.get("AUR_PRIVILEGED", '0') == '1')
206 if len(sys.argv) == 2 and sys.argv[1] == "restore":
207 if 'refs/heads/' + pkgbase not in repo.listall_references():
208 die('{:s}: repository not found: {:s}'.format(sys.argv[1], pkgbase))
209 refname = "refs/heads/master"
210 sha1_old = sha1_new = repo.lookup_reference('refs/heads/' + pkgbase).target
211 elif len(sys.argv) == 4:
212 refname, sha1_old, sha1_new = sys.argv[1:4]
213 else:
214 die("invalid arguments")
216 if refname != "refs/heads/master":
217 die("pushing to a branch other than master is restricted")
219 db = mysql.connector.connect(host=aur_db_host, user=aur_db_user,
220 passwd=aur_db_pass, db=aur_db_name,
221 unix_socket=aur_db_socket, buffered=True)
222 cur = db.cursor()
224 # Detect and deny non-fast-forwards.
225 if sha1_old != "0000000000000000000000000000000000000000":
226 walker = repo.walk(sha1_old, pygit2.GIT_SORT_TOPOLOGICAL)
227 walker.hide(sha1_new)
228 if next(walker, None) is not None:
229 cur.execute("SELECT AccountTypeID FROM Users WHERE UserName = %s ",
230 [user])
231 if cur.fetchone()[0] == 1:
232 die("denying non-fast-forward (you should pull first)")
234 # Prepare the walker that validates new commits.
235 walker = repo.walk(sha1_new, pygit2.GIT_SORT_TOPOLOGICAL)
236 if sha1_old != "0000000000000000000000000000000000000000":
237 walker.hide(sha1_old)
239 # Validate all new commits.
240 for commit in walker:
241 for fname in ('.SRCINFO', 'PKGBUILD'):
242 if fname not in commit.tree:
243 die_commit("missing {:s}".format(fname), str(commit.id))
245 for treeobj in commit.tree:
246 blob = repo[treeobj.id]
248 if isinstance(blob, pygit2.Tree):
249 die_commit("the repository must not contain subdirectories",
250 str(commit.id))
252 if not isinstance(blob, pygit2.Blob):
253 die_commit("not a blob object: {:s}".format(treeobj),
254 str(commit.id))
256 if blob.size > 250000:
257 die_commit("maximum blob size (250kB) exceeded", str(commit.id))
259 metadata_raw = repo[commit.tree['.SRCINFO'].id].data.decode()
260 (metadata, errors) = srcinfo.parse.parse_srcinfo(metadata_raw)
261 if errors:
262 sys.stderr.write("error: The following errors occurred "
263 "when parsing .SRCINFO in commit\n")
264 sys.stderr.write("error: {:s}:\n".format(str(commit.id)))
265 for error in errors:
266 for err in error['error']:
267 sys.stderr.write("error: line {:d}: {:s}\n".format(error['line'], err))
268 exit(1)
270 metadata_pkgbase = metadata['pkgbase']
271 if not re.match(repo_regex, metadata_pkgbase):
272 die_commit('invalid pkgbase: {:s}'.format(metadata_pkgbase),
273 str(commit.id))
275 for pkgname in set(metadata['packages'].keys()):
276 pkginfo = srcinfo.utils.get_merged_package(pkgname, metadata)
278 for field in ('pkgver', 'pkgrel', 'pkgname'):
279 if field not in pkginfo:
280 die_commit('missing mandatory field: {:s}'.format(field),
281 str(commit.id))
283 if 'epoch' in pkginfo and not pkginfo['epoch'].isdigit():
284 die_commit('invalid epoch: {:s}'.format(pkginfo['epoch']),
285 str(commit.id))
287 if not re.match(r'[a-z0-9][a-z0-9\.+_-]*$', pkginfo['pkgname']):
288 die_commit('invalid package name: {:s}'.format(pkginfo['pkgname']),
289 str(commit.id))
291 for field in ('pkgname', 'pkgdesc', 'url'):
292 if field in pkginfo and len(pkginfo[field]) > 255:
293 die_commit('{:s} field too long: {:s}'.format(field, pkginfo[field]),
294 str(commit.id))
296 for field in ('install', 'changelog'):
297 if field in pkginfo and not pkginfo[field] in commit.tree:
298 die_commit('missing {:s} file: {:s}'.format(field, pkginfo[field]),
299 str(commit.id))
301 for field in extract_arch_fields(pkginfo, 'source'):
302 fname = field['value']
303 if "://" in fname or "lp:" in fname:
304 continue
305 if fname not in commit.tree:
306 die_commit('missing source file: {:s}'.format(fname),
307 str(commit.id))
310 # Display a warning if .SRCINFO is unchanged.
311 if sha1_old not in ("0000000000000000000000000000000000000000", sha1_new):
312 srcinfo_id_old = repo[sha1_old].tree['.SRCINFO'].id
313 srcinfo_id_new = repo[sha1_new].tree['.SRCINFO'].id
314 if srcinfo_id_old == srcinfo_id_new:
315 warn(".SRCINFO unchanged. The package database will not be updated!")
317 # Read .SRCINFO from the HEAD commit.
318 metadata_raw = repo[repo[sha1_new].tree['.SRCINFO'].id].data.decode()
319 (metadata, errors) = srcinfo.parse.parse_srcinfo(metadata_raw)
321 # Ensure that the package base name matches the repository name.
322 metadata_pkgbase = metadata['pkgbase']
323 if metadata_pkgbase != pkgbase:
324 die('invalid pkgbase: {:s}, expected {:s}'.format(metadata_pkgbase, pkgbase))
326 # Ensure that packages are neither blacklisted nor overwritten.
327 pkgbase = metadata['pkgbase']
328 cur.execute("SELECT ID FROM PackageBases WHERE Name = %s", [pkgbase])
329 pkgbase_id = cur.fetchone()[0] if cur.rowcount == 1 else 0
331 cur.execute("SELECT Name FROM PackageBlacklist")
332 blacklist = [row[0] for row in cur.fetchall()]
334 for pkgname in srcinfo.utils.get_package_names(metadata):
335 pkginfo = srcinfo.utils.get_merged_package(pkgname, metadata)
336 pkgname = pkginfo['pkgname']
338 if pkgname in blacklist and not privileged:
339 die('package is blacklisted: {:s}'.format(pkgname))
341 cur.execute("SELECT COUNT(*) FROM Packages WHERE Name = %s AND " +
342 "PackageBaseID <> %s", [pkgname, pkgbase_id])
343 if cur.fetchone()[0] > 0:
344 die('cannot overwrite package: {:s}'.format(pkgname))
346 # Store package base details in the database.
347 save_metadata(metadata, db, cur, user)
349 # Create (or update) a branch with the name of the package base for better
350 # accessibility.
351 repo.create_reference('refs/heads/' + pkgbase, sha1_new, True)
353 # Work around a Git bug: The HEAD ref is not updated when using gitnamespaces.
354 # This can be removed once the bug fix is included in Git mainline. See
355 # http://git.661346.n2.nabble.com/PATCH-receive-pack-Create-a-HEAD-ref-for-ref-namespace-td7632149.html
356 # for details.
357 repo.create_reference('refs/namespaces/' + pkgbase + '/HEAD', sha1_new, True)
359 # Send package update notifications.
360 update_notify(db, cur, user, pkgbase_id)
362 # Close the database.
363 db.close()