/
cleanServer.py
220 lines (193 loc) · 7.97 KB
/
cleanServer.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#!/usr/bin/python
import os
import py_compile
import shutil
import sys
import subprocess
import datetime
__author__ = 'Gino'
# setting
IGNORE_DIRECTORY = ('.idea', '.git', '.gitignore', '.svn', '.DS_Store', 'clean_deployment', 'cleanServer.py', '*.log')
COMPARE_PYTHON_FILE = ['.py', '.pyc']
KEEP_PYTHON_FILE = ['wsgi']
NG_DIRECTORY = ['static/js']
IGNORE_JS = ['socket.io-1.2.1']
IS_COMPRESS_JS = True
IS_COMPRESS_CSS = True
# copy project function
def copy_project(args):
print("check environment......")
print("check yuicompressor-2.4.8")
try:
import yuicompressor
jar_path = yuicompressor.get_jar_filename()
except ImportError:
jar_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'yuicompressor-2.4.8.jar')
if not os.path.isfile(jar_path):
print('You need to install yuicompressor jar or python lib first.')
sys.exit()
print("yuicompressor is OK")
print("check ng-annotate")
command_min = ['npm', '-g', 'list', '|', 'grep', 'ng-annotate']
try:
op = subprocess.Popen(command_min, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
temp_str = op.communicate()[0]
if temp_str == '' or 'ng-annotate@' not in temp_str:
print('You need to install npm and ng-annotate first')
sys.exit()
except:
print('You need to install npm and ng-annotate first')
sys.exit()
print("ng-annotate is OK")
print("start to duplicate project")
if len(args) == 0:
answer = raw_input('Do you want to deal with current path?(Y/N):')
if answer.lower().startswith("y"):
root = os.getcwd()
clean_deployment = os.path.join(root, 'clean_deployment')
else:
sys.exit()
elif len(args) == 1:
if not os.path.isdir(args[0]):
print('Please give a directory path.')
sys.exit()
else:
root = args[0]
clean_deployment = os.path.join(args[0], 'clean_deployment')
else:
print("You can't give tow or more args.")
sys.exit()
if os.path.isdir(clean_deployment):
answer = raw_input('Do you want to recreate clean_deployment directory?(Y/N):')
if answer.lower().startswith("y"):
print('Remove old clean_deployment directory!')
shutil.rmtree(clean_deployment)
else:
sys.exit()
print('Copy project and create clean_deployment directory...')
shutil.copytree(root, clean_deployment, ignore=shutil.ignore_patterns(*IGNORE_DIRECTORY))
print('finished copy.')
return clean_deployment, jar_path
def ng_annotate(root):
for item in NG_DIRECTORY:
ngroot = os.path.join(root, item)
for ngroot, subdir, files in os.walk(ngroot):
for f in files:
full_path = os.path.join(ngroot, f)
file_name, file_ext = os.path.splitext(f)
if file_ext != '.js':
continue
# ng-annotate file
command_min = ['ng-annotate', '-a', full_path, '-o', full_path]
try:
op = subprocess.Popen(command_min, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
temp_str = op.communicate()[0]
if temp_str != '':
print(temp_str)
print("File: %s is added ng-annotation." % full_path)
except subprocess.CalledProcessError as e:
print("File: %s can't be added ng-annotation. Reason: %s" % (full_path, e.message))
# delete python file and collect js and css files
def delete_py_and_collect_jc(root):
css_collect = []
js_collect = []
count = 0
for root, subdir, files in os.walk(root):
for f in files:
full_path = os.path.join(root, f)
file_name, file_ext = os.path.splitext(f)
if file_ext == COMPARE_PYTHON_FILE[1] and file_name + COMPARE_PYTHON_FILE[0] not in files:
print("Remove invalid pyc: %s" % full_path)
os.remove(full_path)
if file_ext == COMPARE_PYTHON_FILE[0] and file_name not in KEEP_PYTHON_FILE:
try:
py_compile.compile(full_path)
print("File: %s is compiled" % full_path)
os.remove(full_path)
count += 1
print("File: %s is removed" % full_path)
except OSError as e:
print("File: %s can't removed. Reason: %s" % (full_path, e.strerror))
if IS_COMPRESS_CSS and file_ext == '.css' and 'min' not in file_name:
css_collect.append(full_path)
if IS_COMPRESS_JS and file_ext == '.js' and 'min' not in file_name and file_name not in IGNORE_JS:
js_collect.append(full_path)
if not count:
print('No py file is removed.')
return js_collect, css_collect
def readme(root):
readme_file = os.path.join(root, 'README')
if os.path.isfile(readme_file):
f = open(readme_file, 'a')
else:
f = open(readme_file, 'w+')
f.write('\nThis clean_deployment is created at {0:s}'.format(datetime.datetime.now().strftime("%Y.%m.%d.%H.%M.%S")))
f.close()
class Minify():
def __init__(self, js_list, css_list, yui_path):
self.js_list = js_list
self.css_list = css_list
self.yuicompressorpath = yui_path
# compress js
def compress_js(self):
if len(self.js_list):
for temp in self.js_list:
cwd = '/'
temp_split = temp.split(':')
if len(temp_split) == 2:
cwd = temp_split[0] + ":/"
temp = temp_split[1][1:]
command_args = ['java', '-jar', self.yuicompressorpath, temp,
'--type', 'js',
'-o', temp]
try:
op = subprocess.Popen(command_args, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, cwd=cwd)
temp_str = op.communicate()[0]
if temp_str != '':
print(temp_str)
print("File: %s is compressed." % temp)
except subprocess.CalledProcessError as e:
print("File: %s can't compressed. Reason: %s" % (temp, e.strerror))
else:
print('No JS file is compressed.')
# compress css
def compress_css(self):
if len(self.css_list):
for temp in self.css_list:
cwd = '/'
temp_split = temp.split(':')
if len(temp_split) == 2:
cwd = temp_split[0] + ":/"
temp = temp_split[1][1:]
command_args = ['java', '-jar', self.yuicompressorpath, temp,
'--type', 'css',
'-o', temp]
try:
op = subprocess.Popen(command_args, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, cwd=cwd)
temp_str = op.communicate()[0]
if temp_str != '':
print(temp_str)
print("File: %s is compressed." % temp)
except subprocess.CalledProcessError as e:
print("File: %s can't compressed. Reason: %s" % (temp, e.message))
else:
print('No CSS file is compressed.')
# main
if __name__ == "__main__":
pwd, path = copy_project(sys.argv[1:])
# ng-annotation
print('Start to ng-annotate angularjs file ...')
ng_annotate(pwd)
# directory handle and collect files
print('Start to remove %s files ...' % COMPARE_PYTHON_FILE[0])
js, css = delete_py_and_collect_jc(pwd)
# compress files
minify = Minify(js, css, path)
print('Start to compress js files ...')
minify.compress_js()
print('Start to compress css files ...')
minify.compress_css()
# create log write into README
readme(pwd)
print('finished all')
sys.exit()