2017-12-19 17:03:51 +01:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
2018-05-22 23:51:23 +02:00
|
|
|
from flask_script import Manager, Shell, Command, Server
|
2017-12-19 17:03:51 +01:00
|
|
|
from app import db, create_app
|
2020-11-07 21:00:06 +01:00
|
|
|
from flask_migrate import Migrate, MigrateCommand
|
2017-12-19 17:03:51 +01:00
|
|
|
|
|
|
|
app = create_app()
|
2018-05-30 02:32:31 +02:00
|
|
|
manager = Manager(app)
|
2020-11-07 21:00:06 +01:00
|
|
|
migrate = Migrate(app, db)
|
|
|
|
manager.add_command("db", MigrateCommand)
|
|
|
|
|
2017-12-19 17:03:51 +01:00
|
|
|
def main():
|
|
|
|
manager.add_command('shell', Shell(make_context=lambda:{"app":app, "db":db}))
|
2018-05-22 23:51:23 +02:00
|
|
|
manager.add_command("runserver", Server())
|
2017-12-19 17:03:51 +01:00
|
|
|
manager.run()
|
|
|
|
|
2018-05-30 02:32:31 +02:00
|
|
|
@manager.add_command
|
2017-12-19 17:03:51 +01:00
|
|
|
class Update(Command):
|
2018-05-30 02:32:31 +02:00
|
|
|
name = "update"
|
|
|
|
capture_all_args = True
|
2017-12-19 17:03:51 +01:00
|
|
|
|
2018-05-30 02:32:31 +02:00
|
|
|
def run(self, args=None):
|
2017-12-19 17:03:51 +01:00
|
|
|
|
2018-11-05 17:17:03 +01:00
|
|
|
valid_what = ["applists", "appci", "pr", "appobservatory"]
|
2018-05-30 02:32:31 +02:00
|
|
|
what = args[0] if args else None
|
|
|
|
assert what in valid_what, "Please specify what to update among %s" % ', '.join(valid_what)
|
2017-12-19 17:03:51 +01:00
|
|
|
|
2018-11-05 17:17:03 +01:00
|
|
|
if what == "applists":
|
|
|
|
from app.models.applists import AppList
|
|
|
|
AppList.update()
|
|
|
|
elif what == "appci":
|
2018-01-29 03:39:04 +01:00
|
|
|
from app.models.appci import AppCI
|
|
|
|
AppCI.update()
|
2018-05-30 02:32:31 +02:00
|
|
|
elif what == "pr":
|
2018-01-29 03:39:04 +01:00
|
|
|
from app.models.pr import Repo
|
|
|
|
for repo in Repo.query.all():
|
|
|
|
repo.update()
|
2018-05-30 02:32:31 +02:00
|
|
|
elif what == "appobservatory":
|
|
|
|
from app.models.unlistedapps import UnlistedApp
|
|
|
|
UnlistedApp.update()
|
2018-01-29 03:39:04 +01:00
|
|
|
else:
|
|
|
|
pass
|
2017-12-19 17:03:51 +01:00
|
|
|
|
|
|
|
|
2018-05-30 02:32:31 +02:00
|
|
|
@manager.add_command
|
2017-12-19 17:03:51 +01:00
|
|
|
class Nuke(Command):
|
|
|
|
"""Nuke the database (except the platform table)"""
|
2018-05-30 02:32:31 +02:00
|
|
|
name = "nuke"
|
2017-12-19 17:03:51 +01:00
|
|
|
|
|
|
|
def run(self):
|
|
|
|
|
2018-11-05 17:17:03 +01:00
|
|
|
import app.models.applists
|
2018-01-29 03:39:04 +01:00
|
|
|
import app.models.appci
|
|
|
|
import app.models.pr
|
2018-05-30 02:32:31 +02:00
|
|
|
import app.models.unlistedapps
|
2018-01-29 03:39:04 +01:00
|
|
|
|
2017-12-19 17:03:51 +01:00
|
|
|
print("> Droping tables...")
|
2018-05-30 02:32:31 +02:00
|
|
|
db.drop_all()
|
2017-12-19 17:03:51 +01:00
|
|
|
print("> Creating tables...")
|
2018-05-30 02:32:31 +02:00
|
|
|
db.create_all()
|
2017-12-19 17:03:51 +01:00
|
|
|
print("> Comitting sessions...")
|
2018-05-30 02:32:31 +02:00
|
|
|
db.session.commit()
|
2017-12-19 17:03:51 +01:00
|
|
|
|
2018-01-29 03:39:04 +01:00
|
|
|
|
2018-05-30 02:32:31 +02:00
|
|
|
@manager.add_command
|
2017-12-19 17:03:51 +01:00
|
|
|
class Init(Command):
|
2018-05-30 02:32:31 +02:00
|
|
|
name = "init"
|
2017-12-19 17:03:51 +01:00
|
|
|
|
|
|
|
def run(self):
|
|
|
|
import app.models
|
|
|
|
|
2018-01-29 03:39:04 +01:00
|
|
|
# Black magic to extract list of models from 'models' folder
|
|
|
|
submodules = [ app.models.__dict__.get(m) for m in dir(app.models) if not m.startswith('__') ]
|
|
|
|
stuff = []
|
|
|
|
for submodule in submodules:
|
|
|
|
stuff.extend([submodule.__dict__.get(s) for s in dir(submodule)])
|
|
|
|
models = [s for s in stuff if isinstance(s, type(db.Model))]
|
2018-11-05 17:17:03 +01:00
|
|
|
models = set(models)
|
2017-12-19 17:03:51 +01:00
|
|
|
|
|
|
|
for model in models:
|
|
|
|
objs = model.init()
|
|
|
|
if objs is not None:
|
|
|
|
print("> Feeding model %s with init data" % str(model.__name__))
|
|
|
|
for obj in model.init():
|
|
|
|
db.session.add(obj)
|
|
|
|
db.session.commit()
|
|
|
|
|
2018-01-29 03:39:04 +01:00
|
|
|
|
2017-12-19 17:03:51 +01:00
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|