The command 'cocos new' python script now works both 2.x and 3.x

This commit is contained in:
halx99 2020-10-20 12:51:04 +08:00
parent e59109080c
commit 51e3208e59
9 changed files with 44 additions and 19 deletions

View File

@ -1 +1,5 @@
from project_compile import CCPluginCompile import sys
if(sys.version_info.major >= 3):
from .project_compile import CCPluginCompile
else:
from project_compile import CCPluginCompile

View File

@ -21,7 +21,7 @@ import re
import sys import sys
import shutil import shutil
import json import json
import build_web from . import build_web
import utils import utils
class CCPluginCompile(cocos.CCPlugin): class CCPluginCompile(cocos.CCPlugin):
@ -221,7 +221,7 @@ class CCPluginCompile(cocos.CCPlugin):
try: try:
return multiprocessing.cpu_count() return multiprocessing.cpu_count()
except Exception: except Exception:
print MultiLanguage.get_string('COMPILE_DETECT_CPU_FAILED') print(MultiLanguage.get_string('COMPILE_DETECT_CPU_FAILED'))
return 1 return 1
def _get_output_dir(self): def _get_output_dir(self):

View File

@ -1,3 +1,3 @@
from gen_libs import LibsCompiler from .gen_libs import LibsCompiler
from gen_simulator import SimulatorCompiler from .gen_simulator import SimulatorCompiler
from gen_templates import TemplateGenerator from .gen_templates import TemplateGenerator

View File

@ -6,7 +6,7 @@ import sys
import shutil import shutil
import json import json
import utils import utils
import gen_prebuilt_mk from . import gen_prebuilt_mk
import cocos import cocos
from MultiLanguage import MultiLanguage from MultiLanguage import MultiLanguage

View File

@ -5,7 +5,7 @@ import os
import sys import sys
import json import json
import utils import utils
import modify_template from . import modify_template
import re import re
import cocos import cocos

View File

@ -1 +1,5 @@
from project_new import CCPluginNew import sys
if(sys.version_info.major >= 3):
from .project_new import CCPluginNew
else:
from project_new import CCPluginNew

View File

@ -44,7 +44,7 @@ class CCPluginNew(cocos.CCPlugin):
def init(self, args): def init(self, args):
self._projname = args.name self._projname = args.name
self._projdir = unicode( self._projdir = cocos.encode_with(
os.path.abspath(os.path.join(args.directory, self._projname)), "utf-8") os.path.abspath(os.path.join(args.directory, self._projname)), "utf-8")
self._lang = args.language self._lang = args.language
self._package = args.package self._package = args.package
@ -57,7 +57,7 @@ class CCPluginNew(cocos.CCPlugin):
# search for custom paths # search for custom paths
if args.engine_path is not None: if args.engine_path is not None:
self._cocosroot = os.path.abspath(args.engine_path) self._cocosroot = os.path.abspath(args.engine_path)
self._cocosroot = unicode(self._cocosroot, "utf-8") self._cocosroot = cocos.encode_with(self._cocosroot, "utf-8")
tp_path = os.path.join(self._cocosroot, "templates") tp_path = os.path.join(self._cocosroot, "templates")
if os.path.isdir(tp_path): if os.path.isdir(tp_path):
self._templates_paths.append(tp_path) self._templates_paths.append(tp_path)
@ -248,7 +248,7 @@ class CCPluginNew(cocos.CCPlugin):
data[cocos_project.Project.KEY_HAS_NATIVE] = True data[cocos_project.Project.KEY_HAS_NATIVE] = True
# record the engine version if not predefined # record the engine version if not predefined
if not data.has_key(cocos_project.Project.KEY_ENGINE_VERSION): if not (cocos_project.Project.KEY_ENGINE_VERSION in data):
engine_version = utils.get_engine_version(self._cocosroot) engine_version = utils.get_engine_version(self._cocosroot)
if engine_version is not None: if engine_version is not None:
data[cocos_project.Project.KEY_ENGINE_VERSION] = engine_version data[cocos_project.Project.KEY_ENGINE_VERSION] = engine_version
@ -420,6 +420,11 @@ class TPCreator(object):
message = MultiLanguage.get_string('NEW_WARNING_FILE_NOT_FOUND_FMT', tp_json_path) message = MultiLanguage.get_string('NEW_WARNING_FILE_NOT_FOUND_FMT', tp_json_path)
raise cocos.CCPluginError(message, cocos.CCPluginError.ERROR_PATH_NOT_FOUND) raise cocos.CCPluginError(message, cocos.CCPluginError.ERROR_PATH_NOT_FOUND)
if(sys.version_info.major >= 3):
f = open(tp_json_path, encoding='utf8')
# keep the key order
tpinfo = json.load(f, object_pairs_hook=OrderedDict)
else:
f = open(tp_json_path) f = open(tp_json_path)
# keep the key order # keep the key order
tpinfo = json.load(f, encoding='utf8', object_pairs_hook=OrderedDict) tpinfo = json.load(f, encoding='utf8', object_pairs_hook=OrderedDict)
@ -473,7 +478,12 @@ class TPCreator(object):
self.do_cmds(cmds) self.do_cmds(cmds)
def do_cmds(self, cmds): def do_cmds(self, cmds):
for k, v in cmds.iteritems(): items = None
if(sys.version_info.major >= 3):
items = cmds.items()
else:
items = cmds.iteritems()
for k, v in items:
# call cmd method by method/cmd name # call cmd method by method/cmd name
# get from # get from
# http://stackoverflow.com/questions/3951840/python-how-to-invoke-an-function-on-an-object-dynamically-by-name # http://stackoverflow.com/questions/3951840/python-how-to-invoke-an-function-on-an-object-dynamically-by-name

View File

@ -1,2 +1,2 @@
from project_run import CCPluginRun from .project_run import CCPluginRun

View File

@ -16,12 +16,16 @@ import sys
import os import os
import cocos import cocos
from MultiLanguage import MultiLanguage from MultiLanguage import MultiLanguage
import BaseHTTPServer
import webbrowser import webbrowser
import threading import threading
import subprocess import subprocess
import re import re
if(sys.version_info.major >= 3):
from http.server import BaseHTTPRequestHandler,HTTPServer
else:
import BaseHTTPServer
class CCPluginRun(cocos.CCPlugin): class CCPluginRun(cocos.CCPlugin):
""" """
Compiles a project and runs it on the target Compiles a project and runs it on the target
@ -264,7 +268,10 @@ class CCPluginRun(cocos.CCPlugin):
from SimpleHTTPServer import SimpleHTTPRequestHandler from SimpleHTTPServer import SimpleHTTPRequestHandler
HandlerClass = SimpleHTTPRequestHandler HandlerClass = SimpleHTTPRequestHandler
ServerClass = BaseHTTPServer.HTTPServer if(sys.version_info.major >= 3):
ServerClass = HTTPServer.HTTPServer
else:
ServerClass = BaseHTTPServer.BaseHTTPServer
Protocol = "HTTP/1.0" Protocol = "HTTP/1.0"
HandlerClass.protocol_version = Protocol HandlerClass.protocol_version = Protocol