1#!/usr/bin/env python
2
3"""
4Install.py tool to download, unpack, build, and link to the MS-CG 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, tarfile
10from argparse import ArgumentParser
11
12sys.path.append('..')
13from install_helpers import fullpath, geturl
14
15parser = ArgumentParser(prog='Install.py',
16                        description="LAMMPS library build wrapper script")
17
18# settings
19
20version = "1.7.3.1"
21machine = "g++_simple"
22
23# help message
24
25HELP = """
26Syntax from src dir: make lib-mscg args="-p [path] -m [suffix] -v [version]"
27                 or: make lib-mscg args="-b -m [suffix]"
28Syntax from lib dir: python Install.py -p [path]  -m [suffix] -v [version]
29Syntax from lib dir: python Install.py -b -m [suffix]
30
31Example:
32
33make lib-mscg args="-b -m serial " # download/build in lib/mscg/MSCG-release with settings compatible with "make serial"
34make lib-mscg args="-b -m mpi " # download/build in lib/mscg/MSCG-release with settings compatible with "make mpi"
35make lib-mscg args="-p /usr/local/mscg-release " # use existing MS-CG installation in /usr/local/mscg-release
36"""
37
38# known checksums for different MSCG versions. used to validate the download.
39checksums = { \
40        '1.7.3.1' : '8c45e269ee13f60b303edd7823866a91', \
41        }
42
43# parse and process arguments
44
45pgroup = parser.add_mutually_exclusive_group()
46pgroup.add_argument("-b", "--build", action="store_true",
47                    help="download and build the MSCG library")
48pgroup.add_argument("-p", "--path",
49                    help="specify folder of existing MSCG installation")
50parser.add_argument("-v", "--version", default=version, choices=checksums.keys(),
51                    help="set version of MSCG to download and build (default: %s)" % version)
52parser.add_argument("-m", "--machine", default=machine, choices=['mpi', 'serial', 'g++_simple', 'intel_simple', 'lapack', 'mac'],
53                    help="set machine suffix specifies which src/Make/Makefile.suffix to use. (default: %s)" % machine)
54
55args = parser.parse_args()
56
57# print help message and exit, if neither build nor path options are given
58if not args.build and not args.path:
59  parser.print_help()
60  sys.exit(HELP)
61
62buildflag = args.build
63pathflag = args.path is not None
64mscgpath = args.path
65msuffix = args.machine
66mscgver = args.version
67
68# settings
69
70url = "https://github.com/uchicago-voth/MSCG-release/archive/%s.tar.gz" % mscgver
71tarname = "MS-CG-%s.tar.gz" % mscgver
72tardir = "MSCG-release-%s" % mscgver
73
74homepath = fullpath('.')
75homedir = os.path.join(homepath, tardir)
76
77if pathflag:
78    if not os.path.isdir(mscgpath):
79      sys.exit("MS-CG path %s does not exist" % mscgpath)
80    homedir = fullpath(mscgpath)
81
82# download and unpack MS-CG tarfile
83
84if buildflag:
85  print("Downloading MS-CG ...")
86  tarname = os.path.join(homepath, tarname)
87  geturl(url, tarname)
88
89  print("Unpacking MS-CG tarfile ...")
90
91  if os.path.exists(os.path.join(homepath, tardir)):
92    shutil.rmtree(os.path.join(homepath, tardir))
93
94  if tarfile.is_tarfile(tarname):
95    tgz = tarfile.open(tarname)
96    tgz.extractall(path=homepath)
97    os.remove(tarname)
98  else:
99    sys.exit("File %s is not a supported archive", tarname)
100
101  if os.path.basename(homedir) != tardir:
102    if os.path.exists(homedir):
103      shutil.rmtree(homedir)
104    os.rename(os.path.join(homepath, tardir), homedir)
105
106# build MS-CG
107
108if buildflag:
109  print("Building MS-CG ...")
110  mkf = "Makefile.%s" % msuffix
111  mkp = os.path.join(homedir, 'src', 'Make', mkf)
112  if os.path.exists(mkp):
113    shutil.copyfile(mkp, os.path.join(homedir, 'src', mkf))
114  elif os.path.exists("Makefile.%s" % msuffix):
115    shutil.copyfile("Makefile.%s" % msuffix, os.path.join(homedir, 'src', mkf))
116  else:
117    sys.exit("Cannot find Makefile.%s" % msuffix)
118  try:
119    cmd = 'make -C %s -f Makefile.%s' % (os.path.join(homedir, 'src'), msuffix)
120    txt = subprocess.check_output(cmd, stderr=subprocess.STDOUT, shell=True)
121    print(txt.decode('UTF-8'))
122  except subprocess.CalledProcessError as e:
123    print("Make failed with:\n %s" % e.output.decode('UTF-8'))
124    sys.exit(1)
125
126  if not os.path.exists("Makefile.lammps"):
127    print("Creating Makefile.lammps")
128    if os.path.exists("Makefile.lammps.%s" % msuffix):
129      shutil.copyfile('Makefile.lammps.%s' % msuffix, 'Makefile.lammps')
130    else:
131      shutil.copyfile('Makefile.lammps.default', 'Makefile.lammps')
132  else: print("Makefile.lammps exists. Please check its settings")
133
134# create 2 links in lib/mscg to MS-CG src dir
135
136print("Creating links to MS-CG include and lib files")
137if os.path.isfile("includelink") or os.path.islink("includelink"):
138  os.remove("includelink")
139if os.path.isfile("liblink") or os.path.islink("liblink"):
140  os.remove("liblink")
141os.symlink(os.path.join(homedir, 'src'), 'includelink')
142os.symlink(os.path.join(homedir, 'src'), 'liblink')
143