From de255d9b8bfd8426206344e701eab002f2149765 Mon Sep 17 00:00:00 2001 From: David Ostrovsky Date: Sun, 14 Jan 2018 11:09:43 +0100 Subject: [PATCH] Bazel: Make build tool chain python 3 compatible This change establishes Python 3 compatibility for the major part of Gerrit build tool chain. Only two scripts remain non Python 3 compatible: * Documentation/replace_macros.py * tools/bzl/license-map.py Those scripts explicitly invoked with python2 version. Test Plan: a. Python 2.7 * Switch to system where /usr/bin/python points to Python 2.7 * bazel build release * bazel test //... * tools/eclipse/project.py b. Python 3.6 * Switch to system where /usr/bin/python points to Python 3.6 * bazel build release * bazel test //... * tools/eclipse/project.py Pre-requisites for the test plan: In case bazel action and repository caching is activated on the SUT, the caches would need to be wiped out, to make sure that the complete build tool chain was tested. On my system I had to run these commands: * bazel clean --expunge_async * rm -rf ~/.gerritcodereview/buck-cache/downloaded-artifacts/* * rm -rf ~/.gerritcodereview/bazel-cache/cas/* * rm -rf ~/.gerritcodereview/bazel-cache/repository/* Bug: Issue 8151 Change-Id: Iece59d0c5149b77a02754b3fed4ce84d5d8085ee (cherry picked from commit 0c9e13c11a9a0facd1538d6e5b45b02a90db5ab3) --- Documentation/replace_macros.py | 2 +- tools/bzl/license.bzl | 2 +- tools/eclipse/project.py | 2 +- tools/js/bowerutil.py | 4 ++-- tools/js/download_bower.py | 2 +- tools/js/npm_pack.py | 2 +- 6 files changed, 7 insertions(+), 7 deletions(-) diff --git a/Documentation/replace_macros.py b/Documentation/replace_macros.py index baf08e7d58..2996a98aea 100755 --- a/Documentation/replace_macros.py +++ b/Documentation/replace_macros.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/env python2 # coding=utf-8 # Copyright (C) 2013 The Android Open Source Project # diff --git a/tools/bzl/license.bzl b/tools/bzl/license.bzl index 38dfbe5e28..357817303a 100644 --- a/tools/bzl/license.bzl +++ b/tools/bzl/license.bzl @@ -25,7 +25,7 @@ def license_map(name, targets = [], opts = [], **kwargs): # post process the XML into our favorite format. native.genrule( name = "gen_license_txt_" + name, - cmd = "python $(location //tools/bzl:license-map.py) %s %s > $@" % (" ".join(opts), " ".join(xmls)), + cmd = "python2 $(location //tools/bzl:license-map.py) %s %s > $@" % (" ".join(opts), " ".join(xmls)), outs = [ name + ".txt" ], tools = tools, **kwargs diff --git a/tools/eclipse/project.py b/tools/eclipse/project.py index 97c2b12b3f..86bcf5569f 100755 --- a/tools/eclipse/project.py +++ b/tools/eclipse/project.py @@ -262,7 +262,7 @@ def gen_factorypath(ext): doc.writexml(fd, addindent='\t', newl='\n', encoding='UTF-8') try: - ext_location = retrieve_ext_location() + ext_location = retrieve_ext_location().decode("utf-8") gen_project(args.project_name) gen_classpath(ext_location) gen_factorypath(ext_location) diff --git a/tools/js/bowerutil.py b/tools/js/bowerutil.py index 8e8e835a46..c2e11cdde2 100644 --- a/tools/js/bowerutil.py +++ b/tools/js/bowerutil.py @@ -40,7 +40,7 @@ def hash_bower_component(hash_obj, path): if f == '.bower.json': continue p = os.path.join(root, f) - hash_obj.update(p[len(path)+1:]) - hash_obj.update(open(p).read()) + hash_obj.update(p[len(path)+1:].encode("utf-8")) + hash_obj.update(open(p, "rb").read()) return hash_obj diff --git a/tools/js/download_bower.py b/tools/js/download_bower.py index 80cb56e402..3db39d5bfa 100755 --- a/tools/js/download_bower.py +++ b/tools/js/download_bower.py @@ -68,7 +68,7 @@ def ignore_deps(info): deps = info.get('dependencies') if deps: with open(os.path.join('.bowerrc'), 'w') as f: - json.dump({'ignoredDependencies': deps.keys()}, f) + json.dump({'ignoredDependencies': list(deps.keys())}, f) def cache_entry(name, package, version, sha1): diff --git a/tools/js/npm_pack.py b/tools/js/npm_pack.py index 9eb6e34365..52dc512056 100755 --- a/tools/js/npm_pack.py +++ b/tools/js/npm_pack.py @@ -36,7 +36,7 @@ def is_bundled(tar): def bundle_dependencies(): with open('package.json') as f: package = json.load(f) - package['bundledDependencies'] = package['dependencies'].keys() + package['bundledDependencies'] = list(package['dependencies'].keys()) with open('package.json', 'w') as f: json.dump(package, f)