1#!/usr/bin/env python
2
3"""
4Installer script to install the LAMMPS python package and the corresponding
5shared library into either the system-wide site-packages tree, or - failing
6that - into the corresponding user tree. Called from the 'install-python'
7build target in the conventional and CMake based build systems
8"""
9
10# copy LAMMPS shared library and lammps package to system dirs
11
12from __future__ import print_function
13import sys,os,shutil,time
14from argparse import ArgumentParser
15
16parser = ArgumentParser(prog='install.py',
17                        description='LAMMPS python package installer script')
18
19parser.add_argument("-p", "--package", required=True,
20                    help="path to the LAMMPS Python package")
21parser.add_argument("-l", "--lib", required=True,
22                    help="path to the compiled LAMMPS shared library")
23parser.add_argument("-v", "--version", required=True,
24                    help="path to the LAMMPS version.h header file")
25
26parser.add_argument("-d","--dir",
27                    help="Legacy custom installation folder selection for package and library")
28
29args = parser.parse_args()
30
31# validate arguments and make paths absolute
32
33if args.package:
34  if not os.path.exists(args.package):
35    print( "ERROR: LAMMPS package %s does not exist" % args.package)
36    parser.print_help()
37    sys.exit(1)
38  else:
39    args.package = os.path.abspath(args.package)
40
41if args.lib:
42  if not os.path.exists(args.lib):
43    print( "ERROR: LAMMPS shared library %s does not exist" % args.lib)
44    parser.print_help()
45    sys.exit(1)
46  else:
47    args.lib = os.path.abspath(args.lib)
48
49if args.version:
50  if not os.path.exists(args.version):
51    print( "ERROR: LAMMPS version header file %s does not exist" % args.version)
52    parser.print_help()
53    sys.exit(1)
54  else:
55    args.version = os.path.abspath(args.version)
56
57if args.dir:
58  if not os.path.isdir(args.dir):
59    print( "ERROR: Installation folder %s does not exist" % args.dir)
60    parser.print_help()
61    sys.exit(1)
62  else:
63    args.dir = os.path.abspath(args.dir)
64
65# if a custom directory is given, we copy the files directly
66# without any special processing or additional steps to that folder
67
68if args.dir:
69  print("Copying LAMMPS Python package to custom folder %s" % args.dir)
70  try:
71    shutil.copytree(args.package, os.path.join(args.dir,'lammps'))
72  except shutil.Error:
73    pass # fail silently
74
75  print("Copying LAMMPS shared library to custom folder %s" % args.dir)
76  try:
77    shutil.copyfile(args.lib, os.path.join(args.dir,os.path.basename(args.lib)))
78  except shutil.Error:
79    pass # fail silently
80
81  sys.exit()
82
83# extract LAMMPS version string from header
84# and convert to python packaging compatible version
85def get_lammps_version(header):
86    with open(header, 'r') as f:
87        line = f.readline()
88        start_pos = line.find('"')+1
89        end_pos = line.find('"', start_pos)
90        t = time.strptime("".join(line[start_pos:end_pos].split()), "%d%b%Y")
91        return "{}.{}.{}".format(t.tm_year,t.tm_mon,t.tm_mday)
92
93verstr = get_lammps_version(args.version)
94
95print("Installing LAMMPS Python package version %s into site-packages folder" % verstr)
96
97# we need to switch to the folder of the python package
98os.chdir(os.path.dirname(args.package))
99
100from distutils.core import setup
101from distutils.sysconfig import get_python_lib
102import site
103from sys import version_info
104
105if version_info.major >= 3:
106    pkgs = ['lammps', 'lammps.mliap']
107else:
108    pkgs = ['lammps']
109
110#Arguments common to global or user install -- everything but data_files
111setup_kwargs= dict(name="lammps",
112        version=verstr,
113        author="Steve Plimpton",
114        author_email="sjplimp@sandia.gov",
115        url="https://www.lammps.org",
116        description="LAMMPS Molecular Dynamics Python package",
117        license="GPL",
118        packages=pkgs,
119        )
120
121tryuser=False
122try:
123  sys.argv = ["setup.py","install"]    # as if had run "python setup.py install"
124  setup_kwargs['data_files']=[(os.path.join(get_python_lib(), 'lammps'), [args.lib])]
125  setup(**setup_kwargs)
126except:                                # lgtm [py/catch-base-exception]
127  tryuser=True
128  print ("Installation into global site-packages folder failed.\nTrying user folder %s now." % site.USER_SITE)
129
130if tryuser:
131  try:
132    sys.argv = ["setup.py","install","--user"]    # as if had run "python setup.py install --user"
133    setup_kwargs['data_files']=[(os.path.join(site.USER_SITE, 'lammps'), [args.lib])]
134    setup(**setup_kwargs)
135  except:                              # lgtm [py/catch-base-exception]
136    print("Installation into user site package folder failed.")
137