Skip to content
Snippets Groups Projects
Commit ecfd5d86 authored by Joseph Weston's avatar Joseph Weston
Browse files

parse the version file instead of importing

Python 3 no longer allows implicit relative imports:
https://docs.python.org/release/3.0.1/whatsnew/3.0.html#removed-syntax
Instead of importing the version file we just parse it. By keeping
the extension ".py" we make it so that it will be distributed along
with the rest of the package.
parent 7ed34389
No related branches found
No related tags found
No related merge requests found
...@@ -11,7 +11,9 @@ import os ...@@ -11,7 +11,9 @@ import os
__all__ = ['version', 'KwantDeprecationWarning'] __all__ = ['version', 'KwantDeprecationWarning']
distr_root = os.path.dirname(os.path.dirname(os.path.realpath(__file__))) package_root = os.path.dirname(os.path.realpath(__file__))
distr_root = os.path.dirname(package_root)
version_file = '_kwant_version.py'
def get_version_from_git(): def get_version_from_git():
try: try:
...@@ -66,7 +68,14 @@ def get_version_from_git(): ...@@ -66,7 +68,14 @@ def get_version_from_git():
from _kwant_version import version # version file contains comments (lines starting with '#')
# followed by the version string on its own on a single line
with open(os.path.join(package_root, version_file), 'r') as f:
cruft="\"'\n\t\r " # strip whitespace and quotes
line = next(f).strip(cruft)
while line.startswith('#'):
line = next(f).strip(cruft)
version = line
version_is_from_git = (version == "__use_git__") version_is_from_git = (version == "__use_git__")
if version_is_from_git: if version_is_from_git:
version = get_version_from_git() version = get_version_from_git()
......
# This file will be overwritten by setup.py when a source or binary # This file will be overwritten by setup.py when a source or binary
# distribution is made. The magic value "__use_git__" is interpreted by # distribution is made. The magic value "__use_git__" is interpreted by
# _common.py in this directory. # _common.py in this directory.
version = "__use_git__" "__use_git__"
...@@ -47,11 +47,6 @@ TUT_HIDDEN_PREFIX = '#HIDDEN' ...@@ -47,11 +47,6 @@ TUT_HIDDEN_PREFIX = '#HIDDEN'
# it is not built yet. # it is not built yet.
_dont_write_bytecode_saved = sys.dont_write_bytecode _dont_write_bytecode_saved = sys.dont_write_bytecode
sys.dont_write_bytecode = True sys.dont_write_bytecode = True
try:
imp.load_source(STATIC_VERSION_PATH[-1].split('.')[0],
os.path.join(*STATIC_VERSION_PATH))
except IOError:
pass
_common = imp.load_source('_common', 'kwant/_common.py') _common = imp.load_source('_common', 'kwant/_common.py')
sys.dont_write_bytecode = _dont_write_bytecode_saved sys.dont_write_bytecode = _dont_write_bytecode_saved
...@@ -228,7 +223,7 @@ def write_version(fname): ...@@ -228,7 +223,7 @@ def write_version(fname):
pass pass
with open(fname, 'w') as f: with open(fname, 'w') as f:
f.write("# This file has been created by setup.py.\n") f.write("# This file has been created by setup.py.\n")
f.write("version = '{}'\n".format(version)) f.write(version)
def long_description(): def long_description():
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment