(depot_tools) Move gclient_sync_retry to depot_tools

b/35473293

Change-Id: I30caa802a428ca35debe9a11d9d4ea81baaa321c
diff --git a/gclient_sync_retry b/gclient_sync_retry
new file mode 100755
index 0000000..4d51b73
--- /dev/null
+++ b/gclient_sync_retry
@@ -0,0 +1,22 @@
+#!/usr/bin/env bash
+# Copyright (c) 2017 Google Inc. All rights reserved.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+base_dir=$(dirname "$0")
+
+if [[ "#grep#fetch#cleanup#diff#" != *"#$1#"* ]]; then
+  "$base_dir"/update_depot_tools "$@"
+fi
+
+PYTHONDONTWRITEBYTECODE=1 exec python "$base_dir/gclient_sync_retry.py" "$@"
diff --git a/gclient_sync_retry.bat b/gclient_sync_retry.bat
new file mode 100755
index 0000000..3c1fa6d
--- /dev/null
+++ b/gclient_sync_retry.bat
@@ -0,0 +1,24 @@
+@echo off

+:: Copyright (c) 2017 Google Inc. All rights reserved.

+::

+:: Licensed under the Apache License, Version 2.0 (the "License");

+:: you may not use this file except in compliance with the License.

+:: You may obtain a copy of the License at

+::

+::     http://www.apache.org/licenses/LICENSE-2.0

+::

+:: Unless required by applicable law or agreed to in writing, software

+:: distributed under the License is distributed on an "AS IS" BASIS,

+:: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

+:: See the License for the specific language governing permissions and

+:: limitations under the License.

+setlocal

+

+:: This is required with cygwin only.

+PATH=%~dp0;%PATH%

+

+:: Synchronize the root directory before deferring control back to gclient.py.

+call "%~dp0\update_depot_tools.bat" %*

+

+:: Defer control.

+%~dp0python "%~dp0\gclient_sync_retry.py" %*

diff --git a/gclient_sync_retry.py b/gclient_sync_retry.py
new file mode 100644
index 0000000..7fd17c0
--- /dev/null
+++ b/gclient_sync_retry.py
@@ -0,0 +1,57 @@
+# Copyright 2017 Google Inc. All Rights Reserved.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+"""Gclient sync, retry if failed to connect."""
+
+import random
+import subprocess
+import sys
+import time
+
+
+def main():
+  command = [
+      'gclient',
+      'sync',
+      '--verbose',
+      # Calls git reset --hard HEAD on each
+      # repository
+      '--reset',
+      # Force update even for unchanged
+      # repositories
+      '--force'
+  ]
+
+  print command
+
+  max_runs = 3
+  fail_string = 'Failed to connect'
+  for run in range(1, max_runs + 1):
+    sys.stdout.write('Attempt %d\n' % run)
+    p = subprocess.Popen(
+        command,
+        stdout=subprocess.PIPE,
+        stderr=subprocess.PIPE,
+        stdin=subprocess.PIPE)
+    p_stdout, p_stderr = p.communicate('')
+    sys.stdout.write(p_stdout)
+    sys.stderr.write(p_stderr)
+    if run < max_runs and (fail_string in p_stderr or fail_string in p_stdout):
+      sys.stdout.write('Retrying gclient sync\n')
+      time.sleep(5 * random.randint(0, 2**run - 1))
+      continue
+    break
+
+
+if __name__ == '__main__':
+  sys.exit(main())