tools/*.py: update to python3
only lint-hunks.py is tested as part of the presubmit; the rest may
need further changes as they're used.
Bug: b/229626362
Change-Id: I9d62a92cd10757c38eb898bf79bfe7f7e93caff2
diff --git a/tools/aggregate_entropy_stats.py b/tools/aggregate_entropy_stats.py
index 7cb4d18..0311681 100644
--- a/tools/aggregate_entropy_stats.py
+++ b/tools/aggregate_entropy_stats.py
@@ -1,4 +1,4 @@
-#!/usr/bin/env python
+#!/usr/bin/env python3
## Copyright (c) 2017, Alliance for Open Media. All rights reserved
##
## This source code is subject to the terms of the BSD 2 Clause License and
diff --git a/tools/diff.py b/tools/diff.py
index bac6aab..7bb6b7f 100644
--- a/tools/diff.py
+++ b/tools/diff.py
@@ -1,4 +1,4 @@
-#!/usr/bin/env python
+#!/usr/bin/env python3
##
## Copyright (c) 2016, Alliance for Open Media. All rights reserved
##
diff --git a/tools/gen_constrained_tokenset.py b/tools/gen_constrained_tokenset.py
index 5d12ee1..f5b0816 100755
--- a/tools/gen_constrained_tokenset.py
+++ b/tools/gen_constrained_tokenset.py
@@ -1,4 +1,4 @@
-#!/usr/bin/python
+#!/usr/bin/env python3
##
## Copyright (c) 2016, Alliance for Open Media. All rights reserved
##
@@ -108,7 +108,7 @@
for q in range(1, 256):
parray = get_quantized_spareto(q / 256., beta, bits, first_token)
assert parray.sum() == 2**bits
- print '{', ', '.join('%d' % i for i in parray), '},'
+ print('{', ', '.join('%d' % i for i in parray), '},')
if __name__ == '__main__':
diff --git a/tools/intersect-diffs.py b/tools/intersect-diffs.py
index df13c4e..9601836 100755
--- a/tools/intersect-diffs.py
+++ b/tools/intersect-diffs.py
@@ -1,4 +1,4 @@
-#!/usr/bin/env python
+#!/usr/bin/env python3
##
## Copyright (c) 2016, Alliance for Open Media. All rights reserved
##
@@ -71,7 +71,7 @@
break
if out_hunks:
- print FormatDiffHunks(out_hunks)
+ print(FormatDiffHunks(out_hunks))
sys.exit(1)
if __name__ == "__main__":
diff --git a/tools/lint-hunks.py b/tools/lint-hunks.py
index d02bee1..2267b5e 100755
--- a/tools/lint-hunks.py
+++ b/tools/lint-hunks.py
@@ -1,4 +1,4 @@
-#!/usr/bin/python
+#!/usr/bin/env python3
##
## Copyright (c) 2016, Alliance for Open Media. All rights reserved
##
@@ -12,7 +12,7 @@
"""Performs style checking on each diff hunk."""
import getopt
import os
-import StringIO
+import io
import subprocess
import sys
@@ -65,21 +65,21 @@
try:
try:
opts, args = getopt.getopt(argv[1:], SHORT_OPTIONS, LONG_OPTIONS)
- except getopt.error, msg:
+ except getopt.error as msg:
raise Usage(msg)
# process options
for o, _ in opts:
if o in ("-h", "--help"):
- print __doc__
+ print(__doc__)
sys.exit(0)
if args and len(args) > 1:
- print __doc__
+ print(__doc__)
sys.exit(0)
# Find the fully qualified path to the root of the tree
- tl = Subprocess(TOPLEVEL_CMD, stdout=subprocess.PIPE)
+ tl = Subprocess(TOPLEVEL_CMD, stdout=subprocess.PIPE, text=True)
tl = tl.communicate()[0].strip()
# See if we're working on the index or not.
@@ -95,9 +95,9 @@
# Get a list of all affected lines
file_affected_line_map = {}
- p = Subprocess(diff_cmd, stdout=subprocess.PIPE)
+ p = Subprocess(diff_cmd, stdout=subprocess.PIPE, text=True)
stdout = p.communicate()[0]
- for hunk in diff.ParseDiffHunks(StringIO.StringIO(stdout)):
+ for hunk in diff.ParseDiffHunks(io.StringIO(stdout)):
filename = hunk.right.filename[2:]
if filename not in file_affected_line_map:
file_affected_line_map[filename] = set()
@@ -105,9 +105,11 @@
# Run each affected file through cpplint
lint_failed = False
- for filename, affected_lines in file_affected_line_map.iteritems():
+ for filename, affected_lines in file_affected_line_map.items():
if filename.split(".")[-1] not in ("c", "h", "cc"):
continue
+ if filename.startswith("third_party"):
+ continue
if args:
# File contents come from git
@@ -129,17 +131,17 @@
continue
warning_line_num = int(fields[1])
if warning_line_num in affected_lines:
- print "%s:%d:%s"%(filename, warning_line_num,
- ":".join(fields[2:]))
+ print("%s:%d:%s"%(filename, warning_line_num,
+ ":".join(fields[2:])))
lint_failed = True
# Set exit code if any relevant lint errors seen
if lint_failed:
return 1
- except Usage, err:
- print >>sys.stderr, err
- print >>sys.stderr, "for help use --help"
+ except Usage as err:
+ print(err, file=sys.stderr)
+ print("for help use --help", file=sys.stderr)
return 2
if __name__ == "__main__":
diff --git a/tools/wrap-commit-msg.py b/tools/wrap-commit-msg.py
index 1c78824..c51ed09 100755
--- a/tools/wrap-commit-msg.py
+++ b/tools/wrap-commit-msg.py
@@ -1,4 +1,4 @@
-#!/usr/bin/env python
+#!/usr/bin/env python3
##
## Copyright (c) 2016, Alliance for Open Media. All rights reserved
##