-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmy_module.spec
196 lines (178 loc) · 7.34 KB
/
my_module.spec
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
# -*- mode: python -*-
# vim: ft=python
import glob
class PYD(EXE):
def __init__(self, *args, **kwargs):
self.name = kwargs.get('name', None)
self.entrymodule = kwargs.get('entrymodule', os.path.splitext(self.name)[0])
EXE.__init__(self, *args, **kwargs)
def _bootloader_file(self, module_name, pkg_path):
try:
# use setuptools if available
from setuptools import Distribution, Extension
from setuptools.command.build_ext import build_ext
except ImportError:
from distutils.core import Distribution, Extension
from distutils.command.build_ext import build_ext
import Cython.Compiler.Version
from Cython.Build import cythonize
old_path = sys.path
sys.path.insert(0, os.path.join(os.getcwd(), 'bootloader'))
import templates
sys.path = old_path
logger.info("building with Cython " + Cython.Compiler.Version.version)
module_path = os.path.join(WORKPATH, "build" + module_name)
if not os.path.exists(module_path):
os.makedirs(module_path)
files = [
(os.path.join(module_path, module_name + ".c"), templates.MAIN_C),
(os.path.join(module_path, "payload.c"), templates.PAYLOAD_C),
]
with open(pkg_path, 'rb') as pkg_file:
payload = pkg_file.read()
tpl_vars = {
'module_name': module_name,
'payload': ',\n '.join([
', '.join([
'0x%02x' % (ord(i),)
for i in payload[16*j:16*(j+1)]
])
for j in range((len(payload) + 15) / 16)
]),
'len_payload': len(payload),
'entrymodule': self.entrymodule,
}
for path, tpl in files:
templates.write(path, tpl, **tpl_vars)
libraries = []
macros = []
include_dirs = ['bootloader/zlib', 'bootloader/common']
bootloader_files = []
bootloader_files += glob.glob('bootloader/zlib/*.c')
bootloader_files += glob.glob('bootloader/common/*.c')
if self.debug:
macros.append(('DEBUG', None))
if is_win:
libraries.append('wsock32')
macros.append(('WIN32', None))
macros.append(('_CRT_SECURE_NO_WARNINGS', None))
bootloader_files.append('bootloader/windows/utils.c')
elif is_darwin:
pass
else:
bootloader_files.append('bootloader/linux/getpath.c')
include_dirs.append('bootloader/linux')
print 'Building:\n %s' % ('\n '.join(bootloader_files), )
extensions = cythonize([
Extension(module_name,
include_dirs=include_dirs,
sources=[path for path, tpl in files] + bootloader_files,
libraries=libraries,
define_macros=macros),
])
dist = Distribution({'name': module_name, 'ext_modules': extensions})
dist.package_dir = module_path
cmd = build_ext(dist)
cmd.build_lib = module_path
cmd.build_temp = module_path
cmd.ensure_finalized()
cmd.run()
if is_win:
return os.path.join(module_path, module_name + '.pyd')
else:
return os.path.join(module_path, module_name + '.so')
def assemble(self):
logger.info("building PYD from %s", os.path.basename(self.out))
trash = []
if not os.path.exists(os.path.dirname(self.name)):
os.makedirs(os.path.dirname(self.name))
outf = open(self.name, 'wb')
exe = self._bootloader_file(os.path.splitext(os.path.basename(self.name))[0], self.pkg.name)
if config['hasRsrcUpdate'] and (self.icon or self.versrsrc or
self.resources):
tmpnm = tempfile.mktemp()
shutil.copy2(exe, tmpnm)
os.chmod(tmpnm, 0755)
if self.icon:
icon.CopyIcons(tmpnm, self.icon)
if self.versrsrc:
versioninfo.SetVersion(tmpnm, self.versrsrc)
for res in self.resources:
res = res.split(",")
for i in range(1, len(res)):
try:
res[i] = int(res[i])
except ValueError:
pass
resfile = res[0]
restype = resname = reslang = None
if len(res) > 1:
restype = res[1]
if len(res) > 2:
resname = res[2]
if len(res) > 3:
reslang = res[3]
try:
winresource.UpdateResourcesFromResFile(tmpnm, resfile,
[restype or "*"],
[resname or "*"],
[reslang or "*"])
except winresource.pywintypes.error, exc:
if exc.args[0] != winresource.ERROR_BAD_EXE_FORMAT:
logger.exception(exc)
continue
if not restype or not resname:
logger.error("resource type and/or name not specified")
continue
if "*" in (restype, resname):
logger.error("no wildcards allowed for resource type "
"and name when source file does not "
"contain resources")
continue
try:
winresource.UpdateResourcesFromDataFile(tmpnm,
resfile,
restype,
[resname],
[reslang or 0])
except winresource.pywintypes.error, exc:
logger.exception(exc)
trash.append(tmpnm)
exe = tmpnm
exe = checkCache(exe, strip=self.strip, upx=self.upx)
self.copy(exe, outf)
#if self.append_pkg:
# logger.info("Appending archive to EXE %s", self.name)
# self.copy(self.pkg.name, outf)
#else:
# logger.info("Copying archive to %s", self.pkgname)
# shutil.copy2(self.pkg.name, self.pkgname)
outf.close()
os.chmod(self.name, 0755)
guts = (self.name, self.console, self.debug, self.icon,
self.versrsrc, self.resources, self.strip, self.upx,
mtime(self.name))
assert len(guts) == len(self.GUTS)
_save_data(self.out, guts)
for item in trash:
os.remove(item)
return 1
a = Analysis(['module.py'])
a = Analysis(['module.py'],
hiddenimports=[
'numbers', # Hidden import of PySFML
]
)
pyz = PYZ(a.pure)
pyd = PYD(pyz,
a.scripts,
a.binaries,
a.zipfiles,
a.datas,
name='my_module' + (is_win and '.pyd' or '.so'),
entrymodule='module',
debug=False,
strip=None,
upx=False,
console=True,
)