-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathbuild.py
131 lines (111 loc) · 3.52 KB
/
build.py
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
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# __author__ = Lyon
# TODO: About render build?
# TODO 待修复, 现在还有问题
# 关于使用了模板的文件, 暂时不做打包处理, 放入`IGNORE_FILES`里即可
import os
import time
import shutil
from distutils.core import setup
from Cython.Build import cythonize
ROOT_PATH = os.path.abspath('.')
PROJECT_NAME = ROOT_PATH.split('/')[-1]
# Ignore
EXCEPT_FILES = {
'build.py'
}
# Only copy
IGNORE_FILES = {
'run.py',
'docs/view.py',
'celerys/server.py'
}
PY_FILE_EXCEPT_SUF = ('.pyc', '.pyx')
PY_FILE_SUF = ('.py')
def ls(dir=''):
"""Return all relative path under the current folder."""
dir_path = os.path.join(ROOT_PATH, dir)
for filename in os.listdir(dir_path):
absolute_file_path = os.path.join(dir_path, filename)
file_path = os.path.join(dir, filename)
if filename.startswith('.'):
continue
if os.path.isdir(absolute_file_path) and not filename.startswith('__'):
for file in ls(file_path):
yield file
else:
yield file_path
def move_dist(dist):
"""Move dist/project_name -> dist/"""
files = ls(dist)
for file in files:
src = os.path.join(ROOT_PATH, file)
dst = os.path.join(ROOT_PATH, file.replace('/%s' % PROJECT_NAME, '', 1))
dir = '/'.join(dst.split('/')[:-1])
if not os.path.exists(dir):
os.makedirs(dir)
shutil.move(src, dst)
if os.path.exists(os.path.join(dist, PROJECT_NAME)):
shutil.rmtree(os.path.join(dist, PROJECT_NAME))
def copy_ignore(dist):
"""Copy exclude files"""
files = ls()
for file in files:
if file.split('/')[0] == dist:
continue
suffix = os.path.splitext(file)[1]
if file in IGNORE_FILES:
pass
elif not suffix:
continue
elif suffix in PY_FILE_EXCEPT_SUF:
continue
elif suffix in PY_FILE_SUF:
continue
src = os.path.join(ROOT_PATH, file)
dst = os.path.join(ROOT_PATH, os.path.join(dist, PROJECT_NAME, file.replace(ROOT_PATH, '', 1)))
dir = '/'.join(dst.split('/')[:-1])
if not os.path.exists(dir):
os.makedirs(dir)
shutil.copyfile(src, dst)
def build(dist='dist'):
"""py -> c -> so"""
start = time.time()
files = list(ls())
module_list = list()
for file in files:
if file in EXCEPT_FILES or file in IGNORE_FILES:
continue
suffix = os.path.splitext(file)[1]
if not suffix:
continue
elif suffix in PY_FILE_EXCEPT_SUF:
continue
elif suffix in PY_FILE_SUF:
module_list.append(file)
dist = os.path.join('.', dist)
dist_temp = os.path.join(dist, 'temp')
try:
setup(ext_modules=cythonize(module_list, language_level="3"),
script_args=["build_ext", "-b", dist, "-t", dist_temp])
except Exception as e:
print('Error: ', e)
if os.path.exists(dist_temp):
shutil.rmtree(dist_temp)
for file in ls():
if not file.endswith('.c'):
continue
os.remove(os.path.join(ROOT_PATH, file))
return
if os.path.exists(dist_temp):
shutil.rmtree(dist_temp)
for file in ls():
if not file.endswith('.c'):
continue
os.remove(os.path.join(ROOT_PATH, file))
copy_ignore(dist)
end = time.time()
print('Complete, %.2fs !' % (end - start))
if __name__ == '__main__':
build('dist')