-
Notifications
You must be signed in to change notification settings - Fork 0
/
djadmin.py
154 lines (116 loc) · 4.21 KB
/
djadmin.py
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# -*- coding:utf8 -*-
"""
Author : Myth
Date : 17/3/7S
Email : belongmyth at 163.com
Wrapped django-admin tool for modify project and app template to add encoding, author, date and so on
"""
import commands
import os
import re
import sys
from ConfigParser import ConfigParser
from datetime import datetime
def ask_info():
AUTHOR = None
EMAIL = None
# interactively to ask user name and user email
print 'Hi, this is your first time use `djadmin`'
author = raw_input(u'What\'s your name: ')
if author:
AUTHOR = author
email = raw_input(u'What\'s your email address: ')
if email:
EMAIL = email
if AUTHOR and EMAIL:
if '@' in EMAIL:
name, at, suffix = EMAIL.partition('@')
EMAIL = '%s at %s' % (name, suffix)
c = ConfigParser()
c.add_section('djadmin')
c.set('djadmin', 'author', AUTHOR)
c.set('djadmin', 'email', EMAIL)
c.write(open(os.path.expanduser('~/.djadmin.conf'), 'w'))
def get_config_parser():
config_file = '~/.djadmin.conf'
if not os.path.isfile(os.path.expanduser(config_file)):
ask_info()
if not os.path.isfile(os.path.expanduser(config_file)):
print 'You must provide user name and user email before you use this tool.'
print 'Re execute the command to provide information.'
sys.exit(-1)
c = ConfigParser()
c.read(os.path.expanduser(config_file))
return c
def get_author():
c = get_config_parser()
return c.get('djadmin', 'author')
def get_email():
c = get_config_parser()
return c.get('djadmin', 'email')
TEMPLATE_BANNER_WITHOUT_ENCODING = '''\
"""
Author : %(author)s
Date : %(now)s
Email : %(email)s
"""''' % {
'author': get_author(),
'email': get_email(),
'now': datetime.now().strftime('%Y/%m/%d')
}
TEMPLATE_BANNER_WITH_ENCODING = '''\
# -*- coding:utf8 -*-
%(template)s''' % {
'template': TEMPLATE_BANNER_WITHOUT_ENCODING,
}
def is_hashbang(line):
return line.startswith('#!')
def is_encoding(line):
p = re.compile('coding:')
return p.search(line) is not None
def insert_banner(param):
directory = os.path.abspath(param)
if os.path.isdir(directory):
for root, dirs, files in os.walk(directory):
for file_ in files:
abs_file = os.path.join(root, file_)
rf = open(abs_file, 'rb')
first_line = rf.readline() # may be hashbang(#!) or encoding declare statement( coding: **** )
second_line = rf.readline() # may be encoding declare statement( coding: **** )
remain_old_content = rf.read()
first_line_write_back = False
second_line_write_back = False
encoding_declared = False
wf = open(abs_file, 'wb')
if is_hashbang(first_line):
wf.write(first_line)
first_line_write_back = True
elif is_encoding(first_line):
wf.write(first_line)
encoding_declared = True
first_line_write_back = True
if is_encoding(second_line):
wf.write(second_line)
second_line_write_back = True
if encoding_declared:
wf.write('%s%s' % (TEMPLATE_BANNER_WITHOUT_ENCODING, os.linesep))
else:
wf.write('%s%s' % (TEMPLATE_BANNER_WITH_ENCODING, os.linesep))
if not first_line_write_back:
wf.write(first_line)
if not second_line_write_back:
wf.write(second_line)
wf.write(remain_old_content)
wf.close()
def main():
ret, output = commands.getstatusoutput('django-admin %s' % ' '.join(sys.argv[1:]))
if output:
print output
if ret == 0: # when command is successfully executed
if len(sys.argv) > 2:
command = sys.argv[1]
param = sys.argv[2]
if command in ('startproject', 'startapp'): # if command is either `startproject` or `startapp`
insert_banner(param) # insert banner at the beginning of the file
if __name__ == '__main__':
main()