Commit 603b5b5d authored by Lukas Fleischer's avatar Lukas Fleischer
Browse files

Add a main() method to all Python scripts



Move the main program logic of all scripts to main() methods such that
they can be used as modules and easily be invoked by setuptools wrapper
scripts.
Signed-off-by: Lukas Fleischer's avatarLukas Fleischer <lfleischer@archlinux.org>
parent f3fb614f
...@@ -18,43 +18,49 @@ db_path = config.get('aurblup', 'db-path') ...@@ -18,43 +18,49 @@ db_path = config.get('aurblup', 'db-path')
sync_dbs = config.get('aurblup', 'sync-dbs').split(' ') sync_dbs = config.get('aurblup', 'sync-dbs').split(' ')
servers = config.get('aurblup', 'servers').split(' ') servers = config.get('aurblup', 'servers').split(' ')
blacklist = set()
providers = set() def main():
repomap = dict() blacklist = set()
providers = set()
h = pyalpm.Handle("/", db_path) repomap = dict()
for sync_db in sync_dbs:
repo = h.register_syncdb(sync_db, pyalpm.SIG_DATABASE_OPTIONAL) h = pyalpm.Handle("/", db_path)
repo.servers = [server.replace("%s", sync_db) for server in servers] for sync_db in sync_dbs:
t = h.init_transaction() repo = h.register_syncdb(sync_db, pyalpm.SIG_DATABASE_OPTIONAL)
repo.update(False) repo.servers = [server.replace("%s", sync_db) for server in servers]
t.release() t = h.init_transaction()
repo.update(False)
for pkg in repo.pkgcache: t.release()
blacklist.add(pkg.name)
[blacklist.add(x) for x in pkg.replaces] for pkg in repo.pkgcache:
providers.add((pkg.name, pkg.name)) blacklist.add(pkg.name)
repomap[(pkg.name, pkg.name)] = repo.name [blacklist.add(x) for x in pkg.replaces]
for provision in pkg.provides: providers.add((pkg.name, pkg.name))
provisionname = re.sub(r'(<|=|>).*', '', provision) repomap[(pkg.name, pkg.name)] = repo.name
providers.add((pkg.name, provisionname)) for provision in pkg.provides:
repomap[(pkg.name, provisionname)] = repo.name provisionname = re.sub(r'(<|=|>).*', '', provision)
providers.add((pkg.name, provisionname))
db = mysql.connector.connect(host=aur_db_host, user=aur_db_user, repomap[(pkg.name, provisionname)] = repo.name
passwd=aur_db_pass, db=aur_db_name,
unix_socket=aur_db_socket, buffered=True) db = mysql.connector.connect(host=aur_db_host, user=aur_db_user,
cur = db.cursor() passwd=aur_db_pass, db=aur_db_name,
unix_socket=aur_db_socket, buffered=True)
cur.execute("SELECT Name, Provides FROM OfficialProviders") cur = db.cursor()
oldproviders = set(cur.fetchall())
cur.execute("SELECT Name, Provides FROM OfficialProviders")
for pkg, provides in providers.difference(oldproviders): oldproviders = set(cur.fetchall())
repo = repomap[(pkg, provides)]
cur.execute("INSERT INTO OfficialProviders (Name, Repo, Provides) " for pkg, provides in providers.difference(oldproviders):
"VALUES (%s, %s, %s)", [pkg, repo, provides]) repo = repomap[(pkg, provides)]
for pkg, provides in oldproviders.difference(providers): cur.execute("INSERT INTO OfficialProviders (Name, Repo, Provides) "
cur.execute("DELETE FROM OfficialProviders " "VALUES (%s, %s, %s)", [pkg, repo, provides])
"WHERE Name = %s AND Provides = %s", [pkg, provides]) for pkg, provides in oldproviders.difference(providers):
cur.execute("DELETE FROM OfficialProviders "
db.commit() "WHERE Name = %s AND Provides = %s", [pkg, provides])
db.close()
db.commit()
db.close()
if __name__ == '__main__':
main()
...@@ -17,25 +17,33 @@ aur_db_user = config.get('database', 'user') ...@@ -17,25 +17,33 @@ aur_db_user = config.get('database', 'user')
aur_db_pass = config.get('database', 'password') aur_db_pass = config.get('database', 'password')
aur_db_socket = config.get('database', 'socket') aur_db_socket = config.get('database', 'socket')
db = mysql.connector.connect(host=aur_db_host, user=aur_db_user,
passwd=aur_db_pass, db=aur_db_name, def main():
unix_socket=aur_db_socket, buffered=True) db = mysql.connector.connect(host=aur_db_host, user=aur_db_user,
cur = db.cursor() passwd=aur_db_pass, db=aur_db_name,
unix_socket=aur_db_socket, buffered=True)
datestr = datetime.datetime.utcnow().strftime("%a, %d %b %Y %H:%M:%S GMT") cur = db.cursor()
pkglist_header = "# AUR package list, generated on " + datestr
pkgbaselist_header = "# AUR package base list, generated on " + datestr datestr = datetime.datetime.utcnow().strftime("%a, %d %b %Y %H:%M:%S GMT")
pkglist_header = "# AUR package list, generated on " + datestr
with gzip.open(docroot + "packages.gz", "w") as f: pkgbaselist_header = "# AUR package base list, generated on " + datestr
f.write(bytes(pkglist_header + "\n", "UTF-8"))
cur.execute("SELECT Packages.Name FROM Packages INNER JOIN PackageBases " + with gzip.open(docroot + "packages.gz", "w") as f:
"ON PackageBases.ID = Packages.PackageBaseID " + f.write(bytes(pkglist_header + "\n", "UTF-8"))
"WHERE PackageBases.PackagerUID IS NOT NULL") cur.execute("SELECT Packages.Name FROM Packages " +
f.writelines([bytes(x[0] + "\n", "UTF-8") for x in cur.fetchall()]) "INNER JOIN PackageBases " +
"ON PackageBases.ID = Packages.PackageBaseID " +
with gzip.open(docroot + "pkgbase.gz", "w") as f: "WHERE PackageBases.PackagerUID IS NOT NULL")
f.write(bytes(pkgbaselist_header + "\n", "UTF-8")) f.writelines([bytes(x[0] + "\n", "UTF-8") for x in cur.fetchall()])
cur.execute("SELECT Name FROM PackageBases WHERE PackagerUID IS NOT NULL")
f.writelines([bytes(x[0] + "\n", "UTF-8") for x in cur.fetchall()]) with gzip.open(docroot + "pkgbase.gz", "w") as f:
f.write(bytes(pkgbaselist_header + "\n", "UTF-8"))
db.close() cur.execute("SELECT Name FROM PackageBases " +
"WHERE PackagerUID IS NOT NULL")
f.writelines([bytes(x[0] + "\n", "UTF-8") for x in cur.fetchall()])
db.close()
if __name__ == '__main__':
main()
...@@ -427,7 +427,7 @@ def tu_vote_reminder(cur, vote_id): ...@@ -427,7 +427,7 @@ def tu_vote_reminder(cur, vote_id):
send_notification(to, subject, body, refs) send_notification(to, subject, body, refs)
if __name__ == '__main__': def main():
action = sys.argv[1] action = sys.argv[1]
action_map = { action_map = {
'send-resetkey': send_resetkey, 'send-resetkey': send_resetkey,
...@@ -454,3 +454,7 @@ if __name__ == '__main__': ...@@ -454,3 +454,7 @@ if __name__ == '__main__':
db.commit() db.commit()
db.close() db.close()
if __name__ == '__main__':
main()
...@@ -13,13 +13,20 @@ aur_db_user = config.get('database', 'user') ...@@ -13,13 +13,20 @@ aur_db_user = config.get('database', 'user')
aur_db_pass = config.get('database', 'password') aur_db_pass = config.get('database', 'password')
aur_db_socket = config.get('database', 'socket') aur_db_socket = config.get('database', 'socket')
db = mysql.connector.connect(host=aur_db_host, user=aur_db_user,
passwd=aur_db_pass, db=aur_db_name,
unix_socket=aur_db_socket, buffered=True)
cur = db.cursor()
cur.execute("DELETE FROM PackageBases WHERE " + def main():
"UNIX_TIMESTAMP() - SubmittedTS > 86400 AND PackagerUID IS NULL") db = mysql.connector.connect(host=aur_db_host, user=aur_db_user,
passwd=aur_db_pass, db=aur_db_name,
unix_socket=aur_db_socket, buffered=True)
cur = db.cursor()
db.commit() cur.execute("DELETE FROM PackageBases WHERE " +
db.close() "UNIX_TIMESTAMP() - SubmittedTS > 86400 " +
"AND PackagerUID IS NULL")
db.commit()
db.close()
if __name__ == '__main__':
main()
...@@ -13,18 +13,25 @@ aur_db_user = config.get('database', 'user') ...@@ -13,18 +13,25 @@ aur_db_user = config.get('database', 'user')
aur_db_pass = config.get('database', 'password') aur_db_pass = config.get('database', 'password')
aur_db_socket = config.get('database', 'socket') aur_db_socket = config.get('database', 'socket')
db = mysql.connector.connect(host=aur_db_host, user=aur_db_user,
passwd=aur_db_pass, db=aur_db_name,
unix_socket=aur_db_socket, buffered=True)
cur = db.cursor()
cur.execute("UPDATE PackageBases SET NumVotes = (SELECT COUNT(*) FROM " + def main():
"PackageVotes WHERE PackageVotes.PackageBaseID = PackageBases.ID)") db = mysql.connector.connect(host=aur_db_host, user=aur_db_user,
passwd=aur_db_pass, db=aur_db_name,
unix_socket=aur_db_socket, buffered=True)
cur = db.cursor()
cur.execute("UPDATE PackageBases SET Popularity = (" + cur.execute("UPDATE PackageBases SET NumVotes = (" +
"SELECT COALESCE(SUM(POWER(0.98, (UNIX_TIMESTAMP() - VoteTS) / 86400)), 0.0) " + "SELECT COUNT(*) FROM PackageVotes " +
"FROM PackageVotes WHERE PackageVotes.PackageBaseID = " + "WHERE PackageVotes.PackageBaseID = PackageBases.ID)")
"PackageBases.ID AND NOT VoteTS IS NULL)")
db.commit() cur.execute("UPDATE PackageBases SET Popularity = (" +
db.close() "SELECT COALESCE(SUM(POWER(0.98, (UNIX_TIMESTAMP() - VoteTS) / 86400)), 0.0) " +
"FROM PackageVotes WHERE PackageVotes.PackageBaseID = " +
"PackageBases.ID AND NOT VoteTS IS NULL)")
db.commit()
db.close()
if __name__ == '__main__':
main()
...@@ -16,17 +16,23 @@ aur_db_pass = config.get('database', 'password') ...@@ -16,17 +16,23 @@ aur_db_pass = config.get('database', 'password')
aur_db_socket = config.get('database', 'socket') aur_db_socket = config.get('database', 'socket')
notify_cmd = config.get('notifications', 'notify-cmd') notify_cmd = config.get('notifications', 'notify-cmd')
db = mysql.connector.connect(host=aur_db_host, user=aur_db_user,
passwd=aur_db_pass, db=aur_db_name,
unix_socket=aur_db_socket, buffered=True)
cur = db.cursor()
now = int(time.time()) def main():
filter_from = now + 500 db = mysql.connector.connect(host=aur_db_host, user=aur_db_user,
filter_to = now + 172800 passwd=aur_db_pass, db=aur_db_name,
unix_socket=aur_db_socket, buffered=True)
cur = db.cursor()
cur.execute("SELECT ID FROM TU_VoteInfo WHERE End >= %s AND End <= %s", now = int(time.time())
[filter_from, filter_to]) filter_from = now + 500
filter_to = now + 172800
for vote_id in [row[0] for row in cur.fetchall()]: cur.execute("SELECT ID FROM TU_VoteInfo WHERE End >= %s AND End <= %s",
subprocess.Popen((notify_cmd, 'tu-vote-reminder', str(vote_id))) [filter_from, filter_to])
for vote_id in [row[0] for row in cur.fetchall()]:
subprocess.Popen((notify_cmd, 'tu-vote-reminder', str(vote_id)))
if __name__ == '__main__':
main()
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment