From 51e3208e59f795b1f8fcf0e98002e02a52d5bb95 Mon Sep 17 00:00:00 2001 From: halx99 Date: Tue, 20 Oct 2020 12:51:04 +0800 Subject: [PATCH] The command 'cocos new' python script now works both 2.x and 3.x --- .../plugins/plugin_compile/__init__.py | 6 ++++- .../plugins/plugin_compile/project_compile.py | 4 ++-- .../plugins/plugin_generate/__init__.py | 6 ++--- .../plugins/plugin_generate/gen_libs.py | 2 +- .../plugins/plugin_generate/gen_templates.py | 2 +- .../plugins/plugin_new/__init__.py | 6 ++++- .../plugins/plugin_new/project_new.py | 24 +++++++++++++------ .../plugins/plugin_run/__init__.py | 2 +- .../plugins/plugin_run/project_run.py | 11 +++++++-- 9 files changed, 44 insertions(+), 19 deletions(-) diff --git a/tools/cocos2d-console/plugins/plugin_compile/__init__.py b/tools/cocos2d-console/plugins/plugin_compile/__init__.py index b0a9e3c541..76d4b562f2 100644 --- a/tools/cocos2d-console/plugins/plugin_compile/__init__.py +++ b/tools/cocos2d-console/plugins/plugin_compile/__init__.py @@ -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 diff --git a/tools/cocos2d-console/plugins/plugin_compile/project_compile.py b/tools/cocos2d-console/plugins/plugin_compile/project_compile.py index e4d23789ba..769b7e259d 100644 --- a/tools/cocos2d-console/plugins/plugin_compile/project_compile.py +++ b/tools/cocos2d-console/plugins/plugin_compile/project_compile.py @@ -21,7 +21,7 @@ import re import sys import shutil import json -import build_web +from . import build_web import utils class CCPluginCompile(cocos.CCPlugin): @@ -221,7 +221,7 @@ class CCPluginCompile(cocos.CCPlugin): try: return multiprocessing.cpu_count() except Exception: - print MultiLanguage.get_string('COMPILE_DETECT_CPU_FAILED') + print(MultiLanguage.get_string('COMPILE_DETECT_CPU_FAILED')) return 1 def _get_output_dir(self): diff --git a/tools/cocos2d-console/plugins/plugin_generate/__init__.py b/tools/cocos2d-console/plugins/plugin_generate/__init__.py index df41227779..2bd8ff2f47 100644 --- a/tools/cocos2d-console/plugins/plugin_generate/__init__.py +++ b/tools/cocos2d-console/plugins/plugin_generate/__init__.py @@ -1,3 +1,3 @@ -from gen_libs import LibsCompiler -from gen_simulator import SimulatorCompiler -from gen_templates import TemplateGenerator +from .gen_libs import LibsCompiler +from .gen_simulator import SimulatorCompiler +from .gen_templates import TemplateGenerator diff --git a/tools/cocos2d-console/plugins/plugin_generate/gen_libs.py b/tools/cocos2d-console/plugins/plugin_generate/gen_libs.py index 0e6d4ea6b8..1f2f91a6c2 100644 --- a/tools/cocos2d-console/plugins/plugin_generate/gen_libs.py +++ b/tools/cocos2d-console/plugins/plugin_generate/gen_libs.py @@ -6,7 +6,7 @@ import sys import shutil import json import utils -import gen_prebuilt_mk +from . import gen_prebuilt_mk import cocos from MultiLanguage import MultiLanguage diff --git a/tools/cocos2d-console/plugins/plugin_generate/gen_templates.py b/tools/cocos2d-console/plugins/plugin_generate/gen_templates.py index 4aade838a0..2d28a95a9e 100644 --- a/tools/cocos2d-console/plugins/plugin_generate/gen_templates.py +++ b/tools/cocos2d-console/plugins/plugin_generate/gen_templates.py @@ -5,7 +5,7 @@ import os import sys import json import utils -import modify_template +from . import modify_template import re import cocos diff --git a/tools/cocos2d-console/plugins/plugin_new/__init__.py b/tools/cocos2d-console/plugins/plugin_new/__init__.py index 58f954b5e1..6a2cd64c22 100644 --- a/tools/cocos2d-console/plugins/plugin_new/__init__.py +++ b/tools/cocos2d-console/plugins/plugin_new/__init__.py @@ -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 diff --git a/tools/cocos2d-console/plugins/plugin_new/project_new.py b/tools/cocos2d-console/plugins/plugin_new/project_new.py index 31a1909c07..ce67518240 100644 --- a/tools/cocos2d-console/plugins/plugin_new/project_new.py +++ b/tools/cocos2d-console/plugins/plugin_new/project_new.py @@ -44,7 +44,7 @@ class CCPluginNew(cocos.CCPlugin): def init(self, args): self._projname = args.name - self._projdir = unicode( + self._projdir = cocos.encode_with( os.path.abspath(os.path.join(args.directory, self._projname)), "utf-8") self._lang = args.language self._package = args.package @@ -57,7 +57,7 @@ class CCPluginNew(cocos.CCPlugin): # search for custom paths if args.engine_path is not None: 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") if os.path.isdir(tp_path): self._templates_paths.append(tp_path) @@ -248,7 +248,7 @@ class CCPluginNew(cocos.CCPlugin): data[cocos_project.Project.KEY_HAS_NATIVE] = True # 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) if engine_version is not None: data[cocos_project.Project.KEY_ENGINE_VERSION] = engine_version @@ -420,9 +420,14 @@ class TPCreator(object): message = MultiLanguage.get_string('NEW_WARNING_FILE_NOT_FOUND_FMT', tp_json_path) raise cocos.CCPluginError(message, cocos.CCPluginError.ERROR_PATH_NOT_FOUND) - f = open(tp_json_path) - # keep the key order - tpinfo = json.load(f, encoding='utf8', object_pairs_hook=OrderedDict) + 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) + # keep the key order + tpinfo = json.load(f, encoding='utf8', object_pairs_hook=OrderedDict) # read the default creating step if 'do_default' not in tpinfo: @@ -473,7 +478,12 @@ class TPCreator(object): self.do_cmds(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 # get from # http://stackoverflow.com/questions/3951840/python-how-to-invoke-an-function-on-an-object-dynamically-by-name diff --git a/tools/cocos2d-console/plugins/plugin_run/__init__.py b/tools/cocos2d-console/plugins/plugin_run/__init__.py index bea49f548c..bb6ee75cd6 100644 --- a/tools/cocos2d-console/plugins/plugin_run/__init__.py +++ b/tools/cocos2d-console/plugins/plugin_run/__init__.py @@ -1,2 +1,2 @@ -from project_run import CCPluginRun +from .project_run import CCPluginRun diff --git a/tools/cocos2d-console/plugins/plugin_run/project_run.py b/tools/cocos2d-console/plugins/plugin_run/project_run.py index 786040316c..ebe0ac2718 100644 --- a/tools/cocos2d-console/plugins/plugin_run/project_run.py +++ b/tools/cocos2d-console/plugins/plugin_run/project_run.py @@ -16,12 +16,16 @@ import sys import os import cocos from MultiLanguage import MultiLanguage -import BaseHTTPServer import webbrowser import threading import subprocess import re +if(sys.version_info.major >= 3): + from http.server import BaseHTTPRequestHandler,HTTPServer +else: + import BaseHTTPServer + class CCPluginRun(cocos.CCPlugin): """ Compiles a project and runs it on the target @@ -264,7 +268,10 @@ class CCPluginRun(cocos.CCPlugin): from SimpleHTTPServer import SimpleHTTPRequestHandler HandlerClass = SimpleHTTPRequestHandler - ServerClass = BaseHTTPServer.HTTPServer + if(sys.version_info.major >= 3): + ServerClass = HTTPServer.HTTPServer + else: + ServerClass = BaseHTTPServer.BaseHTTPServer Protocol = "HTTP/1.0" HandlerClass.protocol_version = Protocol