image.png
#!/usr/bin/env python
#-*- coding: utf-8 -*-
import os
import os.path
import sys
import socket
import md5
import json
import zipfile
# nginx服務(wù)器的路徑
nginxServePath = "/usr/local/var/www/"
# weex.json文件目錄
nginxWeexJsonFilePath = nginxServePath + "weex.json"
# js文件夾的路徑流部,即bundle文件夾路徑
jsFileInNginxPath = nginxServePath + "bundle"
# js的zip壓縮包路徑,即bundle.zip文件夾路徑
jsZipFileInNginxPath = nginxServePath + "bundle.zip"
# 圖片所有文件夾路徑
imgFileInNginxPath = nginxServePath + "imgs"
# 圖片壓縮包所有文件夾路徑
imgZipFileInNginxPath = nginxServePath + "imgs.zip"
# 另一種命名,js文件夾的路徑弦叶,即bundle文件夾路徑
jsFileInNginxPath2 = nginxServePath + "dist"
# 另一種命名,js的zip壓縮包路徑,即bundle.zip文件夾路徑
jsZipFileInNginxPath2 = nginxServePath + "dist.zip"
def get_host_ip():
try:
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(('8.8.8.8', 80))
ip = s.getsockname()[0]
finally:
s.close()
return ip
def zip_dir(dirname,zipfilename):
filelist = []
if os.path.isfile(dirname):
filelist.append(dirname)
else :
for root, dirs, files in os.walk(dirname):
for name in files:
filelist.append(os.path.join(root, name))
zf = zipfile.ZipFile(zipfilename, "w", zipfile.zlib.DEFLATED)
for tar in filelist:
arcname = tar[len(dirname):]
zf.write(tar,arcname)
zf.close()
# 刪除之前存在的相關(guān)文件
filePaths = [nginxWeexJsonFilePath, jsFileInNginxPath, jsZipFileInNginxPath, imgFileInNginxPath, imgZipFileInNginxPath, jsFileInNginxPath2, jsZipFileInNginxPath2]
for filePath in filePaths:
if os.path.exists(filePath):
os.system('rm -rf ' + filePath)
# reload(sys)
# sys.setdefaultencoding("utf-8")
pageDicts = []
weexJsonDict={}
jsFileInNginxPaths = []
os.system('weex compile src/ /usr/local/var/www/bundle')
os.system('echo .......... processing ..........')
#檢查incluce文件夾
includeFolderPath = os.path.abspath(os.curdir)+'/src/include'
if os.path.exists(includeFolderPath):
if os.path.exists(jsFileInNginxPath+'/include'):
#檢查遺漏的js文件
f_list = os.listdir(includeFolderPath)
for i in f_list:
if os.path.splitext(i)[1] == '.js':
os.system('cp -r ' + includeFolderPath +'/'+i +' '+ jsFileInNginxPath+'/include/'+i)
else:
#include文件里不包含vue文件
os.system('cp -r ' + includeFolderPath +' '+ jsFileInNginxPath+'/include')
imgsPath = os.path.abspath(os.curdir)+'/src/imgs'
if os.path.exists(imgsPath):
os.system('cp -r ' + imgsPath +' '+ imgFileInNginxPath)
zip_dir(imgsPath,imgZipFileInNginxPath)
for parent,dirnames,filenames in os.walk(jsFileInNginxPath):
for filename in filenames:
path = os.path.join(parent, filename)
if path.endswith('.js'):
jsFileInNginxPaths.append(path)
# 壓縮bundle文件夾為bundle.zip
zip_dir(jsFileInNginxPath,jsFileInNginxPath+'.zip')
#寫入weex.json
if jsFileInNginxPaths.count > 0:
localIP = get_host_ip()
baseUrl = "http://" + localIP + ":8080/"
for jsFileInNginxPath in jsFileInNginxPaths:
jsFileInNginxPaths = jsFileInNginxPath.replace(nginxServePath, baseUrl)
jsFileName = os.path.basename(jsFileInNginxPath)
m = md5.new()
m.update(jsFileInNginxPath)
jsFileMD5 = m.hexdigest()
pageDicts.append({"md5":jsFileMD5,"page":jsFileName,"url":jsFileInNginxPaths})
weexJsonDict["pages"] = pageDicts
weexJsonDict["patch"] = {}
m = md5.new()
m.update(jsFileInNginxPath+'.zip')
bundleZipFileMD5 = m.hexdigest()
weexJsonDict["zip"] = {"md5":bundleZipFileMD5,"url":baseUrl+'bundle.zip'}
jsonStr = json.dumps( weexJsonDict, ensure_ascii=False, encoding='UTF-8')
with open(nginxWeexJsonFilePath, 'wt') as f:
f.write(jsonStr)
os.system('echo .......... done ..........')