| 1 |
#!/usr/bin/env python |
|---|
| 2 |
|
|---|
| 3 |
import os |
|---|
| 4 |
import sys |
|---|
| 5 |
import random |
|---|
| 6 |
import string |
|---|
| 7 |
from datetime import datetime |
|---|
| 8 |
|
|---|
| 9 |
PROGRAM_NAME = 'configure.py' |
|---|
| 10 |
PROGRAM_VERSION = 0.1 |
|---|
| 11 |
|
|---|
| 12 |
BASE_PATH = os.path.abspath(os.path.split(__file__)[0]) |
|---|
| 13 |
SRC_PATH = os.path.join(BASE_PATH, 'local_settings.py.dist') |
|---|
| 14 |
DIST_PATH = os.path.join(BASE_PATH, 'local_settings.py') |
|---|
| 15 |
|
|---|
| 16 |
ACCEPT_CHARS = 'abcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*(-_=+)' |
|---|
| 17 |
|
|---|
| 18 |
def _program_name(): |
|---|
| 19 |
return '%s %s' % (PROGRAM_NAME, PROGRAM_VERSION) |
|---|
| 20 |
|
|---|
| 21 |
def _failed_message(msg=None): |
|---|
| 22 |
msg = msg or '' |
|---|
| 23 |
print '\033[41;37m Error: %s \033[00m' % msg |
|---|
| 24 |
sys.exit(1) |
|---|
| 25 |
|
|---|
| 26 |
def _success_message(msg=None): |
|---|
| 27 |
msg = msg or '' |
|---|
| 28 |
print '\033[36m%s\033[00m' % msg |
|---|
| 29 |
|
|---|
| 30 |
def configure(): |
|---|
| 31 |
""" |
|---|
| 32 |
generate local settings file. |
|---|
| 33 |
""" |
|---|
| 34 |
# Check source file. |
|---|
| 35 |
if not os.path.exists(SRC_PATH) or not os.path.isfile(SRC_PATH): |
|---|
| 36 |
_failed_message("Can't find source file %s" % SRC_PATH) |
|---|
| 37 |
# Setup parameters. |
|---|
| 38 |
parameters = { |
|---|
| 39 |
'GENERATED_BY': _program_name(), |
|---|
| 40 |
'GENERATED_AT': datetime.now().strftime('%Y/%m/%d %H:%M:%S'), |
|---|
| 41 |
'SECRET_KEY': ''.join([random.choice(ACCEPT_CHARS) for x in range(50)]), |
|---|
| 42 |
} |
|---|
| 43 |
# read file. |
|---|
| 44 |
buf = open(SRC_PATH, 'r').read() |
|---|
| 45 |
for k, v in parameters.items(): |
|---|
| 46 |
buf = buf.replace('@%s@' % k, v) |
|---|
| 47 |
# write buffer. |
|---|
| 48 |
fp = open(DIST_PATH, 'w') |
|---|
| 49 |
fp.write(buf) |
|---|
| 50 |
fp.close() |
|---|
| 51 |
_success_message('process complete.') |
|---|
| 52 |
|
|---|
| 53 |
if __name__ == '__main__': |
|---|
| 54 |
configure() |
|---|