#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
import os
import hashlib
import subprocess
import gzip
def doGZip(fileSrc,fileOutPut):
f_in = open(fileSrc, 'rb')
f_in_content = f_in.read()
f_in.close()
f_out = gzip.open(fileOutPut, "wb")
f_out.write(f_in_content)
f_out.close()
cachePath = "/Volumes/Data/work/compressedTexturesCache/"
# textureTypes = ["etc1","etc2"]
# # 创建缓存目录
# for textureType in textureTypes:
# if not os.path.exists(cachePath + textureType):
# os.makedirs(cachePath + textureType)
# if not os.path.exists(cachePath + textureType + "/gzip/"):
# os.makedirs(cachePath + textureType + "/gzip/")
fromPath = sys.argv[1]
targetPath = sys.argv[2]
cacheDir = cachePath + sys.argv[4] + "/" + sys.argv[6]
cacheDirGzip = cacheDir + "/gzip/"
if not os.path.exists(cacheDir):
os.makedirs(cacheDir)
if not os.path.exists(cacheDirGzip):
os.makedirs(cacheDirGzip)
fileDir,fileName = os.path.split(fromPath)
baseName=fileName.split(".")[0]
md5 = hashlib.md5(open(fromPath, "rb").read() + fromPath).hexdigest()
pkmUrl=cacheDir + "/" + md5 + ".pkm"
pkmGzipUrl=cacheDirGzip + "/" + md5 + ".pkm"
if not os.path.exists(pkmUrl):
print("开始压缩",fileName, md5)
# 缓存文件不存在, 压缩此图片
res = subprocess.call(["./etcpacktool", fromPath, cacheDir] + sys.argv[3:])
if res == 0:
# 压缩成功, 根据 MD5 重命名
os.rename(cacheDir + "/" + baseName + ".pkm", pkmUrl)
else:
print("文件存在")
# gzip压缩
if not os.path.exists(pkmGzipUrl):
print("开始gzip压缩")
doGZip(pkmUrl,pkmGzipUrl)
# 拷贝文件
status = subprocess.call("cp" + " " + pkmGzipUrl + " " + targetPath + "/" + baseName + ".pkm", shell=True)
if status != 0:
if status < 0:
print("Killed by signal", status)
else:
print("Command failed with return code - ", status)
else:
print("拷贝成功")
分享一个修改过后的脚本,增加功能:
1.添加GZip压缩
2.额外区别压缩模式
3.额外区别资源路径