Migrate java-event-logs-tags.py to python3.

These are used in the Bazel Android app builds.

Test: m droid
Test: CI
Test: b run //build/make/tools:java-event-log-tags

Change-Id: Iaffe6f974008d1a0532a849353d25df02197afd2
This commit is contained in:
Jingwen Chen
2022-01-13 15:14:35 +00:00
parent 00a48b3df7
commit 2a31f3df87
4 changed files with 33 additions and 32 deletions

View File

@@ -8,7 +8,7 @@ py_binary(
srcs=["java-event-log-tags.py"], srcs=["java-event-log-tags.py"],
deps=[":event_log_tags"], deps=[":event_log_tags"],
visibility = ["//visibility:public"], visibility = ["//visibility:public"],
python_version = "PY2", python_version = "PY3",
) )
py_binary( py_binary(
@@ -16,5 +16,5 @@ py_binary(
srcs=["merge-event-log-tags.py"], srcs=["merge-event-log-tags.py"],
deps=[":event_log_tags"], deps=[":event_log_tags"],
visibility = ["//visibility:public"], visibility = ["//visibility:public"],
python_version = "PY2", python_version = "PY3",
) )

View File

@@ -55,12 +55,13 @@ class TagFile(object):
if file_object is None: if file_object is None:
try: try:
file_object = open(filename, "rb") file_object = open(filename, "rb")
except (IOError, OSError), e: except (IOError, OSError) as e:
self.AddError(str(e)) self.AddError(str(e))
return return
try: try:
for self.linenum, line in enumerate(file_object): for self.linenum, line in enumerate(file_object):
line = line.decode('utf-8')
self.linenum += 1 self.linenum += 1
line = re.sub('#.*$', '', line) # strip trailing comments line = re.sub('#.*$', '', line) # strip trailing comments
line = line.strip() line = line.strip()
@@ -100,7 +101,7 @@ class TagFile(object):
self.tags.append(Tag(tag, tagname, description, self.tags.append(Tag(tag, tagname, description,
self.filename, self.linenum)) self.filename, self.linenum))
except (IOError, OSError), e: except (IOError, OSError) as e:
self.AddError(str(e)) self.AddError(str(e))
@@ -128,8 +129,8 @@ def WriteOutput(output_file, data):
output_file = "<stdout>" output_file = "<stdout>"
else: else:
out = open(output_file, "wb") out = open(output_file, "wb")
out.write(data) out.write(str.encode(data))
out.close() out.close()
except (IOError, OSError), e: except (IOError, OSError) as e:
print >> sys.stderr, "failed to write %s: %s" % (output_file, e) print("failed to write %s: %s" % (output_file, e), file=sys.stderr)
sys.exit(1) sys.exit(1)

View File

@@ -1,4 +1,4 @@
#!/usr/bin/env python #!/usr/bin/env python3
# #
# Copyright (C) 2009 The Android Open Source Project # Copyright (C) 2009 The Android Open Source Project
# #
@@ -23,7 +23,7 @@ tags in the given input file.
-h to display this usage message and exit. -h to display this usage message and exit.
""" """
import cStringIO from io import StringIO
import getopt import getopt
import os import os
import os.path import os.path
@@ -36,24 +36,24 @@ output_file = None
try: try:
opts, args = getopt.getopt(sys.argv[1:], "ho:") opts, args = getopt.getopt(sys.argv[1:], "ho:")
except getopt.GetoptError, err: except getopt.GetoptError as err:
print str(err) print(str(err))
print __doc__ print(__doc__)
sys.exit(2) sys.exit(2)
for o, a in opts: for o, a in opts:
if o == "-h": if o == "-h":
print __doc__ print(__doc__)
sys.exit(2) sys.exit(2)
elif o == "-o": elif o == "-o":
output_file = a output_file = a
else: else:
print >> sys.stderr, "unhandled option %s" % (o,) print("unhandled option %s" % (o,), file=sys.stderr)
sys.exit(1) sys.exit(1)
if len(args) != 1 and len(args) != 2: if len(args) != 1 and len(args) != 2:
print "need one or two input files, not %d" % (len(args),) print("need one or two input files, not %d" % (len(args),))
print __doc__ print(__doc__)
sys.exit(1) sys.exit(1)
fn = args[0] fn = args[0]
@@ -92,10 +92,10 @@ if "javadoc_hide" in tagfile.options:
if tagfile.errors: if tagfile.errors:
for fn, ln, msg in tagfile.errors: for fn, ln, msg in tagfile.errors:
print >> sys.stderr, "%s:%d: error: %s" % (fn, ln, msg) print("%s:%d: error: %s" % (fn, ln, msg), file=sys.stderr)
sys.exit(1) sys.exit(1)
buffer = cStringIO.StringIO() buffer = StringIO()
buffer.write("/* This file is auto-generated. DO NOT MODIFY.\n" buffer.write("/* This file is auto-generated. DO NOT MODIFY.\n"
" * Source file: %s\n" " * Source file: %s\n"
" */\n\n" % (fn,)) " */\n\n" % (fn,))

View File

@@ -1,4 +1,4 @@
#!/usr/bin/env python #!/usr/bin/env python3
# #
# Copyright (C) 2009 The Android Open Source Project # Copyright (C) 2009 The Android Open Source Project
# #
@@ -24,7 +24,7 @@ and fails if they do.
-h to display this usage message and exit. -h to display this usage message and exit.
""" """
import cStringIO from io import StringIO
import getopt import getopt
try: try:
import hashlib import hashlib
@@ -48,21 +48,21 @@ ASSIGN_LIMIT = 1000000
try: try:
opts, args = getopt.getopt(sys.argv[1:], "ho:m:") opts, args = getopt.getopt(sys.argv[1:], "ho:m:")
except getopt.GetoptError, err: except getopt.GetoptError as err:
print str(err) print(str(err))
print __doc__ print(__doc__)
sys.exit(2) sys.exit(2)
for o, a in opts: for o, a in opts:
if o == "-h": if o == "-h":
print __doc__ print(__doc__)
sys.exit(2) sys.exit(2)
elif o == "-o": elif o == "-o":
output_file = a output_file = a
elif o == "-m": elif o == "-m":
pre_merged_file = a pre_merged_file = a
else: else:
print >> sys.stderr, "unhandled option %s" % (o,) print("unhandled option %s" % (o,), file=sys.stderr)
sys.exit(1) sys.exit(1)
# Restrictions on tags: # Restrictions on tags:
@@ -133,12 +133,12 @@ for fn in args:
if errors: if errors:
for fn, ln, msg in errors: for fn, ln, msg in errors:
print >> sys.stderr, "%s:%d: error: %s" % (fn, ln, msg) print("%s:%d: error: %s" % (fn, ln, msg), file=sys.stderr)
sys.exit(1) sys.exit(1)
if warnings: if warnings:
for fn, ln, msg in warnings: for fn, ln, msg in warnings:
print >> sys.stderr, "%s:%d: warning: %s" % (fn, ln, msg) print("%s:%d: warning: %s" % (fn, ln, msg), file=sys.stderr)
# Python's hash function (a) isn't great and (b) varies between # Python's hash function (a) isn't great and (b) varies between
# versions of python. Using md5 is overkill here but is the same from # versions of python. Using md5 is overkill here but is the same from
@@ -154,14 +154,14 @@ def hashname(str):
# If we were provided pre-merged tags (w/ the -m option), then don't # If we were provided pre-merged tags (w/ the -m option), then don't
# ever try to allocate one, just fail if we don't have a number # ever try to allocate one, just fail if we don't have a number
for name, t in sorted(by_tagname.iteritems()): for name, t in sorted(by_tagname.items()):
if t.tagnum is None: if t.tagnum is None:
if pre_merged_tags: if pre_merged_tags:
try: try:
t.tagnum = pre_merged_tags[t.tagname] t.tagnum = pre_merged_tags[t.tagname]
except KeyError: except KeyError:
print >> sys.stderr, ("Error: Tag number not defined for tag `%s'." print("Error: Tag number not defined for tag `%s'. Have you done a full build?" % t.tagname,
+" Have you done a full build?") % t.tagname file=sys.stderr)
sys.exit(1) sys.exit(1)
else: else:
while True: while True:
@@ -174,8 +174,8 @@ for name, t in sorted(by_tagname.iteritems()):
# by_tagnum should be complete now; we've assigned numbers to all tags. # by_tagnum should be complete now; we've assigned numbers to all tags.
buffer = cStringIO.StringIO() buffer = StringIO()
for n, t in sorted(by_tagnum.iteritems()): for n, t in sorted(by_tagnum.items()):
if t.description: if t.description:
buffer.write("%d %s %s\n" % (t.tagnum, t.tagname, t.description)) buffer.write("%d %s %s\n" % (t.tagnum, t.tagname, t.description))
else: else: