diff --git a/.gitignore b/.gitignore index edcbba4d5..eb6be73dd 100644 --- a/.gitignore +++ b/.gitignore @@ -70,6 +70,7 @@ Pipfile.lock *.egg .eggs MANIFEST +spacy/git_info.py # Temporary files *.~* diff --git a/MANIFEST.in b/MANIFEST.in index 1947b9140..9819c7b70 100644 --- a/MANIFEST.in +++ b/MANIFEST.in @@ -6,3 +6,4 @@ include bin/spacy include pyproject.toml recursive-exclude spacy/lang *.json recursive-include spacy/lang *.json.gz +recursive-include licenses * diff --git a/setup.py b/setup.py index 62a09aa73..01e372e91 100755 --- a/setup.py +++ b/setup.py @@ -118,6 +118,55 @@ def is_source_release(path): return os.path.exists(os.path.join(path, "PKG-INFO")) +# Include the git version in the build (adapted from NumPy) +# Copyright (c) 2005-2020, NumPy Developers. +# BSD 3-Clause license, see licenses/3rd_party_licenses.txt +def write_git_info_py(filename="spacy/git_info.py"): + def _minimal_ext_cmd(cmd): + # construct minimal environment + env = {} + for k in ["SYSTEMROOT", "PATH", "HOME"]: + v = os.environ.get(k) + if v is not None: + env[k] = v + # LANGUAGE is used on win32 + env["LANGUAGE"] = "C" + env["LANG"] = "C" + env["LC_ALL"] = "C" + out = subprocess.check_output(cmd, stderr=subprocess.STDOUT, env=env) + return out + + git_version = "Unknown" + if os.path.exists(".git"): + try: + out = _minimal_ext_cmd(["git", "rev-parse", "--short", "HEAD"]) + git_version = out.strip().decode("ascii") + except: + pass + elif os.path.exists(filename): + # must be a source distribution, use existing version file + try: + a = open(filename, "r") + lines = a.readlines() + git_version = lines[-1].split('"')[1] + except: + pass + finally: + a.close() + + text = """# THIS FILE IS GENERATED FROM SPACY SETUP.PY +# +GIT_VERSION = "%(git_version)s" +""" + a = open(filename, "w") + try: + a.write( + text % {"git_version": git_version,} + ) + finally: + a.close() + + def clean(path): for name in MOD_NAMES: name = name.replace(".", "/") @@ -140,6 +189,8 @@ def chdir(new_dir): def setup_package(): + write_git_info_py() + root = os.path.abspath(os.path.dirname(__file__)) if len(sys.argv) > 1 and sys.argv[1] == "clean": diff --git a/spacy/language.py b/spacy/language.py index faa0447a4..e9d195453 100644 --- a/spacy/language.py +++ b/spacy/language.py @@ -34,6 +34,7 @@ from .lang.tag_map import TAG_MAP from .tokens import Doc from .lang.lex_attrs import LEX_ATTRS, is_stop from .errors import Errors, Warnings +from .git_info import GIT_VERSION from . import util from . import about @@ -206,6 +207,7 @@ class Language(object): self._meta.setdefault("email", "") self._meta.setdefault("url", "") self._meta.setdefault("license", "") + self._meta.setdefault("spacy_git_version", GIT_VERSION) self._meta["vectors"] = { "width": self.vocab.vectors_length, "vectors": len(self.vocab.vectors),