python 增量lua文件打包脚本
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 增量lua文件打包脚本相关的知识,希望对你有一定的参考价值。
#!/usr/bin/env python
# coding=utf-8
# Python 2.7.3
import hashlib
import json
import os, sys
def mkdir(_path):
if os.path.exists(_path):
return
if os.path.exists(os.path.dirname(_path)):
os.mkdir(_path)
else:
os.makedirs(_path)
def remove(_path):
if not os.path.exists(_path):
return
if os.path.isfile(_path):
os.remove(_path)
elif os.path.isdir(_path):
shutil.rmtree(_path)
def read_json(_name, _default=None):
if not os.path.exists(_name):
return _default
with open(_name, "r") as f:
data = json.loads(f.read())
return data
def save_json(_data, _name, _human_read=True):
indent = None
sort_keys = None
if _human_read:
indent = 2
sort_keys = True
mkdir(os.path.dirname(_name))
encodedjson = json.dumps(_data, ensure_ascii=False, indent=indent, sort_keys=sort_keys)
with open(_name, "w") as f:
f.write(encodedjson)
return encodedjson
def _list(_path, _type, _format=None, _maxdepth=None):
args = []
args.append("-type")
args.append(_type)
if _maxdepth != None:
args.append("-maxdepth")
args.append(str(_maxdepth))
if _format:
args.append(" -or ".join(["-name \"*.{0}\"".format(i) for i in _format.split("|")]))
command = "(cd %s; find * %s)" %(_path, " ".join(args))
return os.popen(command).read().strip().split("\n")
"""
_format: shtml|css
"""
def file_list(_path, _format=None, _maxdepth=None):
return _list(_path, "f", _format, _maxdepth)
def dir_list(_path, _format=None, _maxdepth=None):
return _list(_path, "d", _format, _maxdepth)
BLOCKSIZE = 65536
def file_hash(_path, _path_weight=False):
if not os.path.exists(_path):
return ""
hasher = hashlib.sha1()
if _path_weight:
hasher.update(_path)
with open(_path, 'rb') as afile:
buf = afile.read(BLOCKSIZE)
while len(buf) > 0:
hasher.update(buf)
buf = afile.read(BLOCKSIZE)
return hasher.hexdigest()
#!/usr/bin/env python
# coding=utf-8
# Python 2.7.3
import os,sys,json
import zipfile
import shutils
HOME = os.path.expanduser("~")
CACHE_ROOT = os.path.join(HOME, ".lua_compile")
LUAC_BIN_PATH = os.path.join(os.environ["QUICK_V3_ROOT"], "quick/bin/mac/luac")
def luac_compile(_src, _dst):
os.system("%s -o %s %s" %(LUAC_BIN_PATH, _dst, _src))
def package(_src, _dst):
cache_path = CACHE_ROOT
shutils.mkdir(cache_path)
hash_file = os.path.join(cache_path, "hash.json")
hash_data = shutils.read_json(hash_file, {})
hash_data_new = {}
shutils.remove(_dst)
dst_zip = zipfile.ZipFile(_dst, "w", zipfile.ZIP_DEFLATED)
for f in shutils.file_list(_src):
if f.startswith(".") or not f.endswith(".lua"):
continue
src_filepath = os.path.abspath(os.path.join(_src, f))
dst_filename = f.replace(".lua", "").replace("/", ".")
dst_filepath = os.path.abspath(os.path.join(cache_path, dst_filename))
src_hash = shutils.file_hash(src_filepath)
dst_hash = shutils.file_hash(dst_filepath)
record_hash = hash_data.get(dst_filename, {})
if not (dst_hash and record_hash.get("src", "") == src_hash and record_hash.get("dst", "") == dst_hash):
shutils.remove(dst_filepath)
print "ADD: " + f
luac_compile(src_filepath, dst_filepath)
dst_hash = shutils.file_hash(dst_filepath)
if not dst_hash:
print "\n---"
print "ERROR: " + src_filepath
return False
dst_zip.write(dst_filepath, dst_filename)
hash_data_new[dst_filename] = {"src": src_hash, "dst": dst_hash}
shutils.save_json(hash_data_new, hash_file)
dst_zip.close()
for f in shutils.file_list(cache_path):
if f == "hash.json":
continue
if not hash_data_new.get(f):
print "REMOVE: " + f
os.remove(os.path.join(cache_path, f))
return True
def main():
src_path = sys.argv[1]
dst_path = sys.argv[2]
package(src_path, dst_path)
if __name__ == '__main__':
main()
以上是关于python 增量lua文件打包脚本的主要内容,如果未能解决你的问题,请参考以下文章