axmol/tools/framework-compile/bin/utils_cocos.py

188 lines
5.1 KiB
Python
Raw Normal View History

2015-06-15 16:33:24 +08:00
#!/usr/bin/python
#-*- coding: UTF-8 -*-
import os
import sys
import shutil
if sys.platform == 'win32':
2015-06-17 09:53:03 +08:00
import _winreg
2015-06-15 16:33:24 +08:00
def os_is_win32():
return sys.platform == 'win32'
def is_32bit_windows():
arch = os.environ['PROCESSOR_ARCHITECTURE'].lower()
archw = os.environ.has_key("PROCESSOR_ARCHITEW6432")
return (arch == "x86" and not archw)
def os_is_mac():
return sys.platform == 'darwin'
def convert_to_python_path(path):
return path.replace("\\","/")
2015-06-15 16:33:24 +08:00
def execute_command(cmdstring, cwd=None, timeout=None, shell=True):
2015-06-17 09:53:03 +08:00
""" 执行一个SHELL命令
封装了subprocess的Popen方法, 支持超时判断支持读取stdout和stderr
参数:
cwd: 运行命令时更改路径如果被设定子进程会直接先更改当前路径到cwd
timeout: 超时时间支持小数精度0.1
shell: 是否通过shell运行
Returns: return_code
Raises: Exception: 执行超时
"""
import shlex
import datetime
import subprocess
import time
if os_is_win32():
cmdstring = convert_to_python_path(cmdstring)
2015-06-17 09:53:03 +08:00
print("")
print("Execute command:")
print(cmdstring)
print("")
if shell:
cmdstring_list = cmdstring
else:
cmdstring_list = shlex.split(cmdstring)
if timeout:
end_time = datetime.datetime.now() + datetime.timedelta(seconds=timeout)
# 没有指定标准输出和错误输出的管道,因此会打印到屏幕上
sub = None
try:
sub = subprocess.Popen(cmdstring_list, cwd=cwd, stdin=subprocess.PIPE, shell=shell, bufsize=4096)
except Exception as e:
print("execute command fail:%s" % cmdstring)
2015-06-17 09:53:03 +08:00
raise e
# subprocess.poll()方法检查子进程是否结束了如果结束了设定并返回码放在subprocess.returncode变量中
while sub.poll() is None:
time.sleep(0.1)
if timeout:
if end_time <= datetime.datetime.now():
raise Exception("Timeout%s"%cmdstring)
if 0 != sub.returncode :
errStr = "[ERROR] execute command fail:%s" % cmdstring
print(errStr)
2015-06-17 09:53:03 +08:00
raise Exception(errStr)
return sub.returncode
2015-06-15 16:33:24 +08:00
def get_vs_cmd_path(vs_version):
if vs_version == 2013:
vs_ver = "12.0"
elif vs_version == 2015:
vs_ver = "14.0"
else:
# not supported VS version
return None
2015-06-17 09:53:03 +08:00
# If the system is 64bit, find VS in both 32bit & 64bit registry
# If the system is 32bit, only find VS in 32bit registry
if is_32bit_windows():
reg_flag_list = [ _winreg.KEY_WOW64_32KEY ]
2015-06-17 09:53:03 +08:00
else:
reg_flag_list = [ _winreg.KEY_WOW64_64KEY, _winreg.KEY_WOW64_32KEY ]
2015-06-17 09:53:03 +08:00
# Find VS path
vsPath = None
for reg_flag in reg_flag_list:
2015-06-17 09:53:03 +08:00
try:
vs = _winreg.OpenKey(
_winreg.HKEY_LOCAL_MACHINE,
r"SOFTWARE\Microsoft\VisualStudio",
0,
_winreg.KEY_READ | reg_flag
)
key = _winreg.OpenKey(vs, r"SxS\VS7")
2015-06-17 09:53:03 +08:00
vsPath, type = _winreg.QueryValueEx(key, vs_ver)
except:
continue
if vsPath is not None:
break
2015-06-17 09:53:03 +08:00
# generate devenv path
if vsPath is not None:
commandPath = os.path.join(vsPath, "Common7", "IDE", "devenv")
else:
commandPath = None
2015-06-17 09:53:03 +08:00
return commandPath
2015-06-15 16:33:24 +08:00
def rmdir(folder):
2015-06-17 09:53:03 +08:00
if os.path.exists(folder):
if sys.platform == 'win32':
execute_command("rd /s/q %s" % folder)
else:
shutil.rmtree(folder)
2015-06-15 16:33:24 +08:00
def mkdir(folder):
2015-06-17 09:53:03 +08:00
if not os.path.exists(folder):
os.makedirs(folder)
2015-06-15 16:33:24 +08:00
def cpdir(source, dest):
2015-06-17 09:53:03 +08:00
source_dir = source
dest_dir = dest
2015-06-15 16:33:24 +08:00
2015-06-17 09:53:03 +08:00
if not os.path.exists(source_dir):
raise Exception("cpdir source_dir (%s) not exists" % source_dir)
2015-06-15 16:33:24 +08:00
2015-06-17 09:53:03 +08:00
mkdir(dest_dir)
2015-06-15 16:33:24 +08:00
2015-06-17 09:53:03 +08:00
for f in os.listdir(source_dir):
path = os.path.join(source_dir, f)
if os.path.isfile(path):
shutil.copy(path, dest_dir)
elif os.path.isdir(path):
new_dest = os.path.join(dest_dir, f)
cpdir(path, new_dest)
2015-06-15 16:33:24 +08:00
def win2unix(filePath):
2015-06-17 09:53:03 +08:00
try:
oldfile = open(filePath, "rb+")
path, name = os.path.split(filePath)
newfile = open(path + '$' + name, "wb+")
old = b'\r'
new = b''
data = b''
while (True):
data = oldfile.read(200)
newData = data.replace(old, new)
newfile.write(newData)
if len(data) < 200:
break
newfile.close()
oldfile.close()
os.remove(filePath)
os.rename(path + '$' + name, filePath)
except IOError as e:
print(e)
2015-06-15 16:33:24 +08:00
def copy_files_with_cb(src, dst, cb):
2015-06-17 09:53:03 +08:00
for item in os.listdir(src):
path = os.path.join(src, item)
if os.path.isfile(path):
if cb is None:
shutil.copy(path, dst)
else:
cb(path, dst)
if os.path.isdir(path):
new_dst = os.path.join(dst, item)
if not os.path.isdir(new_dst):
os.makedirs(new_dst)
copy_files_with_cb(path, new_dst, cb)
2015-06-15 16:33:24 +08:00