初始化

This commit is contained in:
SmallMain
2022-06-25 00:23:03 +08:00
commit ef0589e8e5
2264 changed files with 617829 additions and 0 deletions

View File

@@ -0,0 +1,229 @@
#!/usr/bin/python
# ----------------------------------------------------------------------------
# build_console: Build cocos2d-console into executable binary file with PyInstaller
#
# Author: Bin Zhang
#
# License: MIT
# ----------------------------------------------------------------------------
'''
Build cocos2d-console into executable binary file with PyInstaller
'''
import os
import json
import subprocess
import excopy
import ConfigParser
import sys
import shutil
from argparse import ArgumentParser
def run_shell(cmd, cwd=None):
p = subprocess.Popen(cmd, shell=True, cwd=cwd)
p.wait()
if p.returncode:
raise subprocess.CalledProcessError(returncode=p.returncode, cmd=cmd)
return p.returncode
def os_is_win32():
return sys.platform == 'win32'
def os_is_linux():
return 'linux' in sys.platform
class Builder(object):
CONFIG_FILE = "config.json"
KEY_COPY_CONFIG = "copy_config"
KEY_MODIFY_CONFIG = "modify_config"
KEY_HIDDEN_IMPORT = "hidden_import"
ENTRANCE_FILE = "bin/cocos.py"
CMD_FORMAT = 'pyinstaller -F %s %s --distpath "%s" --specpath "%s" --workpath "%s" --clean -y "%s"'
def __init__(self, args):
self.my_path = os.path.realpath(os.path.dirname(__file__))
# get the source path
if args.src_path is None:
src_path = os.path.abspath(os.path.join(self.my_path, os.path.pardir))
else:
src_path = os.path.expanduser(args.src_path)
if os.path.isabs(src_path):
self.src_path = src_path
else:
self.src_path = os.path.abspath(src_path)
if not os.path.isdir(self.src_path):
raise Exception("%s is not a available path." % self.src_path)
self.entrance_file = os.path.join(self.src_path, Builder.ENTRANCE_FILE)
# get the dst path
if args.dst_path is None:
self.dst_path = os.path.abspath("output")
else:
dst_path = os.path.expanduser(args.dst_path)
if os.path.isabs(dst_path):
self.dst_path = dst_path
else:
self.dst_path = os.path.abspath(dst_path)
if os_is_linux():
self.dst_path = os.path.join(self.dst_path, "linux")
else:
self.dst_path = os.path.join(self.dst_path, sys.platform)
# parse config file
cfg_file = os.path.join(self.my_path, Builder.CONFIG_FILE)
f = open(cfg_file)
self.cfg_info = json.load(f)
f.close()
def _get_dirs(self, path, dir_list=None):
if dir_list is None:
dir_list = []
if not os.path.isdir(path):
return dir_list
for name in os.listdir(path):
full_path = os.path.join(path, name)
if os.path.isdir(full_path):
dir_list.append(full_path)
self._get_dirs(full_path, dir_list)
return dir_list
def modify_files(self, modify_info):
import re
modify_file = modify_info["file_path"]
if not os.path.isabs(modify_file):
modify_file = os.path.abspath(os.path.join(self.dst_path, modify_file))
if not os.path.isfile(modify_file):
return
pattern = modify_info["pattern"]
replace_str = modify_info["replace_string"]
f = open(modify_file)
lines = f.readlines()
f.close()
new_lines = []
for line in lines:
new_line = re.sub(pattern, replace_str, line)
new_lines.append(new_line)
f = open(modify_file, "w")
f.writelines(new_lines)
f.close()
def do_build(self):
print("Source Path : %s" % self.src_path)
print("Output Path : %s" % self.dst_path)
print("Start building")
if os.path.exists(self.dst_path):
shutil.rmtree(self.dst_path)
# copy files
copy_config = self.cfg_info[Builder.KEY_COPY_CONFIG]
copy_cfgs = copy_config["common"]
if sys.platform in copy_config:
copy_cfgs += copy_config[sys.platform]
elif os_is_linux():
copy_cfgs += copy_config["linux"]
for element in copy_cfgs:
excopy.copy_files_with_config(element, self.src_path, self.dst_path)
# modify files
modify_config = self.cfg_info[Builder.KEY_MODIFY_CONFIG]
for element in modify_config:
self.modify_files(element)
# get the path parameter
plugins_path = os.path.join(self.src_path, "plugins")
bin_path = os.path.join(self.src_path, "bin")
dir_list = self._get_dirs(plugins_path)
dir_list.append(plugins_path)
dir_list.append(bin_path)
dir_list.append(self.src_path)
if os_is_win32():
sep = ";"
else:
sep = ":"
path_param = "-p %s" % sep.join(dir_list)
# get the runtime-hook parameter
_cp = ConfigParser.ConfigParser(allow_no_value=True)
_cp.optionxform = str
_cp.read(os.path.join(self.src_path, "bin/cocos2d.ini"))
runtime_hook_param = ""
hidden_import_param = ""
# add hidden import params for config.json
if self.cfg_info.has_key(Builder.KEY_HIDDEN_IMPORT):
hidden_import_cfg = self.cfg_info[Builder.KEY_HIDDEN_IMPORT]
else:
hidden_import_cfg = {}
if len(hidden_import_cfg) > 0:
for key in hidden_import_cfg:
hidden_import_param += "--hidden-import %s " % key
runtime_hook_param += '--runtime-hook "%s" ' % os.path.join(self.src_path, hidden_import_cfg[key])
for s in _cp.sections():
if s == 'plugins':
for classname in _cp.options(s):
parts = classname.split(".")
module_name = parts[0]
hidden_import_param += "--hidden-import %s " % module_name
module_path = os.path.join(plugins_path, module_name)
if os.path.isdir(module_path):
runtime_hook_param += '--runtime-hook "%s" ' % ("%s/__init__.py" % module_path)
else:
module_file = "%s.py" % module_path
if os.path.isfile(module_file):
runtime_hook_param += '--runtime-hook "%s" ' % module_file
# additional hooks path
add_hook_dir_param = '--additional-hooks-dir "%s" ' % plugins_path
add_hook_dir_param += '--additional-hooks-dir "%s" ' % bin_path
add_hook_dir_param += '--additional-hooks-dir "%s"' % self.src_path
# build *.py
if os_is_linux():
spec_path = os.path.join(self.my_path, "build", "linux")
else:
spec_path = os.path.join(self.my_path, "build", sys.platform)
work_path = spec_path
if os.path.exists(spec_path):
shutil.rmtree(spec_path)
build_cmd = Builder.CMD_FORMAT % (path_param, '%s %s %s' % (hidden_import_param, add_hook_dir_param, runtime_hook_param), self.dst_path, spec_path, work_path, self.entrance_file)
run_shell(build_cmd)
print("Building succeed.")
if __name__ == "__main__":
parser = ArgumentParser(description="Generate executable file for cocos2d-console by PyInstaller.")
parser.add_argument('-s', '--src-path', dest='src_path', help='Specify the path of cocos2d-console.')
parser.add_argument('-d', '--dst-path', dest='dst_path', help='Specify the path of output.')
(args, unknown) = parser.parse_known_args()
if len(unknown) > 0:
print("unknown arguments: %s" % unknown)
builder = Builder(args)
builder.do_build()

View File

@@ -0,0 +1,117 @@
{
"copy_config" :
{
"common": [
{
"from" : "bin/cocos2d.ini",
"to": ""
},
{
"from" : "bin/strings.json",
"to": ""
},
{
"from": "plugins/plugin_jscompile/bin",
"to": "bin",
"include": [
"compiler.jar"
]
},
{
"from": "plugins/plugin_luacompile/bin",
"to": "bin",
"include": [
"lua/jit/*"
]
},
{
"from": "plugins/plugin_compile/build_web",
"to": "",
"exclude": [
"*.py$",
"*.pyc$"
]
},
{
"from": "plugins/plugin_package/helper/template",
"to": "helper/template"
},
{
"from": "plugins/plugin_generate",
"to": "",
"include": [
"bin-templates",
"configs"
]
}
],
"darwin": [
{
"from": "plugins/plugin_jscompile/bin",
"to": "bin",
"include": [
"jsbcc$"
]
},
{
"from": "plugins/plugin_luacompile/bin",
"to": "bin",
"include": [
"lua/luajit-mac"
]
},
{
"from": "plugins/plugin_run/bin",
"to": "bin"
}
],
"win32": [
{
"from": "plugins/plugin_jscompile/bin",
"to": "bin",
"include": [
"jsbcc.exe",
"*.dll"
]
},
{
"from": "plugins/plugin_luacompile/bin",
"to": "bin",
"include": [
"luajit.exe",
"*.dll"
]
},
{
"from": "plugins/plugin_generate/proj_modifier/plutil-win32",
"to": "plutil-win32"
}
],
"linux": [
{
"from": "plugins/plugin_jscompile/bin",
"to": "bin",
"include": [
"linux/*"
]
},
{
"from": "plugins/plugin_luacompile/bin",
"to": "bin",
"include": [
"lua/luajit-linux"
]
}
]
},
"modify_config" : [
{
"file_path" : "cocos2d.ini",
"pattern": "plugins=.*",
"replace_string": "plugins=\n"
}
],
"hidden_import" : {
"cocos_stat" : "bin/cocos_stat.py"
}
}

View File

@@ -0,0 +1,100 @@
#!/usr/bin/python
# ----------------------------------------------------------------------------
# extend methods for copy files/dirs
#
# Copyright 2014 (C) zhangbin
#
# License: MIT
# ----------------------------------------------------------------------------
import os
import shutil
def copy_files_in_dir(src, dst):
for item in os.listdir(src):
path = os.path.join(src, item)
if os.path.isfile(path):
shutil.copy(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_in_dir(path, new_dst)
def copy_files_with_config(config, src_root, dst_root):
src_dir = config["from"]
dst_dir = config["to"]
src_dir = os.path.join(src_root, src_dir)
dst_dir = os.path.join(dst_root, dst_dir)
include_rules = None
if config.has_key("include"):
include_rules = config["include"]
include_rules = convert_rules(include_rules)
exclude_rules = None
if config.has_key("exclude"):
exclude_rules = config["exclude"]
exclude_rules = convert_rules(exclude_rules)
copy_files_with_rules(src_dir, src_dir, dst_dir, include_rules, exclude_rules)
def copy_files_with_rules(src_rootDir, src, dst, include = None, exclude = None):
if os.path.isfile(src):
if not os.path.exists(dst):
os.makedirs(dst)
shutil.copy(src, dst)
return
if (include is None) and (exclude is None):
if not os.path.exists(dst):
os.makedirs(dst)
copy_files_in_dir(src, dst)
elif (include is not None):
# have include
for name in os.listdir(src):
abs_path = os.path.join(src, name)
rel_path = os.path.relpath(abs_path, src_rootDir)
if os.path.isdir(abs_path):
sub_dst = os.path.join(dst, name)
copy_files_with_rules(src_rootDir, abs_path, sub_dst, include = include)
elif os.path.isfile(abs_path):
if _in_rules(rel_path, include):
if not os.path.exists(dst):
os.makedirs(dst)
shutil.copy(abs_path, dst)
elif (exclude is not None):
# have exclude
for name in os.listdir(src):
abs_path = os.path.join(src, name)
rel_path = os.path.relpath(abs_path, src_rootDir)
if os.path.isdir(abs_path):
sub_dst = os.path.join(dst, name)
copy_files_with_rules(src_rootDir, abs_path, sub_dst, exclude = exclude)
elif os.path.isfile(abs_path):
if not _in_rules(rel_path, exclude):
if not os.path.exists(dst):
os.makedirs(dst)
shutil.copy(abs_path, dst)
def _in_rules(rel_path, rules):
import re
ret = False
path_str = rel_path.replace("\\", "/")
for rule in rules:
if re.match(rule, path_str):
ret = True
return ret
def convert_rules(rules):
ret_rules = []
for rule in rules:
ret = rule.replace('.', '\\.')
ret = ret.replace('*', '.*')
ret = "%s" % ret
ret_rules.append(ret)
return ret_rules