1#!/usr/bin/env python
2
3"""
4Install.py tool to download, compile, and setup the kim-api library
5used to automate the steps described in the README file in this dir
6"""
7
8from __future__ import print_function
9import sys, os, subprocess, shutil
10from argparse import ArgumentParser
11
12sys.path.append('..')
13from install_helpers import fullpath, geturl, checkmd5sum
14
15parser = ArgumentParser(prog='Install.py',
16                        description="LAMMPS library build wrapper script")
17
18# settings
19
20thisdir = fullpath('.')
21version = "2.2.1"
22
23# known checksums for different KIM-API versions. used to validate the download.
24checksums = { \
25        '2.1.2' : '6ac52e14ef52967fc7858220b208cba5', \
26        '2.1.3' : '6ee829a1bbba5f8b9874c88c4c4ebff8', \
27        '2.2.0' : 'e7f944e1593cffd7444679a660607f6c', \
28        '2.2.1' : 'ae1ddda2ef7017ea07934e519d023dca', \
29        }
30
31
32
33# help message
34
35HELP = """
36Syntax from src dir: make lib-kim args="-b -v version  -a kim-name"
37                 or: make lib-kim args="-b -a everything"
38                 or: make lib-kim args="-n -a kim-name"
39                 or: make lib-kim args="-p /usr/local/open-kim -a kim-name"
40Syntax from lib dir: python Install.py -b -v version  -a kim-name
41                 or: python Install.py -b -a everything
42                 or: python Install.py -n -a kim-name
43                 or: python Install.py -p /usr/local/open-kim -a kim-name
44
45Examples:
46
47make lib-kim args="-b" # install KIM API lib with only example models
48make lib-kim args="-b -a EAM_ErcolessiAdams_1994_Al__MO_324507536345_002" # Ditto plus one model
49make lib-kim args="-b -a everything"   # install KIM API lib with all models
50make lib-kim args="-n -a EAM_Dynamo_Ackland_2003_W__MO_141627196590_005"    # only add one model or model driver
51
52See the list of all KIM models here:
53https://openkim.org/browse/models
54"""
55
56pgroup = parser.add_mutually_exclusive_group()
57pgroup.add_argument("-b", "--build", action="store_true",
58                    help="download and build base KIM API library with example Models.")
59pgroup.add_argument("-n", "--nobuild", action="store_true",
60                    help="use the previously downloaded and compiled base KIM API.")
61pgroup.add_argument("-p", "--path",
62                    help="specify location of existing KIM API installation.")
63parser.add_argument("-v", "--version", default=version, choices=checksums.keys(),
64                    help="set version of KIM API library to download and build (default: %s)" % version)
65parser.add_argument("-a", "--add",
66                    help="add single KIM model or model driver. If adding 'everything', then all available OpenKIM models are added (may take a long time)")
67parser.add_argument("-vv", "--verbose", action="store_true",
68                    help="be more verbose about is happening while this script runs")
69
70args = parser.parse_args()
71
72# print help message and exit, if neither build nor path options are given
73if not args.build and not args.path and not args.nobuild:
74  parser.print_help()
75  sys.exit(HELP)
76
77buildflag = args.build
78pathflag = args.path is not None
79addflag = args.add is not None
80addmodelname = args.add
81everythingflag = False
82if addflag and addmodelname == "everything":
83  everythingflag = True
84  buildflag = True
85verboseflag = args.verbose
86version = args.version
87
88if pathflag:
89  buildflag = False
90  kimdir = args.path
91  if not os.path.isdir(kimdir):
92    sys.exit("KIM API path %s does not exist" % kimdir)
93  kimdir = fullpath(kimdir)
94
95url = "https://s3.openkim.org/kim-api/kim-api-%s.txz" % version
96
97# set KIM API directory
98
99if pathflag:
100  # configure LAMMPS to use existing kim-api installation
101  with open("%s/kim-prefix.txt" % thisdir, 'w') as pffile:
102    pffile.write("%s" % kimdir)
103
104  print("Created %s/kim-prefix.txt\n  using %s" % (thisdir,kimdir))
105else:
106  kimdir = os.path.join(os.path.abspath(thisdir), "installed-" + version)
107  if args.nobuild and not os.path.isdir(kimdir):
108    sys.exit("Cannot use -n/--nobuild without first building the KIM API with -b")
109
110# download KIM tarball, unpack, build KIM
111if buildflag:
112
113  # check to see if an installed kim-api already exists and wipe it out.
114
115  if os.path.isdir(kimdir):
116    print("kim-api is already installed at %s.\nRemoving it for re-install" % kimdir)
117    shutil.rmtree(kimdir)
118
119  # configure LAMMPS to use kim-api to be installed
120
121  with open("%s/kim-prefix.txt" % thisdir, 'w') as pffile:
122    pffile.write("%s" % kimdir)
123
124  print("Created %s/kim-prefix.txt\n  using %s" % (thisdir,kimdir))
125
126  # download entire kim-api tarball
127
128  print("Downloading kim-api tarball ...")
129  filename = "kim-api-%s.txz" % version
130  geturl(url, "%s/%s" % (thisdir, filename))
131
132  # verify downloaded archive integrity via md5 checksum, if known.
133  if version in checksums:
134    if not checkmd5sum(checksums[version], filename):
135      sys.exit("Checksum for KIM-API library does not match")
136
137  print("Unpacking kim-api tarball ...")
138  cmd = 'cd "%s"; rm -rf "kim-api-%s"; tar -xJvf %s' % (thisdir, version, filename)
139  subprocess.check_output(cmd, stderr=subprocess.STDOUT, shell=True)
140
141  # configure kim-api
142
143  print("Configuring kim-api ...")
144  cmd = 'cd "%s/kim-api-%s" && mkdir build && cd build && cmake .. -DCMAKE_INSTALL_PREFIX="%s" -DCMAKE_BUILD_TYPE=Release' % (thisdir,version,kimdir)
145  txt = subprocess.check_output(cmd,stderr=subprocess.STDOUT,shell=True)
146  if verboseflag: print(txt.decode("UTF-8"))
147
148  # build kim-api
149  print("Building kim-api ...")
150  cmd = 'cd "%s/kim-api-%s/build" && make -j2' % (thisdir, version)
151  txt = subprocess.check_output(cmd, stderr=subprocess.STDOUT, shell=True)
152  if verboseflag:
153    print(txt.decode("UTF-8"))
154
155  # install kim-api
156
157  print("Installing kim-api ...")
158  cmd = 'cd "%s/kim-api-%s/build" && make -j2 install' % (thisdir, version)
159  txt = subprocess.check_output(cmd, stderr=subprocess.STDOUT, shell=True)
160  if verboseflag:
161    print(txt.decode("UTF-8"))
162
163  # remove source files
164
165  print("Removing kim-api source and build files ...")
166  cmd = 'cd "%s"; rm -rf kim-api-%s; rm -rf kim-api-%s.txz' % (thisdir, version, version)
167  subprocess.check_output(cmd, stderr=subprocess.STDOUT, shell=True)
168
169  # add all OpenKIM models, if desired
170  if everythingflag:
171    print("Adding all OpenKIM models, this will take a while ...")
172    cmd = '%s/bin/kim-api-collections-management install system OpenKIM' % (kimdir)
173    txt = subprocess.check_output(cmd, stderr=subprocess.STDOUT, shell=True)
174    if verboseflag:
175      print(txt.decode("UTF-8"))
176
177# add single OpenKIM model
178if addflag:
179
180  pf_path = os.path.join(thisdir, "kim-prefix.txt")
181  if os.path.isfile(pf_path):
182    cmd = 'cat %s' % pf_path
183    kimdir = subprocess.check_output(cmd,stderr=subprocess.STDOUT,shell=True)
184
185  if not os.path.isdir(kimdir):
186    sys.exit("\nkim-api is not installed")
187
188  # download single model
189  cmd = '%s/bin/kim-api-collections-management install system %s' % (kimdir.decode("UTF-8"), addmodelname)
190  txt = subprocess.check_output(cmd, stderr=subprocess.STDOUT, shell=True)
191  if verboseflag:
192    print(txt.decode("UTF-8"))
193