1 https://github.com/google/mozc/issues/462
3 --- /src/base/gen_character_set.py
4 +++ /src/base/gen_character_set.py
15 def _LoadTable(filename, column_index, pattern, validater):
17 - for line in open(filename):
20 if line.startswith('#'):
21 # Skip a comment line.
24 ucs = int(match.group(1), 16)
32 # (at most) four code points.
34 for _, group in itertools.groupby(enumerate(category_list),
35 - lambda (codepoint, _): codepoint / 4):
36 + lambda x: x[0] // 4):
37 # Fill bits from LSB to MSB for each group.
39 for index, (_, category) in enumerate(group):
42 # Output the content. Each line would have (at most) 16 bytes.
43 for _, group in itertools.groupby(enumerate(bit_list),
44 - lambda (index, _): index / 16):
45 + lambda x: x[0] // 16):
48 line.append('\\x%02X' % bits)
51 # TODO(hidehiko): the bitmap has two huge 0-bits ranges. Reduce them.
53 - (bits, category) for category, bits in CATEGORY_BITMAP.iteritems()]
54 + (bits, category) for category, bits in CATEGORY_BITMAP.items()]
61 categorizer.GetCategory(codepoint)
62 - for codepoint in xrange(categorizer.MaxCodePoint() + 1)]
63 + for codepoint in range(categorizer.MaxCodePoint() + 1)]
64 generated_character_set_header = GenerateCharacterSetHeader(category_list)
67 --- /src/base/gen_config_file_stream_data.py
68 +++ /src/base/gen_config_file_stream_data.py
71 result.append(' { "%s", "' % os.path.basename(path))
72 with open(path, 'rb') as stream:
73 - result.extend(r'\x%02X' % ord(byte) for byte in stream.read())
74 + result.extend(r'\x%02X' % byte for byte in stream.read())
75 result.append('", %d }' % os.path.getsize(path))
77 return ''.join(result)
80 (options, args) = ParseOptions()
81 if not options.output:
82 - print >>sys.stderr, (
83 - 'usage: gen_config_file_stream_data.py --output=filepath input ...')
84 + print('usage: gen_config_file_stream_data.py --output=filepath input ...',
88 with open(options.output, 'w') as output:
89 --- /src/build_mozc.py
90 +++ /src/build_mozc.py
92 logging.info('running %s...', binary)
94 test_function(binary, gtest_report_dir, options)
95 - except RunOrDieError, e:
96 + except RunOrDieError as e:
98 failed_tests.append(binary)
100 @@ -1082,7 +1082,7 @@
101 # and '-c' and 'Release' are build options.
104 - for i in xrange(len(args)):
105 + for i in range(len(args)):
106 if args[i].startswith('-'):
107 # starting with build options
108 build_options = args[i:]
109 @@ -1190,14 +1190,14 @@
111 def ShowHelpAndExit():
112 """Shows the help message."""
113 - print 'Usage: build_mozc.py COMMAND [ARGS]'
115 - print ' gyp Generate project files.'
116 - print ' build Build the specified target.'
117 - print ' runtests Build all tests and run them.'
118 - print ' clean Clean all the build files and directories.'
120 - print 'See also the comment in the script for typical usage.'
121 + print('Usage: build_mozc.py COMMAND [ARGS]')
122 + print('Commands: ')
123 + print(' gyp Generate project files.')
124 + print(' build Build the specified target.')
125 + print(' runtests Build all tests and run them.')
126 + print(' clean Clean all the build files and directories.')
128 + print('See also the comment in the script for typical usage.')
132 --- /src/build_tools/android_util.py
133 +++ /src/build_tools/android_util.py
135 (devices_result, _) = process.communicate()
136 used_ports = set(int(port) for port
137 in re.findall(r'emulator-(\d+)', devices_result))
138 - return [port for port in xrange(5554, 5586, 2) if port not in used_ports]
139 + return [port for port in range(5554, 5586, 2) if port not in used_ports]
142 def SetUpTestingSdkHomeDirectory(dest_android_sdk_home,
147 - for key, value in options.iteritems():
148 + for key, value in options.items():
149 args.extend([key, value])
150 env = {'ANDROID_SDK_HOME': os.path.abspath(dest_android_sdk_home)}
151 logging.info('Creating AVD: %s', args)
154 for arg in sys.argv[1:]:
155 for item in sorted(GetApkProperties(arg).items()):
156 - print '%s: %s' % item
157 + print('%s: %s' % item)
160 if __name__ == '__main__':
161 --- /src/build_tools/binary_size_checker.py
162 +++ /src/build_tools/binary_size_checker.py
164 actual_size = os.stat(filename).st_size
165 expected_size = EXPECTED_MAXIMUM_SIZES[basename]
166 if actual_size < expected_size * 1024 * 1024:
167 - print 'Pass: %s (size: %d) is smaller than expected (%d MB)' % (
168 - filename, actual_size, expected_size)
169 + print('Pass: %s (size: %d) is smaller than expected (%d MB)' % (
170 + filename, actual_size, expected_size))
173 - print 'WARNING: %s (size: %d) is larger than expected (%d MB)' % (
174 - filename, actual_size, expected_size)
175 + print('WARNING: %s (size: %d) is larger than expected (%d MB)' % (
176 + filename, actual_size, expected_size))
180 --- /src/build_tools/build_and_sign_pkg_mac.py
181 +++ /src/build_tools/build_and_sign_pkg_mac.py
186 -from util import PrintErrorAndExit
187 -from util import RunOrDie
188 +from .util import PrintErrorAndExit
189 +from .util import RunOrDie
193 --- /src/build_tools/build_breakpad.py
194 +++ /src/build_tools/build_breakpad.py
197 subprocess.check_output(command)
198 except subprocess.CalledProcessError as e:
201 sys.exit(e.returncode)
202 - print 'Done: %s' % ' '.join(command)
203 + print('Done: %s' % ' '.join(command))
206 def Xcodebuild(projdir, target, arch, sdk, outdir):
207 --- /src/build_tools/build_diskimage_mac.py
208 +++ /src/build_tools/build_diskimage_mac.py
210 # setup volume directory
211 temp_dir = tempfile.mkdtemp()
212 CopyFile(path.join(build_dir, ".keystone_install"), temp_dir)
213 - os.chmod(path.join(temp_dir, ".keystone_install"), 0755) # rwxr-xr-x
214 + os.chmod(path.join(temp_dir, ".keystone_install"), 0o755) # rwxr-xr-x
216 CopyFile(path.join(build_dir, a), temp_dir)
218 --- /src/build_tools/change_reference_mac.py
219 +++ /src/build_tools/change_reference_mac.py
224 -from util import PrintErrorAndExit
225 -from util import RunOrDie
226 +from .util import PrintErrorAndExit
227 +from .util import RunOrDie
231 --- /src/build_tools/code_generator_util.py
232 +++ /src/build_tools/code_generator_util.py
234 __author__ = "hidehiko"
240 def ToCppStringLiteral(s):
241 """Returns C-style string literal, or NULL if given s is None."""
246 - if all(0x20 <= ord(c) <= 0x7E for c in s):
247 + if all(0x20 <= c <= 0x7E for c in s):
248 # All characters are in ascii code.
249 - return '"%s"' % s.replace('\\', r'\\').replace('"', r'\"')
250 + return b'"%b"' % s.replace(b'\\', br'\\').replace(b'"', br'\"')
252 # One or more characters are non-ascii.
253 - return '"%s"' % ''.join(r'\x%02X' % ord(c) for c in s)
254 + return b'"%b"' % b''.join(br'\x%02X' % c for c in s)
257 def FormatWithCppEscape(format_text, *args):
258 """Returns a string filling format with args."""
261 - if isinstance(arg, (types.StringType, types.NoneType)):
262 + if isinstance(arg, (bytes, type(None))):
263 arg = ToCppStringLiteral(arg)
264 literal_list.append(arg)
267 if target_compiler and target_compiler.startswith('msvs'):
268 stream.write('const uint64 k%s_data_wordtype[] = {\n' % variable_name)
270 - for word_index in xrange(0, len(data), 8):
271 + for word_index in range(0, len(data), 8):
272 word_chunk = data[word_index:word_index + 8].ljust(8, '\x00')
273 stream.write('0x%016X, ' % struct.unpack('<Q', word_chunk))
274 if (word_index / 8) % 4 == 3:
276 stream.write('const char k%s_data[] =\n' % variable_name)
277 # Output 16bytes per line.
279 - for index in xrange(0, len(data), chunk_size):
280 + for index in range(0, len(data), chunk_size):
281 chunk = data[index:index + chunk_size]
283 stream.writelines(r'\x%02X' % ord(c) for c in chunk)
284 @@ -126,36 +125,50 @@
285 if type(codepoint_list) is int:
286 codepoint_list = (codepoint_list,)
287 if codepoint_list is None or len(codepoint_list) == 0:
292 for codepoint in codepoint_list:
293 - utf16_string = unichr(codepoint).encode('utf-16be')
294 + utf16_string = chr(codepoint).encode('utf-16be')
295 if len(utf16_string) == 2:
296 (u0, l0) = utf16_string
297 - result += r'\u%02X%02X' % (ord(u0), ord(l0))
298 + result += br'\u%02X%02X' % (u0, l0)
300 (u0, l0, u1, l1) = utf16_string
301 - result += r'\u%02X%02X\u%02X%02X' % (ord(u0), ord(l0), ord(u1), ord(l1))
303 + result += br'\u%02X%02X\u%02X%02X' % (u0, l0, u1, l1)
308 def SkipLineComment(stream, comment_prefix='#'):
309 """Skips line comments from stream."""
311 + if isinstance(line, bytes):
312 + if isinstance(comment_prefix, str):
313 + comment_prefix = comment_prefix.encode('utf-8')
314 + line_ending = b'\n'
317 stripped_line = line.strip()
318 if stripped_line and not stripped_line.startswith(comment_prefix):
319 - yield line.rstrip('\n')
320 + yield line.rstrip(line_ending)
323 def ParseColumnStream(stream, num_column=None, delimiter=None):
324 """Returns parsed columns read from stream."""
325 if num_column is None:
327 - yield line.rstrip('\n').split(delimiter)
328 + if isinstance(line, bytes):
329 + line_ending = b'\n'
332 + yield line.rstrip(line_ending).split(delimiter)
335 - yield line.rstrip('\n').split(delimiter)[:num_column]
336 + if isinstance(line, bytes):
337 + line_ending = b'\n'
340 + yield line.rstrip(line_ending).split(delimiter)[:num_column]
343 def SelectColumn(stream, column_index):
345 grouper extends the last chunk to make it an n-element chunk by adding
346 appropriate value, but this returns truncated chunk.
348 - for index in xrange(0, len(iterable), n):
349 + for index in range(0, len(iterable), n):
350 yield iterable[index:index + n]
351 --- /src/build_tools/codesign_mac.py
352 +++ /src/build_tools/codesign_mac.py
355 def RunOrDie(command):
356 """Run the command, or die if it failed."""
357 - print "Running: " + command
358 + print("Running: " + command)
360 output = subprocess.check_output(command, shell=True)
361 - print >> sys.stderr, "=========="
362 - print >> sys.stderr, "COMMAND: " + command
363 - print >> sys.stderr, output
364 + print("==========", file=sys.stderr)
365 + print("COMMAND: " + command, file=sys.stderr)
366 + print(output, file=sys.stderr)
367 except subprocess.CalledProcessError as e:
368 - print >> sys.stderr, "=========="
369 - print >> sys.stderr, "ERROR: " + command
370 - print >> sys.stderr, e.output
371 - print >> sys.stderr, "=========="
372 + print("==========", file=sys.stderr)
373 + print("ERROR: " + command, file=sys.stderr)
374 + print(e.output, file=sys.stderr)
375 + print("==========", file=sys.stderr)
379 @@ -119,18 +119,18 @@
380 (options, unused_args) = parser.parse_args()
382 if not options.target:
383 - print "Error: --target should be specified."
384 - print parser.print_help()
385 + print("Error: --target should be specified.")
386 + print(parser.print_help())
393 - print "=== os.environ ==="
394 + print("=== os.environ ===")
395 for key in sorted(os.environ):
396 - print "%s = %s" % (key, os.getenv(key))
397 - print "=================="
398 + print("%s = %s" % (key, os.getenv(key)))
399 + print("==================")
403 --- /src/build_tools/copy_dll_and_symbol.py
404 +++ /src/build_tools/copy_dll_and_symbol.py
409 -from util import PrintErrorAndExit
410 +from .util import PrintErrorAndExit
413 """Parse command line options."""
415 if _GetLastModifiedTime(src) <= target_file_mtime:
416 # Older file found. Ignore.
418 - print 'Copying %s to %s' % (src, target_file_abspath)
419 + print('Copying %s to %s' % (src, target_file_abspath))
420 shutil.copy2(src, target_file_abspath)
423 --- /src/build_tools/copy_file.py
424 +++ /src/build_tools/copy_file.py
427 message: The error message to be printed to stderr.
429 - print >>sys.stderr, message
430 + print(message, file=sys.stderr)
434 --- /src/build_tools/copy_qt_frameworks_mac.py
435 +++ /src/build_tools/copy_qt_frameworks_mac.py
440 -from copy_file import CopyFiles
441 -from util import PrintErrorAndExit
442 -from util import RunOrDie
443 +from .copy_file import CopyFiles
444 +from .util import PrintErrorAndExit
445 +from .util import RunOrDie
449 --- /src/build_tools/embed_file.py
450 +++ /src/build_tools/embed_file.py
453 def _FormatAsUint64LittleEndian(s):
454 """Formats a string as uint64 value in little endian order."""
455 - for _ in xrange(len(s), 8):
457 + for _ in range(len(s), 8):
459 s = s[::-1] # Reverse the string
460 - return '0x%s' % binascii.b2a_hex(s)
461 + return b'0x%b' % binascii.b2a_hex(s)
466 with open(opts.input, 'rb') as infile:
467 with open(opts.output, 'wb') as outfile:
469 - '#ifdef MOZC_EMBEDDED_FILE_%(name)s\n'
470 - '#error "%(name)s was already included or defined elsewhere"\n'
472 - '#define MOZC_EMBEDDED_FILE_%(name)s\n'
473 - 'const uint64 %(name)s_data[] = {\n'
474 - % {'name': opts.name})
475 + b'#ifdef MOZC_EMBEDDED_FILE_%(name)b\n'
476 + b'#error "%(name)b was already included or defined elsewhere"\n'
478 + b'#define MOZC_EMBEDDED_FILE_%(name)b\n'
479 + b'const uint64 %(name)b_data[] = {\n'
480 + % {b'name': opts.name.encode('utf-8')})
483 chunk = infile.read(8)
487 + outfile.write(b' ')
488 outfile.write(_FormatAsUint64LittleEndian(chunk))
489 - outfile.write(',\n')
490 + outfile.write(b',\n')
494 - 'const EmbeddedFile %(name)s = {\n'
495 - ' %(name)s_data,\n'
498 - '#endif // MOZC_EMBEDDED_FILE_%(name)s\n'
499 - % {'name': opts.name,
500 - 'size': os.stat(opts.input).st_size})
502 + b'const EmbeddedFile %(name)b = {\n'
503 + b' %(name)b_data,\n'
506 + b'#endif // MOZC_EMBEDDED_FILE_%(name)b\n'
507 + % {b'name': opts.name.encode('utf-8'),
508 + b'size': os.stat(opts.input).st_size})
511 if __name__ == '__main__':
512 --- /src/build_tools/embed_pathname.py
513 +++ /src/build_tools/embed_pathname.py
515 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
516 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
518 -"""A script to embed the given (relative) path name to C/C++ characters array.
519 +r"""A script to embed the given (relative) path name to C/C++ characters array.
522 ./embed_pathname.py --path_to_be_embedded=d:\data\mozc
525 (options, unused_args) = parser.parse_args()
526 if not all(vars(options).values()):
527 - print parser.print_help()
528 + print(parser.print_help())
534 path = os.path.abspath(opt.path_to_be_embedded)
535 # TODO(yukawa): Consider the case of non-ASCII characters.
536 - escaped_path = path.encode('string-escape')
537 + escaped_path = path.replace('\\', '\\\\')
538 with open(opt.output, 'w') as output_file:
540 'const char %s[] = "%s";\n' % (opt.constant_name, escaped_path))
541 --- /src/build_tools/ensure_gyp_module_path.py
542 +++ /src/build_tools/ensure_gyp_module_path.py
545 (options, _) = parser.parse_args()
546 if not options.expected:
547 - print parser.print_help()
548 + print(parser.print_help())
554 expected_path = os.path.abspath(opt.expected)
555 if not os.path.exists(expected_path):
556 - print '%s does not exist.' % expected_path
557 + print('%s does not exist.' % expected_path)
562 except ImportError as e:
563 - print 'import gyp failed: %s' % e
564 + print('import gyp failed: %s' % e)
567 actual_path = os.path.abspath(gyp.__path__[0])
568 if expected_path != actual_path:
569 - print 'Unexpected gyp module is loaded on this environment.'
570 - print ' expected: %s' % expected_path
571 - print ' actual : %s' % actual_path
572 + print('Unexpected gyp module is loaded on this environment.')
573 + print(' expected: %s' % expected_path)
574 + print(' actual : %s' % actual_path)
577 if __name__ == '__main__':
578 --- /src/build_tools/gen_win32_resource_header.py
579 +++ /src/build_tools/gen_win32_resource_header.py
581 __author__ = "yukawa"
585 +from . import mozc_version
589 --- /src/build_tools/mozc_version.py
590 +++ /src/build_tools/mozc_version.py
592 last_digit = TARGET_PLATFORM_TO_DIGIT.get(target_platform, None)
593 if last_digit is None:
594 logging.critical('target_platform %s is invalid. Accetable ones are %s',
595 - target_platform, TARGET_PLATFORM_TO_DIGIT.keys())
596 + target_platform, list(TARGET_PLATFORM_TO_DIGIT.keys()))
600 @@ -314,13 +314,14 @@
601 self._properties = {}
602 if not os.path.isfile(path):
604 - for line in open(path):
605 - matchobj = re.match(r'(\w+)=(.*)', line.strip())
607 - var = matchobj.group(1)
608 - val = matchobj.group(2)
609 - if var not in self._properties:
610 - self._properties[var] = val
611 + with open(path) as file:
613 + matchobj = re.match(r'(\w+)=(.*)', line.strip())
615 + var = matchobj.group(1)
616 + val = matchobj.group(2)
617 + if var not in self._properties:
618 + self._properties[var] = val
620 # Check mandatory properties.
621 for key in VERSION_PROPERTIES: