Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

# 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) 

 

def ensure_python(required_version=(3, 5)): 

v = sys.version_info 

24 ↛ 25line 24 didn't jump to line 25, because the condition on line 24 was never true 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 

40 ↛ 41line 40 didn't jump to line 41, because the condition on line 40 was never true if p.wait() != 0: 

return 

42 ↛ 47line 42 didn't jump to line 47, because the condition on line 42 was never true 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. 

51 ↛ 61line 51 didn't jump to line 61, because the loop on line 51 didn't complete 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 

58 ↛ 51line 58 didn't jump to line 51, because the condition on line 58 was never false 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 = [] 

67 ↛ 71line 67 didn't jump to line 71, because the condition on line 67 was never false 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: 

76 ↛ 77line 76 didn't jump to line 77, because the condition on line 76 was never true if p.wait() == 1: 

labels.append('dirty') 

 

79 ↛ 83line 79 didn't jump to line 83, because the condition on line 79 was never false if labels: 

version.append('+') 

version.append(".".join(labels)) 

 

return "".join(version) 

 

 

# TODO: change this logic when there is a git pretty-format 

# that gives the same output as 'git describe'. 

# Currently we can only tell the tag the current commit is 

# pointing to, or its hash (with no version info) 

# if it is not tagged. 

def get_version_from_git_archive(version_info): 

try: 

refnames = version_info['refnames'] 

git_hash = version_info['git_hash'] 

except KeyError: 

# These fields are not present if we are running from an sdist. 

# Execution should never reach here, though 

return None 

 

if git_hash.startswith('$Format') or refnames.startswith('$Format'): 

# variables not expanded during 'git archive' 

return None 

 

VTAG = 'tag: v' # Our version tags always start with 'v' 

refs = set(r.strip() for r in refnames.split(",")) 

version_tags = set(r[len(VTAG):] for r in refs if r.startswith(VTAG)) 

if version_tags: 

release, *_ = sorted(version_tags) # prefer e.g. "2.0" over "2.0rc1" 

return release 

else: 

return ''.join(('unknown', '+g', git_hash)) 

 

 

def init(version_file='_kwant_version.py'): 

global version, version_is_from_git 

version_info = {} 

with open(os.path.join(package_root, version_file), 'rb') as f: 

exec(f.read(), {}, version_info) 

version = version_info['version'] 

version_is_from_git = (version == "__use_git__") 

121 ↛ exitline 121 didn't return from function 'init', because the condition on line 121 was never false if version_is_from_git: 

version = get_version_from_git() 

123 ↛ 124line 123 didn't jump to line 124, because the condition on line 123 was never true if not version: 

version = get_version_from_git_archive(version_info) 

125 ↛ 126line 125 didn't jump to line 126, because the condition on line 125 was never true if not version: 

version = "unknown" 

 

init()