/
builder.py
71 lines (57 loc) · 2.58 KB
/
builder.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
import io
import os
from nodewatcher.core.generator.cgm import exceptions as cgm_exceptions
def build_image(result, profile):
"""
Spawns the builder process for the specified firmware image.
:param result: Destination build result
:param profile: Device OpenWRT profile
:return: A list of output firmware files
"""
cfg = result.config
with result.builder.connect() as builder:
temp_path = builder.create_tempdir()
# Prepare configuration files
cfg_path = os.path.join(temp_path, 'etc', 'config')
for fname, content in cfg.items():
if fname.startswith('_'):
continue
builder.write_file(os.path.join(cfg_path, fname), content)
# Prepare user account files
from . import crypt
passwd = io.StringIO()
for account in cfg['_accounts'].get('users', {}).values():
if account['password'] != '*':
account['password'] = crypt.md5crypt(
account['password'],
os.urandom(6).encode('base_64').strip()
)
passwd.write('%(username)s:%(password)s:%(uid)d:%(gid)d:%(username)s:%(home)s:%(shell)s\n' % account)
builder.write_file(os.path.join(temp_path, 'etc', 'passwd'), passwd.getvalue().encode('ascii'))
# Prepare the banner file if configured
if cfg.get('_banner', None):
banner = io.StringIO()
banner.write(cfg['_banner'])
builder.write_file(os.path.join(temp_path, 'etc', 'banner'), banner.getvalue().encode('ascii'))
# Prepare the routing table mappings
tables = io.StringIO()
for identifier, name in cfg['_routing_tables'].items():
tables.write('%s\t%s\n' % (identifier, name))
builder.write_file(os.path.join(temp_path, 'etc', 'iproute2', 'rt_tables'), tables.getvalue().encode('ascii'))
# Run the build system and wait for its completion
result.build_log = builder.call(
'make', 'image',
'PROFILE=%s' % profile["name"],
'FILES=%s' % temp_path,
'PACKAGES=%s' % " ".join(cfg['_packages'])
)
# Collect the output files and return them
fw_files = []
for fw_file in profile['files']:
try:
fw_files.append(
(fw_file, builder.read_result_file(os.path.join('bin', result.builder.architecture, fw_file)))
)
except IOError:
raise cgm_exceptions.BuildError('Output file \'%s\' not found!' % fw_file)
return fw_files