From 23ca0099f7ab655f7ce30295d890f3cc214a2c7b Mon Sep 17 00:00:00 2001
From: Axel Kohlmeyer <akohlmey@gmail.com>
Date: Fri, 4 Aug 2017 14:47:32 -0400
Subject: [PATCH] more updates to kim/Install.py to restore missing
 functionality and align with other scripts

- build into local directory to replace existing installation is now default
- add wrapper function that calls curl in case python package has not ssl support
- have to specify -n flag to avoid wiping out the existing installation
- can specify -p to point to an existing kim-api installation (implies -n)
---
 lib/kim/.gitignore |   1 +
 lib/kim/Install.py | 102 ++++++++++++++++++++++++++++++---------------
 2 files changed, 70 insertions(+), 33 deletions(-)

diff --git a/lib/kim/.gitignore b/lib/kim/.gitignore
index 3be8ecbdd6..c1f57fe64c 100644
--- a/lib/kim/.gitignore
+++ b/lib/kim/.gitignore
@@ -1,2 +1,3 @@
 /Makefile.KIM_DIR
 /Makefile.KIM_Config
+/installed-kim-api-*
diff --git a/lib/kim/Install.py b/lib/kim/Install.py
index 1d022a5875..315bb4e11c 100644
--- a/lib/kim/Install.py
+++ b/lib/kim/Install.py
@@ -4,18 +4,30 @@
 # used to automate the steps described in the README file in this dir
 from __future__ import print_function
 import sys,os,re,subprocess
-try: from urllib.request import urlretrieve as geturl
-except: from urllib import urlretrieve as geturl
+
+# transparently use either urllib or an external tool
+try:
+  import ssl
+  try: from urllib.request import urlretrieve as geturl
+  except: from urllib import urlretrieve as geturl
+except:
+  def geturl(url,fname):
+    cmd = "curl -o %s %s" % (fname,url)
+    txt = subprocess.check_output(cmd,stderr=subprocess.STDOUT,shell=True)
+    return txt
 
 help = """
-Syntax from src dir: make lib-kim args="-v version -b -a kim-name"
-Syntax from lib dir: python Install.py -v version -b -a kim-name
+Syntax from src dir: make lib-kim args="-v version  -a kim-name"
+Syntax from lib dir: python Install.py -v version  -a kim-name
 
 specify one or more options, order does not matter
 
   -v = version of KIM API library to use
        default = kim-api-v1.8.2 (current as of June 2017)
-  -b = download and build KIM API library with example Models
+  -b = download and build base KIM API library with example Models (default)
+       this will delete any previous installation in the current folder
+  -n = do NOT download and build base KIM API library. 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).
        If kim-name = everything, then rebuild KIM API library with
@@ -24,10 +36,10 @@ specify one or more options, order does not matter
 
 Examples:
 
-make lib-kim args="-b"   # install KIM API lib with only example models
-make lib-kim args="-b -a Glue_Ercolessi_Adams_Al__MO_324507536345_001"  # Ditto plus one model
-make lib-kim args="-b -a everything"   # install KIM API lib with all models
-make lib-kim args="-a EAM_Dynamo_Ackland_W__MO_141627196590_002"   # add one model or model driver
+make lib-kim           # install KIM API lib with only example models
+make lib-kim args="-a Glue_Ercolessi_Adams_Al__MO_324507536345_001"  # Ditto plus one model
+make lib-kim args="-a everything"   # install KIM API lib with all models
+make lib-kim args="-n -a EAM_Dynamo_Ackland_W__MO_141627196590_002"   # only add one model or model driver
 
 See the list of KIM model drivers here:
 https://openkim.org/kim-items/model-drivers/alphabetical
@@ -44,19 +56,25 @@ def error():
   print(help)
   sys.exit()
 
+# expand to full path name
+# process leading '~' or relative path
+
+def fullpath(path):
+  return os.path.abspath(os.path.expanduser(path))
+
 # parse args
 
 args = sys.argv[1:]
 nargs = len(args)
-if nargs == 0: error()
 
 thisdir = os.environ['PWD']
 version = "kim-api-v1.8.2"
 
-buildflag = False
+buildflag = True
 everythingflag = False
 addflag = False
 verboseflag = False
+pathflag = False
 
 iarg = 0
 while iarg < len(args):
@@ -67,6 +85,15 @@ while iarg < len(args):
   elif args[iarg] == "-b":
     buildflag = True
     iarg += 1
+  elif args[iarg] == "-n":
+    buildflag = False
+    iarg += 1
+  elif args[iarg] == "-p":
+    if iarg+2 > len(args): error()
+    kimdir = fullpath(args[iarg+1])
+    pathflag = True
+    buildflag = False
+    iarg += 2
   elif args[iarg] == "-a":
     addflag = True
     if iarg+2 > len(args): error()
@@ -84,34 +111,49 @@ while iarg < len(args):
 thisdir = os.path.abspath(thisdir)
 url = "https://s3.openkim.org/kim-api/%s.tgz" % version
 
-# download KIM tarball, unpack, build KIM
-# either in lib/kim or user-requested location
+# set KIM API directory
 
-if buildflag:
+if pathflag:
+  if not os.path.isdir(kimdir):
+    print("\nkim-api is not installed at %s" % kimdir)
+    error()
 
-  # set install directory
+  # configure LAMMPS to use existing kim-api installation
+  with open("%s/Makefile.KIM_DIR" % thisdir, 'w') as mkfile:
+    mkfile.write("KIM_INSTALL_DIR=%s\n\n" % kimdir)
+    mkfile.write(".DUMMY: print_dir\n\n")
+    mkfile.write("print_dir:\n")
+    mkfile.write("	@printf $(KIM_INSTALL_DIR)\n")
+
+  with open("%s/Makefile.KIM_Config" % thisdir, 'w') as cfgfile:
+    cfgfile.write("include %s/lib/kim-api/Makefile.KIM_Config" % kimdir)
 
-  dir = os.path.join(os.path.abspath(thisdir), "installed-" + version)
+  print("Created %s/Makefile.KIM_DIR\n  using %s" % (thisdir,kimdir))
+else:
+  kimdir = os.path.join(os.path.abspath(thisdir), "installed-" + version)
+
+# download KIM tarball, unpack, build KIM
+if buildflag:
 
   # check to see if an installed kim-api already exists and wipe it out.
 
-  if os.path.isdir(dir):
-    print("kim-api is already installed at %s.\nRemoving it for re-install" % dir)
-    cmd = "rm -rf %s" % dir
+  if os.path.isdir(kimdir):
+    print("kim-api is already installed at %s.\nRemoving it for re-install" % kimdir)
+    cmd = "rm -rf %s" % kimdir
     subprocess.check_output(cmd,stderr=subprocess.STDOUT,shell=True)
 
   # configure LAMMPS to use kim-api to be installed
 
   with open("%s/Makefile.KIM_DIR" % thisdir, 'w') as mkfile:
-    mkfile.write("KIM_INSTALL_DIR=%s\n\n" % dir)
+    mkfile.write("KIM_INSTALL_DIR=%s\n\n" % kimdir)
     mkfile.write(".DUMMY: print_dir\n\n")
     mkfile.write("print_dir:\n")
     mkfile.write("	@printf $(KIM_INSTALL_DIR)\n")
 
   with open("%s/Makefile.KIM_Config" % thisdir, 'w') as cfgfile:
-    cfgfile.write("include %s/lib/kim-api/Makefile.KIM_Config" % dir)
+    cfgfile.write("include %s/lib/kim-api/Makefile.KIM_Config" % kimdir)
 
-  print("Created %s/Makefile.KIM_DIR : using %s" % (thisdir,dir))
+  print("Created %s/Makefile.KIM_DIR\n  using %s" % (thisdir,kimdir))
 
   # download entire kim-api tarball
 
@@ -124,7 +166,7 @@ if buildflag:
   # configure kim-api
 
   print("Configuring kim-api ...")
-  cmd = "cd %s/%s; ./configure --prefix='%s'" % (thisdir,version,dir)
+  cmd = "cd %s/%s; ./configure --prefix='%s'" % (thisdir,version,kimdir)
   subprocess.check_output(cmd,stderr=subprocess.STDOUT,shell=True)
 
   # build kim-api
@@ -166,17 +208,11 @@ if buildflag:
 
 if addflag:
 
-  # get location of installed kim-api
-
-  if not os.path.isfile("%s/Makefile.KIM_DIR" % thisdir):
-    print("kim-api is not installed")
+  if not os.path.isdir(kimdir):
+    print("\nkim-api is not installed")
     error()
-  else:
-    cmd = "cd %s; make -f Makefile.KIM_DIR print_dir" % thisdir
-    dir = subprocess.check_output(cmd,stderr=subprocess.STDOUT,shell=True)[1]
 
   # download single model
-  # try first via urllib
 
   print("Downloading tarball for %s..." % addmodelname)
   url = "https://openkim.org/download/%s.tgz" % addmodelname
@@ -206,7 +242,7 @@ if addflag:
       txt = subprocess.check_output(cmd,stderr=subprocess.STDOUT,shell=True)
       adddrivername = txt.decode("UTF-8").strip()
       print("First installing model driver: %s..." % adddrivername)
-      cmd = "cd %s; python Install.py -a %s" % (thisdir,adddrivername)
+      cmd = "cd %s; python Install.py -n -a %s" % (thisdir,adddrivername)
       try:
         txt = subprocess.check_output(cmd,stderr=subprocess.STDOUT,shell=True)
       except subprocess.CalledProcessError as e:
@@ -218,7 +254,7 @@ if addflag:
       # now install the model that needed the driver
 
       print("Now installing model : %s" % addmodelname)
-      cmd = "cd %s; python Install.py -a %s" % (thisdir,addmodelname)
+      cmd = "cd %s; python Install.py -n -a %s" % (thisdir,addmodelname)
       try:
         txt = subprocess.check_output(cmd,stderr=subprocess.STDOUT,shell=True)
       except subprocess.CalledProcessError as e:
-- 
GitLab