-
Notifications
You must be signed in to change notification settings - Fork 153
Expand file tree
/
Copy pathonebinary_gen.py
More file actions
152 lines (130 loc) · 4.16 KB
/
Copy pathonebinary_gen.py
File metadata and controls
152 lines (130 loc) · 4.16 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
#!/usr/bin/env python
# Generates files necessary with hardcoded capabilities for Python
import os
from pathlib import Path
import json
import argparse
parser = argparse.ArgumentParser(
prog='onebinary_gen.py',
description='Generates header files necessary to create the MistServer combined binary'
)
parser.add_argument('--cap-header', help="location of the generated capabilities header", required=True)
parser.add_argument('--entrypoint', help="location of the generated entrypoint file", required=True)
parser.add_argument('files', metavar='N', type=str, nargs='+', help='input binary json files generated elsewhere')
args = parser.parse_args()
json_files = []
header_files = []
for file in args.files:
if file.endswith('.json'):
json_files.append(file)
elif file.endswith('h'):
header_files.append(file)
else:
raise Exception("unknown file type: " + file)
# print('json')
# print(json_files)
# print('h')
# print(header_files)
MIST_IN = "MistIn"
MIST_OUT = "MistOut"
CAP_LINE = ' capabilities["{category}"]["{connector}"] = JSON::fromString({json_str});'
capabilities = []
for name in json_files:
path = Path(name)
stem = path.stem
text = path.read_text().strip("\n")
json_str = json.dumps(text)
category = ''
connector = ''
class_name = ''
if stem.startswith(MIST_IN):
category = "inputs"
connector = stem[len(MIST_IN):]
class_name = "Mist::Input" + connector
func_name = "InputMain"
elif stem.startswith(MIST_OUT):
category = "connectors"
connector = stem[len(MIST_OUT):]
class_name = "Mist::Out" + connector
func_name = "OutputMain"
else:
raise Exception("unknown binary naming convention: " + stem)
capabilities.append({
'json_str': json_str,
'category': category,
'connector': connector,
'class_name': class_name,
'binary_name' : stem,
'func_name': func_name,
})
cap_lines = [
'#include "src/controller/controller_capabilities_static.h"',
'namespace Controller{',
' void addStaticCapabilities(JSON::Value &capabilities) {',
]
for cap in capabilities:
line = CAP_LINE.format(**cap)
cap_lines.append(line)
cap_lines.extend([
' }',
'}',
])
out_fullpath = os.path.join(os.getcwd(), args.cap_header)
Path(out_fullpath).write_text('\n'.join(cap_lines))
entrypoint_lines = []
for header_file in header_files:
entrypoint_lines.append('#include "' + header_file + '"')
entrypoint_lines.extend([
'#include <mist/config.h>',
'#include <mist/defines.h>',
'#include <mist/socket.h>',
'#include <mist/util.h>',
'#include <mist/stream.h>',
'#include "src/session.h"',
'#include "src/controller/controller.h"',
'#include "src/output/mist_out.cpp"',
'#include "src/input/mist_in.cpp"',
])
entrypoint_lines.extend([
# '#include "src/output/mist_out.cpp"',
# '#include "src/input/mist_in.cpp"',
# '#include "src/session.cpp"',
# '#include "src/controller/controller.cpp"',
'int main(int argc, char *argv[]){',
' if (argc < 2) {',
' program_invocation_short_name = (char *)"MistController";'
' return ControllerMain(argc, argv);',
' }',
' // Create a new argv array without argv[1]',
' int new_argc = argc - 1;',
' char** new_argv = new char*[new_argc];',
' for (int i = 0, j = 0; i < argc; ++i) {',
' if (i != 1) {',
' new_argv[j++] = argv[i];',
' }',
' }',
' if (strcmp(argv[1], "MistController") == 0) {',
' return ControllerMain(new_argc, new_argv);',
' }',
])
for cap in capabilities:
entrypoint_lines.extend([
' else if (strcmp(argv[1], "' + cap['binary_name'] + '") == 0) {',
' program_invocation_short_name = argv[1];'
' return ' + cap['func_name'] +'<' + cap['class_name'] + '>(new_argc, new_argv);',
' }',
])
entrypoint_lines.extend([
' else if (strcmp(argv[1], "MistSession") == 0) {',
' return SessionMain(new_argc, new_argv);',
' }',
' else {',
' program_invocation_short_name = (char *)"MistController";',
' return ControllerMain(argc, argv);',
' }',
' INFO_MSG("binary not found: %s", argv[1]);',
' return 202;',
'}',
])
entrypoint_fullpath = os.path.join(os.getcwd(), args.entrypoint)
Path(entrypoint_fullpath).write_text('\n'.join(entrypoint_lines))