update install.py script to build a wheel and install that

This commit is contained in:
Axel Kohlmeyer
2022-02-24 21:47:37 -05:00
parent 891d3c8296
commit 4b7731e831
5 changed files with 44 additions and 98 deletions

View File

@ -751,10 +751,8 @@ if(BUILD_SHARED_LIBS)
if(Python_EXECUTABLE) if(Python_EXECUTABLE)
add_custom_target( add_custom_target(
install-python ${CMAKE_COMMAND} -E remove_directory build install-python ${CMAKE_COMMAND} -E remove_directory build
COMMAND ${Python_EXECUTABLE} install.py -v ${LAMMPS_SOURCE_DIR}/version.h COMMAND ${Python_EXECUTABLE} ${LAMMPS_PYTHON_DIR}/install.py -p ${LAMMPS_PYTHON_DIR}/lammps
-p ${LAMMPS_PYTHON_DIR}/lammps
-l ${CMAKE_BINARY_DIR}/liblammps${LAMMPS_MACHINE}${CMAKE_SHARED_LIBRARY_SUFFIX} -l ${CMAKE_BINARY_DIR}/liblammps${LAMMPS_MACHINE}${CMAKE_SHARED_LIBRARY_SUFFIX}
WORKING_DIRECTORY ${LAMMPS_PYTHON_DIR}
COMMENT "Installing LAMMPS Python module") COMMENT "Installing LAMMPS Python module")
else() else()
add_custom_target( add_custom_target(

View File

@ -10,7 +10,7 @@ build target in the conventional and CMake based build systems
# copy LAMMPS shared library and lammps package to system dirs # copy LAMMPS shared library and lammps package to system dirs
from __future__ import print_function from __future__ import print_function
import sys,os,shutil,time import sys,os,shutil,time,glob
from argparse import ArgumentParser from argparse import ArgumentParser
parser = ArgumentParser(prog='install.py', parser = ArgumentParser(prog='install.py',
@ -20,11 +20,6 @@ parser.add_argument("-p", "--package", required=True,
help="path to the LAMMPS Python package") help="path to the LAMMPS Python package")
parser.add_argument("-l", "--lib", required=True, parser.add_argument("-l", "--lib", required=True,
help="path to the compiled LAMMPS shared library") 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 package and library")
args = parser.parse_args() args = parser.parse_args()
@ -46,91 +41,31 @@ if args.lib:
else: else:
args.lib = os.path.abspath(args.lib) args.lib = os.path.abspath(args.lib)
if args.version:
if not os.path.exists(args.version):
print( "ERROR: LAMMPS version header file %s does not exist" % args.version)
parser.print_help()
sys.exit(1)
else:
args.version = os.path.abspath(args.version)
if args.dir:
if not os.path.isdir(args.dir):
print( "ERROR: Installation folder %s does not exist" % args.dir)
parser.print_help()
sys.exit(1)
else:
args.dir = os.path.abspath(args.dir)
# if a custom directory is given, we copy the files directly
# without any special processing or additional steps to that folder
if args.dir:
print("Copying LAMMPS Python package to custom folder %s" % args.dir)
try:
shutil.copytree(args.package, os.path.join(args.dir,'lammps'))
except shutil.Error:
pass # fail silently
print("Copying LAMMPS shared library to custom folder %s" % args.dir)
try:
shutil.copyfile(args.lib, os.path.join(args.dir,os.path.basename(args.lib)))
except shutil.Error:
pass # fail silently
sys.exit()
# extract LAMMPS version string from header
# and convert to python packaging compatible version
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)
t = time.strptime("".join(line[start_pos:end_pos].split()), "%d%b%Y")
return "{}.{}.{}".format(t.tm_year,t.tm_mon,t.tm_mday)
verstr = get_lammps_version(args.version)
print("Installing LAMMPS Python package version %s into site-packages folder" % verstr)
# we need to switch to the folder of the python package # we need to switch to the folder of the python package
olddir = os.path.abspath('.')
os.chdir(os.path.dirname(args.package)) os.chdir(os.path.dirname(args.package))
from distutils.core import setup print("Purging existing wheels...")
from distutils.sysconfig import get_python_lib for wheel in glob.glob('lammps-*.whl'):
import site print("deleting " + wheel)
from sys import version_info os.remove(wheel)
if version_info.major >= 3: # create virtual environment for building the wheel
pkgs = ['lammps', 'lammps.mliap'] shutil.rmtree('buildwheel',True)
else: os.putenv('LAMMPS_SHARED_LIB',args.lib)
pkgs = ['lammps'] #os.environ['LAMMPS_SHARED_LIB'] = args.lib
shutil.copy(args.lib,'lammps')
os.system(sys.executable + ' -m virtualenv buildwheel')
os.system(sys.executable + ' makewheel.py')
#Arguments common to global or user install -- everything but data_files # remove temporary folders and files
setup_kwargs= dict(name="lammps", shutil.rmtree('buildwheel',True)
version=verstr, shutil.rmtree('build',True)
author="Steve Plimpton", shutil.rmtree('lammps.egg-info',True)
author_email="sjplimp@sandia.gov", os.remove(os.path.join('lammps',os.path.basename(args.lib)))
url="https://www.lammps.org",
description="LAMMPS Molecular Dynamics Python package",
license="GPL",
packages=pkgs,
)
tryuser=False print("Installing wheel")
try: for wheel in glob.glob('lammps-*.whl'):
sys.argv = ["setup.py","install"] # as if had run "python setup.py install" os.system(sys.executable + " -m pip install --force-reinstall " + wheel)
setup_kwargs['data_files']=[(os.path.join(get_python_lib(), 'lammps'), [args.lib])] shutil.copy(wheel, olddir)
setup(**setup_kwargs) os.remove(wheel)
except: # lgtm [py/catch-base-exception]
tryuser=True
print ("Installation into global site-packages folder failed.\nTrying user folder %s now." % site.USER_SITE)
if tryuser:
try:
sys.argv = ["setup.py","install","--user"] # as if had run "python setup.py install --user"
setup_kwargs['data_files']=[(os.path.join(site.USER_SITE, 'lammps'), [args.lib])]
setup(**setup_kwargs)
except: # lgtm [py/catch-base-exception]
print("Installation into user site package folder failed.")

15
python/makewheel.py Normal file
View File

@ -0,0 +1,15 @@
#!/usr/bin/env python
import sys,os,shutil
if sys.platform == 'win32':
virtenv=os.path.join('buildwheel','Scripts','activate_this.py')
else:
virtenv=os.path.join('buildwheel','bin','activate_this.py')
exec(open(virtenv).read(), {'__file__': virtenv})
os.system('python -m pip install --upgrade pip')
os.system('python -m pip install --upgrade -r wheel_requirements.txt')
print("Building new binary wheel")
os.system('python -m build --wheel -o .')

View File

@ -3,7 +3,7 @@
from setuptools import setup from setuptools import setup
from setuptools.dist import Distribution from setuptools.dist import Distribution
from sys import version_info from sys import version_info
import os,time import os,time,shutil
LAMMPS_PYTHON_DIR = os.path.dirname(os.path.realpath(__file__)) LAMMPS_PYTHON_DIR = os.path.dirname(os.path.realpath(__file__))
LAMMPS_DIR = os.path.dirname(LAMMPS_PYTHON_DIR) LAMMPS_DIR = os.path.dirname(LAMMPS_PYTHON_DIR)
LAMMPS_SOURCE_DIR = os.path.join(LAMMPS_DIR, 'src') LAMMPS_SOURCE_DIR = os.path.join(LAMMPS_DIR, 'src')
@ -27,8 +27,7 @@ class BinaryDistribution(Distribution):
def has_ext_modules(foo): def has_ext_modules(foo):
return True return True
libpath = os.environ.get("LAMMPS_SHARED_LIB") libname = os.path.basename(os.environ.get("LAMMPS_SHARED_LIB"))
if version_info.major >= 3: if version_info.major >= 3:
pkgs = ['lammps', 'lammps.mliap'] pkgs = ['lammps', 'lammps.mliap']
else: else:
@ -37,8 +36,8 @@ else:
with open("README", "r", encoding="utf-8") as fh: with open("README", "r", encoding="utf-8") as fh:
long_description = fh.read() long_description = fh.read()
if libpath: if libname:
pkgdata = {'lammps': [ libpath ]} pkgdata = {'lammps': [ libname ]}
bdist = BinaryDistribution bdist = BinaryDistribution
else: else:
pkgdata = {} pkgdata = {}

View File

@ -458,8 +458,7 @@ mpi-stubs:
sinclude ../lib/python/Makefile.lammps sinclude ../lib/python/Makefile.lammps
install-python: install-python:
@rm -rf ../python/build @rm -rf ../python/build
@$(PYTHON) ../python/install.py -v ../src/version.h \ @$(PYTHON) ../python/install.py -p ../python/lammps -l ../src/liblammps.so
-p ../python/lammps -l ../src/liblammps.so
# Create a tarball of src dir and packages # Create a tarball of src dir and packages