py2so.py
6.78 KB
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
import os
import sys
import time
import shutil
import argparse
from distutils.core import setup
from distutils.extension import Extension
from Cython.Build import cythonize
from Cython.Distutils import build_ext
import platform
if sys.version_info[0] < 3:
raise Exception("This program requires at least python3.6")
is_windows = platform.system() == 'Windows'
parser = argparse.ArgumentParser(description='compile the .py to .so(Linux/Mac) or .pdy(Win)')
parser.add_argument('-i', '--input', help='the directory(file) path of your project', required=True)
parser.add_argument('-o', '--output', help='the directory path of compiled file')
parser.add_argument('-e', '--exclude', help='exclude file. eg: ignore/ignore.py,main.py,ignore')
args = parser.parse_args()
def get_export_symbols_fixed(self, ext):
names = ext.name.split('.')
if names[-1] != "__init__":
initfunc_name = "PyInit_" + names[-1]
else:
# take name of the package if it is an __init__-file
initfunc_name = "PyInit_" + names[-2]
if initfunc_name not in ext.export_symbols:
ext.export_symbols.append(initfunc_name)
return ext.export_symbols
def get_exclude_paths(input_path):
exclude_paths = []
if args.exclude is not None and os.path.isdir(input_path):
paths = args.exclude.split(',')
for path in paths:
abs_path = path if os.path.isabs(path) else os.path.join(input_path, path)
if not os.path.exists(abs_path):
print('Warning: exclude path not exists: {0}'.format(abs_path))
continue
exclude_paths.append(abs_path)
return set(exclude_paths)
def get_py_files(dir_path, exclude_paths):
for parent, _, filenames in os.walk(dir_path):
if parent in exclude_paths:
print('exclude path: {0}'.format(parent))
continue
for filename in filenames:
if not filename.endswith('.py'):
continue
file_path = os.path.join(parent, filename)
if file_path in exclude_paths:
print('exclude path: {0}'.format(file_path))
continue
yield file_path
def is_about_py(filename):
if filename.endswith('.py') or filename.endswith('.pyc') or filename.endswith('.pyo') or filename.endswith('.pyd'):
return True
return False
def copy_other_files(input_path, exclude_paths, output_path):
if not os.path.isdir(input_path):
return
for parent, _, filenames in os.walk(input_path):
if parent in exclude_paths:
# relpath = os.path.relpath(parent, input_path)
# target_dir = os.path.join(output_path, relpath)
# shutil.copytree(parent, target_dir)
# print('copy {0} to {1}'.format(parent, target_dir))
continue
for filename in filenames:
file_path = os.path.join(parent, filename)
if file_path in exclude_paths:
continue
elif not is_about_py(filename):
relpath = os.path.relpath(file_path, input_path)
target_path = os.path.join(output_path, relpath)
os.makedirs(os.path.dirname(target_path), exist_ok=True)
shutil.copy(file_path, target_path)
print('copy {0} to {1}'.format(file_path, target_path))
def del_c(dir_path, timestamp):
if not os.path.isdir(dir_path):
c_file_path = '{0}.c'.format(os.path.splitext(dir_path)[0])
os.remove(c_file_path)
print('remove c file path: {0}'.format(c_file_path))
return
for parent, _, filenames in os.walk(dir_path):
for filename in filenames:
file_path = os.path.join(parent, filename)
if filename.endswith('.c') and os.stat(file_path).st_mtime > timestamp:
os.remove(file_path)
print('remove c file path: {0}'.format(file_path))
def get_extensions(file_paths, input_path):
print('---------- python file summary ----------')
extensions = []
for file_path in file_paths:
if os.path.isfile(input_path):
relpath = os.path.basename(file_path)
else:
relpath = os.path.relpath(file_path, start=input_path)
name = os.path.splitext(relpath)[0].replace(os.path.sep, '.')
sources = [file_path]
print('python file path: {0} module name: {1}'.format(file_path, name))
extension = Extension(name, sources)
extension.cython_c_in_temp = True
extensions.append(extension)
return extensions
def main():
input_path = os.path.realpath(args.input)
if not os.path.exists(input_path):
print('input path not exists: {0}'.format(input_path))
return
timestamp = int(time.time())
build_dir_name = 'build_{0}'.format(timestamp)
tmp_dir_name = 'tmp_{0}'.format(timestamp)
if args.output is None:
output_path = os.path.join(os.path.dirname(input_path), build_dir_name)
else:
output_path = os.path.realpath(args.output)
tmp_path = os.path.join(output_path, tmp_dir_name)
os.makedirs(tmp_path, exist_ok=True)
exclude_paths = get_exclude_paths(input_path)
if os.path.isdir(input_path):
file_paths = get_py_files(input_path, exclude_paths)
else:
file_paths = [input_path]
print('---------- directory summary ----------')
print('input path: {0}'.format(input_path))
print('output path: {0}'.format(output_path))
print('output tmp path: {0}'.format(tmp_path))
print('exclude paths: {0}'.format(exclude_paths))
extensions = get_extensions(file_paths, input_path)
print('---------- compile start ----------')
success = False
try:
if is_windows:
# replace wrong version with the fixed:
build_ext.get_export_symbols = get_export_symbols_fixed
compiler_directives = {"language_level": '3'}
ext_modules = cythonize(module_list=extensions,
compiler_directives=compiler_directives)
setup(cmdclass={'build_ext': build_ext},
ext_modules=ext_modules, script_args=["build_ext", "-b", output_path, "-t", tmp_path])
except Extension as e:
print('---------- compile failed ----------')
print('compile error: {0}'.format(e))
else:
print('---------- compile completed ----------')
print('output path: {0}'.format(output_path))
success = True
finally:
print('---------- cleaning ----------')
if os.path.exists(tmp_path):
shutil.rmtree(tmp_path)
print('remove output tmp path: {0}'.format(tmp_path))
del_c(input_path, timestamp)
if success:
print('---------- copy !.py file to {0} ----------'.format(build_dir_name))
copy_other_files(input_path, exclude_paths, output_path)
if __name__ == '__main__':
main()