diff --git a/HISTORY.md b/HISTORY.md new file mode 100644 index 0000000..b874741 --- /dev/null +++ b/HISTORY.md @@ -0,0 +1,8 @@ +更新记录 +========== + + +1.0.1.0 +------ +1. 变更help和参数解析方式 +2. 增加并行转表的功能 diff --git a/print_color.py b/print_color.py index 8d6b1af..521d0b0 100644 --- a/print_color.py +++ b/print_color.py @@ -18,7 +18,7 @@ class print_style: FC_RED = 4 FC_MAGENTA = 5 FC_YELLOW = 6 - FC_WHITE = 7 + FC_WHITE = 7 BC_BLACK = 8 BC_BLUE = 9 @@ -27,14 +27,14 @@ class print_style: BC_RED = 12 BC_MAGENTA = 13 BC_YELLOW = 14 - BC_WHITE = 15 + BC_WHITE = 15 FW_BOLD = 16 def __contains__(self, value): return False -''''' See https://msdn.microsoft.com/zh-cn/windows/apps/ms682088%28v=vs.100%29#_win32_character_attributes +''''' See https://msdn.microsoft.com/zh-cn/windows/apps/ms682088%28v=vs.100%29#_win32_character_attributes for color codes ''' @@ -265,60 +265,41 @@ def cprintf_stderr(options, fmt, *text): """ run as a executable """ if __name__ == "__main__": - import getopt - def print_help_msg(): - print('usage: ' + sys.argv[0] + ' [options...] [format parameters...]') - print('options:') - print('-h, --help help messages') - print('-v, --version show version and exit') - print('-c, --color set font color.(any of: black, blue, green, cyan, red, magenta, yellow, white)') - print('-b, --background-color set background color.(any of: black, blue, green, cyan, red, magenta, yellow, white)') - print('-B, --bold set font weight to bold') - print('-m, --mode set mode.(any of: auto, term, win32_console, none, html)') - print('-s, --output-stream set output stream.(any of: stdout, stderr)') - - opts, left_args = getopt.getopt(sys.argv[1:], 'b:Bc:hm:s:v', [ - 'background-color=', - 'bold', - 'color=', - 'help', - 'mode=', - 'output-stream=', - 'version', - ]) - print_stream = 'stdout' - print_options = [] + from optparse import OptionParser + usage = "usage: %prog [options...] [format parameters...]" + parser = OptionParser(usage) - for opt_key, opt_val in opts: - if opt_key in ('-b', '--background-color'): - full_key = ('BC_' + opt_val).upper() - if full_key in print_style.__dict__: - print_options.append(print_style.__dict__[full_key]) + parser.add_option("-v", "--version", action="store_true", help="show version and exit", dest="verbose") + parser.add_option("-c", "--color", action="append", help="set font color.(any of: black, blue, green, cyan, red, magenta, yellow, white)", dest="color") + parser.add_option("-b", "--background-color", action="append", help="set background color.(any of: black, blue, green, cyan, red, magenta, yellow, white)", dest="background_color") + parser.add_option("-B", "--bold", action="append_const", help="set font weight to bold", const=print_style.FW_BOLD, dest="style") + parser.add_option("-m", "--mode", action="store", help="set mode.(any of: auto, term, win32_console, none, html)", dest="mode") + parser.add_option("-s", "--output-stream", action="store", help="set output stream.(any of: stdout, stderr)", dest="ostream", default="stdout") - elif opt_key in ('-B', '--bold'): - print_options.append(print_style.FW_BOLD) + (options, left_args) = parser.parse_args() - elif opt_key in ('-c', '--color'): - full_key = ('FC_' + opt_val).upper() - if full_key in print_style.__dict__ : - print_options.append(print_style.__dict__[full_key]) + if options.verbose: + print(print_style.version) + exit(0) - elif opt_key in ('-h', '--help'): - print_help_msg() - exit(0) + print_stream = 'stdout' + print_options = [] - elif opt_key in ('-m', '--mode'): - cprintf_set_mode(opt_val) + fc_list = ['FC_' + x.upper() for x in options.color or [] ] + bk_list = ['BC_' + y.upper() for y in options.background_color or [] ] + for style_list in [ fc_list, bk_list ]: + for style_name in style_list: + if style_name in print_style.__dict__ : + print_options.append(print_style.__dict__[style_name]) - elif opt_key in ('-s', '--output-stream'): - print_stream = opt_val + for style_code in options.style or []: + print_options.append(style_code) - elif opt_key in ('-v', '--version'): - print(print_style.version) - exit(0) + if options.mode: + cprintf_set_mode(options.mode) if len(left_args) > 0: - if 'stdout' == print_stream: + if 'stdout' == options.ostream: cprintf_stdout(print_options, *left_args) else: cprintf_stderr(print_options, *left_args) diff --git a/xresconv-cli.py b/xresconv-cli.py index 022d641..12b50e6 100644 --- a/xresconv-cli.py +++ b/xresconv-cli.py @@ -6,7 +6,9 @@ import shutil, re, string import xml.etree.ElementTree as ET import glob, getopt +from multiprocessing import cpu_count from print_color import print_style, cprintf_stdout, cprintf_stderr +from optparse import OptionParser console_encoding = sys.getfilesystemencoding() @@ -18,7 +20,7 @@ sys.setdefaultencoding('utf-8') xconv_options = { - 'version': '1.0.0.0', + 'version': '1.0.1.0', 'conv_list' : None, 'real_run': True, 'args' : {}, @@ -27,39 +29,33 @@ 'work_dir': '.', 'xresloader_path': 'xresloader.jar', - 'rules': { - 'schemes': None - }, - - 'item': [] + 'item': [], + 'parallelism': int((cpu_count() - 1) / 2) + 1 } xconv_xml_global_nodes = [] xconv_xml_list_item_nodes = [] + +usage = "usage: %prog [options...] [xresloader options...]" +parser = OptionParser(usage) +parser.disable_interspersed_args() + + +parser.add_option("-v", "--version", action="store_true", help="show version and exit", dest="version", default=False) +parser.add_option("-s", "--scheme-name", action="append", help="only convert schemes with name ", metavar="",dest="rule_schemes", default=[]) +parser.add_option("-t", "--test", action="store_true", help="test run and show cmds", dest="test", default=False) +parser.add_option("-p", "--parallelism", action="store", help="set parallelism task number(default:" + str(xconv_options['parallelism']) + ')', metavar="", dest="parallelism", type="int", default=xconv_options['parallelism']) + +(options, left_args) = parser.parse_args() + +if options.version: + print(xconv_options['version']) + exit(0) + def print_help_msg(err_code): - print('usage: ' + sys.argv[0] + ' [options] [xresloader options...]') - print('options:') - print('-h, --help help messages') - print('-s, --scheme-name only convert schemes with name ') - print('-v, --version show version and exit') - print('-t, --test test run and show cmds') + parser.print_help() exit(err_code) -opts, left_args = getopt.getopt(sys.argv[1:], 'hs:tv', ['help', 'version', 'scheme-name=', 'test']) -for opt_key, opt_val in opts: - if opt_key in ('-h', '--help'): - print_help_msg(0) - elif opt_key in ('-v', '--version'): - print(xconv_options['version']) - exit(0) - elif opt_key in ('-s', '--scheme-name'): - if xconv_options['rules']['schemes'] is None: - xconv_options['rules']['schemes'] = {} - xconv_options['rules']['schemes'][opt_val] = True - elif opt_key in ('-t', '--test'): - xconv_options['real_run'] = False - else: - print_help_msg(0) if 0 == len(left_args): print_help_msg(-1) @@ -191,7 +187,7 @@ def load_list_item_nodes(lis): conv_item_obj['options'].append(trip_value) # 转换规则 - if xconv_options['rules']['schemes'] is None or conv_item_obj['scheme'] in xconv_options['rules']['schemes']: + if not options.rule_schemes or 0 == len(options.rule_schemes) or conv_item_obj['scheme'] in options.rule_schemes: conv_item_obj['enable'] = True xconv_options['item'].append(conv_item_obj) @@ -201,9 +197,9 @@ def load_list_item_nodes(lis): # ----------------------------------------- 转换配置解析 ----------------------------------------- -# ========================================= 实际开始转换 ========================================= +# ========================================= 生成转换命令 ========================================= ##### 全局命令和配置 -global_cmd_prefix = 'java -jar "{0}"'.format(xconv_options['xresloader_path']) +global_cmd_prefix = 'java -client -jar "{0}"'.format(xconv_options['xresloader_path']) for global_optk in xconv_options['args']: global_optv= xconv_options['args'][global_optk] global_cmd_prefix += ' ' + global_optk + ' ' + global_optv @@ -216,7 +212,7 @@ def load_list_item_nodes(lis): if len(xconv_options['ext_args_l2']) > 0: global_cmd_suffix += ' ' + ' '.join(xconv_options['ext_args_l2']) -exit_code = 0 +cmd_list=[] for conv_item in xconv_options['item']: if not conv_item['enable']: continue @@ -229,12 +225,46 @@ def load_list_item_nodes(lis): run_cmd = global_cmd_prefix + item_cmd_options + cmd_scheme_info + global_cmd_suffix if 'utf-8' != console_encoding.lower(): run_cmd = run_cmd.encode(console_encoding) - cprintf_stdout([print_style.FC_GREEN], '[INFO] {0}\n', run_cmd) - cmd_exit_code = 0 - if xconv_options['real_run']: - cmd_exit_code = os.system(run_cmd) - if cmd_exit_code < 0: - exit_code = cmd_exit_code + + cmd_list.append(run_cmd) + +cmd_list.reverse() +# ----------------------------------------- 生成转换命令 ----------------------------------------- + +# ========================================= 实际开始转换 ========================================= +import threading +exit_code = 0 +all_worker_thread = [] +cmd_picker_lock = threading.Lock() + +def worker_func(): + global exit_code + while True: + cmd_picker_lock.acquire() + if len(cmd_list) <= 0: + cmd_picker_lock.release() + return 0 + + run_cmd = cmd_list.pop() + cmd_picker_lock.release() + + cprintf_stdout([print_style.FC_GREEN], '[INFO] {0}\n', run_cmd) + cmd_exit_code = 0 + if not options.test: + cmd_exit_code = os.system(run_cmd) + if cmd_exit_code < 0: + exit_code = cmd_exit_code + +for i in xrange(0, options.parallelism): + this_worker_thd = threading.Thread(target=worker_func) + this_worker_thd.start() + all_worker_thread.append(this_worker_thd) + + +# 等待退出 +for thd in all_worker_thread: + thd.join() + # ----------------------------------------- 实际开始转换 ----------------------------------------- cprintf_stdout([print_style.FC_MAGENTA], '[INFO] all jobs done.\n')