16 notify_cmd
= config
.get('notifications', 'notify-cmd')
18 repo_path
= config
.get('serve', 'repo-path')
19 repo_regex
= config
.get('serve', 'repo-regex')
21 max_blob_size
= config
.getint('update', 'max-blob-size')
24 def size_humanize(num
):
25 for unit
in ['B', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB']:
27 if isinstance(num
, int):
28 return "{}{}".format(num
, unit
)
30 return "{:.2f}{}".format(num
, unit
)
32 return "{:.2f}{}".format(num
, 'YiB')
35 def extract_arch_fields(pkginfo
, field
):
39 for val
in pkginfo
[field
]:
40 values
.append({"value": val
, "arch": None})
42 for arch
in ['i686', 'x86_64']:
43 if field
+ '_' + arch
in pkginfo
:
44 for val
in pkginfo
[field
+ '_' + arch
]:
45 values
.append({"value": val
, "arch": arch
})
50 def parse_dep(depstring
):
51 dep
, _
, desc
= depstring
.partition(': ')
52 depname
= re
.sub(r
'(<|=|>).*', '', dep
)
53 depcond
= dep
[len(depname
):]
56 return (depname
+ ': ' + desc
, depcond
)
58 return (depname
, depcond
)
61 def save_metadata(metadata
, conn
, user
):
62 # Obtain package base ID and previous maintainer.
63 pkgbase
= metadata
['pkgbase']
64 cur
= conn
.execute("SELECT ID, MaintainerUID FROM PackageBases "
65 "WHERE Name = ?", [pkgbase
])
66 (pkgbase_id
, maintainer_uid
) = cur
.fetchone()
67 was_orphan
= not maintainer_uid
69 # Obtain the user ID of the new maintainer.
70 cur
= conn
.execute("SELECT ID FROM Users WHERE Username = ?", [user
])
71 user_id
= int(cur
.fetchone()[0])
73 # Update package base details and delete current packages.
74 now
= int(time
.time())
75 conn
.execute("UPDATE PackageBases SET ModifiedTS = ?, " +
76 "PackagerUID = ?, OutOfDateTS = NULL WHERE ID = ?",
77 [now
, user_id
, pkgbase_id
])
78 conn
.execute("UPDATE PackageBases SET MaintainerUID = ? " +
79 "WHERE ID = ? AND MaintainerUID IS NULL",
80 [user_id
, pkgbase_id
])
81 for table
in ('Sources', 'Depends', 'Relations', 'Licenses', 'Groups'):
82 conn
.execute("DELETE FROM Package" + table
+ " WHERE EXISTS (" +
83 "SELECT * FROM Packages " +
84 "WHERE Packages.PackageBaseID = ? AND " +
85 "Package" + table
+ ".PackageID = Packages.ID)",
87 conn
.execute("DELETE FROM Packages WHERE PackageBaseID = ?", [pkgbase_id
])
89 for pkgname
in srcinfo
.utils
.get_package_names(metadata
):
90 pkginfo
= srcinfo
.utils
.get_merged_package(pkgname
, metadata
)
92 if 'epoch' in pkginfo
and int(pkginfo
['epoch']) > 0:
93 ver
= '{:d}:{:s}-{:s}'.format(int(pkginfo
['epoch']),
97 ver
= '{:s}-{:s}'.format(pkginfo
['pkgver'], pkginfo
['pkgrel'])
99 for field
in ('pkgdesc', 'url'):
100 if field
not in pkginfo
:
101 pkginfo
[field
] = None
103 # Create a new package.
104 cur
= conn
.execute("INSERT INTO Packages (PackageBaseID, Name, " +
105 "Version, Description, URL) " +
106 "VALUES (?, ?, ?, ?, ?)",
107 [pkgbase_id
, pkginfo
['pkgname'], ver
,
108 pkginfo
['pkgdesc'], pkginfo
['url']])
110 pkgid
= cur
.lastrowid
112 # Add package sources.
113 for source_info
in extract_arch_fields(pkginfo
, 'source'):
114 conn
.execute("INSERT INTO PackageSources (PackageID, Source, " +
115 "SourceArch) VALUES (?, ?, ?)",
116 [pkgid
, source_info
['value'], source_info
['arch']])
118 # Add package dependencies.
119 for deptype
in ('depends', 'makedepends',
120 'checkdepends', 'optdepends'):
121 cur
= conn
.execute("SELECT ID FROM DependencyTypes WHERE Name = ?",
123 deptypeid
= cur
.fetchone()[0]
124 for dep_info
in extract_arch_fields(pkginfo
, deptype
):
125 depname
, depcond
= parse_dep(dep_info
['value'])
126 deparch
= dep_info
['arch']
127 conn
.execute("INSERT INTO PackageDepends (PackageID, " +
128 "DepTypeID, DepName, DepCondition, DepArch) " +
129 "VALUES (?, ?, ?, ?, ?)",
130 [pkgid
, deptypeid
, depname
, depcond
, deparch
])
132 # Add package relations (conflicts, provides, replaces).
133 for reltype
in ('conflicts', 'provides', 'replaces'):
134 cur
= conn
.execute("SELECT ID FROM RelationTypes WHERE Name = ?",
136 reltypeid
= cur
.fetchone()[0]
137 for rel_info
in extract_arch_fields(pkginfo
, reltype
):
138 relname
, relcond
= parse_dep(rel_info
['value'])
139 relarch
= rel_info
['arch']
140 conn
.execute("INSERT INTO PackageRelations (PackageID, " +
141 "RelTypeID, RelName, RelCondition, RelArch) " +
142 "VALUES (?, ?, ?, ?, ?)",
143 [pkgid
, reltypeid
, relname
, relcond
, relarch
])
145 # Add package licenses.
146 if 'license' in pkginfo
:
147 for license
in pkginfo
['license']:
148 cur
= conn
.execute("SELECT ID FROM Licenses WHERE Name = ?",
154 cur
= conn
.execute("INSERT INTO Licenses (Name) " +
155 "VALUES (?)", [license
])
157 licenseid
= cur
.lastrowid
158 conn
.execute("INSERT INTO PackageLicenses (PackageID, " +
159 "LicenseID) VALUES (?, ?)",
162 # Add package groups.
163 if 'groups' in pkginfo
:
164 for group
in pkginfo
['groups']:
165 cur
= conn
.execute("SELECT ID FROM Groups WHERE Name = ?",
171 cur
= conn
.execute("INSERT INTO Groups (Name) VALUES (?)",
174 groupid
= cur
.lastrowid
175 conn
.execute("INSERT INTO PackageGroups (PackageID, "
176 "GroupID) VALUES (?, ?)", [pkgid
, groupid
])
178 # Add user to notification list on adoption.
180 cur
= conn
.execute("SELECT COUNT(*) FROM PackageNotifications WHERE " +
181 "PackageBaseID = ? AND UserID = ?",
182 [pkgbase_id
, user_id
])
183 if cur
.fetchone()[0] == 0:
184 conn
.execute("INSERT INTO PackageNotifications " +
185 "(PackageBaseID, UserID) VALUES (?, ?)",
186 [pkgbase_id
, user_id
])
191 def update_notify(conn
, user
, pkgbase_id
):
192 # Obtain the user ID of the new maintainer.
193 cur
= conn
.execute("SELECT ID FROM Users WHERE Username = ?", [user
])
194 user_id
= int(cur
.fetchone()[0])
196 # Execute the notification script.
197 subprocess
.Popen((notify_cmd
, 'update', str(user_id
), str(pkgbase_id
)))
201 sys
.stderr
.write("error: {:s}\n".format(msg
))
206 sys
.stderr
.write("warning: {:s}\n".format(msg
))
209 def die_commit(msg
, commit
):
210 sys
.stderr
.write("error: The following error " +
211 "occurred when parsing commit\n")
212 sys
.stderr
.write("error: {:s}:\n".format(commit
))
213 sys
.stderr
.write("error: {:s}\n".format(msg
))
217 repo
= pygit2
.Repository(repo_path
)
219 user
= os
.environ
.get("AUR_USER")
220 pkgbase
= os
.environ
.get("AUR_PKGBASE")
221 privileged
= (os
.environ
.get("AUR_PRIVILEGED", '0') == '1')
222 warn_or_die
= warn
if privileged
else die
224 if len(sys
.argv
) == 2 and sys
.argv
[1] == "restore":
225 if 'refs/heads/' + pkgbase
not in repo
.listall_references():
226 die('{:s}: repository not found: {:s}'.format(sys
.argv
[1], pkgbase
))
227 refname
= "refs/heads/master"
228 sha1_old
= sha1_new
= repo
.lookup_reference('refs/heads/' + pkgbase
).target
229 elif len(sys
.argv
) == 4:
230 refname
, sha1_old
, sha1_new
= sys
.argv
[1:4]
232 die("invalid arguments")
234 if refname
!= "refs/heads/master":
235 die("pushing to a branch other than master is restricted")
237 conn
= db
.Connection()
239 # Detect and deny non-fast-forwards.
240 if sha1_old
!= "0000000000000000000000000000000000000000" and not privileged
:
241 walker
= repo
.walk(sha1_old
, pygit2
.GIT_SORT_TOPOLOGICAL
)
242 walker
.hide(sha1_new
)
243 if next(walker
, None) is not None:
244 die("denying non-fast-forward (you should pull first)")
246 # Prepare the walker that validates new commits.
247 walker
= repo
.walk(sha1_new
, pygit2
.GIT_SORT_TOPOLOGICAL
)
248 if sha1_old
!= "0000000000000000000000000000000000000000":
249 walker
.hide(sha1_old
)
251 # Validate all new commits.
252 for commit
in walker
:
253 for fname
in ('.SRCINFO', 'PKGBUILD'):
254 if fname
not in commit
.tree
:
255 die_commit("missing {:s}".format(fname
), str(commit
.id))
257 for treeobj
in commit
.tree
:
258 blob
= repo
[treeobj
.id]
260 if isinstance(blob
, pygit2
.Tree
):
261 die_commit("the repository must not contain subdirectories",
264 if not isinstance(blob
, pygit2
.Blob
):
265 die_commit("not a blob object: {:s}".format(treeobj
),
268 if blob
.size
> max_blob_size
:
269 die_commit("maximum blob size ({:s}) exceeded".format(size_humanize(max_blob_size
)), str(commit
.id))
271 metadata_raw
= repo
[commit
.tree
['.SRCINFO'].id].data
.decode()
272 (metadata
, errors
) = srcinfo
.parse
.parse_srcinfo(metadata_raw
)
274 sys
.stderr
.write("error: The following errors occurred "
275 "when parsing .SRCINFO in commit\n")
276 sys
.stderr
.write("error: {:s}:\n".format(str(commit
.id)))
278 for err
in error
['error']:
279 sys
.stderr
.write("error: line {:d}: {:s}\n".format(error
['line'], err
))
282 metadata_pkgbase
= metadata
['pkgbase']
283 if not re
.match(repo_regex
, metadata_pkgbase
):
284 die_commit('invalid pkgbase: {:s}'.format(metadata_pkgbase
),
287 for pkgname
in set(metadata
['packages'].keys()):
288 pkginfo
= srcinfo
.utils
.get_merged_package(pkgname
, metadata
)
290 for field
in ('pkgver', 'pkgrel', 'pkgname'):
291 if field
not in pkginfo
:
292 die_commit('missing mandatory field: {:s}'.format(field
),
295 if 'epoch' in pkginfo
and not pkginfo
['epoch'].isdigit():
296 die_commit('invalid epoch: {:s}'.format(pkginfo
['epoch']),
299 if not re
.match(r
'[a-z0-9][a-z0-9\.+_-]*$', pkginfo
['pkgname']):
300 die_commit('invalid package name: {:s}'.format(pkginfo
['pkgname']),
303 for field
in ('pkgname', 'pkgdesc', 'url'):
304 if field
in pkginfo
and len(pkginfo
[field
]) > 255:
305 die_commit('{:s} field too long: {:s}'.format(field
, pkginfo
[field
]),
308 for field
in ('install', 'changelog'):
309 if field
in pkginfo
and not pkginfo
[field
] in commit
.tree
:
310 die_commit('missing {:s} file: {:s}'.format(field
, pkginfo
[field
]),
313 for field
in extract_arch_fields(pkginfo
, 'source'):
314 fname
= field
['value']
315 if "://" in fname
or "lp:" in fname
:
317 if fname
not in commit
.tree
:
318 die_commit('missing source file: {:s}'.format(fname
),
322 # Display a warning if .SRCINFO is unchanged.
323 if sha1_old
not in ("0000000000000000000000000000000000000000", sha1_new
):
324 srcinfo_id_old
= repo
[sha1_old
].tree
['.SRCINFO'].id
325 srcinfo_id_new
= repo
[sha1_new
].tree
['.SRCINFO'].id
326 if srcinfo_id_old
== srcinfo_id_new
:
327 warn(".SRCINFO unchanged. The package database will not be updated!")
329 # Read .SRCINFO from the HEAD commit.
330 metadata_raw
= repo
[repo
[sha1_new
].tree
['.SRCINFO'].id].data
.decode()
331 (metadata
, errors
) = srcinfo
.parse
.parse_srcinfo(metadata_raw
)
333 # Ensure that the package base name matches the repository name.
334 metadata_pkgbase
= metadata
['pkgbase']
335 if metadata_pkgbase
!= pkgbase
:
336 die('invalid pkgbase: {:s}, expected {:s}'.format(metadata_pkgbase
, pkgbase
))
338 # Ensure that packages are neither blacklisted nor overwritten.
339 pkgbase
= metadata
['pkgbase']
340 cur
= conn
.execute("SELECT ID FROM PackageBases WHERE Name = ?", [pkgbase
])
342 pkgbase_id
= row
[0] if row
else 0
344 cur
= conn
.execute("SELECT Name FROM PackageBlacklist")
345 blacklist
= [row
[0] for row
in cur
.fetchall()]
347 cur
= conn
.execute("SELECT Name, Repo FROM OfficialProviders")
348 providers
= dict(cur
.fetchall())
350 for pkgname
in srcinfo
.utils
.get_package_names(metadata
):
351 pkginfo
= srcinfo
.utils
.get_merged_package(pkgname
, metadata
)
352 pkgname
= pkginfo
['pkgname']
354 if pkgname
in blacklist
:
355 warn_or_die('package is blacklisted: {:s}'.format(pkgname
))
356 if pkgname
in providers
:
357 repo
= providers
[pkgname
]
358 warn_or_die('package already provided by [{:s}]: {:s}'.format(repo
, pkgname
))
360 cur
= conn
.execute("SELECT COUNT(*) FROM Packages WHERE Name = ? AND " +
361 "PackageBaseID <> ?", [pkgname
, pkgbase_id
])
362 if cur
.fetchone()[0] > 0:
363 die('cannot overwrite package: {:s}'.format(pkgname
))
365 # Store package base details in the database.
366 save_metadata(metadata
, conn
, user
)
368 # Create (or update) a branch with the name of the package base for better
370 repo
.create_reference('refs/heads/' + pkgbase
, sha1_new
, True)
372 # Work around a Git bug: The HEAD ref is not updated when using gitnamespaces.
373 # This can be removed once the bug fix is included in Git mainline. See
374 # http://git.661346.n2.nabble.com/PATCH-receive-pack-Create-a-HEAD-ref-for-ref-namespace-td7632149.html
376 repo
.create_reference('refs/namespaces/' + pkgbase
+ '/HEAD', sha1_new
, True)
378 # Send package update notifications.
379 update_notify(conn
, user
, pkgbase_id
)
381 # Close the database.