-
-
Notifications
You must be signed in to change notification settings - Fork 609
Expand file tree
/
Copy pathmodule.py
More file actions
executable file
·283 lines (233 loc) · 10.5 KB
/
Copy pathmodule.py
File metadata and controls
executable file
·283 lines (233 loc) · 10.5 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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
#!/usr/bin/python3
import re
import os
import subprocess
import operator
import argparse
from functools import reduce
from osv.modules import api, resolve, filemap
class isolated_jvm(api.basic_app):
multimain_manifest = '/etc/javamains'
apps = []
def prepare_manifest(self, build_dir, manifest_type, manifest):
if manifest_type != 'usr':
return
javamains_path = os.path.join(build_dir, 'javamains')
with open(javamains_path, "w") as mains:
for app in self.apps:
mains.write('\n'.join(app.get_multimain_lines()) + '\n')
manifest.write('%s: %s\n' % (self.multimain_manifest, javamains_path))
def get_launcher_args(self):
jvm_args = []
for app in self.apps:
jvm_args.extend(app.get_jvm_args())
return ['java.so'] + jvm_args + ['io.osv.isolated.MultiJarLoader', '-mains', self.multimain_manifest]
def add(self, app):
self.apps.append(app)
def has_any_app(self):
return self.apps
class non_isolated_jvm(api.basic_app):
app = None
def get_launcher_args(self):
return ['java.so'] + self.app.get_jvm_args() + self.app.get_multimain_lines()
def add(self, app):
self.app = app
def has_any_app(self):
return self.app
def expand(text, variables):
def resolve(m):
name = m.group('name')
if not name in variables:
raise Exception('Undefined variable: ' + name)
return variables[name]
return re.sub(r'\${(?P<name>.*)}', resolve, text)
def append_manifest(file_path, dst_file, libgcc_s_dir, variables={}):
with open(file_path) as src_file:
for line in src_file:
line = line.rstrip()
if line != '[manifest]':
expanded_line = expand(line + '\n', variables)
if len(libgcc_s_dir) > 0:
expanded_line = expanded_line.replace('%(libgcc_s_dir)s',libgcc_s_dir)
dst_file.write(expanded_line)
def generate_manifests(modules, basic_apps, usrskel='default'):
for manifest_type in ["usr", "bootfs"]:
manifest_name = "%s.manifest" % manifest_type
print("Preparing %s" % manifest_name)
manifest_skel = "%s.skel" % manifest_name
if manifest_type == "usr" and usrskel != "default":
manifest_skel = usrskel
with open(os.path.join(resolve.get_build_path(), manifest_name), "w") as manifest:
manifest.write('[manifest]\n')
if manifest_skel != 'none':
append_manifest(os.path.join(resolve.get_osv_base(), manifest_skel), manifest, os.getenv('libgcc_s_dir'))
for module in modules:
module_manifest = os.path.join(module.local_path, manifest_name)
if os.path.exists(module_manifest):
print("Appending %s to %s" % (module_manifest, manifest_name))
append_manifest(module_manifest, manifest, os.getenv('libgcc_s_dir'), variables={
'MODULE_DIR': module.local_path,
'OSV_BASE': resolve.get_osv_base()
})
filemap_attr = '%s_files' % manifest_type
if hasattr(module, filemap_attr):
filemap.as_manifest(getattr(module, filemap_attr), manifest.write)
for app in basic_apps:
app.prepare_manifest(resolve.get_build_path(), manifest_type, manifest)
def format_args(args):
if isinstance(args, str):
return args
else:
return ' '.join(args)
def get_command_line(basic_apps):
args = [format_args(app.get_launcher_args()) for app in basic_apps]
return '&'.join((a for a in args if a))
def make_cmd(cmdline, j, jobserver):
ret = 'make ' + cmdline
if jobserver is not None:
ret += ' -j --jobserver-fds=' + jobserver
elif j == '-':
ret += ' -j'
elif j is not None:
ret += ' -j' + j
return ret
def make_modules(modules, args):
for module in modules:
if os.path.exists(os.path.join(module.local_path, 'Makefile')):
if subprocess.call(make_cmd('module', j=args.j, jobserver=args.jobserver_fds),
shell=True, cwd=module.local_path):
raise Exception('make failed for ' + module.name)
def flatten_list(elememnts):
if not elememnts:
return []
if not isinstance(elememnts, list):
return [elememnts]
return reduce(operator.add, [flatten_list(e) for e in elememnts])
def get_basic_apps(apps):
basic_apps = []
_jvm = isolated_jvm()
for app in flatten_list(apps):
if isinstance(app, api.basic_app):
basic_apps.append(app)
elif isinstance(app, api.java_app):
java = resolve.require('java-base')
if hasattr(java,'non_isolated_jvm') and java.non_isolated_jvm:
_jvm = non_isolated_jvm()
_jvm.add(app)
else:
raise Exception("Unknown app type: " + str(app))
if _jvm.has_any_app():
basic_apps.append(_jvm)
return basic_apps
def generate_cmdline(apps):
cmdline_path = os.path.join(resolve.get_build_path(), "cmdline")
print("Saving command line to %s" % cmdline_path)
with open(cmdline_path, "w") as cmdline_file:
if apps:
cmdline_file.write(get_command_line(apps))
else:
print("No apps selected")
def build(args):
add_default = True
if args.image_config[0] == "!":
add_default = False
args.image_config = args.image_config[1:]
image_config_file = os.path.join(image_configs_dir, args.image_config + '.py')
if os.path.exists(image_config_file):
print("Using image config: %s" % image_config_file)
config = resolve.local_import(image_config_file)
run_list = config.get('run', [])
selected_modules = []
else:
# If images/image_config doesn't exist, assume image_config is a
# comma-separated list of module names, and build an image from those
# modules (and their dependencies). The command line to run is to
# run each of the module's "default" command line, in parallel.
# You can choose a module's non-default command line, with a dot,
# e.g.: mgmt.shell use's mgmt's "shell" command line.
print("No such image configuration: " + args.image_config + ". Assuming list of modules.")
run_list = []
disabled_modules = set()
selected_modules = args.image_config.split(",")
for module in selected_modules:
if module[0] == '-':
disabled_modules.add(module[1:])
module_names = []
config = resolve.read_config()
if add_default and "default" in config:
module_names += config["default"]
module_names += selected_modules
module_names = [i for i in module_names if not i in disabled_modules]
for missing in list(disabled_modules - set(module_names)):
raise Exception("Attempt to disable module %s but not enabled" % missing)
for module in module_names:
if module[0] == '-':
continue
a = module.split(".", 1)
name = a[0]
if len(a) > 1:
api.require_running(name, a[1])
else:
api.require_running(name)
# Add modules that are implicitly required if others are present
resolve.resolve_required_modules_if_other_is_present()
# By default append manifests from all modules resolved through api.require()
# otherwise (add_required_to_manifest=False) only append manifests from the selected_modules
if args.add_required_to_manifest:
modules = resolve.get_required_modules()
else:
modules = list(module for module in resolve.get_required_modules() if module.name in selected_modules)
modules_to_run = resolve.get_modules_to_run()
print("Modules:")
if not modules:
print(" None")
for module in modules:
prefix = " " + module.name
if module in modules_to_run:
print(prefix + "." + modules_to_run[module])
else:
print(prefix)
for module, run_config_name in modules_to_run.items():
run_config = resolve.get_run_config(module, run_config_name)
if run_config:
run_list.append(run_config)
make_modules(modules, args)
apps_to_run = get_basic_apps(run_list)
generate_manifests(modules, apps_to_run, args.usrskel)
generate_cmdline(apps_to_run)
def clean(args):
extra_args = {}
if args.quiet:
extra_args['stdout'] = open('/dev/null', 'w')
for local_path in resolve.all_module_directories():
if os.path.exists(os.path.join(local_path, 'Makefile')):
if not args.quiet:
print('Cleaning ' + local_path + ' ...')
if subprocess.call(make_cmd('-q clean', j=args.j, jobserver=args.jobserver_fds),
shell=True, cwd=local_path, stderr=subprocess.PIPE, **extra_args) != 2:
if subprocess.call(make_cmd('clean', j=args.j, jobserver=args.jobserver_fds),
shell=True, cwd=local_path, **extra_args):
raise Exception('\'make clean\' failed in ' + local_path)
if __name__ == "__main__":
image_configs_dir = resolve.get_images_dir()
parser = argparse.ArgumentParser(prog='module.py')
parser.add_argument('--jobserver-fds', action='store', default=None,
help='make -j support')
parser.add_argument('-j', action='store', default=None,
help='make -j support')
subparsers = parser.add_subparsers(help="Command")
build_cmd = subparsers.add_parser("build", help="Build modules")
build_cmd.add_argument("-c", "--image-config", action="store", default="default",
help="image configuration name. Looked up in " + image_configs_dir)
build_cmd.add_argument("--usrskel", action="store", default="default",
help="override default usr.manifest.skel")
build_cmd.add_argument("--no-required", dest="add_required_to_manifest", action="store_false",
help="do not add files to usr.manifest from modules implicitly resolved through api.require()")
build_cmd.set_defaults(func=build,add_required_to_manifest=True)
clean_cmd = subparsers.add_parser("clean", help="Clean modules")
clean_cmd.add_argument("-q", "--quiet", action="store_true")
clean_cmd.set_defaults(func=clean)
args = parser.parse_args()
if 'j' not in args:
args.jobserver_fds = None
args.func(args)