From c6d923b6c889bc93d7e6e638c5622bf933b573d1 Mon Sep 17 00:00:00 2001
From: Axel Kohlmeyer <akohlmey@gmail.com>
Date: Thu, 10 Aug 2017 21:15:35 -0400
Subject: [PATCH] make download function more resilient by also catching failed
 execution

---
 lib/kim/Install.py     | 26 +++++++++++++++++++++-----
 lib/mscg/Install.py    | 22 ++++++++++++++++++----
 lib/smd/Install.py     | 22 ++++++++++++++++++----
 lib/voronoi/Install.py | 22 ++++++++++++++++++----
 4 files changed, 75 insertions(+), 17 deletions(-)

diff --git a/lib/kim/Install.py b/lib/kim/Install.py
index 21ea859852..aa244ee6ea 100644
--- a/lib/kim/Install.py
+++ b/lib/kim/Install.py
@@ -6,6 +6,8 @@
 from __future__ import print_function
 import sys,os,re,subprocess
 
+# help message
+
 help = """
 Syntax from src dir: make lib-kim args="-b -v version  -a kim-name"
                  or: make lib-kim args="-b -a everything"
@@ -23,7 +25,7 @@ specify one or more options, order does not matter
   -b = download and build base KIM API library with example Models
        this will delete any previous installation in the current folder
   -n = do NOT download and build base KIM API library.
-       Use an existing installation 
+       Use an existing installation
   -p = specify location of KIM API installation (implies -n)
   -a = add single KIM model or model driver with kim-name
        to existing KIM API lib (see example below).
@@ -78,13 +80,27 @@ def which(program):
   return None
 
 def geturl(url,fname):
+  success = False
+
   if which('curl') != None:
     cmd = 'curl -L -o "%s" %s' % (fname,url)
-  elif which('wget') != None:
+    try:
+      subprocess.check_output(cmd,stderr=subprocess.STDOUT,shell=True)
+      success = True
+    except subprocess.CalledProcessError as e:
+      print("Calling curl failed with: %s" % e.output.decode('UTF-8'))
+
+  if not success and which('wget') != None:
     cmd = 'wget -O "%s" %s' % (fname,url)
-  else: error("cannot find 'wget' or 'curl' to download source code")
-  txt = subprocess.check_output(cmd,stderr=subprocess.STDOUT,shell=True)
-  return txt
+    try:
+      subprocess.check_output(cmd,stderr=subprocess.STDOUT,shell=True)
+      success = True
+    except subprocess.CalledProcessError as e:
+      print("Calling wget failed with: %s" % e.output.decode('UTF-8'))
+
+  if not success:
+    error("Failed to download source code with 'curl' or 'wget'")
+  return
 
 # parse args
 
diff --git a/lib/mscg/Install.py b/lib/mscg/Install.py
index 154f5aa522..76c986ef6d 100644
--- a/lib/mscg/Install.py
+++ b/lib/mscg/Install.py
@@ -65,13 +65,27 @@ def which(program):
   return None
 
 def geturl(url,fname):
+  success = False
+
   if which('curl') != None:
     cmd = 'curl -L -o "%s" %s' % (fname,url)
-  elif which('wget') != None:
+    try:
+      subprocess.check_output(cmd,stderr=subprocess.STDOUT,shell=True)
+      success = True
+    except subprocess.CalledProcessError as e:
+      print("Calling curl failed with: %s" % e.output.decode('UTF-8'))
+
+  if not success and which('wget') != None:
     cmd = 'wget -O "%s" %s' % (fname,url)
-  else: error("cannot find 'wget' or 'curl' to download source code")
-  txt = subprocess.check_output(cmd,stderr=subprocess.STDOUT,shell=True)
-  return txt
+    try:
+      subprocess.check_output(cmd,stderr=subprocess.STDOUT,shell=True)
+      success = True
+    except subprocess.CalledProcessError as e:
+      print("Calling wget failed with: %s" % e.output.decode('UTF-8'))
+
+  if not success:
+    error("Failed to download source code with 'curl' or 'wget'")
+  return
 
 # parse args
 
diff --git a/lib/smd/Install.py b/lib/smd/Install.py
index 00891339d0..9247cb449b 100644
--- a/lib/smd/Install.py
+++ b/lib/smd/Install.py
@@ -65,13 +65,27 @@ def which(program):
   return None
 
 def geturl(url,fname):
+  success = False
+
   if which('curl') != None:
     cmd = 'curl -L -o "%s" %s' % (fname,url)
-  elif which('wget') != None:
+    try:
+      subprocess.check_output(cmd,stderr=subprocess.STDOUT,shell=True)
+      success = True
+    except subprocess.CalledProcessError as e:
+      print("Calling curl failed with: %s" % e.output.decode('UTF-8'))
+
+  if not success and which('wget') != None:
     cmd = 'wget -O "%s" %s' % (fname,url)
-  else: error("cannot find 'wget' or 'curl' to download source code")
-  txt = subprocess.check_output(cmd,stderr=subprocess.STDOUT,shell=True)
-  return txt
+    try:
+      subprocess.check_output(cmd,stderr=subprocess.STDOUT,shell=True)
+      success = True
+    except subprocess.CalledProcessError as e:
+      print("Calling wget failed with: %s" % e.output.decode('UTF-8'))
+
+  if not success:
+    error("Failed to download source code with 'curl' or 'wget'")
+  return
 
 # parse args
 
diff --git a/lib/voronoi/Install.py b/lib/voronoi/Install.py
index 4998358d27..f40eb53bc6 100644
--- a/lib/voronoi/Install.py
+++ b/lib/voronoi/Install.py
@@ -64,13 +64,27 @@ def which(program):
   return None
 
 def geturl(url,fname):
+  success = False
+
   if which('curl') != None:
     cmd = 'curl -L -o "%s" %s' % (fname,url)
-  elif which('wget') != None:
+    try:
+      subprocess.check_output(cmd,stderr=subprocess.STDOUT,shell=True)
+      success = True
+    except subprocess.CalledProcessError as e:
+      print("Calling curl failed with: %s" % e.output.decode('UTF-8'))
+
+  if not success and which('wget') != None:
     cmd = 'wget -O "%s" %s' % (fname,url)
-  else: error("cannot find 'wget' or 'curl' to download source code")
-  txt = subprocess.check_output(cmd,stderr=subprocess.STDOUT,shell=True)
-  return txt
+    try:
+      subprocess.check_output(cmd,stderr=subprocess.STDOUT,shell=True)
+      success = True
+    except subprocess.CalledProcessError as e:
+      print("Calling wget failed with: %s" % e.output.decode('UTF-8'))
+
+  if not success:
+    error("Failed to download source code with 'curl' or 'wget'")
+  return
 
 # parse args
 
-- 
GitLab