replace flask.ext.script with custom command interface
This commit is contained in:
parent
fc4ccb8083
commit
7bd6181e06
3 changed files with 197 additions and 210 deletions
|
@ -6,10 +6,24 @@ from __future__ import division
|
||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
import app
|
import api
|
||||||
|
import commands
|
||||||
import server
|
import server
|
||||||
|
|
||||||
if len(sys.argv) > 1 and sys.argv[1] == 'server':
|
if len(sys.argv) > 1 and sys.argv[1] == 'server':
|
||||||
server.run()
|
server.run()
|
||||||
else:
|
else:
|
||||||
app.run()
|
names = [c[8:] for c in dir(commands) if c.startswith('command_')]
|
||||||
|
command = sys.argv[1] if len(sys.argv) > 1 else None
|
||||||
|
if command and command in names:
|
||||||
|
getattr(commands, "command_%s"%command)(sys.argv[1:])
|
||||||
|
else:
|
||||||
|
print 'usage: %s [action]' % 'ctl'
|
||||||
|
indent = max([len(command) for command in names]) + 4
|
||||||
|
for command in sorted(names):
|
||||||
|
space = ' ' * (indent - len(command))
|
||||||
|
info = getattr(commands, "command_%s"%command).__doc__.split('\n')
|
||||||
|
info = [' %s%s' % (' ' * indent, i.strip()) for i in info]
|
||||||
|
info = '\n'.join(info).strip()
|
||||||
|
print " %s%s%s"%(command, space, info)
|
||||||
|
sys.exit(1)
|
||||||
|
|
50
oml/app.py
50
oml/app.py
|
@ -1,50 +0,0 @@
|
||||||
# -*- coding: utf-8 -*-
|
|
||||||
# vi:si:et:sw=4:sts=4:ts=4
|
|
||||||
from __future__ import division
|
|
||||||
|
|
||||||
import logging
|
|
||||||
|
|
||||||
|
|
||||||
import settings
|
|
||||||
|
|
||||||
import db
|
|
||||||
import changelog
|
|
||||||
import item.models
|
|
||||||
import user.models
|
|
||||||
import item.person
|
|
||||||
|
|
||||||
import api
|
|
||||||
|
|
||||||
import commands
|
|
||||||
|
|
||||||
'''
|
|
||||||
app = Flask('openmedialibrary', static_folder=settings.static_path)
|
|
||||||
|
|
||||||
|
|
||||||
manager = Manager(app, with_default_commands=False)
|
|
||||||
manager.add_command('release', commands.Release)
|
|
||||||
manager.add_command('debug', commands.Debug)
|
|
||||||
manager.add_command('update', commands.Update)
|
|
||||||
manager.add_command('install_update', commands.InstallUpdate)
|
|
||||||
manager.add_command('start', commands.Start)
|
|
||||||
manager.add_command('stop', commands.Stop)
|
|
||||||
manager.add_command('setup', commands.Setup)
|
|
||||||
manager.add_command('version', commands.Version)
|
|
||||||
manager.add_command('postupdate', commands.PostUpdate)
|
|
||||||
manager.add_command('shell', Shell)
|
|
||||||
manager.add_command('update_static', commands.UpdateStatic)
|
|
||||||
|
|
||||||
@app.route('/')
|
|
||||||
@app.route('/<path:path>')
|
|
||||||
def main(path=None):
|
|
||||||
return app.send_static_file('html/oml.html')
|
|
||||||
|
|
||||||
'''
|
|
||||||
def run():
|
|
||||||
import sys
|
|
||||||
command = sys.argv[1] if len(sys.argv) > 1 else None
|
|
||||||
if command and getattr(commands, "command_%s"%command):
|
|
||||||
getattr(commands, "command_%s"%command)(sys.argv[1:])
|
|
||||||
else:
|
|
||||||
print 'usage: ... fixme'
|
|
||||||
sys.exit(1)
|
|
|
@ -7,8 +7,6 @@ from os.path import join, exists, dirname
|
||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
from flask.ext.script import Command, Option
|
|
||||||
|
|
||||||
import settings
|
import settings
|
||||||
|
|
||||||
root_dir = dirname(settings.base_dir)
|
root_dir = dirname(settings.base_dir)
|
||||||
|
@ -38,64 +36,56 @@ def version(module):
|
||||||
version = settings.release['modules'][module]['version']
|
version = settings.release['modules'][module]['version']
|
||||||
return version
|
return version
|
||||||
|
|
||||||
class Version(Command):
|
def command_version(*args):
|
||||||
"""
|
"""
|
||||||
Print current version
|
Print current version
|
||||||
"""
|
"""
|
||||||
def run(self):
|
|
||||||
print version('openmedialibrary')
|
print version('openmedialibrary')
|
||||||
|
|
||||||
class Debug(Command):
|
def command_debug(*args):
|
||||||
"""
|
"""
|
||||||
Start in debug mode
|
Start in debug mode
|
||||||
"""
|
"""
|
||||||
def run(self):
|
|
||||||
pass
|
pass
|
||||||
|
|
||||||
class Start(Command):
|
def command_start(*args):
|
||||||
"""
|
"""
|
||||||
Start Open Media Libary
|
Start Open Media Libary
|
||||||
"""
|
"""
|
||||||
def run(self):
|
|
||||||
pass
|
pass
|
||||||
|
|
||||||
class Stop(Command):
|
def command_stop(*args):
|
||||||
"""
|
"""
|
||||||
Stop Open Media Libary
|
Stop Open Media Libary
|
||||||
"""
|
"""
|
||||||
def run(self):
|
|
||||||
pass
|
pass
|
||||||
|
|
||||||
class InstallUpdate(Command):
|
def command_install_update(*args):
|
||||||
"""
|
"""
|
||||||
Update to latest development version
|
Install available updates
|
||||||
"""
|
"""
|
||||||
def run(self):
|
|
||||||
import update
|
import update
|
||||||
if not update.install():
|
if not update.install():
|
||||||
print "UPDATE FAILED"
|
print "UPDATE FAILED"
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
class Update(Command):
|
def command_update(*args):
|
||||||
"""
|
"""
|
||||||
Update to latest development version
|
Update to latest development version
|
||||||
"""
|
"""
|
||||||
def run(self):
|
|
||||||
import update
|
import update
|
||||||
if not (update.download() and update.install()):
|
if not (update.download() and update.install()):
|
||||||
print "UPDATE FAILED"
|
print "UPDATE FAILED"
|
||||||
|
|
||||||
class PostUpdate(Command):
|
def command_postupdate(*args):
|
||||||
"""
|
"""
|
||||||
Called by update to fix stuff
|
Called after update with -o old -n new
|
||||||
"""
|
"""
|
||||||
def get_options(self):
|
def run(*args):
|
||||||
return [
|
o, old, n, new = args
|
||||||
Option('-o', '--old', dest='old'),
|
if o != '-o' or n != '-n':
|
||||||
Option('-n', '--new', dest='new'),
|
print 'usage: -o oldversion -n newversion'
|
||||||
]
|
sys.exit(1)
|
||||||
|
|
||||||
def run(self, old, new):
|
|
||||||
if old <= '20140521-65-e14c686' and new > '20140521-65-e14c686':
|
if old <= '20140521-65-e14c686' and new > '20140521-65-e14c686':
|
||||||
if not os.path.exists(settings.db_path):
|
if not os.path.exists(settings.db_path):
|
||||||
r('./ctl', 'setup')
|
r('./ctl', 'setup')
|
||||||
|
@ -117,20 +107,18 @@ class PostUpdate(Command):
|
||||||
import item.models
|
import item.models
|
||||||
item.models.Find.query.filter_by(key='list').delete()
|
item.models.Find.query.filter_by(key='list').delete()
|
||||||
|
|
||||||
class Setup(Command):
|
def command_setup(*args):
|
||||||
"""
|
"""
|
||||||
Setup new node
|
Setup new node
|
||||||
"""
|
"""
|
||||||
def run(self):
|
|
||||||
import setup
|
import setup
|
||||||
setup.create_db()
|
setup.create_db()
|
||||||
setup.create_default_lists()
|
setup.create_default_lists()
|
||||||
|
|
||||||
class UpdateStatic(Command):
|
def command_update_static(*args):
|
||||||
"""
|
"""
|
||||||
Update static files
|
Update static files
|
||||||
"""
|
"""
|
||||||
def run(self):
|
|
||||||
oxjs = os.path.join(settings.static_path, 'oxjs')
|
oxjs = os.path.join(settings.static_path, 'oxjs')
|
||||||
if not os.path.exists(oxjs):
|
if not os.path.exists(oxjs):
|
||||||
r('git', 'clone', 'https://git.0x2620.org/oxjs.git', oxjs)
|
r('git', 'clone', 'https://git.0x2620.org/oxjs.git', oxjs)
|
||||||
|
@ -139,11 +127,10 @@ class UpdateStatic(Command):
|
||||||
r('python2', os.path.join(oxjs, 'tools', 'build', 'build.py'))
|
r('python2', os.path.join(oxjs, 'tools', 'build', 'build.py'))
|
||||||
r('python2', os.path.join(settings.static_path, 'py', 'build.py'))
|
r('python2', os.path.join(settings.static_path, 'py', 'build.py'))
|
||||||
|
|
||||||
class Release(Command):
|
def command_release(*args):
|
||||||
"""
|
"""
|
||||||
Release new version
|
Release new version
|
||||||
"""
|
"""
|
||||||
def run(self):
|
|
||||||
print 'checking...'
|
print 'checking...'
|
||||||
import os
|
import os
|
||||||
import json
|
import json
|
||||||
|
@ -199,3 +186,39 @@ class Release(Command):
|
||||||
with open('updates/release.json', 'w') as fd:
|
with open('updates/release.json', 'w') as fd:
|
||||||
json.dump(release, fd, indent=2)
|
json.dump(release, fd, indent=2)
|
||||||
print 'signed latest release in updates/release.json'
|
print 'signed latest release in updates/release.json'
|
||||||
|
|
||||||
|
def command_shell(*args):
|
||||||
|
'''
|
||||||
|
Runs a Python shell inside the application context.
|
||||||
|
'''
|
||||||
|
context = None
|
||||||
|
banner = 'Open Media Library'
|
||||||
|
|
||||||
|
import db
|
||||||
|
with db.session():
|
||||||
|
# Try BPython
|
||||||
|
try:
|
||||||
|
from bpython import embed
|
||||||
|
embed(banner=banner, locals_=context)
|
||||||
|
return
|
||||||
|
except ImportError:
|
||||||
|
pass
|
||||||
|
|
||||||
|
# Try IPython
|
||||||
|
try:
|
||||||
|
try:
|
||||||
|
# 0.10.x
|
||||||
|
from IPython.Shell import IPShellEmbed
|
||||||
|
ipshell = IPShellEmbed(banner=banner)
|
||||||
|
ipshell(global_ns=dict(), local_ns=context)
|
||||||
|
except ImportError:
|
||||||
|
# 0.12+
|
||||||
|
from IPython import embed
|
||||||
|
embed(banner1=banner, user_ns=context)
|
||||||
|
return
|
||||||
|
except ImportError:
|
||||||
|
pass
|
||||||
|
|
||||||
|
import code
|
||||||
|
# Use basic python shell
|
||||||
|
code.interact(banner, local=context)
|
||||||
|
|
Loading…
Reference in a new issue