Restructured everything.
This commit is contained in:
90
bin/setup.py
Executable file
90
bin/setup.py
Executable file
@@ -0,0 +1,90 @@
|
||||
#!/usr/bin/env python
|
||||
# -*- coding: utf-8 -*-
|
||||
from __future__ import unicode_literals
|
||||
import argparse
|
||||
import os
|
||||
import shutil
|
||||
import sys
|
||||
|
||||
|
||||
class Command(object):
|
||||
settings_module_name = 'main'
|
||||
|
||||
@staticmethod
|
||||
def _setup_argparser():
|
||||
kwargs = {
|
||||
'description': 'Setup and configure the django project.',
|
||||
}
|
||||
parser = argparse.ArgumentParser(**kwargs)
|
||||
|
||||
parser.add_argument('--force',
|
||||
action='store_true', dest='force',
|
||||
help='Overwrite existing django settings')
|
||||
return parser
|
||||
|
||||
def _parse_args(self, argv=None):
|
||||
if argv is None:
|
||||
argv = sys.argv[1:]
|
||||
|
||||
return self._argparser.parse_args(argv)
|
||||
|
||||
def _run(self, force=False):
|
||||
settings_module_name = self.settings_module_name
|
||||
settings_file = os.path.join(settings_module_name, 'settings.py')
|
||||
|
||||
if os.path.exists(settings_file):
|
||||
if not force:
|
||||
sys.stderr.write('Django settings file detected. Abort!\n')
|
||||
return os.EX_NOPERM
|
||||
|
||||
if os.path.exists('manage.py') or \
|
||||
os.path.exists(settings_module_name):
|
||||
sys.stdout.write('Replacing django files...\n')
|
||||
if os.path.exists('manage.py'):
|
||||
os.unlink('manage.py')
|
||||
if os.path.exists(settings_module_name):
|
||||
shutil.rmtree(settings_module_name)
|
||||
else:
|
||||
sys.stdout.write('Installing django files...\n')
|
||||
|
||||
cmd = 'django-admin startproject {name} .'.format(name=settings_module_name)
|
||||
exitval = os.system(cmd)
|
||||
if exitval != os.EX_OK:
|
||||
return exitval
|
||||
|
||||
sys.stdout.write('Configure django...\n')
|
||||
add_settings_file = os.path.join('etc', 'django', 'additional_settings.py')
|
||||
with open(settings_file, 'a') as f_out:
|
||||
with open(add_settings_file) as f_in:
|
||||
f_out.write(f_in.read())
|
||||
|
||||
sys.stdout.write('Creating directories...\n')
|
||||
dirs = [
|
||||
os.path.join('var', 'db'),
|
||||
os.path.join('var', 'log'),
|
||||
os.path.join('var', 'www', 'static'),
|
||||
]
|
||||
for d in dirs:
|
||||
sys.stdout.write(' - %s\n' % d)
|
||||
if not os.path.exists(d):
|
||||
os.makedirs(d)
|
||||
|
||||
return os.EX_OK
|
||||
|
||||
def __init__(self):
|
||||
self._argparser = self._setup_argparser()
|
||||
|
||||
def __call__(self, argv=None):
|
||||
cmd_args = self._parse_args(argv)
|
||||
exitval = self._run(cmd_args.force)
|
||||
return exitval
|
||||
|
||||
|
||||
def main():
|
||||
cmd = Command()
|
||||
exitval = cmd()
|
||||
sys.exit(exitval)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
Reference in New Issue
Block a user