Retry npm calls since they seem a bit unreliable.

BUG=451985
R=kjellander@chromium.org

Review URL: https://codereview.chromium.org/878903003

git-svn-id: http://src.chromium.org/svn/trunk/deps/third_party/webrtc/webrtc.DEPS@293822 4ff67af0-8c30-449e-8e8b-ad334ec8d88c
diff --git a/build_apprtc_closure.py b/build_apprtc_closure.py
index 60703cf..81a8e76 100755
--- a/build_apprtc_closure.py
+++ b/build_apprtc_closure.py
@@ -11,7 +11,6 @@
 
 import os
 import shutil
-import subprocess
 import sys
 
 import utils
@@ -34,14 +33,15 @@
     npm_bin = os.path.join(node_path, 'bin', 'npm')
     node_bin = os.path.join(node_path, 'bin', 'node')
 
-  subprocess.check_call([npm_bin, 'install'])
+  utils.RunSubprocessWithRetry([npm_bin, 'install'])
   local_grunt_bin = os.path.join('node_modules', 'grunt-cli', 'bin', 'grunt')
 
   if not os.path.exists(local_grunt_bin):
     return ('Missing grunt-cli in the webrtc-samples checkout; did '
             'npm install fail?')
 
-  subprocess.check_call([node_bin, local_grunt_bin, 'closurecompiler:debug'])
+  utils.RunSubprocessWithRetry([node_bin, local_grunt_bin,
+                               'closurecompiler:debug'])
 
 
 if __name__ == '__main__':
diff --git a/utils.py b/utils.py
index 827cd3e..b32af09 100755
--- a/utils.py
+++ b/utils.py
@@ -11,9 +11,25 @@
 import sys
 import subprocess
 import tarfile
+import time
 import zipfile
 
 
+def RunSubprocessWithRetry(cmd):
+  """Invokes the subprocess and backs off exponentially on fail."""
+  for i in range(5):
+    try:
+      subprocess.check_call(cmd)
+      return
+    except subprocess.CalledProcessError as exception:
+      backoff = pow(2, i)
+      print 'Got %s, retrying in %d seconds...' % (exception, backoff)
+      time.sleep(backoff)
+  
+  print 'Giving up.'
+  raise exception
+
+
 def DownloadFilesFromGoogleStorage(path):
   print 'Downloading files in %s...' % path