#!/usr/bin/python
# coding:utf-8
import hashlib
import os, sys
import zipfile
print("\n ======== Start hash resource ======== \n")
#計算md5值
def CalcMD5(filepath):
with open(filepath,'rb') as f:
md5obj = hashlib.md5()
md5obj.update(f.read())
hash = md5obj.hexdigest()
return hash
#給文件名加hash值
def createHashName(oldFileName):
hash = CalcMD5(oldFileName)
if (oldFileName.endswith('dart.js')):
return oldFileName.replace('dart.js',hash+'.dart.js')
if (oldFileName.endswith('part.js')):
return oldFileName.replace('part.js',hash+'.part.js')
# 查看當前工作目錄
currentPath = os.getcwd()
# 修改當前工作目錄
path = "./web"
os.chdir( path )
webPath = os.getcwd()
print("找到.js結尾的文件並拼接hash")
jsFileList = []
jsFileHashMap = {}
pathList = os.listdir(webPath)
for path in pathList:
if (path.endswith("dart.js") | path.endswith("part.js")):
jsFileList.append(path)
jsFileHashMap[path] = createHashName(path)
print(jsFileHashMap)
#檢查文件,替換文件中的一些文件引用
def checkFile(file):
with open(file, "r") as f1,open("%s.bak" % file, "w") as f2:
for line in f1:
for old_str in jsFileList:
if old_str in line:
if old_str == 'main.dart.js':
if 'part.js' in line:
continue
new_str = jsFileHashMap[old_str]
line = line.replace(old_str, new_str)
lastLine = line
f2.write(line)
os.remove(file)
os.rename("%s.bak" % file, file)
print("整理main.dart.js")
mainDartJs = 'main.dart.js'
checkFile(mainDartJs)
print("整理flutter_service_worker.js")
flutterServiceWorkerJs = 'flutter_service_worker.js'
checkFile(flutterServiceWorkerJs)
print("整理index.html")
indexHtml = 'index.html'
checkFile(indexHtml)
print("將part.js的引用手動添加到index.html中")
partJsFile = []
for jsFile in jsFileList:
if (jsFile.endswith('part.js')):
partJsFile.append(jsFileHashMap[jsFile])
with open(indexHtml, "r") as f1,open("%s.bak" % indexHtml, "w") as f2:
for line in f1:
if "js/plugin.js" in line :
f2.write(line)
for partFile in partJsFile:
f2.write("<script src=\"%s\" type=\"text/javascript\"></script>\n" % partFile)
continue
f2.write(line)
os.remove(indexHtml)
os.rename("%s.bak" % indexHtml, indexHtml)
print("更改文件名")
for jsFile in jsFileList :
newFile = jsFileHashMap[jsFile]
os.rename(jsFile,newFile)
print("返回上層文件夾,更改web文件爲static-meeting-manager並壓縮")
managerFile = "static-meeting-manager"
os.chdir('..')
os.rename('web',managerFile)
zipFile = "static-meeting-manager.zip"
if (os.path.exists(zipFile)):
os.remove(zipFile)
zip = zipfile.ZipFile(zipFile,"w")
for path,dirnames,filenames in os.walk(managerFile):
zip.write(path)
for filename in filenames:
zip.write(os.path.join(path,filename))
zip.close()
print("\n========= 操作完成 ===========\n")
壓縮main.dart.js 腳本
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.