From b3efe882bcb27c02d4d030c4f71c0c32c48ee8ae Mon Sep 17 00:00:00 2001 From: Moritz Hoffmann Date: Thu, 3 Mar 2022 13:17:23 +0100 Subject: [PATCH] Use CMake as primary build system via scikit-build (#215) * general except clause in module_available * switched to scikit-build as build system * update versioneer * remove legacy dependency specifiers from setup.py * use pytest xdist with 2 jobs --- .gitignore | 1 + CMakeLists.txt | 47 +- deeptime/__init__.py | 1 - deeptime/_version.py | 250 ++++-- deeptime/basis/CMakeLists.txt | 8 +- deeptime/basis/setup.py | 5 - deeptime/clustering/CMakeLists.txt | 6 +- deeptime/clustering/setup.py | 6 - deeptime/covariance/setup.py | 8 - .../covariance/util/covar_c/CMakeLists.txt | 5 +- deeptime/covariance/util/covar_c/setup.py | 5 - deeptime/data/CMakeLists.txt | 8 +- deeptime/data/setup.py | 7 - deeptime/decomposition/setup.py | 6 - deeptime/kernels/setup.py | 0 deeptime/markov/CMakeLists.txt | 9 + deeptime/markov/_bindings/CMakeLists.txt | 8 - deeptime/markov/hmm/CMakeLists.txt | 4 + deeptime/markov/hmm/_bindings/CMakeLists.txt | 8 - deeptime/markov/hmm/init/setup.py | 8 - deeptime/markov/hmm/setup.py | 9 - .../hmm/{_bindings => }/src/hmm_module.cpp | 0 deeptime/markov/msm/setup.py | 6 - deeptime/markov/msm/tram/CMakeLists.txt | 4 + .../markov/msm/tram/_bindings/CMakeLists.txt | 5 - deeptime/markov/msm/tram/setup.py | 5 - deeptime/markov/setup.py | 12 - .../{_bindings => }/src/markov_module.cpp | 0 .../tools/estimation/dense/CMakeLists.txt | 3 + .../estimation/dense/_bindings/CMakeLists.txt | 7 - .../markov/tools/estimation/dense/setup.py | 6 - .../dense/{_bindings => }/src/mle_module.cpp | 0 deeptime/markov/tools/estimation/setup.py | 8 - .../tools/estimation/sparse/CMakeLists.txt | 3 + .../sparse/_bindings/CMakeLists.txt | 7 - .../markov/tools/estimation/sparse/setup.py | 5 - .../{_bindings => }/src/mle_sparse_module.cpp | 0 deeptime/markov/tools/setup.py | 11 - deeptime/numeric/CMakeLists.txt | 4 + deeptime/numeric/_bindings/CMakeLists.txt | 8 - deeptime/numeric/setup.py | 8 - .../{_bindings => }/src/numeric_module.cpp | 0 deeptime/setup.py | 23 - deeptime/src/include/deeptime/data/systems.h | 6 +- deeptime/util/platform.py | 6 + devtools/conda-recipe/meta.yaml | 10 +- devtools/setup+build+test.yml | 2 +- noxfile.py | 13 +- pyproject.toml | 14 +- setup.cfg | 1 - setup.py | 148 +--- tests/requirements.txt | 1 + versioneer.py | 713 ++++++++++++------ 53 files changed, 834 insertions(+), 614 deletions(-) delete mode 100644 deeptime/basis/setup.py delete mode 100644 deeptime/clustering/setup.py delete mode 100644 deeptime/covariance/setup.py delete mode 100644 deeptime/covariance/util/covar_c/setup.py delete mode 100644 deeptime/data/setup.py delete mode 100644 deeptime/decomposition/setup.py delete mode 100644 deeptime/kernels/setup.py create mode 100644 deeptime/markov/CMakeLists.txt delete mode 100644 deeptime/markov/_bindings/CMakeLists.txt create mode 100644 deeptime/markov/hmm/CMakeLists.txt delete mode 100644 deeptime/markov/hmm/_bindings/CMakeLists.txt delete mode 100644 deeptime/markov/hmm/init/setup.py delete mode 100644 deeptime/markov/hmm/setup.py rename deeptime/markov/hmm/{_bindings => }/src/hmm_module.cpp (100%) delete mode 100644 deeptime/markov/msm/setup.py create mode 100644 deeptime/markov/msm/tram/CMakeLists.txt delete mode 100644 deeptime/markov/msm/tram/_bindings/CMakeLists.txt delete mode 100644 deeptime/markov/msm/tram/setup.py delete mode 100644 deeptime/markov/setup.py rename deeptime/markov/{_bindings => }/src/markov_module.cpp (100%) create mode 100644 deeptime/markov/tools/estimation/dense/CMakeLists.txt delete mode 100644 deeptime/markov/tools/estimation/dense/_bindings/CMakeLists.txt delete mode 100644 deeptime/markov/tools/estimation/dense/setup.py rename deeptime/markov/tools/estimation/dense/{_bindings => }/src/mle_module.cpp (100%) delete mode 100644 deeptime/markov/tools/estimation/setup.py create mode 100644 deeptime/markov/tools/estimation/sparse/CMakeLists.txt delete mode 100644 deeptime/markov/tools/estimation/sparse/_bindings/CMakeLists.txt delete mode 100644 deeptime/markov/tools/estimation/sparse/setup.py rename deeptime/markov/tools/estimation/sparse/{_bindings => }/src/mle_sparse_module.cpp (100%) delete mode 100644 deeptime/markov/tools/setup.py create mode 100644 deeptime/numeric/CMakeLists.txt delete mode 100644 deeptime/numeric/_bindings/CMakeLists.txt delete mode 100644 deeptime/numeric/setup.py rename deeptime/numeric/{_bindings => }/src/numeric_module.cpp (100%) delete mode 100644 deeptime/setup.py diff --git a/.gitignore b/.gitignore index 199d92794..1b9333c32 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ .idea build +_skbuild *.so *.egg-info cmake-build-* diff --git a/CMakeLists.txt b/CMakeLists.txt index 49e92827c..6ac665593 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,13 +1,29 @@ -cmake_minimum_required(VERSION 3.9) +cmake_minimum_required(VERSION 3.15...3.19) set(CMAKE_CXX_STANDARD 17) +set(CMAKE_CXX_STANDARD_REQUIRED ON) +set(DEEPTIME_VERSION "0.0.0" CACHE STRING "The version without commit offset. Defaults to 0.0.0") +set(DEEPTIME_VERSION_INFO "0.0.0" CACHE STRING "The full version. Defaults to 0.0.0") set(DEEPTIME_BUILD_CPP_TESTS OFF CACHE BOOL "Whether to build the c++ unit tests") if(DEFINED PROJECT_NAME) set(DEEPTIME_IS_SUBPROJECT ON) endif() -project(deeptime LANGUAGES C CXX VERSION 0.0.0) +project(deeptime LANGUAGES C CXX VERSION ${DEEPTIME_VERSION}) +set(DEEPTIME_ROOT_DIRECTORY "${CMAKE_CURRENT_LIST_DIR}") + +if(SKBUILD) + # Scikit-Build does not add your site-packages to the search path + # automatically, so we need to add it _or_ the pybind11 specific directory + # here. + execute_process( + COMMAND "${PYTHON_EXECUTABLE}" -c + "import pybind11; print(pybind11.get_cmake_dir())" + OUTPUT_VARIABLE _tmp_dir + OUTPUT_STRIP_TRAILING_WHITESPACE COMMAND_ECHO STDOUT) + list(APPEND CMAKE_PREFIX_PATH "${_tmp_dir}") +endif() find_package(OpenMP 4) if(OpenMP_FOUND) @@ -18,19 +34,32 @@ if(MSVC) add_compile_options(/W3 /EHsc /bigobj /permissive- /std:c++17) endif() -find_package(pybind11 REQUIRED) +# use cmake 3.12+ FindPython +# find_package(Python COMPONENTS Interpreter Development) # this doesn't work with conda, picks up the wrong python interpreter on OSX +# now find pybind +find_package(pybind11 REQUIRED CONFIG) + +function(register_pybind_module name) + cmake_parse_arguments(PARSE_ARGV 1 ARG "" "" "") + pybind11_add_module(${name} ${ARG_UNPARSED_ARGUMENTS}) + target_link_libraries(${name} PUBLIC deeptime::deeptime) + if (OpenMP_FOUND) + target_link_libraries(${name} PUBLIC OpenMP::OpenMP_CXX) + endif() + file(RELATIVE_PATH REL_PATH "${DEEPTIME_ROOT_DIRECTORY}/deeptime" "${CMAKE_CURRENT_LIST_DIR}") + install(TARGETS ${name} DESTINATION ${REL_PATH}) + if(NOT SKBUILD) + set_target_properties(${name} PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${CMAKE_CURRENT_LIST_DIR}) + endif() +endfunction() add_subdirectory(deeptime/src) -add_subdirectory(deeptime/numeric/_bindings) +add_subdirectory(deeptime/numeric) add_subdirectory(deeptime/data) add_subdirectory(deeptime/covariance/util/covar_c) add_subdirectory(deeptime/clustering) add_subdirectory(deeptime/basis) -add_subdirectory(deeptime/markov/_bindings) -add_subdirectory(deeptime/markov/hmm/_bindings) -add_subdirectory(deeptime/markov/msm/tram/_bindings) -add_subdirectory(deeptime/markov/tools/estimation/dense/_bindings) -add_subdirectory(deeptime/markov/tools/estimation/sparse/_bindings) +add_subdirectory(deeptime/markov) add_subdirectory(examples/clustering_custom_metric) diff --git a/deeptime/__init__.py b/deeptime/__init__.py index 735c45a25..6ae8983fc 100644 --- a/deeptime/__init__.py +++ b/deeptime/__init__.py @@ -1,5 +1,4 @@ from ._version import get_versions - __version__ = get_versions()['version'] del get_versions diff --git a/deeptime/_version.py b/deeptime/_version.py index 2a809351e..32964bbda 100644 --- a/deeptime/_version.py +++ b/deeptime/_version.py @@ -6,7 +6,7 @@ # that just contains the computed version number. # This file is released into the public domain. Generated by -# versioneer-0.18 (https://github.com/warner/python-versioneer) +# versioneer-0.21 (https://github.com/python-versioneer/python-versioneer) """Git implementation of _version.py.""" @@ -15,6 +15,7 @@ import re import subprocess import sys +from typing import Callable, Dict def get_keywords(): @@ -52,12 +53,12 @@ class NotThisMethod(Exception): """Exception raised if a method is not valid for the current scenario.""" -LONG_VERSION_PY = {} -HANDLERS = {} +LONG_VERSION_PY: Dict[str, str] = {} +HANDLERS: Dict[str, Dict[str, Callable]] = {} def register_vcs_handler(vcs, method): # decorator - """Decorator to mark a method as the handler for a particular VCS.""" + """Create decorator to mark a method as the handler of a VCS.""" def decorate(f): """Store f in HANDLERS[vcs][method].""" if vcs not in HANDLERS: @@ -71,17 +72,17 @@ def run_command(commands, args, cwd=None, verbose=False, hide_stderr=False, env=None): """Call the given command(s).""" assert isinstance(commands, list) - p = None - for c in commands: + process = None + for command in commands: try: - dispcmd = str([c] + args) + dispcmd = str([command] + args) # remember shell=False, so use git.cmd on windows, not just git - p = subprocess.Popen([c] + args, cwd=cwd, env=env, - stdout=subprocess.PIPE, - stderr=(subprocess.PIPE if hide_stderr - else None)) + process = subprocess.Popen([command] + args, cwd=cwd, env=env, + stdout=subprocess.PIPE, + stderr=(subprocess.PIPE if hide_stderr + else None)) break - except EnvironmentError: + except OSError: e = sys.exc_info()[1] if e.errno == errno.ENOENT: continue @@ -93,15 +94,13 @@ def run_command(commands, args, cwd=None, verbose=False, hide_stderr=False, if verbose: print("unable to find command, tried %s" % (commands,)) return None, None - stdout = p.communicate()[0].strip() - if sys.version_info[0] >= 3: - stdout = stdout.decode() - if p.returncode != 0: + stdout = process.communicate()[0].strip().decode() + if process.returncode != 0: if verbose: print("unable to run %s (error)" % dispcmd) print("stdout was %s" % stdout) - return None, p.returncode - return stdout, p.returncode + return None, process.returncode + return stdout, process.returncode def versions_from_parentdir(parentdir_prefix, root, verbose): @@ -113,15 +112,14 @@ def versions_from_parentdir(parentdir_prefix, root, verbose): """ rootdirs = [] - for i in range(3): + for _ in range(3): dirname = os.path.basename(root) if dirname.startswith(parentdir_prefix): return {"version": dirname[len(parentdir_prefix):], "full-revisionid": None, "dirty": False, "error": None, "date": None} - else: - rootdirs.append(root) - root = os.path.dirname(root) # up a level + rootdirs.append(root) + root = os.path.dirname(root) # up a level if verbose: print("Tried directories %s but none started with prefix %s" % @@ -138,22 +136,21 @@ def git_get_keywords(versionfile_abs): # _version.py. keywords = {} try: - f = open(versionfile_abs, "r") - for line in f.readlines(): - if line.strip().startswith("git_refnames ="): - mo = re.search(r'=\s*"(.*)"', line) - if mo: - keywords["refnames"] = mo.group(1) - if line.strip().startswith("git_full ="): - mo = re.search(r'=\s*"(.*)"', line) - if mo: - keywords["full"] = mo.group(1) - if line.strip().startswith("git_date ="): - mo = re.search(r'=\s*"(.*)"', line) - if mo: - keywords["date"] = mo.group(1) - f.close() - except EnvironmentError: + with open(versionfile_abs, "r") as fobj: + for line in fobj: + if line.strip().startswith("git_refnames ="): + mo = re.search(r'=\s*"(.*)"', line) + if mo: + keywords["refnames"] = mo.group(1) + if line.strip().startswith("git_full ="): + mo = re.search(r'=\s*"(.*)"', line) + if mo: + keywords["full"] = mo.group(1) + if line.strip().startswith("git_date ="): + mo = re.search(r'=\s*"(.*)"', line) + if mo: + keywords["date"] = mo.group(1) + except OSError: pass return keywords @@ -161,10 +158,14 @@ def git_get_keywords(versionfile_abs): @register_vcs_handler("git", "keywords") def git_versions_from_keywords(keywords, tag_prefix, verbose): """Get version information from git keywords.""" - if not keywords: - raise NotThisMethod("no keywords at all, weird") + if "refnames" not in keywords: + raise NotThisMethod("Short version file found") date = keywords.get("date") if date is not None: + # Use only the last line. Previous lines may contain GPG signature + # information. + date = date.splitlines()[-1] + # git-2.2.0 added "%cI", which expands to an ISO-8601 -compliant # datestamp. However we prefer "%ci" (which expands to an "ISO-8601 # -like" string, which we must then edit to make compliant), because @@ -177,11 +178,11 @@ def git_versions_from_keywords(keywords, tag_prefix, verbose): if verbose: print("keywords are unexpanded, not using") raise NotThisMethod("unexpanded keywords, not a git-archive tarball") - refs = set([r.strip() for r in refnames.strip("()").split(",")]) + refs = {r.strip() for r in refnames.strip("()").split(",")} # starting in git-1.8.3, tags are listed as "tag: foo-1.0" instead of # just "foo-1.0". If we see a "tag: " prefix, prefer those. TAG = "tag: " - tags = set([r[len(TAG):] for r in refs if r.startswith(TAG)]) + tags = {r[len(TAG):] for r in refs if r.startswith(TAG)} if not tags: # Either we're using git < 1.8.3, or there really are no tags. We use # a heuristic: assume all version tags have a digit. The old git %d @@ -190,7 +191,7 @@ def git_versions_from_keywords(keywords, tag_prefix, verbose): # between branches and tags. By ignoring refnames without digits, we # filter out many common branch names like "release" and # "stabilization", as well as "HEAD" and "master". - tags = set([r for r in refs if re.search(r'\d', r)]) + tags = {r for r in refs if re.search(r'\d', r)} if verbose: print("discarding '%s', no digits" % ",".join(refs - tags)) if verbose: @@ -199,6 +200,11 @@ def git_versions_from_keywords(keywords, tag_prefix, verbose): # sorting will prefer e.g. "2.0" over "2.0rc1" if ref.startswith(tag_prefix): r = ref[len(tag_prefix):] + # Filter out refs that exactly match prefix or that don't start + # with a number once the prefix is stripped (mostly a concern + # when prefix is '') + if not re.match(r'\d', r): + continue if verbose: print("picking %s" % r) return {"version": r, @@ -214,7 +220,7 @@ def git_versions_from_keywords(keywords, tag_prefix, verbose): @register_vcs_handler("git", "pieces_from_vcs") -def git_pieces_from_vcs(tag_prefix, root, verbose, run_command=run_command): +def git_pieces_from_vcs(tag_prefix, root, verbose, runner=run_command): """Get version from 'git describe' in the root of the source tree. This only gets called if the git-archive 'subst' keywords were *not* @@ -222,11 +228,13 @@ def git_pieces_from_vcs(tag_prefix, root, verbose, run_command=run_command): version string, meaning we're inside a checked out source tree. """ GITS = ["git"] + TAG_PREFIX_REGEX = "*" if sys.platform == "win32": GITS = ["git.cmd", "git.exe"] + TAG_PREFIX_REGEX = r"\*" - out, rc = run_command(GITS, ["rev-parse", "--git-dir"], cwd=root, - hide_stderr=True) + _, rc = runner(GITS, ["rev-parse", "--git-dir"], cwd=root, + hide_stderr=True) if rc != 0: if verbose: print("Directory %s not under git control" % root) @@ -234,15 +242,16 @@ def git_pieces_from_vcs(tag_prefix, root, verbose, run_command=run_command): # if there is a tag matching tag_prefix, this yields TAG-NUM-gHEX[-dirty] # if there isn't one, this yields HEX[-dirty] (no NUM) - describe_out, rc = run_command(GITS, ["describe", "--tags", "--dirty", - "--always", "--long", - "--match", "%s*" % tag_prefix], - cwd=root) + describe_out, rc = runner(GITS, ["describe", "--tags", "--dirty", + "--always", "--long", + "--match", + "%s%s" % (tag_prefix, TAG_PREFIX_REGEX)], + cwd=root) # --long was added in git-1.5.5 if describe_out is None: raise NotThisMethod("'git describe' failed") describe_out = describe_out.strip() - full_out, rc = run_command(GITS, ["rev-parse", "HEAD"], cwd=root) + full_out, rc = runner(GITS, ["rev-parse", "HEAD"], cwd=root) if full_out is None: raise NotThisMethod("'git rev-parse' failed") full_out = full_out.strip() @@ -252,6 +261,39 @@ def git_pieces_from_vcs(tag_prefix, root, verbose, run_command=run_command): pieces["short"] = full_out[:7] # maybe improved later pieces["error"] = None + branch_name, rc = runner(GITS, ["rev-parse", "--abbrev-ref", "HEAD"], + cwd=root) + # --abbrev-ref was added in git-1.6.3 + if rc != 0 or branch_name is None: + raise NotThisMethod("'git rev-parse --abbrev-ref' returned error") + branch_name = branch_name.strip() + + if branch_name == "HEAD": + # If we aren't exactly on a branch, pick a branch which represents + # the current commit. If all else fails, we are on a branchless + # commit. + branches, rc = runner(GITS, ["branch", "--contains"], cwd=root) + # --contains was added in git-1.5.4 + if rc != 0 or branches is None: + raise NotThisMethod("'git branch --contains' returned error") + branches = branches.split("\n") + + # Remove the first line if we're running detached + if "(" in branches[0]: + branches.pop(0) + + # Strip off the leading "* " from the list of branches. + branches = [branch[2:] for branch in branches] + if "master" in branches: + branch_name = "master" + elif not branches: + branch_name = None + else: + # Pick the first branch that is returned. Good or bad. + branch_name = branches[0] + + pieces["branch"] = branch_name + # parse describe_out. It will be like TAG-NUM-gHEX[-dirty] or HEX[-dirty] # TAG might have hyphens. git_describe = describe_out @@ -268,7 +310,7 @@ def git_pieces_from_vcs(tag_prefix, root, verbose, run_command=run_command): # TAG-NUM-gHEX mo = re.search(r'^(.+)-(\d+)-g([0-9a-f]+)$', git_describe) if not mo: - # unparseable. Maybe git-describe is misbehaving? + # unparsable. Maybe git-describe is misbehaving? pieces["error"] = ("unable to parse git-describe output: '%s'" % describe_out) return pieces @@ -293,13 +335,14 @@ def git_pieces_from_vcs(tag_prefix, root, verbose, run_command=run_command): else: # HEX: no tags pieces["closest-tag"] = None - count_out, rc = run_command(GITS, ["rev-list", "HEAD", "--count"], - cwd=root) + count_out, rc = runner(GITS, ["rev-list", "HEAD", "--count"], cwd=root) pieces["distance"] = int(count_out) # total number of commits # commit date: see ISO-8601 comment in git_versions_from_keywords() - date = run_command(GITS, ["show", "-s", "--format=%ci", "HEAD"], - cwd=root)[0].strip() + date = runner(GITS, ["show", "-s", "--format=%ci", "HEAD"], cwd=root)[0].strip() + # Use only the last line. Previous lines may contain GPG signature + # information. + date = date.splitlines()[-1] pieces["date"] = date.strip().replace(" ", "T", 1).replace(" ", "", 1) return pieces @@ -337,19 +380,67 @@ def render_pep440(pieces): return rendered -def render_pep440_pre(pieces): - """TAG[.post.devDISTANCE] -- No -dirty. +def render_pep440_branch(pieces): + """TAG[[.dev0]+DISTANCE.gHEX[.dirty]] . + + The ".dev0" means not master branch. Note that .dev0 sorts backwards + (a feature branch will appear "older" than the master branch). Exceptions: - 1: no tags. 0.post.devDISTANCE + 1: no tags. 0[.dev0]+untagged.DISTANCE.gHEX[.dirty] """ if pieces["closest-tag"]: rendered = pieces["closest-tag"] + if pieces["distance"] or pieces["dirty"]: + if pieces["branch"] != "master": + rendered += ".dev0" + rendered += plus_or_dot(pieces) + rendered += "%d.g%s" % (pieces["distance"], pieces["short"]) + if pieces["dirty"]: + rendered += ".dirty" + else: + # exception #1 + rendered = "0" + if pieces["branch"] != "master": + rendered += ".dev0" + rendered += "+untagged.%d.g%s" % (pieces["distance"], + pieces["short"]) + if pieces["dirty"]: + rendered += ".dirty" + return rendered + + +def pep440_split_post(ver): + """Split pep440 version string at the post-release segment. + + Returns the release segments before the post-release and the + post-release version number (or -1 if no post-release segment is present). + """ + vc = str.split(ver, ".post") + return vc[0], int(vc[1] or 0) if len(vc) == 2 else None + + +def render_pep440_pre(pieces): + """TAG[.postN.devDISTANCE] -- No -dirty. + + Exceptions: + 1: no tags. 0.post0.devDISTANCE + """ + if pieces["closest-tag"]: if pieces["distance"]: - rendered += ".post.dev%d" % pieces["distance"] + # update the post release segment + tag_version, post_version = pep440_split_post(pieces["closest-tag"]) + rendered = tag_version + if post_version is not None: + rendered += ".post%d.dev%d" % (post_version+1, pieces["distance"]) + else: + rendered += ".post0.dev%d" % (pieces["distance"]) + else: + # no commits, use the tag as the version + rendered = pieces["closest-tag"] else: # exception #1 - rendered = "0.post.dev%d" % pieces["distance"] + rendered = "0.post0.dev%d" % pieces["distance"] return rendered @@ -380,12 +471,41 @@ def render_pep440_post(pieces): return rendered +def render_pep440_post_branch(pieces): + """TAG[.postDISTANCE[.dev0]+gHEX[.dirty]] . + + The ".dev0" means not master branch. + + Exceptions: + 1: no tags. 0.postDISTANCE[.dev0]+gHEX[.dirty] + """ + if pieces["closest-tag"]: + rendered = pieces["closest-tag"] + if pieces["distance"] or pieces["dirty"]: + rendered += ".post%d" % pieces["distance"] + if pieces["branch"] != "master": + rendered += ".dev0" + rendered += plus_or_dot(pieces) + rendered += "g%s" % pieces["short"] + if pieces["dirty"]: + rendered += ".dirty" + else: + # exception #1 + rendered = "0.post%d" % pieces["distance"] + if pieces["branch"] != "master": + rendered += ".dev0" + rendered += "+g%s" % pieces["short"] + if pieces["dirty"]: + rendered += ".dirty" + return rendered + + def render_pep440_old(pieces): """TAG[.postDISTANCE[.dev0]] . The ".dev0" means dirty. - Eexceptions: + Exceptions: 1: no tags. 0.postDISTANCE[.dev0] """ if pieces["closest-tag"]: @@ -456,10 +576,14 @@ def render(pieces, style): if style == "pep440": rendered = render_pep440(pieces) + elif style == "pep440-branch": + rendered = render_pep440_branch(pieces) elif style == "pep440-pre": rendered = render_pep440_pre(pieces) elif style == "pep440-post": rendered = render_pep440_post(pieces) + elif style == "pep440-post-branch": + rendered = render_pep440_post_branch(pieces) elif style == "pep440-old": rendered = render_pep440_old(pieces) elif style == "git-describe": @@ -495,7 +619,7 @@ def get_versions(): # versionfile_source is the relative path from the top of the source # tree (where the .git directory might live) to this file. Invert # this to find the root from __file__. - for i in cfg.versionfile_source.split('/'): + for _ in cfg.versionfile_source.split('/'): root = os.path.dirname(root) except NameError: return {"version": "0+unknown", "full-revisionid": None, diff --git a/deeptime/basis/CMakeLists.txt b/deeptime/basis/CMakeLists.txt index f57474dee..3548870e9 100644 --- a/deeptime/basis/CMakeLists.txt +++ b/deeptime/basis/CMakeLists.txt @@ -1,8 +1,4 @@ -project(deeptime_basis CXX) +project(_basis_bindings CXX) set(SRC src/basis_bindings.cpp) -pybind11_add_module(_basis_bindings ${SRC}) -target_link_libraries(_basis_bindings PUBLIC deeptime::deeptime) -if(OpenMP_FOUND) - target_link_libraries(_basis_bindings PUBLIC OpenMP::OpenMP_CXX) -endif() +register_pybind_module(${PROJECT_NAME} ${SRC}) diff --git a/deeptime/basis/setup.py b/deeptime/basis/setup.py deleted file mode 100644 index 0f781c85c..000000000 --- a/deeptime/basis/setup.py +++ /dev/null @@ -1,5 +0,0 @@ -def configuration(parent_package='', top_path=None): - from numpy.distutils.misc_util import Configuration - config = Configuration('basis', parent_package, top_path) - config.add_extension('_basis_bindings', sources=['src/basis_bindings.cpp'], language='c++') - return config diff --git a/deeptime/clustering/CMakeLists.txt b/deeptime/clustering/CMakeLists.txt index 909ec5516..64e36fd03 100644 --- a/deeptime/clustering/CMakeLists.txt +++ b/deeptime/clustering/CMakeLists.txt @@ -1,8 +1,4 @@ project(_clustering_bindings CXX) set(SRC src/clustering_module.cpp) -pybind11_add_module(${PROJECT_NAME} ${SRC}) -target_link_libraries(${PROJECT_NAME} PUBLIC deeptime::deeptime) -if(OpenMP_FOUND) - target_link_libraries(${PROJECT_NAME} PUBLIC OpenMP::OpenMP_CXX) -endif() +register_pybind_module(${PROJECT_NAME} ${SRC}) diff --git a/deeptime/clustering/setup.py b/deeptime/clustering/setup.py deleted file mode 100644 index 305580e9c..000000000 --- a/deeptime/clustering/setup.py +++ /dev/null @@ -1,6 +0,0 @@ -def configuration(parent_package='', top_path=None): - from numpy.distutils.misc_util import Configuration - config = Configuration('clustering', parent_package, top_path) - - config.add_extension('_clustering_bindings', sources=['src/clustering_module.cpp'], language='c++') - return config diff --git a/deeptime/covariance/setup.py b/deeptime/covariance/setup.py deleted file mode 100644 index b93f1165a..000000000 --- a/deeptime/covariance/setup.py +++ /dev/null @@ -1,8 +0,0 @@ -def configuration(parent_package='', top_path=None): - from numpy.distutils.misc_util import Configuration - - config = Configuration('covariance', parent_package, top_path) - config.add_subpackage('util') - config.add_subpackage('util.covar_c') - - return config diff --git a/deeptime/covariance/util/covar_c/CMakeLists.txt b/deeptime/covariance/util/covar_c/CMakeLists.txt index c59148c34..d4a133010 100644 --- a/deeptime/covariance/util/covar_c/CMakeLists.txt +++ b/deeptime/covariance/util/covar_c/CMakeLists.txt @@ -1,5 +1,4 @@ -project(covartools CXX) +project(_covartools CXX) set(SRC covartools.hpp covartools.cpp) -pybind11_add_module(${PROJECT_NAME} ${SRC}) -target_link_libraries(${PROJECT_NAME} PUBLIC deeptime::deeptime) +register_pybind_module(${PROJECT_NAME} ${SRC}) diff --git a/deeptime/covariance/util/covar_c/setup.py b/deeptime/covariance/util/covar_c/setup.py deleted file mode 100644 index 72e3a0d9c..000000000 --- a/deeptime/covariance/util/covar_c/setup.py +++ /dev/null @@ -1,5 +0,0 @@ -def configuration(parent_package='', top_path=None): - from numpy.distutils.misc_util import Configuration - config = Configuration('covar_c', parent_package, top_path) - config.add_extension('_covartools', sources=['covartools.hpp', 'covartools.cpp'], language='c++') - return config diff --git a/deeptime/data/CMakeLists.txt b/deeptime/data/CMakeLists.txt index a479736ee..140d24004 100644 --- a/deeptime/data/CMakeLists.txt +++ b/deeptime/data/CMakeLists.txt @@ -1,8 +1,4 @@ -project(deeptime_data CXX) +project(_data_bindings CXX) set(SRC src/data_module.cpp) -pybind11_add_module(_data_bindings ${SRC}) -target_link_libraries(_data_bindings PUBLIC deeptime::deeptime) -if(OpenMP_FOUND) - target_link_libraries(_data_bindings PUBLIC OpenMP::OpenMP_CXX) -endif() +register_pybind_module(${PROJECT_NAME} ${SRC}) diff --git a/deeptime/data/setup.py b/deeptime/data/setup.py deleted file mode 100644 index d4a2614a1..000000000 --- a/deeptime/data/setup.py +++ /dev/null @@ -1,7 +0,0 @@ -def configuration(parent_package='', top_path=None): - from numpy.distutils.misc_util import Configuration - - config = Configuration('data', parent_package, top_path) - config.add_data_files('data/double_well_discrete.npz') - config.add_extension("_data_bindings", sources=["src/data_module.cpp"]) - return config diff --git a/deeptime/decomposition/setup.py b/deeptime/decomposition/setup.py deleted file mode 100644 index 0a9447c41..000000000 --- a/deeptime/decomposition/setup.py +++ /dev/null @@ -1,6 +0,0 @@ -def configuration(parent_package='', top_path=None): - from numpy.distutils.misc_util import Configuration - - config = Configuration('decomposition', parent_package, top_path) - config.add_subpackage('deep') - return config diff --git a/deeptime/kernels/setup.py b/deeptime/kernels/setup.py deleted file mode 100644 index e69de29bb..000000000 diff --git a/deeptime/markov/CMakeLists.txt b/deeptime/markov/CMakeLists.txt new file mode 100644 index 000000000..588cc9b52 --- /dev/null +++ b/deeptime/markov/CMakeLists.txt @@ -0,0 +1,9 @@ +project(_markov_bindings CXX) + +add_subdirectory(hmm) +add_subdirectory(msm/tram) +add_subdirectory(tools/estimation/dense) +add_subdirectory(tools/estimation/sparse) + +set(SRC src/markov_module.cpp) +register_pybind_module(${PROJECT_NAME} ${SRC}) diff --git a/deeptime/markov/_bindings/CMakeLists.txt b/deeptime/markov/_bindings/CMakeLists.txt deleted file mode 100644 index f819c6d00..000000000 --- a/deeptime/markov/_bindings/CMakeLists.txt +++ /dev/null @@ -1,8 +0,0 @@ -project(deeptime_markov CXX) - -set(SRC src/markov_module.cpp) -pybind11_add_module(_markov_bindings ${SRC}) -target_link_libraries(_markov_bindings PUBLIC deeptime::deeptime) -if(OpenMP_FOUND) - target_link_libraries(_markov_bindings PUBLIC OpenMP::OpenMP_CXX) -endif() diff --git a/deeptime/markov/hmm/CMakeLists.txt b/deeptime/markov/hmm/CMakeLists.txt new file mode 100644 index 000000000..b121f5b53 --- /dev/null +++ b/deeptime/markov/hmm/CMakeLists.txt @@ -0,0 +1,4 @@ +project(_hmm_bindings CXX) + +set(SRC src/hmm_module.cpp) +register_pybind_module(${PROJECT_NAME} ${SRC}) diff --git a/deeptime/markov/hmm/_bindings/CMakeLists.txt b/deeptime/markov/hmm/_bindings/CMakeLists.txt deleted file mode 100644 index 879155126..000000000 --- a/deeptime/markov/hmm/_bindings/CMakeLists.txt +++ /dev/null @@ -1,8 +0,0 @@ -project(deeptime_hmm CXX) - -set(SRC src/hmm_module.cpp) -pybind11_add_module(_hmm_bindings ${SRC}) -target_link_libraries(_hmm_bindings PUBLIC deeptime::deeptime) -if(OpenMP_FOUND) - target_link_libraries(_hmm_bindings PUBLIC OpenMP::OpenMP_CXX) -endif() diff --git a/deeptime/markov/hmm/init/setup.py b/deeptime/markov/hmm/init/setup.py deleted file mode 100644 index 718aa0681..000000000 --- a/deeptime/markov/hmm/init/setup.py +++ /dev/null @@ -1,8 +0,0 @@ -def configuration(parent_package='', top_path=None): - from numpy.distutils.misc_util import Configuration - config = Configuration('init', parent_package, top_path) - - config.add_subpackage("discrete") - config.add_subpackage("gaussian") - - return config diff --git a/deeptime/markov/hmm/setup.py b/deeptime/markov/hmm/setup.py deleted file mode 100644 index a879a3f37..000000000 --- a/deeptime/markov/hmm/setup.py +++ /dev/null @@ -1,9 +0,0 @@ -def configuration(parent_package='', top_path=None): - from numpy.distutils.misc_util import Configuration - config = Configuration('hmm', parent_package, top_path) - - config.add_subpackage("init") - - config.add_extension('_hmm_bindings', sources=['_bindings/src/hmm_module.cpp'], language='c++') - - return config diff --git a/deeptime/markov/hmm/_bindings/src/hmm_module.cpp b/deeptime/markov/hmm/src/hmm_module.cpp similarity index 100% rename from deeptime/markov/hmm/_bindings/src/hmm_module.cpp rename to deeptime/markov/hmm/src/hmm_module.cpp diff --git a/deeptime/markov/msm/setup.py b/deeptime/markov/msm/setup.py deleted file mode 100644 index 0139114bb..000000000 --- a/deeptime/markov/msm/setup.py +++ /dev/null @@ -1,6 +0,0 @@ -def configuration(parent_package='', top_path=None): - from numpy.distutils.misc_util import Configuration - config = Configuration('msm', parent_package, top_path) - config.add_subpackage('tram') - - return config diff --git a/deeptime/markov/msm/tram/CMakeLists.txt b/deeptime/markov/msm/tram/CMakeLists.txt new file mode 100644 index 000000000..10cd0d2ae --- /dev/null +++ b/deeptime/markov/msm/tram/CMakeLists.txt @@ -0,0 +1,4 @@ +project(_tram_bindings) + +set(SRC _bindings/src/tram_module.cpp) +register_pybind_module(${PROJECT_NAME} ${SRC}) diff --git a/deeptime/markov/msm/tram/_bindings/CMakeLists.txt b/deeptime/markov/msm/tram/_bindings/CMakeLists.txt deleted file mode 100644 index 036f19363..000000000 --- a/deeptime/markov/msm/tram/_bindings/CMakeLists.txt +++ /dev/null @@ -1,5 +0,0 @@ -pybind11_add_module(_tram_bindings ../_bindings/src/tram_module.cpp) -target_link_libraries(_tram_bindings PUBLIC deeptime::deeptime) -if(OpenMP_FOUND) - target_link_libraries(_tram_bindings PUBLIC OpenMP::OpenMP_CXX) -endif() diff --git a/deeptime/markov/msm/tram/setup.py b/deeptime/markov/msm/tram/setup.py deleted file mode 100644 index f606c8509..000000000 --- a/deeptime/markov/msm/tram/setup.py +++ /dev/null @@ -1,5 +0,0 @@ -def configuration(parent_package='', top_path=None): - from numpy.distutils.misc_util import Configuration - config = Configuration('tram', parent_package, top_path) - config.add_extension('_tram_bindings', sources=['_bindings/src/tram_module.cpp'], language='c++') - return config diff --git a/deeptime/markov/setup.py b/deeptime/markov/setup.py deleted file mode 100644 index e4b8b37fa..000000000 --- a/deeptime/markov/setup.py +++ /dev/null @@ -1,12 +0,0 @@ -def configuration(parent_package='', top_path=None): - from numpy.distutils.misc_util import Configuration - config = Configuration('markov', parent_package, top_path) - - config.add_extension('_markov_bindings', sources=['_bindings/src/markov_module.cpp'], language='c++') - - config.add_subpackage('msm') - config.add_subpackage('hmm') - config.add_subpackage('tools') - config.add_subpackage('sample') - - return config diff --git a/deeptime/markov/_bindings/src/markov_module.cpp b/deeptime/markov/src/markov_module.cpp similarity index 100% rename from deeptime/markov/_bindings/src/markov_module.cpp rename to deeptime/markov/src/markov_module.cpp diff --git a/deeptime/markov/tools/estimation/dense/CMakeLists.txt b/deeptime/markov/tools/estimation/dense/CMakeLists.txt new file mode 100644 index 000000000..231b47e9f --- /dev/null +++ b/deeptime/markov/tools/estimation/dense/CMakeLists.txt @@ -0,0 +1,3 @@ +project(_mle_bindings CXX) + +register_pybind_module(${PROJECT_NAME} src/mle_module.cpp) diff --git a/deeptime/markov/tools/estimation/dense/_bindings/CMakeLists.txt b/deeptime/markov/tools/estimation/dense/_bindings/CMakeLists.txt deleted file mode 100644 index 350c6495d..000000000 --- a/deeptime/markov/tools/estimation/dense/_bindings/CMakeLists.txt +++ /dev/null @@ -1,7 +0,0 @@ -project(deeptime_msm_mle CXX) - -pybind11_add_module(_mle_bindings src/mle_module.cpp) -target_link_libraries(_mle_bindings PUBLIC deeptime::deeptime) -if(OpenMP_FOUND) - target_link_libraries(_mle_bindings PUBLIC OpenMP::OpenMP_CXX) -endif() diff --git a/deeptime/markov/tools/estimation/dense/setup.py b/deeptime/markov/tools/estimation/dense/setup.py deleted file mode 100644 index ddd0a29a2..000000000 --- a/deeptime/markov/tools/estimation/dense/setup.py +++ /dev/null @@ -1,6 +0,0 @@ -def configuration(parent_package='', top_path=None): - from numpy.distutils.misc_util import Configuration - config = Configuration('dense', parent_package, top_path) - config.add_subpackage('tmat_sampling') - config.add_extension('_mle_bindings', sources=['_bindings/src/mle_module.cpp'], language='c++') - return config diff --git a/deeptime/markov/tools/estimation/dense/_bindings/src/mle_module.cpp b/deeptime/markov/tools/estimation/dense/src/mle_module.cpp similarity index 100% rename from deeptime/markov/tools/estimation/dense/_bindings/src/mle_module.cpp rename to deeptime/markov/tools/estimation/dense/src/mle_module.cpp diff --git a/deeptime/markov/tools/estimation/setup.py b/deeptime/markov/tools/estimation/setup.py deleted file mode 100644 index 93e6023b1..000000000 --- a/deeptime/markov/tools/estimation/setup.py +++ /dev/null @@ -1,8 +0,0 @@ - -def configuration(parent_package='', top_path=None): - from numpy.distutils.misc_util import Configuration - config = Configuration('estimation', parent_package, top_path) - config.add_subpackage('dense') - config.add_subpackage('sparse') - - return config diff --git a/deeptime/markov/tools/estimation/sparse/CMakeLists.txt b/deeptime/markov/tools/estimation/sparse/CMakeLists.txt new file mode 100644 index 000000000..50a528895 --- /dev/null +++ b/deeptime/markov/tools/estimation/sparse/CMakeLists.txt @@ -0,0 +1,3 @@ +project(_mle_sparse_bindings CXX) + +register_pybind_module(${PROJECT_NAME} src/mle_sparse_module.cpp) diff --git a/deeptime/markov/tools/estimation/sparse/_bindings/CMakeLists.txt b/deeptime/markov/tools/estimation/sparse/_bindings/CMakeLists.txt deleted file mode 100644 index 6b40eb565..000000000 --- a/deeptime/markov/tools/estimation/sparse/_bindings/CMakeLists.txt +++ /dev/null @@ -1,7 +0,0 @@ -project(deeptime_msm_mle_sparse CXX) - -pybind11_add_module(_mle_sparse_bindings src/mle_sparse_module.cpp) -target_link_libraries(_mle_sparse_bindings PUBLIC deeptime::deeptime) -if(OpenMP_FOUND) - target_link_libraries(_mle_sparse_bindings PUBLIC OpenMP::OpenMP_CXX) -endif() diff --git a/deeptime/markov/tools/estimation/sparse/setup.py b/deeptime/markov/tools/estimation/sparse/setup.py deleted file mode 100644 index bedf46f59..000000000 --- a/deeptime/markov/tools/estimation/sparse/setup.py +++ /dev/null @@ -1,5 +0,0 @@ -def configuration(parent_package='', top_path=None): - from numpy.distutils.misc_util import Configuration - config = Configuration('sparse', parent_package, top_path) - config.add_extension('_mle_sparse_bindings', sources=['_bindings/src/mle_sparse_module.cpp'], language='c++') - return config diff --git a/deeptime/markov/tools/estimation/sparse/_bindings/src/mle_sparse_module.cpp b/deeptime/markov/tools/estimation/sparse/src/mle_sparse_module.cpp similarity index 100% rename from deeptime/markov/tools/estimation/sparse/_bindings/src/mle_sparse_module.cpp rename to deeptime/markov/tools/estimation/sparse/src/mle_sparse_module.cpp diff --git a/deeptime/markov/tools/setup.py b/deeptime/markov/tools/setup.py deleted file mode 100644 index da5f12175..000000000 --- a/deeptime/markov/tools/setup.py +++ /dev/null @@ -1,11 +0,0 @@ -def configuration(parent_package='', top_path=None): - from numpy.distutils.misc_util import Configuration - - config = Configuration('tools', parent_package, top_path) - - config.add_subpackage('analysis') - config.add_subpackage('analysis.dense') - config.add_subpackage('estimation') - config.add_subpackage('flux') - - return config diff --git a/deeptime/numeric/CMakeLists.txt b/deeptime/numeric/CMakeLists.txt new file mode 100644 index 000000000..239a08a97 --- /dev/null +++ b/deeptime/numeric/CMakeLists.txt @@ -0,0 +1,4 @@ +project(_numeric_bindings CXX) + +set(SRC src/numeric_module.cpp) +register_pybind_module(${PROJECT_NAME} ${SRC}) diff --git a/deeptime/numeric/_bindings/CMakeLists.txt b/deeptime/numeric/_bindings/CMakeLists.txt deleted file mode 100644 index 2ee94c838..000000000 --- a/deeptime/numeric/_bindings/CMakeLists.txt +++ /dev/null @@ -1,8 +0,0 @@ -project(_numeric_bindings CXX) - -set(SRC src/numeric_module.cpp) -pybind11_add_module(${PROJECT_NAME} ${SRC}) -target_link_libraries(${PROJECT_NAME} PUBLIC deeptime::deeptime) -if (OpenMP_FOUND) - target_link_libraries(${PROJECT_NAME} PUBLIC OpenMP::OpenMP_CXX) -endif() diff --git a/deeptime/numeric/setup.py b/deeptime/numeric/setup.py deleted file mode 100644 index 3d43a13ef..000000000 --- a/deeptime/numeric/setup.py +++ /dev/null @@ -1,8 +0,0 @@ -def configuration(parent_package='', top_path=None): - from numpy.distutils.misc_util import Configuration - config = Configuration('numeric', parent_package, top_path) - - config.add_extension('eig_qr', sources=['deeptime/numeric/eig_qr.pyx']) - config.add_extension('_numeric_bindings', sources=['_bindings/src/numeric_module.cpp'], language='c++') - - return config diff --git a/deeptime/numeric/_bindings/src/numeric_module.cpp b/deeptime/numeric/src/numeric_module.cpp similarity index 100% rename from deeptime/numeric/_bindings/src/numeric_module.cpp rename to deeptime/numeric/src/numeric_module.cpp diff --git a/deeptime/setup.py b/deeptime/setup.py deleted file mode 100644 index 1de4304c6..000000000 --- a/deeptime/setup.py +++ /dev/null @@ -1,23 +0,0 @@ -def configuration(parent_package='', top_path=None): - from numpy.distutils.misc_util import Configuration - - config = Configuration('deeptime', parent_package, top_path) - - config.add_subpackage('clustering') - config.add_subpackage('covariance') - config.add_subpackage('data') - config.add_subpackage('decomposition') - config.add_subpackage('markov') - config.add_subpackage('numeric') - config.add_subpackage('kernels') - config.add_subpackage('basis') - config.add_subpackage('util') - config.add_subpackage('sindy') - config.add_subpackage('plots') - - from Cython.Build import cythonize - config.ext_modules = cythonize( - config.ext_modules, - compiler_directives={'language_level': '3'}) - - return config diff --git a/deeptime/src/include/deeptime/data/systems.h b/deeptime/src/include/deeptime/data/systems.h index 7f293e5fb..86104ea4b 100644 --- a/deeptime/src/include/deeptime/data/systems.h +++ b/deeptime/src/include/deeptime/data/systems.h @@ -95,13 +95,13 @@ struct BickleyJet { } constexpr State f(double t, const State &xVec) const { - using namespace std::complex_literals; auto[x, y] = Boundary::apply(xVec); + std::complex im(0, 1); std::complex fc{0}; std::complex df_dx_c{0}; for (int j = 0; j < 3; ++j) { - fc += eps[j] * std::exp(-1i * k[j] * c[j] * t) * std::exp(1i * k[j] * x); - df_dx_c += eps[j] * std::exp(-1i * k[j] * c[j] * t) * 1i * k[j] * std::exp(1i * k[j] * x); + fc += eps[j] * std::exp(-im * k[j] * c[j] * t) * std::exp(im * k[j] * x); + df_dx_c += eps[j] * std::exp(-im * k[j] * c[j] * t) * im * k[j] * std::exp(im * k[j] * x); } auto f = fc.real(); auto df_dx = df_dx_c.real(); diff --git a/deeptime/util/platform.py b/deeptime/util/platform.py index 729bf10a0..1b26a1a02 100644 --- a/deeptime/util/platform.py +++ b/deeptime/util/platform.py @@ -1,3 +1,5 @@ +import warnings + def module_available(modname: str) -> bool: r"""Checks whether a module is installed and available for import by the current interpreter. @@ -17,6 +19,10 @@ def module_available(modname: str) -> bool: return True except ImportError: return False + except BaseException as e: + warnings.warn(f"There was a problem importing {modname}, treating it as unavailable. Stacktrace: {e}", + RuntimeWarning) + return False def handle_progress_bar(progress): diff --git a/devtools/conda-recipe/meta.yaml b/devtools/conda-recipe/meta.yaml index 4d8b2de72..7bff915b6 100644 --- a/devtools/conda-recipe/meta.yaml +++ b/devtools/conda-recipe/meta.yaml @@ -6,9 +6,8 @@ source: path: ../.. build: - skip: true # [win and vc<14] script: - - "{{ PYTHON }} -m pip install . --no-deps --ignore-installed --no-cache-dir -v" + - "{{ PYTHON }} -m pip install . -vvv" requirements: build: @@ -19,10 +18,13 @@ requirements: - python - cython - pip - - numpy - scipy - - pybind11 - toml + - ninja + - cmake >=3.18 + - numpy >=1.14 + - pybind11 >=2.9.0 + - scikit-build - llvm-openmp # [osx] - libgomp # [linux] diff --git a/devtools/setup+build+test.yml b/devtools/setup+build+test.yml index 0e9f6068b..2d81aa2f1 100644 --- a/devtools/setup+build+test.yml +++ b/devtools/setup+build+test.yml @@ -4,7 +4,7 @@ steps: - script: pip install nox displayName: 'Install nox' -- script: nox -s "tests-$(python.version)" -- cov +- script: nox -s "tests-$(python.version)" -- cov numprocesses=2 displayName: 'Run tests' - script: nox -s "tests-$(python.version)" --reuse-existing-virtualenvs -- cpp diff --git a/noxfile.py b/noxfile.py index de8e58c1f..c1cf97078 100644 --- a/noxfile.py +++ b/noxfile.py @@ -24,6 +24,12 @@ def tests(session: nox.Session) -> None: "-Dpybind11_DIR={}".format(pybind11_module_dir), '-DCMAKE_BUILD_TYPE=Release', silent=True) session.run("cmake", "--build", tmpdir, "--target", "run_tests") else: + pytest_args = [] + for arg in session.posargs: + if arg.startswith('numprocesses'): + n_processes = arg.split('=')[1] + session.log(f"Running tests with n={n_processes} jobs.") + pytest_args.append(f'--numprocesses={n_processes}') session.install("-r", "tests/requirements.txt") session.install("-e", ".", '-v', silent=False) if 'cov' in session.posargs: @@ -34,11 +40,10 @@ def tests(session: nox.Session) -> None: junit_xml = os.path.join(xml_results_dest, 'junit.xml') cov_xml = os.path.join(xml_results_dest, 'coverage.xml') - cov_args = [f'--cov={cover_pkg}', f"--cov-report=xml:{cov_xml}", f"--junit-xml={junit_xml}", - "--cov-config=.coveragerc"] + pytest_args += [f'--cov={cover_pkg}', f"--cov-report=xml:{cov_xml}", f"--junit-xml={junit_xml}", + "--cov-config=.coveragerc"] else: session.log("Running without coverage") - cov_args = [] test_dirs = ["tests/"] try: @@ -48,7 +53,7 @@ def tests(session: nox.Session) -> None: except ImportError: pass - session.run("pytest", '-vv', '--doctest-modules', '--durations=20', *cov_args, '--pyargs', *test_dirs) + session.run("pytest", '-vv', '--doctest-modules', '--durations=20', *pytest_args, '--pyargs', *test_dirs) @nox.session(reuse_venv=True) diff --git a/pyproject.toml b/pyproject.toml index 6f4d39f61..3df4f0dda 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -20,7 +20,19 @@ deep-learning = ['pytorch'] plotting = ['matplotlib', 'networkx'] [build-system] -requires = ["toml", "setuptools", "wheel", "numpy>=1.14", "scipy", "Cython", "pybind11"] +requires = [ + "setuptools>=42", + "wheel", + "scikit-build>=0.13", + "cython", + "pybind11>=2.9.0", + "numpy>=1.14", + "cmake>=3.22", + "toml", + "scipy", + "ninja; platform_system!='Windows'" +] +build-backend = "setuptools.build_meta" [tool.pytest.ini_options] filterwarnings = ["once", "ignore::UserWarning"] diff --git a/setup.cfg b/setup.cfg index 6f6324bd5..5de8032b1 100644 --- a/setup.cfg +++ b/setup.cfg @@ -8,7 +8,6 @@ style = pep440 versionfile_source = deeptime/_version.py versionfile_build = deeptime/_version.py tag_prefix = v -#parentdir_prefix = [flake8] ignore=E24,E121,E123,E126,E226,E704,W503,W504 diff --git a/setup.py b/setup.py index 66bc4af66..d7a3a3a75 100644 --- a/setup.py +++ b/setup.py @@ -1,103 +1,35 @@ -import sys +# based on https://github.com/pybind/scikit_build_example/blob/master/setup.py + +import os +import re from pathlib import Path -import setuptools # noqa # pylint: disable=unused-import +import sys import toml -from numpy.distutils.command.build_ext import build_ext +from setuptools import find_packages, Extension +from Cython.Build import cythonize +sys.path.insert(0, os.path.dirname(__file__)) import versioneer -import pybind11 +try: + from skbuild import setup +except ImportError: + print( + "Please update pip, you need pip 10 or greater,\n" + " or you need to install the PEP 518 requirements in pyproject.toml yourself", + file=sys.stderr, + ) + raise pyproject = toml.load("pyproject.toml") -def _gen_ccode(includes, code): - template = """{includes} - int main(void) {{ - {code} - return 0; - }}""" - - if not isinstance(includes, (list, tuple)): - includes = [includes] - return template.format(includes="\n".join(["#include <{}>".format(inc) for inc in includes]), code=code) - - -def supports_omp(cc): - import os - import tempfile - import shutil - from copy import deepcopy - from distutils.errors import CompileError, LinkError - - cc = deepcopy(cc) # avoid side-effects - if sys.platform == 'darwin': - cc.add_library('iomp5') - elif sys.platform.startswith('linux'): - cc.add_library('gomp') - - tmpdir = None - try: - tmpdir = tempfile.mkdtemp() - tmpfile = tempfile.mkstemp(suffix=".c", dir=tmpdir)[1] - with open(tmpfile, 'w') as f: - f.write(_gen_ccode("omp.h", "omp_get_num_threads();")) - obj = cc.compile([os.path.abspath(tmpfile)], output_dir=tmpdir) - cc.link_executable(obj, output_progname=os.path.join(tmpdir, 'a.out')) - except (CompileError, LinkError): - return False - finally: - # cleanup - if tmpdir is not None: - shutil.rmtree(tmpdir, ignore_errors=True) - return True - - -class Build(build_ext): - def build_extensions(self): - extra_compile_args = [] - extra_link_args = [] - define_macros = [] - - from numpy import get_include as _np_inc - np_inc = _np_inc() - pybind_inc = Path(pybind11.get_include()) - common_inc = Path('deeptime') / 'src' / 'include' - - if self.compiler.compiler_type == 'msvc': - cxx_flags = ['/EHsc', '/std:c++17', '/bigobj', f'/DVERSION_INFO=\\"{self.distribution.get_version()}\\"'] - extra_link_args.append('/machine:X64') - else: - cxx_flags = ['-std=c++17', "-fvisibility=hidden", "-g0", "-Wno-register"] - extra_compile_args += ['-pthread'] - extra_link_args = ['-lpthread'] - - self.setup_openmp(define_macros, extra_compile_args, extra_link_args) - for ext in self.extensions: - ext.include_dirs.append(common_inc.resolve()) - ext.include_dirs.append(np_inc) - ext.include_dirs.append(pybind_inc.resolve()) - - if ext.language == 'c++': - ext.extra_compile_args += cxx_flags - ext.extra_compile_args += extra_compile_args - ext.extra_link_args += extra_link_args - ext.define_macros += define_macros - - super(Build, self).build_extensions() - - def setup_openmp(self, define_macros, extra_compile_args, extra_link_args): - has_openmp = supports_omp(self.compiler) - if has_openmp: - extra_compile_args += ['-fopenmp' if sys.platform != 'darwin' else '-fopenmp=libiomp5'] - if sys.platform.startswith('linux'): - extra_link_args += ['-lgomp'] - elif sys.platform == 'darwin': - extra_link_args += ['-liomp5'] - else: - raise ValueError("Should not happen.") - define_macros += [('USE_OPENMP', None)] +def eig_qr_extension(): + module_name = 'deeptime.numeric.eig_qr' + sources = ["/".join(module_name.split(".")) + '.pyx'] + return cythonize([Extension(module_name, sources=sources, extra_compile_args=['-std=c99'])], + compiler_directives={'language_level': '3'})[0] def load_long_description(): @@ -105,8 +37,10 @@ def load_long_description(): return f.read() -cmdclass = versioneer.get_cmdclass() -cmdclass['build_ext'] = Build +cmake_args = [ + f"-DDEEPTIME_VERSION={versioneer.get_version().split('+')[0]}", + f"-DDEEPTIME_VERSION_INFO={versioneer.get_version()}" +] metadata = \ dict( @@ -118,32 +52,20 @@ def load_long_description(): description=pyproject["project"]["description"], long_description=load_long_description(), long_description_content_type='text/markdown', - cmdclass=cmdclass, zip_safe=False, - setup_requires=pyproject["build-system"]["requires"], - install_requires=pyproject["project"]["dependencies"], extras_require=pyproject["project"]["optional-dependencies"], - package_data={ - 'deeptime.data': ['data/*.npz'], - 'deeptime.src.include': ['*.h'], - }, + packages=find_packages(where="."), + package_dir={"deeptime": "deeptime", "versioneer": "."}, + cmake_install_dir="deeptime/", + cmake_args=cmake_args, include_package_data=True, python_requires=pyproject["project"]["requires-python"], + ext_modules=[eig_qr_extension()] ) - -def configuration(parent_package='', top_path=None): - from numpy.distutils.misc_util import Configuration - config = Configuration(None, parent_package, top_path) - config.set_options(ignore_setup_xxx_py=True, - assume_default_configuration=True, - delegate_options_to_subpackages=True) - config.add_subpackage('deeptime') - return config - - if __name__ == '__main__': - from numpy.distutils.core import setup - - metadata['configuration'] = configuration + # see issue https://github.com/scikit-build/scikit-build/issues/521 + # invalidates _skbuild cache + for i in (Path(__file__).resolve().parent / "_skbuild").rglob("CMakeCache.txt"): + i.write_text(re.sub("^//.*$\n^[^#].*pip-build-env.*$", "", i.read_text(), flags=re.M)) setup(**metadata) diff --git a/tests/requirements.txt b/tests/requirements.txt index e04ab9ad8..a430aabc3 100644 --- a/tests/requirements.txt +++ b/tests/requirements.txt @@ -13,5 +13,6 @@ pytest==6.2.5 pytest-cov pytest-faulthandler==2.0.1 pytest-sugar==0.9.4 +pytest-xdist flaky tqdm diff --git a/versioneer.py b/versioneer.py index 64fea1c89..b4cd1d6c7 100644 --- a/versioneer.py +++ b/versioneer.py @@ -1,5 +1,5 @@ -# Version: 0.18 +# Version: 0.21 """The Versioneer - like a rocketeer, but for versions. @@ -7,16 +7,12 @@ ============== * like a rocketeer, but for versions! -* https://github.com/warner/python-versioneer +* https://github.com/python-versioneer/python-versioneer * Brian Warner * License: Public Domain -* Compatible With: python2.6, 2.7, 3.2, 3.3, 3.4, 3.5, 3.6, and pypy -* [![Latest Version] -(https://pypip.in/version/versioneer/badge.svg?style=flat) -](https://pypi.python.org/pypi/versioneer/) -* [![Build Status] -(https://travis-ci.org/warner/python-versioneer.png?branch=master) -](https://travis-ci.org/warner/python-versioneer) +* Compatible with: Python 3.6, 3.7, 3.8, 3.9 and pypy3 +* [![Latest Version][pypi-image]][pypi-url] +* [![Build Status][travis-image]][travis-url] This is a tool for managing a recorded version number in distutils-based python projects. The goal is to remove the tedious and error-prone "update @@ -27,9 +23,10 @@ ## Quick Install -* `pip install versioneer` to somewhere to your $PATH -* add a `[versioneer]` section to your setup.cfg (see below) +* `pip install versioneer` to somewhere in your $PATH +* add a `[versioneer]` section to your setup.cfg (see [Install](INSTALL.md)) * run `versioneer install` in your source tree, commit the results +* Verify version information with `python setup.py version` ## Version Identifiers @@ -61,7 +58,7 @@ for example `git describe --tags --dirty --always` reports things like "0.7-1-g574ab98-dirty" to indicate that the checkout is one revision past the 0.7 tag, has a unique revision id of "574ab98", and is "dirty" (it has -uncommitted changes. +uncommitted changes). The version identifier is used for multiple purposes: @@ -166,7 +163,7 @@ Some situations are known to cause problems for Versioneer. This details the most significant ones. More can be found on Github -[issues page](https://github.com/warner/python-versioneer/issues). +[issues page](https://github.com/python-versioneer/python-versioneer/issues). ### Subprojects @@ -180,7 +177,7 @@ `setup.cfg`, and `tox.ini`. Projects like these produce multiple PyPI distributions (and upload multiple independently-installable tarballs). * Source trees whose main purpose is to contain a C library, but which also - provide bindings to Python (and perhaps other langauges) in subdirectories. + provide bindings to Python (and perhaps other languages) in subdirectories. Versioneer will look for `.git` in parent directories, and most operations should get the right version string. However `pip` and `setuptools` have bugs @@ -194,9 +191,9 @@ Pip-8.1.1 is known to have this problem, but hopefully it will get fixed in some later version. -[Bug #38](https://github.com/warner/python-versioneer/issues/38) is tracking +[Bug #38](https://github.com/python-versioneer/python-versioneer/issues/38) is tracking this issue. The discussion in -[PR #61](https://github.com/warner/python-versioneer/pull/61) describes the +[PR #61](https://github.com/python-versioneer/python-versioneer/pull/61) describes the issue from the Versioneer side in more detail. [pip PR#3176](https://github.com/pypa/pip/pull/3176) and [pip PR#3615](https://github.com/pypa/pip/pull/3615) contain work to improve @@ -224,22 +221,10 @@ cause egg_info to be rebuilt (including `sdist`, `wheel`, and installing into a different virtualenv), so this can be surprising. -[Bug #83](https://github.com/warner/python-versioneer/issues/83) describes +[Bug #83](https://github.com/python-versioneer/python-versioneer/issues/83) describes this one, but upgrading to a newer version of setuptools should probably resolve it. -### Unicode version strings - -While Versioneer works (and is continually tested) with both Python 2 and -Python 3, it is not entirely consistent with bytes-vs-unicode distinctions. -Newer releases probably generate unicode version strings on py2. It's not -clear that this is wrong, but it may be surprising for applications when then -write these strings to a network connection or include them in bytes-oriented -APIs like cryptographic checksums. - -[Bug #71](https://github.com/warner/python-versioneer/issues/71) investigates -this question. - ## Updating Versioneer @@ -265,6 +250,14 @@ direction and include code from all supported VCS systems, reducing the number of intermediate scripts. +## Similar projects + +* [setuptools_scm](https://github.com/pypa/setuptools_scm/) - a non-vendored build-time + dependency +* [minver](https://github.com/jbweston/miniver) - a lightweight reimplementation of + versioneer +* [versioningit](https://github.com/jwodder/versioningit) - a PEP 518-based setuptools + plugin ## License @@ -274,19 +267,27 @@ Dedication" license (CC0-1.0), as described in https://creativecommons.org/publicdomain/zero/1.0/ . +[pypi-image]: https://img.shields.io/pypi/v/versioneer.svg +[pypi-url]: https://pypi.python.org/pypi/versioneer/ +[travis-image]: +https://img.shields.io/travis/com/python-versioneer/python-versioneer.svg +[travis-url]: https://travis-ci.com/github/python-versioneer/python-versioneer + """ +# pylint:disable=invalid-name,import-outside-toplevel,missing-function-docstring +# pylint:disable=missing-class-docstring,too-many-branches,too-many-statements +# pylint:disable=raise-missing-from,too-many-lines,too-many-locals,import-error +# pylint:disable=too-few-public-methods,redefined-outer-name,consider-using-with +# pylint:disable=attribute-defined-outside-init,too-many-arguments -from __future__ import print_function -try: - import configparser -except ImportError: - import ConfigParser as configparser +import configparser import errno import json import os import re import subprocess import sys +from typing import Callable, Dict class VersioneerConfig: @@ -321,12 +322,12 @@ def get_root(): # module-import table will cache the first one. So we can't use # os.path.dirname(__file__), as that will find whichever # versioneer.py was first imported, even in later projects. - me = os.path.realpath(os.path.abspath(__file__)) - me_dir = os.path.normcase(os.path.splitext(me)[0]) + my_path = os.path.realpath(os.path.abspath(__file__)) + me_dir = os.path.normcase(os.path.splitext(my_path)[0]) vsr_dir = os.path.normcase(os.path.splitext(versioneer_py)[0]) if me_dir != vsr_dir: print("Warning: build in %s is using versioneer.py from %s" - % (os.path.dirname(me), versioneer_py)) + % (os.path.dirname(my_path), versioneer_py)) except NameError: pass return root @@ -334,30 +335,29 @@ def get_root(): def get_config_from_root(root): """Read the project setup.cfg file to determine Versioneer config.""" - # This might raise EnvironmentError (if setup.cfg is missing), or + # This might raise OSError (if setup.cfg is missing), or # configparser.NoSectionError (if it lacks a [versioneer] section), or # configparser.NoOptionError (if it lacks "VCS="). See the docstring at # the top of versioneer.py for instructions on writing your setup.cfg . setup_cfg = os.path.join(root, "setup.cfg") - parser = configparser.SafeConfigParser() - with open(setup_cfg, "r") as f: - parser.readfp(f) + parser = configparser.ConfigParser() + with open(setup_cfg, "r") as cfg_file: + parser.read_file(cfg_file) VCS = parser.get("versioneer", "VCS") # mandatory - def get(parser, name): - if parser.has_option("versioneer", name): - return parser.get("versioneer", name) - return None + # Dict-like interface for non-mandatory entries + section = parser["versioneer"] + cfg = VersioneerConfig() cfg.VCS = VCS - cfg.style = get(parser, "style") or "" - cfg.versionfile_source = get(parser, "versionfile_source") - cfg.versionfile_build = get(parser, "versionfile_build") - cfg.tag_prefix = get(parser, "tag_prefix") + cfg.style = section.get("style", "") + cfg.versionfile_source = section.get("versionfile_source") + cfg.versionfile_build = section.get("versionfile_build") + cfg.tag_prefix = section.get("tag_prefix") if cfg.tag_prefix in ("''", '""'): cfg.tag_prefix = "" - cfg.parentdir_prefix = get(parser, "parentdir_prefix") - cfg.verbose = get(parser, "verbose") + cfg.parentdir_prefix = section.get("parentdir_prefix") + cfg.verbose = section.get("verbose") return cfg @@ -366,17 +366,15 @@ class NotThisMethod(Exception): # these dictionaries contain VCS-specific tools -LONG_VERSION_PY = {} -HANDLERS = {} +LONG_VERSION_PY: Dict[str, str] = {} +HANDLERS: Dict[str, Dict[str, Callable]] = {} def register_vcs_handler(vcs, method): # decorator - """Decorator to mark a method as the handler for a particular VCS.""" + """Create decorator to mark a method as the handler of a VCS.""" def decorate(f): """Store f in HANDLERS[vcs][method].""" - if vcs not in HANDLERS: - HANDLERS[vcs] = {} - HANDLERS[vcs][method] = f + HANDLERS.setdefault(vcs, {})[method] = f return f return decorate @@ -385,17 +383,17 @@ def run_command(commands, args, cwd=None, verbose=False, hide_stderr=False, env=None): """Call the given command(s).""" assert isinstance(commands, list) - p = None - for c in commands: + process = None + for command in commands: try: - dispcmd = str([c] + args) + dispcmd = str([command] + args) # remember shell=False, so use git.cmd on windows, not just git - p = subprocess.Popen([c] + args, cwd=cwd, env=env, - stdout=subprocess.PIPE, - stderr=(subprocess.PIPE if hide_stderr - else None)) + process = subprocess.Popen([command] + args, cwd=cwd, env=env, + stdout=subprocess.PIPE, + stderr=(subprocess.PIPE if hide_stderr + else None)) break - except EnvironmentError: + except OSError: e = sys.exc_info()[1] if e.errno == errno.ENOENT: continue @@ -407,18 +405,16 @@ def run_command(commands, args, cwd=None, verbose=False, hide_stderr=False, if verbose: print("unable to find command, tried %s" % (commands,)) return None, None - stdout = p.communicate()[0].strip() - if sys.version_info[0] >= 3: - stdout = stdout.decode() - if p.returncode != 0: + stdout = process.communicate()[0].strip().decode() + if process.returncode != 0: if verbose: print("unable to run %s (error)" % dispcmd) print("stdout was %s" % stdout) - return None, p.returncode - return stdout, p.returncode + return None, process.returncode + return stdout, process.returncode -LONG_VERSION_PY['git'] = ''' +LONG_VERSION_PY['git'] = r''' # This file helps to compute a version number in source trees obtained from # git-archive tarball (such as those provided by githubs download-from-tag # feature). Distribution tarballs (built by setup.py sdist) and build @@ -426,7 +422,7 @@ def run_command(commands, args, cwd=None, verbose=False, hide_stderr=False, # that just contains the computed version number. # This file is released into the public domain. Generated by -# versioneer-0.18 (https://github.com/warner/python-versioneer) +# versioneer-0.21 (https://github.com/python-versioneer/python-versioneer) """Git implementation of _version.py.""" @@ -435,6 +431,7 @@ def run_command(commands, args, cwd=None, verbose=False, hide_stderr=False, import re import subprocess import sys +from typing import Callable, Dict def get_keywords(): @@ -472,12 +469,12 @@ class NotThisMethod(Exception): """Exception raised if a method is not valid for the current scenario.""" -LONG_VERSION_PY = {} -HANDLERS = {} +LONG_VERSION_PY: Dict[str, str] = {} +HANDLERS: Dict[str, Dict[str, Callable]] = {} def register_vcs_handler(vcs, method): # decorator - """Decorator to mark a method as the handler for a particular VCS.""" + """Create decorator to mark a method as the handler of a VCS.""" def decorate(f): """Store f in HANDLERS[vcs][method].""" if vcs not in HANDLERS: @@ -491,17 +488,17 @@ def run_command(commands, args, cwd=None, verbose=False, hide_stderr=False, env=None): """Call the given command(s).""" assert isinstance(commands, list) - p = None - for c in commands: + process = None + for command in commands: try: - dispcmd = str([c] + args) + dispcmd = str([command] + args) # remember shell=False, so use git.cmd on windows, not just git - p = subprocess.Popen([c] + args, cwd=cwd, env=env, - stdout=subprocess.PIPE, - stderr=(subprocess.PIPE if hide_stderr - else None)) + process = subprocess.Popen([command] + args, cwd=cwd, env=env, + stdout=subprocess.PIPE, + stderr=(subprocess.PIPE if hide_stderr + else None)) break - except EnvironmentError: + except OSError: e = sys.exc_info()[1] if e.errno == errno.ENOENT: continue @@ -513,15 +510,13 @@ def run_command(commands, args, cwd=None, verbose=False, hide_stderr=False, if verbose: print("unable to find command, tried %%s" %% (commands,)) return None, None - stdout = p.communicate()[0].strip() - if sys.version_info[0] >= 3: - stdout = stdout.decode() - if p.returncode != 0: + stdout = process.communicate()[0].strip().decode() + if process.returncode != 0: if verbose: print("unable to run %%s (error)" %% dispcmd) print("stdout was %%s" %% stdout) - return None, p.returncode - return stdout, p.returncode + return None, process.returncode + return stdout, process.returncode def versions_from_parentdir(parentdir_prefix, root, verbose): @@ -533,15 +528,14 @@ def versions_from_parentdir(parentdir_prefix, root, verbose): """ rootdirs = [] - for i in range(3): + for _ in range(3): dirname = os.path.basename(root) if dirname.startswith(parentdir_prefix): return {"version": dirname[len(parentdir_prefix):], "full-revisionid": None, "dirty": False, "error": None, "date": None} - else: - rootdirs.append(root) - root = os.path.dirname(root) # up a level + rootdirs.append(root) + root = os.path.dirname(root) # up a level if verbose: print("Tried directories %%s but none started with prefix %%s" %% @@ -558,22 +552,21 @@ def git_get_keywords(versionfile_abs): # _version.py. keywords = {} try: - f = open(versionfile_abs, "r") - for line in f.readlines(): - if line.strip().startswith("git_refnames ="): - mo = re.search(r'=\s*"(.*)"', line) - if mo: - keywords["refnames"] = mo.group(1) - if line.strip().startswith("git_full ="): - mo = re.search(r'=\s*"(.*)"', line) - if mo: - keywords["full"] = mo.group(1) - if line.strip().startswith("git_date ="): - mo = re.search(r'=\s*"(.*)"', line) - if mo: - keywords["date"] = mo.group(1) - f.close() - except EnvironmentError: + with open(versionfile_abs, "r") as fobj: + for line in fobj: + if line.strip().startswith("git_refnames ="): + mo = re.search(r'=\s*"(.*)"', line) + if mo: + keywords["refnames"] = mo.group(1) + if line.strip().startswith("git_full ="): + mo = re.search(r'=\s*"(.*)"', line) + if mo: + keywords["full"] = mo.group(1) + if line.strip().startswith("git_date ="): + mo = re.search(r'=\s*"(.*)"', line) + if mo: + keywords["date"] = mo.group(1) + except OSError: pass return keywords @@ -581,10 +574,14 @@ def git_get_keywords(versionfile_abs): @register_vcs_handler("git", "keywords") def git_versions_from_keywords(keywords, tag_prefix, verbose): """Get version information from git keywords.""" - if not keywords: - raise NotThisMethod("no keywords at all, weird") + if "refnames" not in keywords: + raise NotThisMethod("Short version file found") date = keywords.get("date") if date is not None: + # Use only the last line. Previous lines may contain GPG signature + # information. + date = date.splitlines()[-1] + # git-2.2.0 added "%%cI", which expands to an ISO-8601 -compliant # datestamp. However we prefer "%%ci" (which expands to an "ISO-8601 # -like" string, which we must then edit to make compliant), because @@ -597,11 +594,11 @@ def git_versions_from_keywords(keywords, tag_prefix, verbose): if verbose: print("keywords are unexpanded, not using") raise NotThisMethod("unexpanded keywords, not a git-archive tarball") - refs = set([r.strip() for r in refnames.strip("()").split(",")]) + refs = {r.strip() for r in refnames.strip("()").split(",")} # starting in git-1.8.3, tags are listed as "tag: foo-1.0" instead of # just "foo-1.0". If we see a "tag: " prefix, prefer those. TAG = "tag: " - tags = set([r[len(TAG):] for r in refs if r.startswith(TAG)]) + tags = {r[len(TAG):] for r in refs if r.startswith(TAG)} if not tags: # Either we're using git < 1.8.3, or there really are no tags. We use # a heuristic: assume all version tags have a digit. The old git %%d @@ -610,7 +607,7 @@ def git_versions_from_keywords(keywords, tag_prefix, verbose): # between branches and tags. By ignoring refnames without digits, we # filter out many common branch names like "release" and # "stabilization", as well as "HEAD" and "master". - tags = set([r for r in refs if re.search(r'\d', r)]) + tags = {r for r in refs if re.search(r'\d', r)} if verbose: print("discarding '%%s', no digits" %% ",".join(refs - tags)) if verbose: @@ -619,6 +616,11 @@ def git_versions_from_keywords(keywords, tag_prefix, verbose): # sorting will prefer e.g. "2.0" over "2.0rc1" if ref.startswith(tag_prefix): r = ref[len(tag_prefix):] + # Filter out refs that exactly match prefix or that don't start + # with a number once the prefix is stripped (mostly a concern + # when prefix is '') + if not re.match(r'\d', r): + continue if verbose: print("picking %%s" %% r) return {"version": r, @@ -634,7 +636,7 @@ def git_versions_from_keywords(keywords, tag_prefix, verbose): @register_vcs_handler("git", "pieces_from_vcs") -def git_pieces_from_vcs(tag_prefix, root, verbose, run_command=run_command): +def git_pieces_from_vcs(tag_prefix, root, verbose, runner=run_command): """Get version from 'git describe' in the root of the source tree. This only gets called if the git-archive 'subst' keywords were *not* @@ -642,11 +644,13 @@ def git_pieces_from_vcs(tag_prefix, root, verbose, run_command=run_command): version string, meaning we're inside a checked out source tree. """ GITS = ["git"] + TAG_PREFIX_REGEX = "*" if sys.platform == "win32": GITS = ["git.cmd", "git.exe"] + TAG_PREFIX_REGEX = r"\*" - out, rc = run_command(GITS, ["rev-parse", "--git-dir"], cwd=root, - hide_stderr=True) + _, rc = runner(GITS, ["rev-parse", "--git-dir"], cwd=root, + hide_stderr=True) if rc != 0: if verbose: print("Directory %%s not under git control" %% root) @@ -654,15 +658,16 @@ def git_pieces_from_vcs(tag_prefix, root, verbose, run_command=run_command): # if there is a tag matching tag_prefix, this yields TAG-NUM-gHEX[-dirty] # if there isn't one, this yields HEX[-dirty] (no NUM) - describe_out, rc = run_command(GITS, ["describe", "--tags", "--dirty", - "--always", "--long", - "--match", "%%s*" %% tag_prefix], - cwd=root) + describe_out, rc = runner(GITS, ["describe", "--tags", "--dirty", + "--always", "--long", + "--match", + "%%s%%s" %% (tag_prefix, TAG_PREFIX_REGEX)], + cwd=root) # --long was added in git-1.5.5 if describe_out is None: raise NotThisMethod("'git describe' failed") describe_out = describe_out.strip() - full_out, rc = run_command(GITS, ["rev-parse", "HEAD"], cwd=root) + full_out, rc = runner(GITS, ["rev-parse", "HEAD"], cwd=root) if full_out is None: raise NotThisMethod("'git rev-parse' failed") full_out = full_out.strip() @@ -672,6 +677,39 @@ def git_pieces_from_vcs(tag_prefix, root, verbose, run_command=run_command): pieces["short"] = full_out[:7] # maybe improved later pieces["error"] = None + branch_name, rc = runner(GITS, ["rev-parse", "--abbrev-ref", "HEAD"], + cwd=root) + # --abbrev-ref was added in git-1.6.3 + if rc != 0 or branch_name is None: + raise NotThisMethod("'git rev-parse --abbrev-ref' returned error") + branch_name = branch_name.strip() + + if branch_name == "HEAD": + # If we aren't exactly on a branch, pick a branch which represents + # the current commit. If all else fails, we are on a branchless + # commit. + branches, rc = runner(GITS, ["branch", "--contains"], cwd=root) + # --contains was added in git-1.5.4 + if rc != 0 or branches is None: + raise NotThisMethod("'git branch --contains' returned error") + branches = branches.split("\n") + + # Remove the first line if we're running detached + if "(" in branches[0]: + branches.pop(0) + + # Strip off the leading "* " from the list of branches. + branches = [branch[2:] for branch in branches] + if "master" in branches: + branch_name = "master" + elif not branches: + branch_name = None + else: + # Pick the first branch that is returned. Good or bad. + branch_name = branches[0] + + pieces["branch"] = branch_name + # parse describe_out. It will be like TAG-NUM-gHEX[-dirty] or HEX[-dirty] # TAG might have hyphens. git_describe = describe_out @@ -688,7 +726,7 @@ def git_pieces_from_vcs(tag_prefix, root, verbose, run_command=run_command): # TAG-NUM-gHEX mo = re.search(r'^(.+)-(\d+)-g([0-9a-f]+)$', git_describe) if not mo: - # unparseable. Maybe git-describe is misbehaving? + # unparsable. Maybe git-describe is misbehaving? pieces["error"] = ("unable to parse git-describe output: '%%s'" %% describe_out) return pieces @@ -713,13 +751,14 @@ def git_pieces_from_vcs(tag_prefix, root, verbose, run_command=run_command): else: # HEX: no tags pieces["closest-tag"] = None - count_out, rc = run_command(GITS, ["rev-list", "HEAD", "--count"], - cwd=root) + count_out, rc = runner(GITS, ["rev-list", "HEAD", "--count"], cwd=root) pieces["distance"] = int(count_out) # total number of commits # commit date: see ISO-8601 comment in git_versions_from_keywords() - date = run_command(GITS, ["show", "-s", "--format=%%ci", "HEAD"], - cwd=root)[0].strip() + date = runner(GITS, ["show", "-s", "--format=%%ci", "HEAD"], cwd=root)[0].strip() + # Use only the last line. Previous lines may contain GPG signature + # information. + date = date.splitlines()[-1] pieces["date"] = date.strip().replace(" ", "T", 1).replace(" ", "", 1) return pieces @@ -757,19 +796,67 @@ def render_pep440(pieces): return rendered -def render_pep440_pre(pieces): - """TAG[.post.devDISTANCE] -- No -dirty. +def render_pep440_branch(pieces): + """TAG[[.dev0]+DISTANCE.gHEX[.dirty]] . + + The ".dev0" means not master branch. Note that .dev0 sorts backwards + (a feature branch will appear "older" than the master branch). Exceptions: - 1: no tags. 0.post.devDISTANCE + 1: no tags. 0[.dev0]+untagged.DISTANCE.gHEX[.dirty] """ if pieces["closest-tag"]: rendered = pieces["closest-tag"] + if pieces["distance"] or pieces["dirty"]: + if pieces["branch"] != "master": + rendered += ".dev0" + rendered += plus_or_dot(pieces) + rendered += "%%d.g%%s" %% (pieces["distance"], pieces["short"]) + if pieces["dirty"]: + rendered += ".dirty" + else: + # exception #1 + rendered = "0" + if pieces["branch"] != "master": + rendered += ".dev0" + rendered += "+untagged.%%d.g%%s" %% (pieces["distance"], + pieces["short"]) + if pieces["dirty"]: + rendered += ".dirty" + return rendered + + +def pep440_split_post(ver): + """Split pep440 version string at the post-release segment. + + Returns the release segments before the post-release and the + post-release version number (or -1 if no post-release segment is present). + """ + vc = str.split(ver, ".post") + return vc[0], int(vc[1] or 0) if len(vc) == 2 else None + + +def render_pep440_pre(pieces): + """TAG[.postN.devDISTANCE] -- No -dirty. + + Exceptions: + 1: no tags. 0.post0.devDISTANCE + """ + if pieces["closest-tag"]: if pieces["distance"]: - rendered += ".post.dev%%d" %% pieces["distance"] + # update the post release segment + tag_version, post_version = pep440_split_post(pieces["closest-tag"]) + rendered = tag_version + if post_version is not None: + rendered += ".post%%d.dev%%d" %% (post_version+1, pieces["distance"]) + else: + rendered += ".post0.dev%%d" %% (pieces["distance"]) + else: + # no commits, use the tag as the version + rendered = pieces["closest-tag"] else: # exception #1 - rendered = "0.post.dev%%d" %% pieces["distance"] + rendered = "0.post0.dev%%d" %% pieces["distance"] return rendered @@ -800,12 +887,41 @@ def render_pep440_post(pieces): return rendered +def render_pep440_post_branch(pieces): + """TAG[.postDISTANCE[.dev0]+gHEX[.dirty]] . + + The ".dev0" means not master branch. + + Exceptions: + 1: no tags. 0.postDISTANCE[.dev0]+gHEX[.dirty] + """ + if pieces["closest-tag"]: + rendered = pieces["closest-tag"] + if pieces["distance"] or pieces["dirty"]: + rendered += ".post%%d" %% pieces["distance"] + if pieces["branch"] != "master": + rendered += ".dev0" + rendered += plus_or_dot(pieces) + rendered += "g%%s" %% pieces["short"] + if pieces["dirty"]: + rendered += ".dirty" + else: + # exception #1 + rendered = "0.post%%d" %% pieces["distance"] + if pieces["branch"] != "master": + rendered += ".dev0" + rendered += "+g%%s" %% pieces["short"] + if pieces["dirty"]: + rendered += ".dirty" + return rendered + + def render_pep440_old(pieces): """TAG[.postDISTANCE[.dev0]] . The ".dev0" means dirty. - Eexceptions: + Exceptions: 1: no tags. 0.postDISTANCE[.dev0] """ if pieces["closest-tag"]: @@ -876,10 +992,14 @@ def render(pieces, style): if style == "pep440": rendered = render_pep440(pieces) + elif style == "pep440-branch": + rendered = render_pep440_branch(pieces) elif style == "pep440-pre": rendered = render_pep440_pre(pieces) elif style == "pep440-post": rendered = render_pep440_post(pieces) + elif style == "pep440-post-branch": + rendered = render_pep440_post_branch(pieces) elif style == "pep440-old": rendered = render_pep440_old(pieces) elif style == "git-describe": @@ -915,7 +1035,7 @@ def get_versions(): # versionfile_source is the relative path from the top of the source # tree (where the .git directory might live) to this file. Invert # this to find the root from __file__. - for i in cfg.versionfile_source.split('/'): + for _ in cfg.versionfile_source.split('/'): root = os.path.dirname(root) except NameError: return {"version": "0+unknown", "full-revisionid": None, @@ -950,22 +1070,21 @@ def git_get_keywords(versionfile_abs): # _version.py. keywords = {} try: - f = open(versionfile_abs, "r") - for line in f.readlines(): - if line.strip().startswith("git_refnames ="): - mo = re.search(r'=\s*"(.*)"', line) - if mo: - keywords["refnames"] = mo.group(1) - if line.strip().startswith("git_full ="): - mo = re.search(r'=\s*"(.*)"', line) - if mo: - keywords["full"] = mo.group(1) - if line.strip().startswith("git_date ="): - mo = re.search(r'=\s*"(.*)"', line) - if mo: - keywords["date"] = mo.group(1) - f.close() - except EnvironmentError: + with open(versionfile_abs, "r") as fobj: + for line in fobj: + if line.strip().startswith("git_refnames ="): + mo = re.search(r'=\s*"(.*)"', line) + if mo: + keywords["refnames"] = mo.group(1) + if line.strip().startswith("git_full ="): + mo = re.search(r'=\s*"(.*)"', line) + if mo: + keywords["full"] = mo.group(1) + if line.strip().startswith("git_date ="): + mo = re.search(r'=\s*"(.*)"', line) + if mo: + keywords["date"] = mo.group(1) + except OSError: pass return keywords @@ -973,10 +1092,14 @@ def git_get_keywords(versionfile_abs): @register_vcs_handler("git", "keywords") def git_versions_from_keywords(keywords, tag_prefix, verbose): """Get version information from git keywords.""" - if not keywords: - raise NotThisMethod("no keywords at all, weird") + if "refnames" not in keywords: + raise NotThisMethod("Short version file found") date = keywords.get("date") if date is not None: + # Use only the last line. Previous lines may contain GPG signature + # information. + date = date.splitlines()[-1] + # git-2.2.0 added "%cI", which expands to an ISO-8601 -compliant # datestamp. However we prefer "%ci" (which expands to an "ISO-8601 # -like" string, which we must then edit to make compliant), because @@ -989,11 +1112,11 @@ def git_versions_from_keywords(keywords, tag_prefix, verbose): if verbose: print("keywords are unexpanded, not using") raise NotThisMethod("unexpanded keywords, not a git-archive tarball") - refs = set([r.strip() for r in refnames.strip("()").split(",")]) + refs = {r.strip() for r in refnames.strip("()").split(",")} # starting in git-1.8.3, tags are listed as "tag: foo-1.0" instead of # just "foo-1.0". If we see a "tag: " prefix, prefer those. TAG = "tag: " - tags = set([r[len(TAG):] for r in refs if r.startswith(TAG)]) + tags = {r[len(TAG):] for r in refs if r.startswith(TAG)} if not tags: # Either we're using git < 1.8.3, or there really are no tags. We use # a heuristic: assume all version tags have a digit. The old git %d @@ -1002,7 +1125,7 @@ def git_versions_from_keywords(keywords, tag_prefix, verbose): # between branches and tags. By ignoring refnames without digits, we # filter out many common branch names like "release" and # "stabilization", as well as "HEAD" and "master". - tags = set([r for r in refs if re.search(r'\d', r)]) + tags = {r for r in refs if re.search(r'\d', r)} if verbose: print("discarding '%s', no digits" % ",".join(refs - tags)) if verbose: @@ -1011,6 +1134,11 @@ def git_versions_from_keywords(keywords, tag_prefix, verbose): # sorting will prefer e.g. "2.0" over "2.0rc1" if ref.startswith(tag_prefix): r = ref[len(tag_prefix):] + # Filter out refs that exactly match prefix or that don't start + # with a number once the prefix is stripped (mostly a concern + # when prefix is '') + if not re.match(r'\d', r): + continue if verbose: print("picking %s" % r) return {"version": r, @@ -1026,7 +1154,7 @@ def git_versions_from_keywords(keywords, tag_prefix, verbose): @register_vcs_handler("git", "pieces_from_vcs") -def git_pieces_from_vcs(tag_prefix, root, verbose, run_command=run_command): +def git_pieces_from_vcs(tag_prefix, root, verbose, runner=run_command): """Get version from 'git describe' in the root of the source tree. This only gets called if the git-archive 'subst' keywords were *not* @@ -1034,11 +1162,13 @@ def git_pieces_from_vcs(tag_prefix, root, verbose, run_command=run_command): version string, meaning we're inside a checked out source tree. """ GITS = ["git"] + TAG_PREFIX_REGEX = "*" if sys.platform == "win32": GITS = ["git.cmd", "git.exe"] + TAG_PREFIX_REGEX = r"\*" - out, rc = run_command(GITS, ["rev-parse", "--git-dir"], cwd=root, - hide_stderr=True) + _, rc = runner(GITS, ["rev-parse", "--git-dir"], cwd=root, + hide_stderr=True) if rc != 0: if verbose: print("Directory %s not under git control" % root) @@ -1046,15 +1176,16 @@ def git_pieces_from_vcs(tag_prefix, root, verbose, run_command=run_command): # if there is a tag matching tag_prefix, this yields TAG-NUM-gHEX[-dirty] # if there isn't one, this yields HEX[-dirty] (no NUM) - describe_out, rc = run_command(GITS, ["describe", "--tags", "--dirty", - "--always", "--long", - "--match", "%s*" % tag_prefix], - cwd=root) + describe_out, rc = runner(GITS, ["describe", "--tags", "--dirty", + "--always", "--long", + "--match", + "%s%s" % (tag_prefix, TAG_PREFIX_REGEX)], + cwd=root) # --long was added in git-1.5.5 if describe_out is None: raise NotThisMethod("'git describe' failed") describe_out = describe_out.strip() - full_out, rc = run_command(GITS, ["rev-parse", "HEAD"], cwd=root) + full_out, rc = runner(GITS, ["rev-parse", "HEAD"], cwd=root) if full_out is None: raise NotThisMethod("'git rev-parse' failed") full_out = full_out.strip() @@ -1064,6 +1195,39 @@ def git_pieces_from_vcs(tag_prefix, root, verbose, run_command=run_command): pieces["short"] = full_out[:7] # maybe improved later pieces["error"] = None + branch_name, rc = runner(GITS, ["rev-parse", "--abbrev-ref", "HEAD"], + cwd=root) + # --abbrev-ref was added in git-1.6.3 + if rc != 0 or branch_name is None: + raise NotThisMethod("'git rev-parse --abbrev-ref' returned error") + branch_name = branch_name.strip() + + if branch_name == "HEAD": + # If we aren't exactly on a branch, pick a branch which represents + # the current commit. If all else fails, we are on a branchless + # commit. + branches, rc = runner(GITS, ["branch", "--contains"], cwd=root) + # --contains was added in git-1.5.4 + if rc != 0 or branches is None: + raise NotThisMethod("'git branch --contains' returned error") + branches = branches.split("\n") + + # Remove the first line if we're running detached + if "(" in branches[0]: + branches.pop(0) + + # Strip off the leading "* " from the list of branches. + branches = [branch[2:] for branch in branches] + if "master" in branches: + branch_name = "master" + elif not branches: + branch_name = None + else: + # Pick the first branch that is returned. Good or bad. + branch_name = branches[0] + + pieces["branch"] = branch_name + # parse describe_out. It will be like TAG-NUM-gHEX[-dirty] or HEX[-dirty] # TAG might have hyphens. git_describe = describe_out @@ -1080,7 +1244,7 @@ def git_pieces_from_vcs(tag_prefix, root, verbose, run_command=run_command): # TAG-NUM-gHEX mo = re.search(r'^(.+)-(\d+)-g([0-9a-f]+)$', git_describe) if not mo: - # unparseable. Maybe git-describe is misbehaving? + # unparsable. Maybe git-describe is misbehaving? pieces["error"] = ("unable to parse git-describe output: '%s'" % describe_out) return pieces @@ -1105,13 +1269,14 @@ def git_pieces_from_vcs(tag_prefix, root, verbose, run_command=run_command): else: # HEX: no tags pieces["closest-tag"] = None - count_out, rc = run_command(GITS, ["rev-list", "HEAD", "--count"], - cwd=root) + count_out, rc = runner(GITS, ["rev-list", "HEAD", "--count"], cwd=root) pieces["distance"] = int(count_out) # total number of commits # commit date: see ISO-8601 comment in git_versions_from_keywords() - date = run_command(GITS, ["show", "-s", "--format=%ci", "HEAD"], - cwd=root)[0].strip() + date = runner(GITS, ["show", "-s", "--format=%ci", "HEAD"], cwd=root)[0].strip() + # Use only the last line. Previous lines may contain GPG signature + # information. + date = date.splitlines()[-1] pieces["date"] = date.strip().replace(" ", "T", 1).replace(" ", "", 1) return pieces @@ -1130,27 +1295,26 @@ def do_vcs_install(manifest_in, versionfile_source, ipy): if ipy: files.append(ipy) try: - me = __file__ - if me.endswith(".pyc") or me.endswith(".pyo"): - me = os.path.splitext(me)[0] + ".py" - versioneer_file = os.path.relpath(me) + my_path = __file__ + if my_path.endswith(".pyc") or my_path.endswith(".pyo"): + my_path = os.path.splitext(my_path)[0] + ".py" + versioneer_file = os.path.relpath(my_path) except NameError: versioneer_file = "versioneer.py" files.append(versioneer_file) present = False try: - f = open(".gitattributes", "r") - for line in f.readlines(): - if line.strip().startswith(versionfile_source): - if "export-subst" in line.strip().split()[1:]: - present = True - f.close() - except EnvironmentError: + with open(".gitattributes", "r") as fobj: + for line in fobj: + if line.strip().startswith(versionfile_source): + if "export-subst" in line.strip().split()[1:]: + present = True + break + except OSError: pass if not present: - f = open(".gitattributes", "a+") - f.write("%s export-subst\n" % versionfile_source) - f.close() + with open(".gitattributes", "a+") as fobj: + fobj.write(f"{versionfile_source} export-subst\n") files.append(".gitattributes") run_command(GITS, ["add", "--"] + files) @@ -1164,15 +1328,14 @@ def versions_from_parentdir(parentdir_prefix, root, verbose): """ rootdirs = [] - for i in range(3): + for _ in range(3): dirname = os.path.basename(root) if dirname.startswith(parentdir_prefix): return {"version": dirname[len(parentdir_prefix):], "full-revisionid": None, "dirty": False, "error": None, "date": None} - else: - rootdirs.append(root) - root = os.path.dirname(root) # up a level + rootdirs.append(root) + root = os.path.dirname(root) # up a level if verbose: print("Tried directories %s but none started with prefix %s" % @@ -1181,7 +1344,7 @@ def versions_from_parentdir(parentdir_prefix, root, verbose): SHORT_VERSION_PY = """ -# This file was generated by 'versioneer.py' (0.18) from +# This file was generated by 'versioneer.py' (0.21) from # revision-control system data, or from the parent directory name of an # unpacked source archive. Distribution tarballs contain a pre-generated copy # of this file. @@ -1203,7 +1366,7 @@ def versions_from_file(filename): try: with open(filename) as f: contents = f.read() - except EnvironmentError: + except OSError: raise NotThisMethod("unable to read _version.py") mo = re.search(r"version_json = '''\n(.*)''' # END VERSION_JSON", contents, re.M | re.S) @@ -1258,19 +1421,67 @@ def render_pep440(pieces): return rendered -def render_pep440_pre(pieces): - """TAG[.post.devDISTANCE] -- No -dirty. +def render_pep440_branch(pieces): + """TAG[[.dev0]+DISTANCE.gHEX[.dirty]] . + + The ".dev0" means not master branch. Note that .dev0 sorts backwards + (a feature branch will appear "older" than the master branch). Exceptions: - 1: no tags. 0.post.devDISTANCE + 1: no tags. 0[.dev0]+untagged.DISTANCE.gHEX[.dirty] """ if pieces["closest-tag"]: rendered = pieces["closest-tag"] + if pieces["distance"] or pieces["dirty"]: + if pieces["branch"] != "master": + rendered += ".dev0" + rendered += plus_or_dot(pieces) + rendered += "%d.g%s" % (pieces["distance"], pieces["short"]) + if pieces["dirty"]: + rendered += ".dirty" + else: + # exception #1 + rendered = "0" + if pieces["branch"] != "master": + rendered += ".dev0" + rendered += "+untagged.%d.g%s" % (pieces["distance"], + pieces["short"]) + if pieces["dirty"]: + rendered += ".dirty" + return rendered + + +def pep440_split_post(ver): + """Split pep440 version string at the post-release segment. + + Returns the release segments before the post-release and the + post-release version number (or -1 if no post-release segment is present). + """ + vc = str.split(ver, ".post") + return vc[0], int(vc[1] or 0) if len(vc) == 2 else None + + +def render_pep440_pre(pieces): + """TAG[.postN.devDISTANCE] -- No -dirty. + + Exceptions: + 1: no tags. 0.post0.devDISTANCE + """ + if pieces["closest-tag"]: if pieces["distance"]: - rendered += ".post.dev%d" % pieces["distance"] + # update the post release segment + tag_version, post_version = pep440_split_post(pieces["closest-tag"]) + rendered = tag_version + if post_version is not None: + rendered += ".post%d.dev%d" % (post_version+1, pieces["distance"]) + else: + rendered += ".post0.dev%d" % (pieces["distance"]) + else: + # no commits, use the tag as the version + rendered = pieces["closest-tag"] else: # exception #1 - rendered = "0.post.dev%d" % pieces["distance"] + rendered = "0.post0.dev%d" % pieces["distance"] return rendered @@ -1301,12 +1512,41 @@ def render_pep440_post(pieces): return rendered +def render_pep440_post_branch(pieces): + """TAG[.postDISTANCE[.dev0]+gHEX[.dirty]] . + + The ".dev0" means not master branch. + + Exceptions: + 1: no tags. 0.postDISTANCE[.dev0]+gHEX[.dirty] + """ + if pieces["closest-tag"]: + rendered = pieces["closest-tag"] + if pieces["distance"] or pieces["dirty"]: + rendered += ".post%d" % pieces["distance"] + if pieces["branch"] != "master": + rendered += ".dev0" + rendered += plus_or_dot(pieces) + rendered += "g%s" % pieces["short"] + if pieces["dirty"]: + rendered += ".dirty" + else: + # exception #1 + rendered = "0.post%d" % pieces["distance"] + if pieces["branch"] != "master": + rendered += ".dev0" + rendered += "+g%s" % pieces["short"] + if pieces["dirty"]: + rendered += ".dirty" + return rendered + + def render_pep440_old(pieces): """TAG[.postDISTANCE[.dev0]] . The ".dev0" means dirty. - Eexceptions: + Exceptions: 1: no tags. 0.postDISTANCE[.dev0] """ if pieces["closest-tag"]: @@ -1377,10 +1617,14 @@ def render(pieces, style): if style == "pep440": rendered = render_pep440(pieces) + elif style == "pep440-branch": + rendered = render_pep440_branch(pieces) elif style == "pep440-pre": rendered = render_pep440_pre(pieces) elif style == "pep440-post": rendered = render_pep440_post(pieces) + elif style == "pep440-post-branch": + rendered = render_pep440_post_branch(pieces) elif style == "pep440-old": rendered = render_pep440_old(pieces) elif style == "git-describe": @@ -1480,8 +1724,12 @@ def get_version(): return get_versions()["version"] -def get_cmdclass(): - """Get the custom setuptools/distutils subclasses used by Versioneer.""" +def get_cmdclass(cmdclass=None): + """Get the custom setuptools/distutils subclasses used by Versioneer. + + If the package uses a different cmdclass (e.g. one from numpy), it + should be provide as an argument. + """ if "versioneer" in sys.modules: del sys.modules["versioneer"] # this fixes the "python setup.py develop" case (also 'install' and @@ -1495,9 +1743,9 @@ def get_cmdclass(): # parent is protected against the child's "import versioneer". By # removing ourselves from sys.modules here, before the child build # happens, we protect the child from the parent's versioneer too. - # Also see https://github.com/warner/python-versioneer/issues/52 + # Also see https://github.com/python-versioneer/python-versioneer/issues/52 - cmds = {} + cmds = {} if cmdclass is None else cmdclass.copy() # we add "version" to both distutils and setuptools from distutils.core import Command @@ -1539,7 +1787,9 @@ def run(self): # setup.py egg_info -> ? # we override different "build_py" commands for both environments - if "setuptools" in sys.modules: + if 'build_py' in cmds: + _build_py = cmds['build_py'] + elif "setuptools" in sys.modules: from setuptools.command.build_py import build_py as _build_py else: from distutils.command.build_py import build_py as _build_py @@ -1559,6 +1809,33 @@ def run(self): write_to_version_file(target_versionfile, versions) cmds["build_py"] = cmd_build_py + if 'build_ext' in cmds: + _build_ext = cmds['build_ext'] + elif "setuptools" in sys.modules: + from setuptools.command.build_ext import build_ext as _build_ext + else: + from distutils.command.build_ext import build_ext as _build_ext + + class cmd_build_ext(_build_ext): + def run(self): + root = get_root() + cfg = get_config_from_root(root) + versions = get_versions() + _build_ext.run(self) + if self.inplace: + # build_ext --inplace will only build extensions in + # build/lib<..> dir with no _version.py to write to. + # As in place builds will already have a _version.py + # in the module dir, we do not need to write one. + return + # now locate _version.py in the new build/ directory and replace + # it with an updated value + target_versionfile = os.path.join(self.build_lib, + cfg.versionfile_build) + print("UPDATING %s" % target_versionfile) + write_to_version_file(target_versionfile, versions) + cmds["build_ext"] = cmd_build_ext + if "cx_Freeze" in sys.modules: # cx_freeze enabled? from cx_Freeze.dist import build_exe as _build_exe # nczeczulin reports that py2exe won't like the pep440-style string @@ -1592,10 +1869,7 @@ def run(self): del cmds["build_py"] if 'py2exe' in sys.modules: # py2exe enabled? - try: - from py2exe.distutils_buildexe import py2exe as _py2exe # py3 - except ImportError: - from py2exe.build_exe import py2exe as _py2exe # py2 + from py2exe.distutils_buildexe import py2exe as _py2exe class cmd_py2exe(_py2exe): def run(self): @@ -1620,7 +1894,9 @@ def run(self): cmds["py2exe"] = cmd_py2exe # we override different "sdist" commands for both environments - if "setuptools" in sys.modules: + if 'sdist' in cmds: + _sdist = cmds['sdist'] + elif "setuptools" in sys.modules: from setuptools.command.sdist import sdist as _sdist else: from distutils.command.sdist import sdist as _sdist @@ -1687,21 +1963,26 @@ def make_release_tree(self, base_dir, files): """ -INIT_PY_SNIPPET = """ +OLD_SNIPPET = """ from ._version import get_versions __version__ = get_versions()['version'] del get_versions """ +INIT_PY_SNIPPET = """ +from . import {0} +__version__ = {0}.get_versions()['version'] +""" + def do_setup(): - """Main VCS-independent setup function for installing Versioneer.""" + """Do main VCS-independent setup function for installing Versioneer.""" root = get_root() try: cfg = get_config_from_root(root) - except (EnvironmentError, configparser.NoSectionError, + except (OSError, configparser.NoSectionError, configparser.NoOptionError) as e: - if isinstance(e, (EnvironmentError, configparser.NoSectionError)): + if isinstance(e, (OSError, configparser.NoSectionError)): print("Adding sample versioneer config to setup.cfg", file=sys.stderr) with open(os.path.join(root, "setup.cfg"), "a") as f: @@ -1725,12 +2006,18 @@ def do_setup(): try: with open(ipy, "r") as f: old = f.read() - except EnvironmentError: + except OSError: old = "" - if INIT_PY_SNIPPET not in old: + module = os.path.splitext(os.path.basename(cfg.versionfile_source))[0] + snippet = INIT_PY_SNIPPET.format(module) + if OLD_SNIPPET in old: + print(" replacing boilerplate in %s" % ipy) + with open(ipy, "w") as f: + f.write(old.replace(OLD_SNIPPET, snippet)) + elif snippet not in old: print(" appending to %s" % ipy) with open(ipy, "a") as f: - f.write(INIT_PY_SNIPPET) + f.write(snippet) else: print(" %s unmodified" % ipy) else: @@ -1749,7 +2036,7 @@ def do_setup(): if line.startswith("include "): for include in line.split()[1:]: simple_includes.add(include) - except EnvironmentError: + except OSError: pass # That doesn't cover everything MANIFEST.in can do # (http://docs.python.org/2/distutils/sourcedist.html#commands), so