修改部分代码格式

This commit is contained in:
virusdefender
2015-09-13 11:47:12 +08:00
parent 34246f7345
commit e6c9916a12
5 changed files with 23 additions and 18 deletions

View File

@@ -20,32 +20,40 @@ shutil.rmtree(static_release_path)
# 复制一份静态文件文件夹到 release
shutil.copytree(static_src_path, static_release_path)
r = re.compile(r'<script src="(.+)"></script>')
js_re = re.compile(r'<script src="(.+)"></script>')
css_re = re.compile(r'<link href="(.+)" rel="stylesheet">')
name_map = {}
def do(match):
js_path = match.group(1).lstrip("/static/")
def process(match):
file_path = match.group(1).replace("/static/", "")
# print file_path, match.group(), match.group(1)
if not os.path.exists(static_release_path + js_path):
if not os.path.exists(static_release_path + file_path):
return match.group(0)
if js_path in name_map:
md5 = name_map[js_path]
if file_path in name_map:
md5 = name_map[file_path]
else:
# rename
md5 = hashlib.md5(open(static_release_path + js_path, "r").read()).hexdigest()
os.rename(static_release_path + js_path, static_release_path + js_path + "?v=" + md5)
return '<script src="%s"></script>' % (js_path + "?v=" + md5,)
md5 = hashlib.md5(open(static_release_path + file_path, "r").read()).hexdigest()
# os.rename(static_release_path + js_path, static_release_path + js_path + "?v=" + md5)
if ".js" in file_path:
return '<script src="/static/%s"></script>' % (file_path + "?v=" + md5[:6], )
elif ".css" in file_path:
return '<link rel="stylesheet" type="text/css" href="/static/%s">' % (file_path + "?v=" + md5[:6], )
else:
return match.group(0)
for root, dirs, files in os.walk(template_release_path):
for name in files:
html_path = os.path.join(root, name)
html_content = open(html_path, "r").read()
replaced_html_content = re.sub(r, do, html_content)
js_replaced_html_content = re.sub(js_re, process, html_content)
css_replaced_html_content = re.sub(css_re, process, js_replaced_html_content)
f = open(html_path, "w")
f.write(replaced_html_content)
f.write(css_replaced_html_content)
f.close()