introducing the manage.py checkmap command
This commit is contained in:
parent
ef3969a4f5
commit
b314bcae5d
4 changed files with 129 additions and 45 deletions
30
src/c3nav/mapdata/management/commands/checkmap.py
Normal file
30
src/c3nav/mapdata/management/commands/checkmap.py
Normal file
|
@ -0,0 +1,30 @@
|
|||
import os
|
||||
import tempfile
|
||||
|
||||
from django.core.management import call_command
|
||||
from django.core.management.base import BaseCommand
|
||||
from django.db import connections
|
||||
|
||||
|
||||
class Command(BaseCommand):
|
||||
help = 'Check if there are errors in the map package files'
|
||||
|
||||
def add_arguments(self, parser):
|
||||
parser.add_argument('--no-prettify', dest='prettify', action='store_const', const=False, default=True,
|
||||
help='ignore formatting errors')
|
||||
|
||||
def handle(self, *args, **options):
|
||||
print('Creating temporary database for checking…\n')
|
||||
|
||||
_, tmp = tempfile.mkstemp(suffix='.sqlite3', prefix='c3nav-checkmap-')
|
||||
connections.databases['tmpdb'] = {
|
||||
'ENGINE': 'django.db.backends.sqlite3',
|
||||
'NAME': tmp,
|
||||
}
|
||||
|
||||
try:
|
||||
call_command('migrate', database='tmpdb')
|
||||
call_command('loadmap', yes=True)
|
||||
call_command('dumpmap', prettify=options['prettify'], check_only=True)
|
||||
finally:
|
||||
os.remove(tmp)
|
|
@ -1,17 +1,23 @@
|
|||
from django.core.management.base import BaseCommand
|
||||
from django.db import transaction
|
||||
from django.core.management.base import BaseCommand, CommandError
|
||||
|
||||
from ...packageio import write_packages
|
||||
|
||||
|
||||
class Command(BaseCommand):
|
||||
help = 'Dump the map database'
|
||||
help = 'Dump the map database into the map package files'
|
||||
|
||||
def add_arguments(self, parser):
|
||||
parser.add_argument('--no-prettify', dest='prettify', action='store_const', const=False, default=True,
|
||||
help='dont\'t prettify existing files')
|
||||
parser.add_argument('--check-only', action='store_const', const=True, default=False,
|
||||
help='check if there are files to update')
|
||||
|
||||
def handle(self, *args, **options):
|
||||
with transaction.atomic():
|
||||
write_packages(prettify=options['prettify'])
|
||||
print()
|
||||
count = write_packages(prettify=options['prettify'], check_only=options['check_only'])
|
||||
if options['check_only']:
|
||||
if count == 0:
|
||||
print('No errors found!')
|
||||
else:
|
||||
raise CommandError('Found errors in %s file(s)' % count)
|
||||
else:
|
||||
print('%s file(s) affected' % count)
|
||||
|
|
|
@ -5,15 +5,15 @@ from ...packageio import read_packages
|
|||
|
||||
|
||||
class Command(BaseCommand):
|
||||
help = 'Update the map database'
|
||||
help = 'Load the map package files into the database'
|
||||
|
||||
def add_arguments(self, parser):
|
||||
parser.add_argument('-y', action='store_const', const=True, default=False,
|
||||
parser.add_argument('--yes', '-y', action='store_const', const=True, default=False,
|
||||
help='don\'t ask for confirmation')
|
||||
|
||||
def handle(self, *args, **options):
|
||||
with transaction.atomic():
|
||||
read_packages()
|
||||
print()
|
||||
if input('Confirm (y/N): ') != 'y':
|
||||
if not options['yes'] and input('Confirm (y/N): ') != 'y':
|
||||
raise CommandError('Aborted.')
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue