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

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

""" 

@file 

@brief Looks for paths, Miktek, Graphviz... 

""" 

import sys 

import os 

import errno 

 

 

def find_in_PATH(prog): 

""" 

look into every path mentioned in ``%PATH%`` a specific file, 

it raises an exception if not Found 

 

@param prog program to look for 

@return path 

""" 

sep = ";" if sys.platform.startswith("win") else ":" 

path = os.environ["PATH"] 

for p in path.split(sep): 

f = os.path.join(p, prog) 

if os.path.exists(f): 

return p 

return None 

 

 

def find_graphviz_dot(exc=True): 

""" 

Determines the path to graphviz (on Windows), 

the function tests the existence of versions 34 to 45 

assuming it was installed in a standard folder: 

``C:\\Program Files\\MiKTeX 2.9\\miktex\\bin\\x64``. 

 

:param exc: raise exception of be silent 

:return: path to dot 

:raises FileNotFoundError: if graphviz not found 

""" 

if sys.platform.startswith("win"): # pragma: no cover 

version = range(34, 45) 

for v in version: 

graphviz_dot = "C:\\Program Files (x86)\\Graphviz2.{0}\\bin\\dot.exe".format( 

v) 

if os.path.exists(graphviz_dot): 

return graphviz_dot 

extra = ['build/update_modules/Graphviz/bin'] 

for ext in extra: 

graphviz_dot = os.path.join(ext, "dot.exe") 

if os.path.exists(graphviz_dot): 

return graphviz_dot 

p = find_in_PATH("dot.exe") 

if p is None: 

if exc: 

typstr = str 

raise FileNotFoundError( 

"Unable to find graphviz, look into paths such as: {}".format( 

typstr(graphviz_dot))) 

return None 

else: 

return os.path.join(p, "dot.exe") 

else: 

# linux 

return "dot" 

 

 

def find_latex_path(exc=True): 

""" 

Finds latex path. 

Returns an empty string on :epkg:`linux`. 

 

:param exc: raises an exception or be silent 

:return: something like ``C:\\Program Files\\MiKTeX 2.9\\miktex\\bin\\x64`` 

:raises FileNotFoundError: if latex not found 

""" 

if sys.platform.startswith("win"): # pragma: no cover 

latex = latex0 = r"C:\Program Files\MiKTeX 2.9\miktex\bin\x64" 

if not os.path.exists(latex): 

latex = find_in_PATH("latex.exe") 

if latex is None or not os.path.exists(latex): 

if exc: 

typstr = str 

raise FileNotFoundError( 

"unable to find latex (miktex), look into paths such as: " + typstr(latex0)) 

return None 

return latex 

else: 

# linux, should be in PATH. 

return "" 

 

 

def find_pandoc_path(exc=True): 

""" 

Determines :epkg:`pandoc` location. 

Returns an empty string on :epkg:`linux`. 

 

@return path to :epkg:`pandoc` 

""" 

if sys.platform.startswith("win"): # pragma: no cover 

path = os.environ["USERPROFILE"] 

pandoc = os.path.join(path, "AppData", "Local", "Pandoc") 

pdoc = os.path.join(pandoc, "pandoc.exe") 

if os.path.exists(pdoc): 

return pandoc 

tries = [pandoc] 

 

path = os.environ["ProgramFiles(x86)"] 

pandoc = os.path.join(path, "Pandoc") 

pdoc = os.path.join(pandoc, "pandoc.exe") 

if os.path.exists(pdoc): 

return pandoc 

tries.append(pandoc) 

 

if not os.path.exists(pandoc): 

# we try others users because pandoc goes into a user folder by 

# default 

root = os.path.normpath(os.path.join(path, "..")) 

users = os.listdir(root) 

for u in users: 

p = os.path.join(root, u) 

if os.path.isdir(p): 

pandoc = os.path.join(p, "AppData", "Local", "Pandoc") 

if os.path.exists(pandoc): 

return pandoc 

tries.append(pandoc) 

pandoc = find_in_PATH("pandoc.exe") 

if pandoc is None and exc: 

raise FileNotFoundError( 

"unable to find pandoc, look into paths such as:\n" + "\n".join(tries)) 

return pandoc 

else: 

return pandoc 

else: 

# linux, should be in PATH. 

return "" 

 

 

def custom_ensuredir(path): 

# type: (unicode) -> None 

"""Ensure that a path exists.""" 

if "IMPOSSIBLE:TOFIND" in path: 

return 

try: 

os.makedirs(path) 

except OSError as err: 

# 0 for Jython/Win32 

EEXIST = getattr(errno, 'EEXIST', 0) 

if err.errno not in [0, EEXIST]: 

raise 

 

 

def find_dvipng_path(exc=True): 

""" 

Determines :epkg:`dvipng` location. 

 

@return *imgmath_latex*, *imgmath_dvipng*, *imgmath_dvisvgm* 

 

.. versionadded:: 1.8 

""" 

 

if sys.platform.startswith("win"): # pragma: no cover 

sep = ";" 

imgmath_latex = find_latex_path(exc=exc) 

if imgmath_latex is None: 

imgmath_dvipng = None 

else: 

imgmath_dvipng = os.path.join(imgmath_latex, "dvipng.exe") 

if imgmath_dvipng is None or not os.path.exists(imgmath_dvipng): 

if exc: 

raise FileNotFoundError(imgmath_dvipng) 

imgmath_dvipng = "dvipng" 

if imgmath_latex is None: 

imgmath_dvisvgm = None 

else: 

imgmath_dvisvgm = os.path.join(imgmath_latex, "dvisvgm.exe") 

if imgmath_dvisvgm is None or not os.path.exists(imgmath_dvisvgm): 

if exc: 

raise FileNotFoundError(imgmath_dvisvgm) 

imgmath_dvisvgm = "dvisvgm" 

 

env_path = os.environ.get("PATH", "") 

if imgmath_latex and imgmath_latex not in env_path: 

if len(env_path) > 0: 

env_path += sep 

env_path += imgmath_latex 

 

if imgmath_latex is not None and sys.platform.startswith("win"): 

imgmath_latex = os.path.join(imgmath_latex, "latex.exe") 

 

# verification 

if imgmath_latex is None or not os.path.exists(imgmath_latex): 

if exc: 

raise FileNotFoundError(imgmath_latex) 

imgmath_latex = "latex" 

if imgmath_dvipng is None or not os.path.exists(imgmath_dvipng): 

if exc: 

raise FileNotFoundError(imgmath_dvipng) 

imgmath_dvipng = "dvipng" 

else: 

# On linux, we expect latex, dvipng, dvisvgm to be available. 

imgmath_latex = "latex" 

imgmath_dvipng = "dvipng" 

imgmath_dvisvgm = "dvisvgm" 

 

return imgmath_latex, imgmath_dvipng, imgmath_dvisvgm