Refactoring of manage.py

Change-Id: I82ef5bc2586e78cc8d0815e5e0db7048e19484f0
This commit is contained in:
Nikolay Markov 2014-01-15 17:43:28 +04:00
parent ffeb9151af
commit f65b4e876c
2 changed files with 142 additions and 83 deletions

View File

@ -15,15 +15,13 @@
# License for the specific language governing permissions and limitations
# under the License.
import __main__
import argparse
import code
import sys
if __name__ == "__main__":
parser = argparse.ArgumentParser()
subparsers = parser.add_subparsers(
dest="action", help='actions'
)
def load_run_parsers(subparsers):
run_parser = subparsers.add_parser(
'run', help='run application locally'
)
@ -59,22 +57,16 @@ if __name__ == "__main__":
'--fake-tasks-tick-interval', action='store', type=int,
help='Fake tasks tick interval in seconds'
)
test_parser = subparsers.add_parser(
'test', help='run unit tests'
)
syncdb_parser = subparsers.add_parser(
def load_db_parsers(subparsers):
subparsers.add_parser(
'syncdb', help='sync application database'
)
dropdb_parser = subparsers.add_parser(
subparsers.add_parser(
'dropdb', help='drop application database'
)
shell_parser = subparsers.add_parser(
'shell', help='open python REPL'
)
shell_parser.add_argument(
'-c', '--config', dest='config_file', action='store', type=str,
help='custom config file', default=None
)
# fixtures
loaddata_parser = subparsers.add_parser(
'loaddata', help='load data from fixture'
)
@ -88,77 +80,144 @@ if __name__ == "__main__":
'model', action='store', help='model name to dump; underscored name'
'should be used, e.g. network_group for NetworkGroup model'
)
loaddefault_parser = subparsers.add_parser(
subparsers.add_parser(
'loaddefault',
help='load data from default fixtures '
'(settings.FIXTURES_TO_IPLOAD)'
)
dump_settings = subparsers.add_parser(
def load_test_parsers(subparsers):
subparsers.add_parser(
'test', help='run unit tests'
)
def load_shell_parsers(subparsers):
shell_parser = subparsers.add_parser(
'shell', help='open python REPL'
)
shell_parser.add_argument(
'-c', '--config', dest='config_file', action='store', type=str,
help='custom config file', default=None
)
def load_settings_parsers(subparsers):
subparsers.add_parser(
'dump_settings', help='dump current settings to YAML'
)
def action_dumpdata(params):
import logging
logging.disable(logging.WARNING)
from nailgun.db.sqlalchemy import fixman
fixman.dump_fixture(params.model)
sys.exit(0)
def action_loaddata(params):
from nailgun.db.sqlalchemy import fixman
from nailgun.logger import logger
logger.info("Uploading fixture...")
with open(params.fixture, "r") as fileobj:
fixman.upload_fixture(fileobj)
logger.info("Done")
def action_loaddefault(params):
from nailgun.db.sqlalchemy import fixman
from nailgun.logger import logger
logger.info("Uploading fixture...")
fixman.upload_fixtures()
logger.info("Done")
def action_syncdb(params):
from nailgun.db import syncdb
from nailgun.logger import logger
logger.info("Syncing database...")
syncdb()
logger.info("Done")
def action_dropdb(params):
from nailgun.db import dropdb
from nailgun.logger import logger
logger.info("Dropping database...")
dropdb()
logger.info("Done")
def action_test(params):
from nailgun.logger import logger
from nailgun.unit_test import TestRunner
logger.info("Running tests...")
TestRunner.run()
logger.info("Done")
def action_dump_settings(params):
from nailgun.settings import settings
sys.stdout.write(settings.dump())
def action_shell(params):
from nailgun.db import db
from nailgun.settings import settings
if params.config_file:
settings.update_from_file(params.config_file)
try:
from IPython import embed
embed()
except ImportError:
code.interact(local={'db': db, 'settings': settings})
def action_run(params):
from nailgun.settings import settings
settings.update({
'LISTEN_PORT': int(params.port),
'LISTEN_ADDRESS': params.address,
})
for attr in ['FAKE_TASKS', 'FAKE_TASKS_TICK_COUNT',
'FAKE_TASKS_TICK_INTERVAL', 'FAKE_TASKS_AMQP']:
param = getattr(params, attr.lower())
if param is not None:
settings.update({attr: param})
if params.config_file:
settings.update_from_file(params.config_file)
from nailgun.wsgi import appstart
appstart(keepalive=params.keepalive)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
subparsers = parser.add_subparsers(
dest="action", help='actions'
)
load_run_parsers(subparsers)
load_db_parsers(subparsers)
load_test_parsers(subparsers)
load_shell_parsers(subparsers)
load_settings_parsers(subparsers)
params, other_params = parser.parse_known_args()
sys.argv.pop(1)
if params.action == "dumpdata":
import logging
logging.disable(logging.WARNING)
from nailgun.db.sqlalchemy import fixman
fixman.dump_fixture(params.model)
sys.exit(0)
from nailgun.logger import logger
from nailgun.settings import settings
if params.action == "syncdb":
logger.info("Syncing database...")
from nailgun.db import syncdb
syncdb()
logger.info("Done")
elif params.action == "dropdb":
logger.info("Dropping database...")
from nailgun.db import dropdb
dropdb()
logger.info("Done")
elif params.action == "test":
logger.info("Running tests...")
from nailgun.unit_test import TestRunner
TestRunner.run()
logger.info("Done")
elif params.action == "loaddata":
logger.info("Uploading fixture...")
from nailgun.db.sqlalchemy import fixman
with open(params.fixture, "r") as fileobj:
fixman.upload_fixture(fileobj)
logger.info("Done")
elif params.action == "loaddefault":
logger.info("Uploading fixture...")
from nailgun.db.sqlalchemy import fixman
fixman.upload_fixtures()
logger.info("Done")
elif params.action == "dump_settings":
sys.stdout.write(settings.dump())
elif params.action in ("run",):
settings.update({
'LISTEN_PORT': int(params.port),
'LISTEN_ADDRESS': params.address,
})
for attr in ['FAKE_TASKS', 'FAKE_TASKS_TICK_COUNT',
'FAKE_TASKS_TICK_INTERVAL', 'FAKE_TASKS_AMQP']:
param = getattr(params, attr.lower())
if param is not None:
settings.update({attr: param})
if params.config_file:
settings.update_from_file(params.config_file)
from nailgun.wsgi import appstart
appstart(keepalive=params.keepalive)
elif params.action == "shell":
from nailgun.db import db
if params.config_file:
settings.update_from_file(params.config_file)
try:
from IPython import embed
embed()
except ImportError:
code.interact(local={'db': db, 'settings': settings})
else:
parser.print_help()
action = getattr(
__main__,
"action_{0}".format(params.action)
)
action(params) if action else parser.print_help()

View File

@ -18,7 +18,7 @@ from datetime import datetime
from unittest import TestCase
from paste.fixture import TestApp
from sqlalchemy.orm.events import orm
from sqlalchemy import orm
from nailgun.db import engine
from nailgun.db import flush