Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
174 changes: 124 additions & 50 deletions instaseis/version.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
# -*- coding: utf-8 -*-
# Author: Douglas Creager <dcreager@dcreager.net>
# 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
Expand Down Expand Up @@ -32,81 +30,117 @@
# 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()

Expand All @@ -122,6 +156,11 @@ def get_git_version(abbrev=4): # pragma: no cover
if version is None:
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
# 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.
if version != release_version:
Expand All @@ -131,5 +170,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())
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down