Transform LAMMPS Python module into package

- Moves lammps.py into its own package
- Imports entire module in __init__.py
- Changes both how legacy and CMake build systems install
- Added traditional setup.py for Python-only installation

Note: the CMake install target runs setup.py build and install
in a way that produces files in CMAKE_BINARY_DIR/python instead
of python/build. This is to maintain out-of-source compilation
support.
This commit is contained in:
Richard Berger
2020-12-15 15:11:21 -05:00
parent 1fee2add51
commit aca2eefce5
6 changed files with 60 additions and 32 deletions

View File

@ -1,42 +1,42 @@
#!/usr/bin/env python
"""
Installer script to install the LAMMPS python module and the corresponding
Installer script to install the LAMMPS python package and the corresponding
shared library into either the system-wide site-packages tree, or - failing
that - into the corresponding user tree. Called from the 'install-python'
build target in the conventional and CMake based build systems
"""
# copy LAMMPS shared library and lammps.py to system dirs
# copy LAMMPS shared library and lammps package to system dirs
from __future__ import print_function
import sys,os,shutil
from argparse import ArgumentParser
parser = ArgumentParser(prog='install.py',
description='LAMMPS python module installer script')
description='LAMMPS python package installer script')
parser.add_argument("-m", "--module", required=True,
help="path to the source of the LAMMPS Python module")
parser.add_argument("-p", "--package", required=True,
help="path to the LAMMPS Python package")
parser.add_argument("-l", "--lib", required=True,
help="path to the compiled LAMMPS shared library")
parser.add_argument("-v", "--version", required=True,
help="path to the LAMMPS version.h header file")
parser.add_argument("-d","--dir",
help="Legacy custom installation folder selection for module and library")
help="Legacy custom installation folder selection for package and library")
args = parser.parse_args()
# validate arguments and make paths absolute
if args.module:
if not os.path.exists(args.module):
print( "ERROR: LAMMPS module file %s does not exist" % args.module)
if args.package:
if not os.path.exists(args.package):
print( "ERROR: LAMMPS package %s does not exist" % args.package)
parser.print_help()
sys.exit(1)
else:
args.module = os.path.abspath(args.module)
args.package = os.path.abspath(args.package)
if args.lib:
if not os.path.exists(args.lib):
@ -66,9 +66,9 @@ if args.dir:
# without any special processing or additional steps to that folder
if args.dir:
print("Copying LAMMPS Python module to custom folder %s" % args.dir)
print("Copying LAMMPS Python package to custom folder %s" % args.dir)
try:
shutil.copyfile(args.module, os.path.join(args.dir,'lammps.py'))
shutil.copytree(args.package, os.path.join(args.dir,'lammps'))
except shutil.Error:
pass # fail silently
@ -81,15 +81,19 @@ if args.dir:
sys.exit()
# extract version string from header
fp = open(args.version,'r')
txt=fp.read().split('"')[1].split()
verstr=txt[0]+txt[1]+txt[2]
fp.close()
def get_lammps_version(header):
with open(header, 'r') as f:
line = f.readline()
start_pos = line.find('"')+1
end_pos = line.find('"', start_pos)
return "".join(line[start_pos:end_pos].split())
print("Installing LAMMPS Python module version %s into site-packages folder" % verstr)
verstr = get_lammps_version(args.version)
# we need to switch to the folder of the python module
os.chdir(os.path.dirname(args.module))
print("Installing LAMMPS Python package version %s into site-packages folder" % verstr)
# we need to switch to the folder of the python package
os.chdir(os.path.dirname(args.package))
from distutils.core import setup
from distutils.sysconfig import get_python_lib
@ -103,10 +107,10 @@ try:
author = "Steve Plimpton",
author_email = "sjplimp@sandia.gov",
url = "https://lammps.sandia.gov",
description = "LAMMPS Molecular Dynamics Python module",
description = "LAMMPS Molecular Dynamics Python package",
license = "GPL",
py_modules = ["lammps"],
data_files = [(get_python_lib(), [args.lib])])
packages=['lammps'],
data_files = [(os.path.join(get_python_lib(), 'lammps'), [args.lib])])
except:
tryuser=True
print ("Installation into global site-packages folder failed.\nTrying user folder %s now." % site.USER_SITE)
@ -119,9 +123,9 @@ if tryuser:
author = "Steve Plimpton",
author_email = "sjplimp@sandia.gov",
url = "https://lammps.sandia.gov",
description = "LAMMPS Molecular Dynamics Python module",
description = "LAMMPS Molecular Dynamics Python package",
license = "GPL",
py_modules = ["lammps"],
data_files = [(site.USER_SITE, [args.lib])])
packages=['lammps'],
data_files = [(os.path.join(site.USER_SITE, 'lammps'), [args.lib])])
except:
print("Installation into user site package folder failed.")

View File

@ -0,0 +1 @@
from .lammps import *

26
python/setup.py Normal file
View File

@ -0,0 +1,26 @@
# this only installs the LAMMPS python package
# it assumes the LAMMPS shared library is already installed
from distutils.core import setup
import os
LAMMPS_PYTHON_DIR = os.path.dirname(os.path.realpath(__file__))
LAMMPS_DIR = os.path.dirname(LAMMPS_PYTHON_DIR)
LAMMPS_SOURCE_DIR = os.path.join(LAMMPS_DIR, 'src')
def get_lammps_version():
with open(os.path.join(LAMMPS_SOURCE_DIR, 'version.h'), 'r') as f:
line = f.readline()
start_pos = line.find('"')+1
end_pos = line.find('"', start_pos)
return "".join(line[start_pos:end_pos].split())
setup(
name = "lammps",
version = get_lammps_version(),
author = "Steve Plimpton",
author_email = "sjplimp@sandia.gov",
url = "https://lammps.sandia.gov",
description = "LAMMPS Molecular Dynamics Python package",
license = "GPL",
packages=["lammps"]
)