From 58cb65e11aba10f774f5f01deef8f23f0fee861f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20St=C3=A4hler?= Date: Mon, 2 Oct 2023 23:03:38 +0200 Subject: [PATCH 1/3] updated version file --- instaseis/version.py | 186 +++++++++++++++++++++++++++++++------------ 1 file changed, 133 insertions(+), 53 deletions(-) diff --git a/instaseis/version.py b/instaseis/version.py index 0c7a5ff5..deb65292 100644 --- a/instaseis/version.py +++ b/instaseis/version.py @@ -1,8 +1,6 @@ # -*- coding: utf-8 -*- # Author: Douglas Creager # This file is placed into the public domain. -# -# Modified by the ObsPy project and instaseis. # Calculates the current version number. If possible, this is the # output of “git describe”, modified to conform to the versioning @@ -32,87 +30,124 @@ # contains the following line: # # include RELEASE-VERSION -# NO IMPORTS FROM INSTASEIS OR FUTURE IN THIS FILE! (file gets used at -# installation time) - +# NO IMPORTS FROM OBSPY OR FUTURE IN THIS FILE! (file gets used at +# installation time) +import inspect import io import os -import inspect -from subprocess import Popen, PIPE +import re +from subprocess import STDOUT, CalledProcessError, check_output +import warnings -__all__ = "get_git_version" +__all__ = ["get_git_version"] -script_dir = os.path.abspath( - os.path.dirname(inspect.getfile(inspect.currentframe())) -) +script_dir = os.path.abspath(os.path.dirname(inspect.getfile( + inspect.currentframe()))) INSTASEIS_ROOT = os.path.abspath(os.path.join(script_dir, os.pardir)) -VERSION_FILE = os.path.join(script_dir, "RELEASE-VERSION") +VERSION_FILE = os.path.join(INSTASEIS_ROOT, "RELEASE-VERSION") -def call_git_describe(abbrev=4): # pragma: no cover +def call_git_describe(abbrev=10, dirty=True, + append_remote_tracking_branch=True): try: - p = Popen( - ["git", "rev-parse", "--show-toplevel"], - cwd=INSTASEIS_ROOT, - stdout=PIPE, - stderr=PIPE, - ) - p.stderr.close() - path = p.stdout.readline().decode().strip() - p.stdout.close() - except Exception: + p = check_output(['git', 'rev-parse', '--show-toplevel'], + cwd=INSTASEIS_ROOT, stderr=STDOUT) + path = p.decode().strip() + except (OSError, CalledProcessError): return None + if os.path.normpath(path) != INSTASEIS_ROOT: return None + + command = ['git', 'describe', '--abbrev=%d' % abbrev, '--always', '--tags'] + if dirty: + command.append("--dirty") try: - p = Popen( - [ - "git", - "describe", - "--dirty", - "--abbrev=%d" % abbrev, - "--always", - "--tags", - ], - cwd=INSTASEIS_ROOT, - stdout=PIPE, - stderr=PIPE, - ) - - p.stderr.close() - line = p.stdout.readline().decode() - p.stdout.close() - - if "-" not in line and "." not in line: - line = "0.0.0-g%s" % line - return line.strip() - except Exception: + p = check_output(['git', 'describe', '--dirty', '--abbrev=%d' % abbrev, + '--always', '--tags'], + cwd=INSTASEIS_ROOT, stderr=STDOUT) + line = p.decode().strip() + except (OSError, CalledProcessError): return None + remote_tracking_branch = None + if append_remote_tracking_branch: + try: + # find out local alias of remote and name of remote tracking branch + p = check_output(['git', 'branch', '-vv'], + cwd=INSTASEIS_ROOT, stderr=STDOUT) + remote_info = [line_.rstrip() + for line_ in p.decode().splitlines()] + remote_info = [line_ for line_ in remote_info + if line_.startswith('*')][0] + remote_info = re.sub(r".*? \[([^ :]*).*?\] .*", r"\1", remote_info) + remote, branch = remote_info.split("/") + # find out real name of remote + p = check_output(['git', 'remote', '-v'], + cwd=INSTASEIS_ROOT, stderr=STDOUT) + stdout = [line_.strip() for line_ in p.decode().splitlines()] + remote = [line_ for line_ in stdout + if line_.startswith(remote)][0].split()[1] + if remote.startswith("git@github.com:"): + remote = re.sub(r"git@github.com:(.*?)/.*", r"\1", remote) + elif remote.startswith("https://github.com/"): + remote = re.sub(r"https://github.com/(.*?)/.*", r"\1", remote) + elif remote.startswith("git://github.com"): + remote = re.sub(r"git://github.com/(.*?)/.*", r"\1", remote) + else: + remote = None + if remote is not None: + remote_tracking_branch = re.sub(r'[^A-Za-z0-9._-]', r'_', + '%s-%s' % (remote, branch)) + except (IndexError, OSError, ValueError, CalledProcessError): + pass + + # (this line prevents official releases) + # should work again now, see #482 and obspy/obspy@b437f31 + if "-" not in line and "." not in line: + version = "0.0.0.dev+0.g%s" % line + else: + parts = line.split('-', 1) + version = parts[0] + try: + modifier = '+' if '.post' in version else '.post+' + version += modifier + parts[1] + if remote_tracking_branch is not None: + version += '.' + remote_tracking_branch + # IndexError means we are at a release version tag cleanly, + # add nothing additional + except IndexError: + pass + return version + -def read_release_version(): # pragma: no cover + +def read_release_version(): try: with io.open(VERSION_FILE, "rt") as fh: version = fh.readline() return version.strip() - except Exception: + except IOError: return None -def write_release_version(version): # pragma: no cover + +def write_release_version(version): with io.open(VERSION_FILE, "wb") as fh: - fh.write(("%s\n" % version).encode("ascii", "strict")) + fh.write(("%s\n" % version).encode('ascii', 'strict')) + -def get_git_version(abbrev=4): # pragma: no cover +def get_git_version(abbrev=10, dirty=True, append_remote_tracking_branch=True): # Read in the version that's currently in RELEASE-VERSION. release_version = read_release_version() # First try to get the current version using “git describe”. - version = call_git_describe(abbrev) - + version = call_git_describe( + abbrev, dirty=dirty, + append_remote_tracking_branch=append_remote_tracking_branch) # If that doesn't work, fall back on the value that's in # RELEASE-VERSION. if version is None: @@ -120,7 +155,17 @@ def get_git_version(abbrev=4): # pragma: no cover # If we still don't have anything, that's an error. if version is None: - return "0.0.0-tar/zipball" + warnings.warn("ObsPy could not determine its version number. Make " + "sure it is properly installed. This for example " + "happens when installing from a zip archive " + "of the ObsPy repository which is not a supported way " + "of installing ObsPy.") + return '0.0.0+archive' + + # pip uses its normalized version number (strict PEP440) instead of our + # original version number, so we bow to pip and use the normalized version + # number internally, too, to avoid discrepancies. + version = _normalize_version(version) # If the current version is different from what's in the # RELEASE-VERSION file, update the file to be current. @@ -131,5 +176,40 @@ def get_git_version(abbrev=4): # pragma: no cover return version + +def _normalize_version(version): + """ + Normalize version number string to adhere with PEP440 strictly. + """ + pattern = ( + r'^[0-9]+?\.[0-9]+?\.[0-9]+?' + r'((a|b|rc)[0-9]+?)?' + r'(\.post[0-9]+?)?' + r'(\.dev[0-9]+?)?$' + ) + # we have a clean release version or another clean version + # according to PEP 440 + if re.match(pattern, version): + return version + # we have an old-style version (i.e. a git describe string), prepare it for + # the rest of clean up, i.e. put the '.post+' as separator for the local + # version number part + elif '.post' in version: + version = re.sub(r'-', '+', version, count=1) + elif re.match(r'^[0-9]+?\.[0-9]+?\.[0-9]+?-[0-9]+?-g[0-9a-z]+?$', version): + version = re.sub(r'-', '.post+', version, count=1) + # only adapt local version part right + version = re.match(r'(.*?\+)(.*)', version) + # no upper case letters + local_version = version.group(2).lower() + # only alphanumeric and "." in local part + local_version = re.sub(r'[^A-Za-z0-9.]', r'.', local_version) + version = version.group(1) + local_version + # make sure there's a "0" after ".post" + version = re.sub(r'\.post\+', r'.post0+', version) + return version + + + if __name__ == "__main__": print(get_git_version()) From f870473f396d2266843f2d46c0826fc49213ca46 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20St=C3=A4hler?= Date: Mon, 2 Oct 2023 23:26:10 +0200 Subject: [PATCH 2/3] Fix #86, PEP440 compatible version - use version.py script from current ObsPy --- instaseis/version.py | 30 ++++++++++++------------------ 1 file changed, 12 insertions(+), 18 deletions(-) diff --git a/instaseis/version.py b/instaseis/version.py index deb65292..aa212b85 100644 --- a/instaseis/version.py +++ b/instaseis/version.py @@ -111,15 +111,15 @@ def call_git_describe(abbrev=10, dirty=True, else: parts = line.split('-', 1) version = parts[0] - try: - modifier = '+' if '.post' in version else '.post+' - version += modifier + parts[1] - if remote_tracking_branch is not None: - version += '.' + remote_tracking_branch - # IndexError means we are at a release version tag cleanly, - # add nothing additional - except IndexError: - pass + # try: + # modifier = '+' if '.post' in version else '.post+' + # version += modifier + parts[1] + # if remote_tracking_branch is not None: + # version += '.' + remote_tracking_branch + # # IndexError means we are at a release version tag cleanly, + # # add nothing additional + # except IndexError: + # pass return version @@ -145,9 +145,8 @@ def get_git_version(abbrev=10, dirty=True, append_remote_tracking_branch=True): release_version = read_release_version() # First try to get the current version using “git describe”. - version = call_git_describe( - abbrev, dirty=dirty, - append_remote_tracking_branch=append_remote_tracking_branch) + version = call_git_describe(abbrev) + # If that doesn't work, fall back on the value that's in # RELEASE-VERSION. if version is None: @@ -155,12 +154,7 @@ def get_git_version(abbrev=10, dirty=True, append_remote_tracking_branch=True): # If we still don't have anything, that's an error. if version is None: - warnings.warn("ObsPy could not determine its version number. Make " - "sure it is properly installed. This for example " - "happens when installing from a zip archive " - "of the ObsPy repository which is not a supported way " - "of installing ObsPy.") - return '0.0.0+archive' + return "0.0.0-tar/zipball" # pip uses its normalized version number (strict PEP440) instead of our # original version number, so we bow to pip and use the normalized version From 1b71b1f112f693dd7eb93e21fe82ada559e1954b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20St=C3=A4hler?= Date: Mon, 2 Oct 2023 23:27:36 +0200 Subject: [PATCH 3/3] Fix #85: Order of Fortran source files - stupid workaround: Renamed global_parameters.f90 to all_global_parameters.f90 --- .../src/{global_parameters.f90 => all_global_parameters.f90} | 0 setup.py | 2 +- 2 files changed, 1 insertion(+), 1 deletion(-) rename instaseis/src/{global_parameters.f90 => all_global_parameters.f90} (100%) diff --git a/instaseis/src/global_parameters.f90 b/instaseis/src/all_global_parameters.f90 similarity index 100% rename from instaseis/src/global_parameters.f90 rename to instaseis/src/all_global_parameters.f90 diff --git a/setup.py b/setup.py index c43e3b2d..48eb8217 100644 --- a/setup.py +++ b/setup.py @@ -146,7 +146,7 @@ def get_libgfortran_dir(): library_dirs=get_libgfortran_dir(), # Be careful with the order. sources=[ - os.path.join(src, "global_parameters.f90"), + os.path.join(src, "all_global_parameters.f90"), os.path.join(src, "finite_elem_mapping.f90"), os.path.join(src, "spectral_basis.f90"), os.path.join(src, "sem_derivatives.f90"),