build: python3 support
Porting of files to work with both Python 2 and Python 3.
We may want to introduce a CI run that runs with Python 3 to catch
any diversions early, run PyLint to lint compatibilty or introduce
tox to run scripts against multiple versions on the CI.
This doesn't port all files, just ones required to build and submit
CLs.
We may also want to include `six` rather than our `compat` module.
Change-Id: I72bf02daade0127e2141ee750c806d4f6e277d33
diff --git a/tools/gen_binary_descriptors b/tools/gen_binary_descriptors
index a6eb630..867ed90 100755
--- a/tools/gen_binary_descriptors
+++ b/tools/gen_binary_descriptors
@@ -24,6 +24,7 @@
import subprocess
import hashlib
import textwrap
+from compat import iteritems
SOURCE_TARGET = {
'protos/perfetto/config/perfetto_config.proto':
@@ -39,7 +40,7 @@
def hash_path(path):
hash = hashlib.sha1()
- with open(os.path.join(ROOT_DIR, path)) as f:
+ with open(os.path.join(ROOT_DIR, path), 'rb') as f:
hash.update(f.read())
return hash.hexdigest()
@@ -61,7 +62,7 @@
with open(target, 'rb') as f:
s = f.read()
- hashes = re.findall(r'// SHA1\((.*)\)\n// (.*)\n', s)
+ hashes = re.findall(r'// SHA1\((.*)\)\n// (.*)\n', s.decode())
assert sorted([SCRIPT_PATH, source]) == sorted([key for key, _ in hashes])
for path, expected_sha1 in hashes:
actual_sha1 = hash_path(os.path.join(ROOT_DIR, path))
@@ -86,7 +87,12 @@
s = fdescriptor.read()
proto_name = source_name[:-len('.proto')].title().replace("_", "")
constant_name = 'k' + proto_name + 'Descriptor'
- binary = '{' + ', '.join('{0:#04x}'.format(ord(c)) for c in s) + '}'
+ try:
+ ord(s[0])
+ ordinal = ord
+ except TypeError:
+ ordinal = lambda x: x
+ binary = '{' + ', '.join('{0:#04x}'.format(ordinal(c)) for c in s) + '}'
binary = textwrap.fill(binary,
width=80,
initial_indent=' ',
@@ -121,17 +127,17 @@
}} // namespace perfetto
#endif // {include_guard}
-""".format(**{
- 'proto_name': proto_name,
- 'size': len(s),
- 'constant_name': constant_name,
- 'binary': binary,
- 'include_guard': include_guard,
- 'script_path': SCRIPT_PATH,
- 'script_hash': hash_path(__file__),
- 'source_path': source,
- 'source_hash': hash_path(os.path.join(source)),
- }))
+""".format(
+ proto_name=proto_name,
+ size=len(s),
+ constant_name=constant_name,
+ binary=binary,
+ include_guard=include_guard,
+ script_path=SCRIPT_PATH,
+ script_hash=hash_path(__file__),
+ source_path=source,
+ source_hash=hash_path(os.path.join(source)),
+ ).encode())
def main():
@@ -141,7 +147,7 @@
args = parser.parse_args()
try:
- for source, target in SOURCE_TARGET.iteritems():
+ for source, target in iteritems(SOURCE_TARGET):
if args.check_only:
check(source, target)
else: