25
25
26
26
Note: this script does not check any of the dependent C++ libraries.
27
27
"""
28
- from __future__ import print_function
29
28
30
29
import os
31
30
import sys
35
34
import argparse
36
35
37
36
38
- HASH_FILE = ' cythonize.json'
37
+ HASH_FILE = " cythonize.json"
39
38
40
39
41
40
def process_pyx (fromfile , tofile ):
42
- print (' Processing %s' % fromfile )
41
+ print (" Processing %s" % fromfile )
43
42
try :
44
43
from Cython .Compiler .Version import version as cython_version
45
44
from distutils .version import LooseVersion
46
- if LooseVersion (cython_version ) < LooseVersion ('0.19' ):
47
- raise Exception ('Require Cython >= 0.19' )
45
+
46
+ if LooseVersion (cython_version ) < LooseVersion ("0.19" ):
47
+ raise Exception ("Require Cython >= 0.19" )
48
48
49
49
except ImportError :
50
50
pass
51
51
52
- flags = [' --fast-fail' ]
53
- if tofile .endswith (' .cpp' ):
54
- flags += [' --cplus' ]
52
+ flags = [" --fast-fail" ]
53
+ if tofile .endswith (" .cpp" ):
54
+ flags += [" --cplus" ]
55
55
56
56
try :
57
57
try :
58
- r = subprocess .call (['cython' ] + flags + ['-o' , tofile , fromfile ],
59
- env = os .environ ) # See Issue #791
58
+ r = subprocess .call (
59
+ ["cython" ] + flags + ["-o" , tofile , fromfile ], env = os .environ
60
+ ) # See Issue #791
60
61
if r != 0 :
61
- raise Exception (' Cython failed' )
62
+ raise Exception (" Cython failed" )
62
63
except OSError :
63
64
# There are ways of installing Cython that don't result in a cython
64
65
# executable on the path, see gh-2397.
65
- r = subprocess .call ([sys .executable , '-c' ,
66
- 'import sys; from Cython.Compiler.Main import '
67
- 'setuptools_main as main; sys.exit(main())' ] + flags +
68
- ['-o' , tofile , fromfile ])
66
+ r = subprocess .call (
67
+ [
68
+ sys .executable ,
69
+ "-c" ,
70
+ "import sys; from Cython.Compiler.Main import "
71
+ "setuptools_main as main; sys.exit(main())" ,
72
+ ]
73
+ + flags
74
+ + ["-o" , tofile , fromfile ]
75
+ )
69
76
if r != 0 :
70
- raise Exception (' Cython failed' )
77
+ raise Exception (" Cython failed" )
71
78
except OSError :
72
- raise OSError (' Cython needs to be installed' )
79
+ raise OSError (" Cython needs to be installed" )
73
80
74
81
75
82
def preserve_cwd (path , func , * args ):
@@ -89,12 +96,12 @@ def load_hashes(filename):
89
96
90
97
91
98
def save_hashes (hash_db , filename ):
92
- with open (filename , 'w' ) as f :
99
+ with open (filename , "w" ) as f :
93
100
f .write (json .dumps (hash_db ))
94
101
95
102
96
103
def get_hash (path ):
97
- return hashlib .md5 (open (path , 'rb' ).read ()).hexdigest ()
104
+ return hashlib .md5 (open (path , "rb" ).read ()).hexdigest ()
98
105
99
106
100
107
def hash_changed (base , path , db ):
@@ -109,25 +116,27 @@ def hash_add(base, path, db):
109
116
110
117
def process (base , filename , db ):
111
118
root , ext = os .path .splitext (filename )
112
- if ext in ['.pyx' , '.cpp' ]:
113
- if hash_changed (base , filename , db ) or not os .path .isfile (os .path .join (base , root + '.cpp' )):
114
- preserve_cwd (base , process_pyx , root + '.pyx' , root + '.cpp' )
115
- hash_add (base , root + '.cpp' , db )
116
- hash_add (base , root + '.pyx' , db )
119
+ if ext in [".pyx" , ".cpp" ]:
120
+ if hash_changed (base , filename , db ) or not os .path .isfile (
121
+ os .path .join (base , root + ".cpp" )
122
+ ):
123
+ preserve_cwd (base , process_pyx , root + ".pyx" , root + ".cpp" )
124
+ hash_add (base , root + ".cpp" , db )
125
+ hash_add (base , root + ".pyx" , db )
117
126
118
127
119
128
def check_changes (root , db ):
120
129
res = False
121
130
new_db = {}
122
131
123
- setup_filename = ' setup.py'
124
- hash_add ('.' , setup_filename , new_db )
125
- if hash_changed ('.' , setup_filename , db ):
132
+ setup_filename = " setup.py"
133
+ hash_add ("." , setup_filename , new_db )
134
+ if hash_changed ("." , setup_filename , db ):
126
135
res = True
127
136
128
137
for base , _ , files in os .walk (root ):
129
138
for filename in files :
130
- if filename .endswith (' .pxd' ):
139
+ if filename .endswith (" .pxd" ):
131
140
hash_add (base , filename , new_db )
132
141
if hash_changed (base , filename , db ):
133
142
res = True
@@ -150,8 +159,10 @@ def run(root):
150
159
save_hashes (db , HASH_FILE )
151
160
152
161
153
- if __name__ == '__main__' :
154
- parser = argparse .ArgumentParser (description = 'Cythonize pyx files into C++ files as needed' )
155
- parser .add_argument ('root' , help = 'root directory' )
162
+ if __name__ == "__main__" :
163
+ parser = argparse .ArgumentParser (
164
+ description = "Cythonize pyx files into C++ files as needed"
165
+ )
166
+ parser .add_argument ("root" , help = "root directory" )
156
167
args = parser .parse_args ()
157
168
run (args .root )
0 commit comments