-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgenerate-binary.py
executable file
·109 lines (93 loc) · 3.22 KB
/
generate-binary.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
#!/usr/bin/env python3
###############################################################################
# Copyright (c) 2013 INRIA
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation;
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
# Authors: Daniel Camara <[email protected]>
# Mathieu Lacage <[email protected]>
###############################################################################
def collect_source(source):
import os
sources = []
for root, dirs, files in os.walk(source):
for f in files:
if f.endswith('.py'):
sources.append(os.path.join(root, f))
return sources
def calculate_hash(sources):
import hashlib
m = hashlib.sha256()
for source in sources:
f = open(source, 'r')
for line in f:
m.update(line.encode())
f.close()
sha256hash = m.hexdigest()
return sha256hash
def generate_zip(sources):
import zipfile
import tempfile
import os
(handle, pathname) = tempfile.mkstemp()
f = zipfile.ZipFile(pathname, 'w')
for source in sources:
f.write(source)
f.close()
os.close(handle)
return pathname
def generate_binary(source_dir, output):
import base64
sources = collect_source(source_dir)
sources_sha256 = calculate_hash(sources)
zipfile = generate_zip(sources)
with open(zipfile, "rb") as f:
zipdata = base64.b64encode(f.read()).decode()
f = open(output, 'w')
f.write("""#!/usr/bin/env python3
sources = [%s]
sources_sha256 = "%s"
zipdata = \"\"\"%s\"\"\"
def decompress(output):
import zipfile
import base64
decoded = base64.b64decode(zipdata).encode()
f = zipfile.ZipFile(decoded, 'r')
f.extractall(pathname)
f.close()
import os
import sys
pathname = os.path.join('.bake', sources_sha256)
if not os.path.exists(pathname):
os.makedirs(pathname)
decompress(pathname)
elif not os.path.isdir(pathname):
import tempfile
pathname = tempfile.mkdtemp()
decompress(pathname)
sys.path.append(pathname)
import %s as source
source.main(sys.argv)
""" % (','.join(["'%s'" % source for source in sources]), sources_sha256, zipdata,
source_dir))
f.close()
if __name__ == '__main__':
import optparse
parser = optparse.OptionParser()
parser.add_option('-s', '--source', dest='source', type='string', action='store', default='bake',
help='Source directory to embed')
parser.add_option('-o', '--output', dest='output', type='string', action='store', default='bake.binary',
help='Executable to generate')
(options, args) = parser.parse_args()
generate_binary(options.source, options.output)