From 0684984e88f7d68b6a902e45c4330ab4b1115aae Mon Sep 17 00:00:00 2001 From: tushushu <33303747+tushushu@users.noreply.github.com> Date: Mon, 22 Jan 2018 14:09:17 +0800 Subject: [PATCH 1/3] Update resource_library.py MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 48行{base_dir, src}改为[base_dir, src] --- resource_library.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/resource_library.py b/resource_library.py index 5e589c0..5b9f273 100644 --- a/resource_library.py +++ b/resource_library.py @@ -46,7 +46,7 @@ def generate_line(byte_list): def generate_resource_and_src_list(base_dir, src_list): resource_list = [] for src in src_list: - prefix = os.path.commonprefix({base_dir, src}) + prefix = os.path.commonprefix([base_dir, src]) component = src[len(prefix):].lstrip('/') resource_name = component.replace('/', '_').replace('.', '_') c_filename = resource_name + ".c" From a851c53b518a34653f9217c2f7e1622e5b98e6df Mon Sep 17 00:00:00 2001 From: tushushu <33303747+tushushu@users.noreply.github.com> Date: Mon, 22 Jan 2018 20:31:37 +0800 Subject: [PATCH 2/3] Update resource_library.py --- resource_library.py | 35 +++++++++++++++++++++++------------ 1 file changed, 23 insertions(+), 12 deletions(-) diff --git a/resource_library.py b/resource_library.py index 5b9f273..4b3c570 100644 --- a/resource_library.py +++ b/resource_library.py @@ -38,7 +38,18 @@ import sys +def print_line(file, string): + # In python 2 and python 3, the way to call print functions is different. + if sys.version_info[0] > 2: + exec("print(string, file=file)") + else: + exec("print >> file, string") + + def generate_line(byte_list): + # Assume 'reduce' fuction needs to be imported in this way in future versions. + if sys.version_info[0] > 2: + from functools import reduce line = reduce(lambda x, y: x + ', ' + "%s" % str(y), byte_list) return line + ',' @@ -68,9 +79,9 @@ def generate_resource_and_src_list(base_dir, src_list): resource_list = generate_resource_and_src_list(base_dir, srcs) hfile = open(header_file, 'w') - print >> hfile, '#ifdef __cplusplus' - print >> hfile, 'extern "C" {' - print >> hfile, '#endif' + print_line(hfile, '#ifdef __cplusplus') + print_line(hfile, 'extern "C" {') + print_line(hfile, '#endif') for (source, resource_name, c_filename, size) in resource_list: f = open(source, 'rb') @@ -87,16 +98,16 @@ def generate_resource_and_src_list(base_dir, src_list): if len(byte_list) > 0: line_list.append(generate_line(byte_list)) - print >> hfile, "extern const char RESOURCE_%s[%d];" % (resource_name, size) + print_line(hfile, "extern const char RESOURCE_%s[%d];" % (resource_name, size)) with open(c_filename, "w") as c_file: - print >> c_file, "const char RESOURCE_%s[] = {" % resource_name - line_list[len(line_list) - 1] = line_list[len(line_list) - 1][ : -1] + print_line(c_file, "const char RESOURCE_%s[] = {" % resource_name) + line_list[len(line_list) - 1] = line_list[len(line_list) - 1][:-1] for line in line_list: - print >> c_file, ' ', line - print >> c_file, '};' - print >> c_file, "unsigned int %s_len = %d;" % (resource_name, size) + print_line(c_file, ' ' + line) + print_line(c_file, '};') + print_line(c_file, "unsigned int %s_len = %d;" % (resource_name, size)) - print >> hfile, '#ifdef __cplusplus' - print >> hfile, '}' - print >> hfile, '#endif' + print_line(hfile, '#ifdef __cplusplus') + print_line(hfile, '}') + print_line(hfile, '#endif') hfile.close() From 51a4c12b1c9bdaf366e3983775869bcd9728f609 Mon Sep 17 00:00:00 2001 From: tushushu <33303747+tushushu@users.noreply.github.com> Date: Mon, 22 Jan 2018 21:06:58 +0800 Subject: [PATCH 3/3] Update resource_library.py --- resource_library.py | 37 ++++++++++++++++--------------------- 1 file changed, 16 insertions(+), 21 deletions(-) diff --git a/resource_library.py b/resource_library.py index 4b3c570..51563bd 100644 --- a/resource_library.py +++ b/resource_library.py @@ -33,23 +33,18 @@ ${resource_src_name} is mapped from src_name by replacing '/' or '.' to '_'. """ - +from __future__ import print_function import os import sys -def print_line(file, string): - # In python 2 and python 3, the way to call print functions is different. - if sys.version_info[0] > 2: - exec("print(string, file=file)") - else: - exec("print >> file, string") +# Assume 'reduce' fuction needs to be imported in this way in future versions. +version_info = int(sys.version.split('.')[0]) +if version_info > 2: + from functools import reduce def generate_line(byte_list): - # Assume 'reduce' fuction needs to be imported in this way in future versions. - if sys.version_info[0] > 2: - from functools import reduce line = reduce(lambda x, y: x + ', ' + "%s" % str(y), byte_list) return line + ',' @@ -79,9 +74,9 @@ def generate_resource_and_src_list(base_dir, src_list): resource_list = generate_resource_and_src_list(base_dir, srcs) hfile = open(header_file, 'w') - print_line(hfile, '#ifdef __cplusplus') - print_line(hfile, 'extern "C" {') - print_line(hfile, '#endif') + print('#ifdef __cplusplus', file=hfile) + print('extern "C" {', file=hfile) + print('#endif', file=hfile) for (source, resource_name, c_filename, size) in resource_list: f = open(source, 'rb') @@ -98,16 +93,16 @@ def generate_resource_and_src_list(base_dir, src_list): if len(byte_list) > 0: line_list.append(generate_line(byte_list)) - print_line(hfile, "extern const char RESOURCE_%s[%d];" % (resource_name, size)) + print("extern const char RESOURCE_%s[%d];" % (resource_name, size), file=hfile) with open(c_filename, "w") as c_file: - print_line(c_file, "const char RESOURCE_%s[] = {" % resource_name) + print("const char RESOURCE_%s[] = {" % resource_name, file=c_file) line_list[len(line_list) - 1] = line_list[len(line_list) - 1][:-1] for line in line_list: - print_line(c_file, ' ' + line) - print_line(c_file, '};') - print_line(c_file, "unsigned int %s_len = %d;" % (resource_name, size)) + print(' ' + line, file=c_file) + print('};', file=c_file) + print("unsigned int %s_len = %d;" % (resource_name, size), file=c_file) - print_line(hfile, '#ifdef __cplusplus') - print_line(hfile, '}') - print_line(hfile, '#endif') + print('#ifdef __cplusplus', file=hfile) + print('}', file=hfile) + print('#endif', file = hfile) hfile.close()