From 176ac65faf22fae7cf3d1874a97fc16636ef1be3 Mon Sep 17 00:00:00 2001
From: Masood Malekghassemi <atash@google.com>
Date: Tue, 3 May 2016 11:55:40 -0700
Subject: [PATCH] Don't truncate deps file on failure to find bazel

---
 tools/distrib/python/check_grpcio_tools.py |  5 ++---
 tools/distrib/python/make_grpcio_tools.py  | 18 +++++++++++++++---
 2 files changed, 17 insertions(+), 6 deletions(-)

diff --git a/tools/distrib/python/check_grpcio_tools.py b/tools/distrib/python/check_grpcio_tools.py
index f9f869e990..baf2ff4eff 100755
--- a/tools/distrib/python/check_grpcio_tools.py
+++ b/tools/distrib/python/check_grpcio_tools.py
@@ -37,10 +37,9 @@ OUT_OF_DATE_MESSAGE = """file {} is out of date
 
 Have you called tools/distrib/python/make_grpcio_tools.py since upgrading protobuf?"""
 
-check_protoc_lib_deps_file = cStringIO.StringIO()
-make.write_deps(make.BAZEL_DEPS_PROTOC_LIB_QUERY, check_protoc_lib_deps_file)
+check_protoc_lib_deps_content = make.get_deps(make.BAZEL_DEPS_PROTOC_LIB_QUERY)
 
 with open(make.GRPC_PYTHON_PROTOC_LIB_DEPS, 'r') as protoc_lib_deps_file:
-  if protoc_lib_deps_file.read() != check_protoc_lib_deps_file.getvalue():
+  if protoc_lib_deps_file.read() != check_protoc_lib_deps_content:
     print(OUT_OF_DATE_MESSAGE.format(make.GRPC_PYTHON_PROTOC_LIB_DEPS))
     raise SystemExit(1)
diff --git a/tools/distrib/python/make_grpcio_tools.py b/tools/distrib/python/make_grpcio_tools.py
index 27e7d5f0a4..50fbdbb14c 100755
--- a/tools/distrib/python/make_grpcio_tools.py
+++ b/tools/distrib/python/make_grpcio_tools.py
@@ -33,6 +33,8 @@ import os
 import os.path
 import shutil
 import subprocess
+import sys
+import traceback
 
 DEPS_FILE_CONTENT="""
 # Copyright 2016, Google Inc.
@@ -93,7 +95,7 @@ BAZEL_DEPS = os.path.join(GRPC_ROOT, 'tools/distrib/python/bazel_deps.sh')
 BAZEL_DEPS_PROTOC_LIB_QUERY = '//:protoc_lib'
 
 
-def write_deps(query, out_file):
+def get_deps(query):
   """Write the result of the bazel query `query` against protobuf to
      `out_file`."""
   output = subprocess.check_output([BAZEL_DEPS, query])
@@ -103,7 +105,7 @@ def write_deps(query, out_file):
       if name.endswith('.cc') and name.startswith(PROTOBUF_CC_PREFIX)]
   cc_files = [cc_file[len(PROTOBUF_CC_PREFIX):] for cc_file in cc_files]
   deps_file_content = DEPS_FILE_CONTENT.format(cc_files)
-  out_file.write(deps_file_content)
+  return deps_file_content
 
 
 def main():
@@ -120,8 +122,18 @@ def main():
   shutil.copytree(GRPC_PROTOC_PLUGINS, GRPC_PYTHON_PROTOC_PLUGINS)
   shutil.copytree(GRPC_INCLUDE, GRPC_PYTHON_INCLUDE)
 
+  try:
+    protoc_lib_deps_content = get_deps(BAZEL_DEPS_PROTOC_LIB_QUERY)
+  except Exception as error:
+    # We allow this script to succeed even if we couldn't get the dependencies,
+    # as then we can assume that even without a successful bazel run the
+    # dependencies currently in source control are 'good enough'.
+    sys.stderr.write("Got non-fatal error:\n")
+    traceback.print_exc(file=sys.stderr)
+    return
+  # If we successfully got the dependencies, truncate and rewrite the deps file.
   with open(GRPC_PYTHON_PROTOC_LIB_DEPS, 'w') as deps_file:
-    write_deps(BAZEL_DEPS_PROTOC_LIB_QUERY, deps_file)
+    deps_file.write(protoc_lib_deps_content)
 
 if __name__ == '__main__':
   main()
-- 
GitLab