git-update: Move entry point to a main() method

Move the main program logic of git-update to a main() method such that
it can be used as a module and easily be invoked by setuptools wrapper
scripts.

Signed-off-by: Lukas Fleischer <lfleischer@archlinux.org>
This commit is contained in:
Lukas Fleischer 2016-09-20 09:08:33 +02:00
parent 8468b6be4b
commit 1946486a67

View file

@ -233,6 +233,7 @@ def die_commit(msg, commit):
exit(1) exit(1)
def main():
repo = pygit2.Repository(repo_path) repo = pygit2.Repository(repo_path)
user = os.environ.get("AUR_USER") user = os.environ.get("AUR_USER")
@ -242,9 +243,11 @@ warn_or_die = warn if privileged else die
if len(sys.argv) == 2 and sys.argv[1] == "restore": if len(sys.argv) == 2 and sys.argv[1] == "restore":
if 'refs/heads/' + pkgbase not in repo.listall_references(): if 'refs/heads/' + pkgbase not in repo.listall_references():
die('{:s}: repository not found: {:s}'.format(sys.argv[1], pkgbase)) die('{:s}: repository not found: {:s}'.format(sys.argv[1],
pkgbase))
refname = "refs/heads/master" refname = "refs/heads/master"
sha1_old = sha1_new = repo.lookup_reference('refs/heads/' + pkgbase).target branchref = 'refs/heads/' + pkgbase
sha1_old = sha1_new = repo.lookup_reference(branchref).target
elif len(sys.argv) == 4: elif len(sys.argv) == 4:
refname, sha1_old, sha1_new = sys.argv[1:4] refname, sha1_old, sha1_new = sys.argv[1:4]
else: else:
@ -256,7 +259,7 @@ if refname != "refs/heads/master":
conn = db.Connection() conn = db.Connection()
# Detect and deny non-fast-forwards. # Detect and deny non-fast-forwards.
if sha1_old != "0000000000000000000000000000000000000000" and not privileged: if sha1_old != "0" * 40 and not privileged:
walker = repo.walk(sha1_old, pygit2.GIT_SORT_TOPOLOGICAL) walker = repo.walk(sha1_old, pygit2.GIT_SORT_TOPOLOGICAL)
walker.hide(sha1_new) walker.hide(sha1_new)
if next(walker, None) is not None: if next(walker, None) is not None:
@ -264,7 +267,7 @@ if sha1_old != "0000000000000000000000000000000000000000" and not privileged:
# Prepare the walker that validates new commits. # Prepare the walker that validates new commits.
walker = repo.walk(sha1_new, pygit2.GIT_SORT_TOPOLOGICAL) walker = repo.walk(sha1_new, pygit2.GIT_SORT_TOPOLOGICAL)
if sha1_old != "0000000000000000000000000000000000000000": if sha1_old != "0" * 40:
walker.hide(sha1_old) walker.hide(sha1_old)
# Validate all new commits. # Validate all new commits.
@ -285,7 +288,8 @@ for commit in walker:
str(commit.id)) str(commit.id))
if blob.size > max_blob_size: if blob.size > max_blob_size:
die_commit("maximum blob size ({:s}) exceeded".format(size_humanize(max_blob_size)), str(commit.id)) die_commit("maximum blob size ({:s}) exceeded".format(
size_humanize(max_blob_size)), str(commit.id))
metadata_raw = repo[commit.tree['.SRCINFO'].id].data.decode() metadata_raw = repo[commit.tree['.SRCINFO'].id].data.decode()
(metadata, errors) = srcinfo.parse.parse_srcinfo(metadata_raw) (metadata, errors) = srcinfo.parse.parse_srcinfo(metadata_raw)
@ -295,7 +299,8 @@ for commit in walker:
sys.stderr.write("error: {:s}:\n".format(str(commit.id))) sys.stderr.write("error: {:s}:\n".format(str(commit.id)))
for error in errors: for error in errors:
for err in error['error']: for err in error['error']:
sys.stderr.write("error: line {:d}: {:s}\n".format(error['line'], err)) sys.stderr.write("error: line {:d}: {:s}\n".format(
error['line'], err))
exit(1) exit(1)
metadata_pkgbase = metadata['pkgbase'] metadata_pkgbase = metadata['pkgbase']
@ -316,18 +321,18 @@ for commit in walker:
str(commit.id)) str(commit.id))
if not re.match(r'[a-z0-9][a-z0-9\.+_-]*$', pkginfo['pkgname']): if not re.match(r'[a-z0-9][a-z0-9\.+_-]*$', pkginfo['pkgname']):
die_commit('invalid package name: {:s}'.format(pkginfo['pkgname']), die_commit('invalid package name: {:s}'.format(
str(commit.id)) pkginfo['pkgname']), str(commit.id))
for field in ('pkgname', 'pkgdesc', 'url'): for field in ('pkgname', 'pkgdesc', 'url'):
if field in pkginfo and len(pkginfo[field]) > 255: if field in pkginfo and len(pkginfo[field]) > 255:
die_commit('{:s} field too long: {:s}'.format(field, pkginfo[field]), die_commit('{:s} field too long: {:s}'.format(field,
str(commit.id)) pkginfo[field]), str(commit.id))
for field in ('install', 'changelog'): for field in ('install', 'changelog'):
if field in pkginfo and not pkginfo[field] in commit.tree: if field in pkginfo and not pkginfo[field] in commit.tree:
die_commit('missing {:s} file: {:s}'.format(field, pkginfo[field]), die_commit('missing {:s} file: {:s}'.format(field,
str(commit.id)) pkginfo[field]), str(commit.id))
for field in extract_arch_fields(pkginfo, 'source'): for field in extract_arch_fields(pkginfo, 'source'):
fname = field['value'] fname = field['value']
@ -337,13 +342,13 @@ for commit in walker:
die_commit('missing source file: {:s}'.format(fname), die_commit('missing source file: {:s}'.format(fname),
str(commit.id)) str(commit.id))
# Display a warning if .SRCINFO is unchanged. # Display a warning if .SRCINFO is unchanged.
if sha1_old not in ("0000000000000000000000000000000000000000", sha1_new): if sha1_old not in ("0000000000000000000000000000000000000000", sha1_new):
srcinfo_id_old = repo[sha1_old].tree['.SRCINFO'].id srcinfo_id_old = repo[sha1_old].tree['.SRCINFO'].id
srcinfo_id_new = repo[sha1_new].tree['.SRCINFO'].id srcinfo_id_new = repo[sha1_new].tree['.SRCINFO'].id
if srcinfo_id_old == srcinfo_id_new: if srcinfo_id_old == srcinfo_id_new:
warn(".SRCINFO unchanged. The package database will not be updated!") warn(".SRCINFO unchanged. "
"The package database will not be updated!")
# Read .SRCINFO from the HEAD commit. # Read .SRCINFO from the HEAD commit.
metadata_raw = repo[repo[sha1_new].tree['.SRCINFO'].id].data.decode() metadata_raw = repo[repo[sha1_new].tree['.SRCINFO'].id].data.decode()
@ -352,7 +357,8 @@ metadata_raw = repo[repo[sha1_new].tree['.SRCINFO'].id].data.decode()
# Ensure that the package base name matches the repository name. # Ensure that the package base name matches the repository name.
metadata_pkgbase = metadata['pkgbase'] metadata_pkgbase = metadata['pkgbase']
if metadata_pkgbase != pkgbase: if metadata_pkgbase != pkgbase:
die('invalid pkgbase: {:s}, expected {:s}'.format(metadata_pkgbase, pkgbase)) die('invalid pkgbase: {:s}, expected {:s}'.format(metadata_pkgbase,
pkgbase))
# Ensure that packages are neither blacklisted nor overwritten. # Ensure that packages are neither blacklisted nor overwritten.
pkgbase = metadata['pkgbase'] pkgbase = metadata['pkgbase']
@ -373,10 +379,11 @@ for pkgname in srcinfo.utils.get_package_names(metadata):
if pkgname in blacklist: if pkgname in blacklist:
warn_or_die('package is blacklisted: {:s}'.format(pkgname)) warn_or_die('package is blacklisted: {:s}'.format(pkgname))
if pkgname in providers: if pkgname in providers:
warn_or_die('package already provided by [{:s}]: {:s}'.format(providers[pkgname], pkgname)) warn_or_die('package already provided by [{:s}]: {:s}'.format(
providers[pkgname], pkgname))
cur = conn.execute("SELECT COUNT(*) FROM Packages WHERE Name = ? AND " + cur = conn.execute("SELECT COUNT(*) FROM Packages WHERE Name = ? " +
"PackageBaseID <> ?", [pkgname, pkgbase_id]) "AND PackageBaseID <> ?", [pkgname, pkgbase_id])
if cur.fetchone()[0] > 0: if cur.fetchone()[0] > 0:
die('cannot overwrite package: {:s}'.format(pkgname)) die('cannot overwrite package: {:s}'.format(pkgname))
@ -389,13 +396,16 @@ save_metadata(metadata, conn, user)
# Create (or update) a branch with the name of the package base for better # Create (or update) a branch with the name of the package base for better
# accessibility. # accessibility.
repo.create_reference('refs/heads/' + pkgbase, sha1_new, True) branchref = 'refs/heads/' + pkgbase
repo.create_reference(branchref, sha1_new, True)
# Work around a Git bug: The HEAD ref is not updated when using gitnamespaces. # Work around a Git bug: The HEAD ref is not updated when using
# This can be removed once the bug fix is included in Git mainline. See # gitnamespaces. This can be removed once the bug fix is included in Git
# mainline. See
# http://git.661346.n2.nabble.com/PATCH-receive-pack-Create-a-HEAD-ref-for-ref-namespace-td7632149.html # http://git.661346.n2.nabble.com/PATCH-receive-pack-Create-a-HEAD-ref-for-ref-namespace-td7632149.html
# for details. # for details.
repo.create_reference('refs/namespaces/' + pkgbase + '/HEAD', sha1_new, True) headref = 'refs/namespaces/' + pkgbase + '/HEAD'
repo.create_reference(headref, sha1_new, True)
# Send package update notifications. # Send package update notifications.
update_notify(conn, user, pkgbase_id) update_notify(conn, user, pkgbase_id)
@ -403,3 +413,7 @@ update_notify(conn, user, pkgbase_id)
# Close the database. # Close the database.
cur.close() cur.close()
conn.close() conn.close()
if __name__ == '__main__':
main()