Commit 874b6c29 authored by Christoph Groth's avatar Christoph Groth
Browse files

move ensure_python() from setup.py into Kwant and use it

This allows to provide a helpful error message when someone tries to
import Kwant with Python 2.

Note that I moved the version code into a module of its own:
kwant.version.  The rationale is to separate this code from unrelated
stuff in kwant._common that requires various external libs.  The
kwant.version module should work on its own without any third-party
libraries.
parent 8f17eae6
# Copyright 2011-2013 Kwant authors.
# Copyright 2011-2017 Kwant authors.
#
# This file is part of Kwant. It is subject to the license terms in the file
# LICENSE.rst found in the top-level directory of this distribution and at
......@@ -8,6 +8,10 @@
__all__ = []
from . import version
version.ensure_python()
__version__ = version.version
import numpy # Needed by C. Gohlke's Windows package.
import warnings
......@@ -28,8 +32,6 @@ from ._common import KwantDeprecationWarning, UserCodeError
__all__.extend(['KwantDeprecationWarning', 'UserCodeError'])
from ._common import version as __version__
for module in ['system', 'builder', 'lattice', 'solvers', 'digest', 'rmt',
'operator', 'kpm', 'wraparound', 'continuum']:
exec('from . import {0}'.format(module))
......
......@@ -6,81 +6,11 @@
# the file AUTHORS.rst at the top-level directory of this distribution and at
# http://kwant-project.org/authors.
import subprocess
import os
import numpy as np
import numbers
import inspect
__all__ = ['version', 'KwantDeprecationWarning', 'UserCodeError']
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():
try:
p = subprocess.Popen(['git', 'rev-parse', '--show-toplevel'],
cwd=distr_root,
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
except OSError:
return
if p.wait() != 0:
return
if not os.path.samefile(p.communicate()[0].decode().rstrip('\n'), distr_root):
# The top-level directory of the current Git repository is not the same
# as the root directory of the Kwant distribution: do not extract the
# version from Git.
return
# git describe --first-parent does not take into account tags from branches
# that were merged-in.
for opts in [['--first-parent'], []]:
try:
p = subprocess.Popen(['git', 'describe', '--long'] + opts,
cwd=distr_root,
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
except OSError:
return
if p.wait() == 0:
break
else:
return
description = p.communicate()[0].decode().strip('v').rstrip('\n')
release, dev, git = description.rsplit('-', 2)
version = [release]
labels = []
if dev != "0":
version.append(".dev{}".format(dev))
labels.append(git)
try:
p = subprocess.Popen(['git', 'diff', '--quiet'], cwd=distr_root)
except OSError:
labels.append('confused') # This should never happen.
else:
if p.wait() == 1:
labels.append('dirty')
if labels:
version.append('+')
version.append(".".join(labels))
return "".join(version)
# populate the version_info dictionary with values stored in the version file
version_info = {}
with open(os.path.join(package_root, version_file), 'r') as f:
exec(f.read(), {}, version_info)
version = version_info['version']
version_is_from_git = (version == "__use_git__")
if version_is_from_git:
version = get_version_from_git()
if not version:
version = "unknown"
__all__ = ['KwantDeprecationWarning', 'UserCodeError']
class KwantDeprecationWarning(Warning):
......
# Copyright 2011-2017 Kwant authors.
#
# This file is part of Kwant. It is subject to the license terms in the file
# LICENSE.rst found in the top-level directory of this distribution and at
# http://kwant-project.org/license. A list of Kwant authors can be found in
# the file AUTHORS.rst at the top-level directory of this distribution and at
# http://kwant-project.org/authors.
# This module must also work with Python 2.
from __future__ import print_function
import sys
import subprocess
import os
# No public API
__all__ = []
package_root = os.path.dirname(os.path.realpath(__file__))
distr_root = os.path.dirname(package_root)
version_file = '_kwant_version.py'
def ensure_python(required_version=(3, 4)):
v = sys.version_info
if v[:3] < required_version:
error = "This version of Kwant requires Python {} or above.".format(
".".join(str(p) for p in required_version))
if v[0] == 2:
error += "\nKwant 1.1 is the last version to support Python 2."
print(error, file=sys.stderr)
sys.exit(1)
def get_version_from_git():
try:
p = subprocess.Popen(['git', 'rev-parse', '--show-toplevel'],
cwd=distr_root,
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
except OSError:
return
if p.wait() != 0:
return
if not os.path.samefile(p.communicate()[0].decode().rstrip('\n'),
distr_root):
# The top-level directory of the current Git repository is not the same
# as the root directory of the Kwant distribution: do not extract the
# version from Git.
return
# git describe --first-parent does not take into account tags from branches
# that were merged-in.
for opts in [['--first-parent'], []]:
try:
p = subprocess.Popen(['git', 'describe', '--long'] + opts,
cwd=distr_root,
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
except OSError:
return
if p.wait() == 0:
break
else:
return
description = p.communicate()[0].decode().strip('v').rstrip('\n')
release, dev, git = description.rsplit('-', 2)
version = [release]
labels = []
if dev != "0":
version.append(".dev{}".format(dev))
labels.append(git)
try:
p = subprocess.Popen(['git', 'diff', '--quiet'], cwd=distr_root)
except OSError:
labels.append('confused') # This should never happen.
else:
if p.wait() == 1:
labels.append('dirty')
if labels:
version.append('+')
version.append(".".join(labels))
return "".join(version)
# populate the version_info dictionary with values stored in the version file
version_info = {}
with open(os.path.join(package_root, version_file), 'r') as f:
exec(f.read(), {}, version_info)
version = version_info['version']
version_is_from_git = (version == "__use_git__")
if version_is_from_git:
version = get_version_from_git()
if not version:
version = "unknown"
......@@ -12,19 +12,6 @@ from __future__ import print_function
import sys
def ensure_python(required_version):
v = sys.version_info
if v[:3] < required_version:
error = "This version of Kwant requires Python {} or above.".format(
".".join(str(p) for p in required_version))
if v[0] == 2:
error += "\nKwant 1.1 is the last version to support Python 2."
print(error, file=sys.stderr)
sys.exit(1)
ensure_python((3, 4))
import re
import os
import glob
......@@ -131,18 +118,19 @@ def configure_extensions(exts, aliases=(), build_summary=None):
return exts
def get_version():
def check_versions():
global version, version_is_from_git
# Let Kwant itself determine its own version. We cannot simply import
# kwant, as it is not built yet.
_dont_write_bytecode_saved = sys.dont_write_bytecode
sys.dont_write_bytecode = True
_common = imp.load_source('_common', 'kwant/_common.py')
version_module = imp.load_source('version', 'kwant/version.py')
sys.dont_write_bytecode = _dont_write_bytecode_saved
version = _common.version
version_is_from_git = _common.version_is_from_git
version_module.ensure_python()
version = version_module.version
version_is_from_git = version_module.version_is_from_git
def init_cython():
......@@ -560,6 +548,8 @@ def maybe_cythonize(exts):
def main():
check_versions()
exts = collections.OrderedDict([
('kwant._system',
dict(sources=['kwant/_system.pyx'],
......@@ -606,7 +596,6 @@ def main():
aliases = [('lapack', 'kwant.linalg.lapack'),
('mumps', 'kwant.linalg._mumps')]
get_version()
init_cython()
global build_summary
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment