-
Notifications
You must be signed in to change notification settings - Fork 40
/
manager
executable file
·93 lines (68 loc) · 2.4 KB
/
manager
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#!/usr/bin/env python
from __future__ import print_function
import os
import sys
import subprocess
import functools
shell = functools.partial(subprocess.call, shell=True)
def django_environ(testing=False):
def decorator(func):
settings_module = 'sampleproject.settings'
def setup_environ():
sys.path.insert(0, 'sampleproject')
os.environ['DJANGO_SETTINGS_MODULE'] = settings_module
print('Using settings: {0}'.format(settings_module))
def create_database():
import django
MAJOR, MINOR = django.VERSION[0], django.VERSION[1]
if MAJOR >= 2 or (MAJOR == 1 and MINOR >= 7):
django.setup()
from django.core.management import call_command
from django.contrib.auth.models import User
if MAJOR >= 2 or (MAJOR == 1 and MINOR >= 7):
call_command('migrate', interactive=False)
else:
call_command('syncdb', interactive=False)
admin, created = User.objects.get_or_create(
username='admin',
is_staff=True,
is_superuser=True
)
if created:
admin.set_password('admin')
admin.save()
print('Superuser created. Login "admin" password "admin"')
@functools.wraps(func)
def wrapper(*args, **kw):
cleanup()
setup_environ()
create_database()
return func(*args, **kw)
return wrapper
return decorator
@django_environ()
def serve(argv):
'''Start django development server'''
from django.core.management import execute_from_command_line
execute_from_command_line(['manage.py', 'runserver'])
def cleanup(*args):
'''Removes temporary files'''
shell('find . -name *.pyc -delete')
shell('find . -name __pycache__ -delete')
shell('find . -name db_test.sqlite -delete')
def pypi(*args):
'''Upload to pypi'''
shell('python setup.py sdist upload')
def help(*args):
'''Show help message and exit'''
print('Available tasks: \n')
for task in tasks:
print(' - {0}: {1}'.format(task.__name__, task.__doc__), file=sys.stderr)
print()
tasks = serve, cleanup, pypi, help
if __name__ == '__main__':
try:
globals()[sys.argv[1]](sys.argv[2:])
except (IndexError, KeyError):
help()
sys.exit(1)