typo; more Pythonic

This commit is contained in:
Jerry Zhang 2013-07-23 13:37:17 +08:00
parent 8ead893e72
commit 749e8f96eb
1 changed files with 63 additions and 72 deletions

View File

@ -5,8 +5,12 @@
# Author: WangZhe
# define global variables
context = {}.fromkeys(("language", "src_project_name", "src_package_name", "dst_project_name", "dst_package_name", "src_project_path", "dst_project_path", "script_dir"));
platforms_list = []
PLATFORMS = {
"cpp" : ["ios", "android", "win32", "mac", "blackberry", "linux", "marmalade"],
"lua" : ["ios", "android", "win32", "blackberry", "linux", "marmalade"],
"javascript" : ["ios", "android", "win32"]
}
# begin
import sys
@ -14,7 +18,7 @@ import os, os.path
import json
import shutil
def checkParams(context):
def checkParams():
from optparse import OptionParser
# set the parser to parse input params
@ -26,18 +30,20 @@ def checkParams(context):
metavar="PROGRAMMING_NAME",
type="choice",
choices=["cpp", "lua", "javascript"],
help="Major programing lanauge you want to used, should be [cpp | lua | javascript]")
help="Major programing language you want to used, should be [cpp | lua | javascript]")
#parse the params
(opts, args) = parser.parse_args()
# generate our internal params
context["script_dir"] = os.getcwd() + "/"
global platforms_list
context = {}.fromkeys(("language", "src_project_name", "src_package_name", "dst_project_name", "dst_package_name", "src_project_path", "dst_project_path", "script_dir"))
platforms_list = []
context["script_dir"] = os.path.abspath(os.path.dirname(__file__))
if opts.project:
context["dst_project_name"] = opts.project
context["dst_project_path"] = os.getcwd() + "/../../projects/" + context["dst_project_name"]
context["dst_project_path"] = os.path.abspath(os.path.join(context["script_dir"], "..", "..", "projects", context["dst_project_name"]))
else:
parser.error("-p or --project is not specified")
@ -52,34 +58,21 @@ def checkParams(context):
parser.error("-l or --language is not specified")
# fill in src_project_name and src_package_name according to "language"
template_dir = os.path.abspath(os.path.join(context["script_dir"], "..", "..", "template"))
if ("cpp" == context["language"]):
context["src_project_name"] = "HelloCpp"
context["src_package_name"] = "org.cocos2dx.hellocpp"
context["src_project_path"] = os.getcwd() + "/../../template/multi-platform-cpp"
platforms_list = ["ios",
"android",
"win32",
"mac",
"blackberry",
"linux",
"marmalade"]
context["src_project_path"] = os.path.join(template_dir, "multi-platform-cpp")
elif ("lua" == context["language"]):
context["src_project_name"] = "HelloLua"
context["src_package_name"] = "org.cocos2dx.hellolua"
context["src_project_path"] = os.getcwd() + "/../../template/multi-platform-lua"
platforms_list = ["ios",
"android",
"win32",
"blackberry",
"linux",
"marmalade"]
context["src_project_path"] = os.path.join(template_dir, "multi-platform-lua")
elif ("javascript" == context["language"]):
context["src_project_name"] = "HelloJavascript"
context["src_package_name"] = "org.cocos2dx.hellojavascript"
context["src_project_path"] = os.getcwd() + "/../../template/multi-platform-js"
platforms_list = ["ios",
"android",
"win32"]
context["src_project_path"] = os.path.join(template_dir, "multi-platform-js")
platforms_list = PLATFORMS.get(context["language"], [])
return context, platforms_list
# end of checkParams(context) function
def replaceString(filepath, src_string, dst_string):
@ -96,54 +89,53 @@ def replaceString(filepath, src_string, dst_string):
f2.close()
# end of replaceString
def processPlatformProjects(platform):
def processPlatformProjects(context, platform):
# determine proj_path
proj_path = context["dst_project_path"] + "/proj.%s/" % platform
proj_path = os.path.join(context["dst_project_path"], "proj." + platform)
java_package_path = ""
# read josn config file or the current platform
# read json config file or the current platform
f = open("%s.json" % platform)
data = json.load(f)
# rename package path, like "org.cocos2dx.hello" to "com.company.game". This is a special process for android
if (platform == "android"):
if platform == "android":
src_pkg = context["src_package_name"].split('.')
dst_pkg = context["dst_package_name"].split('.')
os.rename(proj_path + "src/" + src_pkg[0],
proj_path + "src/" + dst_pkg[0])
os.rename(proj_path + "src/" + dst_pkg[0] + "/" + src_pkg[1],
proj_path + "src/" + dst_pkg[0] + "/" + dst_pkg[1])
os.rename(proj_path + "src/" + dst_pkg[0] + "/" + dst_pkg[1] + "/" + src_pkg[2],
proj_path + "src/" + dst_pkg[0] + "/" + dst_pkg[1] + "/" + dst_pkg[2])
java_package_path = dst_pkg[0] + "/" + dst_pkg[1] + "/" + dst_pkg[2]
src_dir = os.path.join(proj_path, "src", *src_pkg)
dst_dir = os.path.join(proj_path, "src", *dst_pkg)
os.renames(src_dir, dst_dir)
java_package_path = os.path.join(*dst_pkg)
# rename files and folders
for i in range(0, len(data["rename"])):
tmp = data["rename"][i].replace("PACKAGE_PATH", java_package_path)
for item in data["rename"]:
tmp = item.replace("PACKAGE_PATH", java_package_path)
src = tmp.replace("PROJECT_NAME", context["src_project_name"])
dst = tmp.replace("PROJECT_NAME", context["dst_project_name"])
if (os.path.exists(proj_path + src) == True):
os.rename(proj_path + src, proj_path + dst)
if os.path.exists(os.path.join(proj_path, src)):
os.rename(os.path.join(proj_path, src), os.path.join(proj_path, dst))
# remove useless files and folders
for i in range(0, len(data["remove"])):
dst = data["remove"][i].replace("PROJECT_NAME", context["dst_project_name"])
if (os.path.exists(proj_path + dst) == True):
shutil.rmtree(proj_path + dst)
for item in data["remove"]:
dst = item.replace("PROJECT_NAME", context["dst_project_name"])
if os.path.exists(os.path.join(proj_path, dst)):
shutil.rmtree(os.path.join(proj_path, dst))
# rename package_name. This should be replaced at first. Don't change this sequence
for i in range(0, len(data["replace_package_name"])):
tmp = data["replace_package_name"][i].replace("PACKAGE_PATH", java_package_path)
for item in data["replace_package_name"]:
tmp = item.replace("PACKAGE_PATH", java_package_path)
dst = tmp.replace("PROJECT_NAME", context["dst_project_name"])
if (os.path.exists(proj_path + dst) == True):
replaceString(proj_path + dst, context["src_package_name"], context["dst_package_name"])
if os.path.exists(os.path.join(proj_path, dst)):
replaceString(os.path.join(proj_path, dst), context["src_package_name"], context["dst_package_name"])
# rename project_name
for i in range(0, len(data["replace_project_name"])):
tmp = data["replace_project_name"][i].replace("PACKAGE_PATH", java_package_path)
for item in data["replace_project_name"]:
tmp = item.replace("PACKAGE_PATH", java_package_path)
dst = tmp.replace("PROJECT_NAME", context["dst_project_name"])
if (os.path.exists(proj_path + dst) == True):
replaceString(proj_path + dst, context["src_project_name"], context["dst_project_name"])
if os.path.exists(os.path.join(proj_path, dst)):
replaceString(os.path.join(proj_path, dst), context["src_project_name"], context["dst_project_name"])
# done!
print "proj.%s\t\t: Done!" % platform
@ -154,26 +146,25 @@ def processPlatformProjects(platform):
# -------------- main --------------
# dump argvs
# print sys.argv
if __name__ == '__main__':
# prepare valid "context" dictionary
context, platforms_list = checkParams()
# print context, platforms_list
# prepare valid "context" dictionary
checkParams(context)
# import pprint
# pprint.pprint(context)
# copy "lauguage"(cpp/lua/javascript) platform.proj into cocos2d-x/projects/<project_name>/folder
if os.path.exists(context["dst_project_path"]):
print "Error:" + context["dst_project_path"] + " folder is already existing"
print "Please remove the old project or choose a new PROJECT_NAME in -project parameter"
sys.exit()
else:
shutil.copytree(context["src_project_path"], context["dst_project_path"], True)
# copy "lauguage"(cpp/lua/javascript) platform.proj into cocos2d-x/projects/<project_name>/folder
if (os.path.exists(context["dst_project_path"]) == True):
print "Error:" + context["dst_project_path"] + " folder is already existing"
print "Please remove the old project or choose a new PROJECT_NAME in -project parameter"
sys.exit()
else:
shutil.copytree(context["src_project_path"], context["dst_project_path"], True)
# call process_proj from each platform's script folder
for platform in platforms_list:
processPlatformProjects(context, platform)
# exec "import %s.handle_project_files" % (platform)
# exec "%s.handle_project_files.handle_project_files(context)" % (platform)
# call process_proj from each platform's script folder
for platform in platforms_list:
processPlatformProjects(platform)
# exec "import %s.handle_project_files" % (platform)
# exec "%s.handle_project_files.handle_project_files(context)" % (platform)
print "New project has been created in this path: " + context["dst_project_path"].replace("/tools/project-creator/../..", "")
print "Have Fun!"
print "New project has been created in this path: " + context["dst_project_path"].replace("/tools/project-creator/../..", "")
print "Have Fun!"