#!/usr/bin/python
# coding:utf-8
# author:Xueping.Lau
import sys
import os,shutil
import build
import time
import zipfile
import AutoGit
import webUpdate
from string import Template
from setting import pack_id_list
from setting import project_cover_path_list
from setting import project_path_list
from setting import project_zip_name_list
from setting import git_work_path
from setting import web_courseware_name_list
from setting import web_courseware_git_list
from setting import web_courseware_year_list
from setting import web_courseware_quarter_list
from setting import web_courseware_subject_list
from setting import web_courseware_class_list
def getPackConfig(inx):
file = './config.txt'
with open(file, "r", encoding="utf-8") as f:
for line in f:
if str(inx) in line:
print(str(inx),line)
return line
return ''
def zip_pack(_path,outZip,build_path):
z = zipfile.ZipFile(outZip,'w',zipfile.ZIP_DEFLATED)
for dirpath, dirnames, filenames in os.walk(_path):
fpath = dirpath.replace(build_path,'')
fpath = fpath and fpath + os.sep or ''
for filename in filenames:
z.write(os.path.join(dirpath, filename),fpath+filename)
z.close()
def modify_IS_TEACHER(old_str,new_str,project_path):
file = project_path + '/assets/scripts/Data/ConstValue.ts'
file_data = ""
with open(file, "r", encoding="utf-8") as f:
for line in f:
if old_str in line:
line = line.replace(old_str,new_str)
else:
line = line.replace(new_str,new_str)
file_data += line
with open(file,"w",encoding="utf-8") as f:
f.write(file_data)
def getSceneUUID(strScene,project_path):
file = project_path + '/assets/scene/' + strScene + '.meta'
with open(file, "r", encoding="utf-8") as f:
for line in f:
if '"uuid":' in line:
uidArr = line.split(':')
uid = uidArr[1].replace(',','')
return uid
def buildPack(_project_cover_path,_project_path,_project_zip_name):
zip_name = _project_zip_name + '.zip'
build_path = _project_path + '/build'
student_path = build_path + '/student'
teacher_path = build_path + '/teacher'
try:
if os.path.exists(build_path):
shutil.rmtree(build_path)
os.makedirs(build_path + '/' + _project_zip_name + '/' + _project_zip_name)
old_str = 'public static readonly IS_TEACHER = false'
new_str = 'public static readonly IS_TEACHER = true'
modify_IS_TEACHER(old_str,new_str,_project_path)
build.buildScene(getSceneUUID('Teacher.fire',_project_path),_project_path)
os.rename(build_path + '/web-mobile', teacher_path)
shutil.copy(_project_cover_path,teacher_path)
print('教师端打包完成。。。')
#代码执行速度快了,有时候会找不到文件或目录所以sleep,待优化
time.sleep(3)
old_str = 'public static readonly IS_TEACHER = true'
new_str = 'public static readonly IS_TEACHER = false'
modify_IS_TEACHER(old_str,new_str,_project_path)
build.buildScene(getSceneUUID('Student.fire',_project_path),_project_path)
os.rename(build_path + '/web-mobile', student_path)
shutil.copy(_project_cover_path,student_path)
subPath = build_path + '/' + _project_zip_name
print('学生端打包完成。。。')
if not os.path.exists(subPath + '/student'):
os.makedirs(subPath + '/student')
if os.path.exists(student_path):
shutil.rmtree(subPath)
shutil.copytree(student_path,subPath + '/student')
zip_pack(subPath + '/student',build_path + '/' + zip_name,build_path)
#shutil.make_archive(subPath + '/student','zip',subPath)
shutil.rmtree(subPath)
if not os.path.exists(subPath):
os.makedirs(subPath)
shutil.move(teacher_path,subPath)
shutil.move(student_path,subPath)
zip_pack(build_path,_project_path + '/' + zip_name,build_path)
#可尝试优化,启用双线程并行同时打包教师端学生端,节省时间
#zip_pack()
time.sleep(2)
print('压缩包完成。。。')
zip_path = _project_path + '/' + zip_name
command_template = Template('cd "$zip_path"')
command = command_template.substitute(zip_path=zip_path)
os.system(command)
#shutil.copy(zip_path,os.getcwd())
time.sleep(1)
command_template = Template('zip -d $zipName "$delFile"')
#command = command_template.substitute(zipName=_project_zip_name,delFile=zip_name)
#os.system(command)
#下面两行其实可以不写,压缩包里面貌似没有__MACOSX
command = command_template.substitute(zipName=_project_zip_name,delFile="__MACOSX*")
os.system(command)
print('压缩包清理完成。。。')
shutil.copy(zip_path,git_work_path + '/dist/static/gameZip')
print('拷贝压缩包到Git完成。。。')
except Exception as e:
print(e)
sys.exit(1)
def main():
for x in range(len(pack_id_list)):
l = getPackConfig(pack_id_list[x])
if l != '':
line = l.split(',')
project_path_list.append(line[1])
project_cover_path_list.append(line[2])
project_zip_name_list.append(line[3])
web_courseware_name_list.append(line[4])
web_courseware_git_list.append(line[5])
web_courseware_year_list.append(line[6])
web_courseware_quarter_list.append(line[7])
web_courseware_subject_list.append(line[8])
web_courseware_class_list.append(line[9])
if len(project_cover_path_list) <= 0:
print('ID配置表里没有找到。。。')
return
for x in range(len(project_cover_path_list)):
print('开始打包第' + str(x + 1) + '个。。。')
buildPack(project_cover_path_list[x],project_path_list[x],project_zip_name_list[x])
#可以优化多线程同时打包
AutoGit.gitCommit()
print('git 上传完成了。。。')
webUpdate.webOpen(project_zip_name_list,web_courseware_name_list,web_courseware_git_list,
web_courseware_year_list,web_courseware_quarter_list,web_courseware_subject_list,
web_courseware_class_list)
sys.exit(0)
if __name__ == '__main__':
main()