From 62a152e4a2a5767d20edd637e9b02fb1b095d66f Mon Sep 17 00:00:00 2001 From: Axel Kohlmeyer Date: Tue, 9 Feb 2021 14:13:18 -0500 Subject: [PATCH] get version number from package version instead of rewriting the __init__.py file --- python/install.py | 33 +-------------------------------- python/lammps/__init__.py | 35 +++++++++++++++++++++++++++++++++-- 2 files changed, 34 insertions(+), 34 deletions(-) diff --git a/python/install.py b/python/install.py index bd0874593f..6765c33925 100644 --- a/python/install.py +++ b/python/install.py @@ -10,7 +10,7 @@ build target in the conventional and CMake based build systems # copy LAMMPS shared library and lammps package to system dirs from __future__ import print_function -import sys,os,re,shutil +import sys,os,shutil from argparse import ArgumentParser parser = ArgumentParser(prog='install.py', @@ -90,35 +90,11 @@ def get_lammps_version(header): verstr = get_lammps_version(args.version) -# convert string version to numeric version -vernum = 0 -vregex = re.compile(r"([0-9]+)([A-Za-z]+)(2[0-9]+)") -m = vregex.match(verstr) -if (m): - month2num = { 'Jan' : 1, 'Feb' : 2, 'Mar' : 3, 'Apr' : 4, 'May' : 5, 'Jun' : 6, - 'Jul' : 7, 'Aug' : 8, 'Sep' : 9, 'Oct' : 10, 'Nov' : 11, 'Dec' : 12 } - try: - vernum = int(m.group(3))*10000 - vernum += month2num[m.group(2)]*100 - vernum += int(m.group(1)) - except: - exit('Failure to parse version string: %s' % verstr) - 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)) - - -# update version number in lammps module -vregex = re.compile(r".*(__version__ += +)[0-9]+") -with open(os.path.join('lammps','__init__.py'), "r+") as f: - content = f.read() - f.seek(0) - f.write(re.sub(vregex, lambda match: '{}{}'.format(match.group(1), vernum), content)) - f.truncate() - from distutils.core import setup from distutils.sysconfig import get_python_lib import site @@ -150,10 +126,3 @@ if tryuser: setup(**setup_kwargs) except: print("Installation into user site package folder failed.") - -# restore __version__ == 0 for in place usage -with open(os.path.join('lammps','__init__.py'), "r+") as f: - content = f.read() - f.seek(0) - f.write(re.sub(vregex, lambda match: '{}{}'.format(match.group(1), 0), content)) - f.truncate() diff --git a/python/lammps/__init__.py b/python/lammps/__init__.py index be496167e8..89ff30ead9 100644 --- a/python/lammps/__init__.py +++ b/python/lammps/__init__.py @@ -13,6 +13,37 @@ from .core import * from .data import * from .pylammps import * -# automatically updated during installation +# convert module string version to numeric version +def get_version_number(): + import re + from sys import version_info + vstring = None + if version_info.major == 3 and version_info.minor >= 8: + from importlib.metadata import version + try: + vstring = version('lammps') + except: pass + else: + from pkg_resources import get_distribution + try: + vstring = get_distribution('lammps').version + except: pass -__version__ = 0 + if not vstring: + return 0 + + vregex = re.compile(r"([0-9]+)([A-Za-z]+)(2[0-9]+)") + m = vregex.match(vstring) + + if (m): + month2num = { 'Jan' : 1, 'Feb' : 2, 'Mar' : 3, 'Apr' : 4, 'May' : 5, 'Jun' : 6, + 'Jul' : 7, 'Aug' : 8, 'Sep' : 9, 'Oct' : 10, 'Nov' : 11, 'Dec' : 12 } + try: + vernum = int(m.group(3))*10000 + vernum += month2num[m.group(2)]*100 + vernum += int(m.group(1)) + except: + exit('Failure to parse version string: %s' % verstr) + return vernum + +__version__ = get_version_number()