#!/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 脚本
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- VSCode如果运行 不起来一直提示这句话,说明路径错了,但是如果发现就在lib下面 就是提示找不到 请尝试赋值路...
- 更多技术文章请关注:http://www.vimkid.com #!/bin/bash echo "=======...
- 目录 Flutter 探索(一)入门前言 Flutter 探索(二)环境搭建配置 Flutter 探索(三)Dar...
- VSCode编写代码遇到:Dart_LoadScriptFromKernel: The binary progra...