From 80f14970e1be8aa575ad9271bdf3a813c848354a Mon Sep 17 00:00:00 2001 From: aozhiwei Date: Fri, 15 Feb 2019 20:52:29 +0800 Subject: [PATCH] 1 --- scripts/construct/build_pb.py | 111 ++++++++++++++++++++++++++++++ scripts/construct/build_script.py | 61 ++++++++++++++++ 2 files changed, 172 insertions(+) create mode 100644 scripts/construct/build_pb.py create mode 100644 scripts/construct/build_script.py diff --git a/scripts/construct/build_pb.py b/scripts/construct/build_pb.py new file mode 100644 index 0000000..97721e0 --- /dev/null +++ b/scripts/construct/build_pb.py @@ -0,0 +1,111 @@ +#coding utf8 +#!/usr/bin/python + +import os +import sys +import time +import threading +import subprocess +from optparse import OptionParser + +g_is_terminated = False +def is_terminated(): + return g_is_terminated + +def printp_stdout(p): + try: + while not is_terminated(): + line = p.stdout.readline() + if len(line) > 0: + print(line, end = '') + except Exception as e: + print('build_pb stdout error:' + e) + +def printp_stderr(p): + try: + while is_terminated(): + line = p.stderr.readline() + if len(line) > 0: + print(line, end = '') + except Exception as e: + print('build_pb stderr error:' + e) + +def need_rebuild(pb_files): + for proto_name in pb_files: + if not os.path.isfile(proto_name + '.pb.cc'): + return True + s1 = os.stat(proto_name + '.pb.cc') + s2 = os.stat('../tools/protobuild/' + proto_name + '.proto') + if s1.st_mtime < s2.st_mtime: + return True + return False + +def rebuild(cpp_out, python_out, pb_files): + def genParams(): + cpp_param = '' + py_param = '' + for pb_file in pb_files: + if cpp_out != '': + cpp_param += 'protoc --proto_path=../tools/protobuild --cpp_out=%s ../tools/protobuild/%s.proto && ' % (cpp_out, pb_file) + if python_out != '': + py_param += '../../third_party/tools/bin/protoc --proto_path=../tools/protobuild --python_out=%s ../tools/protobuild/%s.proto && ' % (python_out, pb_file) + return cpp_param + py_param + ' echo ""' + global g_is_terminated + try: +# print(genParams()) + p = subprocess.Popen( + genParams(), + stdin = subprocess.PIPE, + stdout = subprocess.PIPE, + stderr = None, + shell = True) + t1 = threading.Thread(target = printp_stdout, args=(p, )) + t2 = threading.Thread(target = printp_stderr, args=(p, )) + t1.start() + t2.start() + p.wait() + g_is_terminated = True + t1.join() + t2.join() + sys.exit(p.returncode) + except Exception as e: + print('build_protocol rebuild error:' + str(e)) + +def repair_githooks(): + os.system('/bin/bash ../tools/scripts/githooks/install.sh') + +parser = OptionParser(usage="%prog [options]") +parser.add_option( + "-n", + "--nohooks", + dest = "nohooks", + help = "no repair git hooks", +) +parser.add_option( + "", + "--cpp_out", + dest = "cpp_out", + default = True, + help = "", +) +parser.add_option( + "", + "--python_out", + dest = "python_out", + default = True, + help = "", +) +parser.add_option( + "", + "--pb_files", + dest = "pb_files", + help = "", +) +(options, args) = parser.parse_args() +#if not options.nohooks: +# repair_githooks() + +if need_rebuild(options.pb_files.split(',')): + rebuild(options.cpp_out, options.python_out, options.pb_files.split(',')) +else: + print('pb files already is the latest') diff --git a/scripts/construct/build_script.py b/scripts/construct/build_script.py new file mode 100644 index 0000000..383eb35 --- /dev/null +++ b/scripts/construct/build_script.py @@ -0,0 +1,61 @@ +#coding utf8 +#!/usr/bin/python + +import os +import sys +import threading +import subprocess + +g_is_terminated = False +def is_terminated(): + return g_is_terminated + +def printp_stdout(p): + try: + while not is_terminated(): + line = p.stdout.readline() + if len(line) > 0: + print(line, end = '') + except Exception as e: + print('build_script stdout error:' + e) + +def printp_stderr(p): + try: + while is_terminated(): + line = p.stderr.readline() + if len(line) > 0: + print(line, end = '') + except Exception as e: + print('build_script stderr error:' + e) + +def need_rebuild(): + if not os.path.isfile('game_wrap.cxx'): + return True + s1 = os.stat('game_wrap.cxx') + s2 = os.stat('game.i') + return s1.st_mtime < s2.st_mtime + +def rebuild(): + global g_is_terminated + try: + p = subprocess.Popen('swig -builtin -c++ -python -o game_wrap.cxx game.i', + stdin = subprocess.PIPE, + stdout = subprocess.PIPE, + stderr = None, + shell = True) + t1 = threading.Thread(target = printp_stdout, args=(p, )) + t2 = threading.Thread(target = printp_stderr, args=(p, )) + t1.start() + t2.start() + p.wait() + g_is_terminated = True + t1.join() + t2.join() + sys.exit(p.returncode) + except Exception as e: + print('build_script rebuild error:' + str(e)) + +if need_rebuild(): + rebuild() +else: + print('script files already is the latest')