forked from AeonLucid/POGOProtos
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathcompile_single.py
235 lines (179 loc) · 8.04 KB
/
compile_single.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
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
#!/usr/bin/env python
import argparse
import os
import shutil
import re
from helpers import compile_helper
from subprocess import call
# Add this to your path
protoc_path = "protoc"
# Specify desired language / output
parser = argparse.ArgumentParser()
parser.add_argument("-l", "--lang", help="Language to produce protoc files")
parser.add_argument("-o", "--out_path", help="Output path for protoc files")
parser.add_argument("-d", "--desc_file", action='store_true', help="For generating a .desc file only")
parser.add_argument("--go_import_prefix", help="Prefix all imports in output go files for vendoring all dependencies")
parser.add_argument("--go_root_package", help="The root package of the output files as it should be in your `$GOPATH` eg. `github.com/xxx/yyy/pogoprotos`")
parser.add_argument("--java_multiple_files", action='store_true', help="Write each message to a separate .java file.")
args = parser.parse_args()
# Set defaults
lang = args.lang or "csharp"
out_path = args.out_path or "out"
desc_file = args.desc_file
default_out_path = out_path == "out"
go_import_prefix = args.go_import_prefix
go_root_package = args.go_root_package
java_multiple_files = args.java_multiple_files
# Determine where to store
proto_path = os.path.abspath("src")
tmp_path = os.path.abspath("tmp")
out_path = os.path.abspath(out_path)
# Clean up previous
if os.path.exists(tmp_path):
shutil.rmtree(tmp_path)
if default_out_path and os.path.exists(out_path):
shutil.rmtree(out_path)
# Create necessary directory
os.makedirs(tmp_path)
if not os.path.exists(out_path):
os.makedirs(out_path)
created_packages = []
package_mappings = []
# Go specific
go_package_mappings = []
def get_package(path):
for file_name in os.listdir(path):
file_name_path = os.path.join(path, file_name)
if os.path.isfile(file_name_path) and file_name.endswith('.proto'):
with open(file_name_path, 'r') as proto_file:
for proto_line in proto_file.readlines():
if proto_line.startswith("package"):
return re.search('package (.*?);', proto_line).group(1)
return None
def convert_to_go_package(pkg):
pkg = pkg.replace("POGOProtos.", "")
pkg = pkg.replace(".", "_").lower()
if pkg == "map":
pkg = "maps"
return pkg
def walk_files(main_file, path, package, imports=None):
if imports is None:
imports = []
if not desc_file and package == "POGOProtos":
print("Can't compile..")
print("File: '%s'" % path)
print("Please place the file in 'src/POGOProtos/' in a sub-directory.")
exit()
main_file.write('syntax = "proto3";\n')
short_package_name = str.split(package, '.')[-1].lower()
main_file.write('package %s;\n\n' % package)
if lang == "go":
go_pkg = convert_to_go_package(package)
package = "%s/%s" % (go_pkg, go_pkg)
main_file.write('option go_package = "%s";\n' % go_pkg)
if java_multiple_files:
main_file.write('option java_multiple_files = true;\n')
messages = ""
for file_name in os.listdir(path):
file_name_path = os.path.join(path, file_name)
if file_name_path.endswith(".proto") and os.path.isfile(file_name_path):
with open(file_name_path, 'r') as proto_file:
is_header = True
for proto_line in proto_file.readlines():
if proto_line.startswith("message") or proto_line.startswith("enum"):
is_header = False
if is_header:
if proto_line.startswith("import"):
import_from_package_re = re.search('import (public )?"(.*?)(\/)?([a-zA-Z0-9]+\.proto)";', proto_line)
if import_from_package_re is None:
print("Can't compile..")
print("File: '%s'" % file_name_path)
print("Bad import line: '%s'" % proto_line)
exit()
import_from_package = import_from_package_re.group(2).replace("/", ".")
if lang == "go":
go_pkg = convert_to_go_package(import_from_package)
import_from_package = "%s/%s" % (go_pkg, go_pkg)
if import_from_package not in imports:
imports.append(import_from_package)
if not is_header:
messages += proto_line
if proto_line == "}":
messages += "\n"
for package_import in imports:
if package_import != package:
main_file.write('import public "%s.proto";\n' % package_import)
if len(imports) is not 0:
main_file.write('\n')
main_file.writelines(messages)
def walk_directory(path):
for dir_name in os.listdir(path):
dir_name_path = os.path.join(path, dir_name)
if os.path.isdir(dir_name_path):
package = get_package(dir_name_path)
if package is not None:
if lang == "go":
go_pkg = convert_to_go_package(package)
file_name = "%s/%s" % (go_pkg, go_pkg)
package_mappings.append([go_pkg, (file_name + ".proto")])
else:
file_name = package
package_mappings.append([file_name, (file_name + ".proto")])
package_file_path = os.path.join(tmp_path, file_name + ".proto")
if lang == "go":
package_directory = os.path.dirname(package_file_path)
os.makedirs(package_directory)
with open(package_file_path, 'a') as package_file:
walk_files(package_file, dir_name_path, package)
created_packages.append(package)
walk_directory(dir_name_path)
def compile_directories(path):
for proto_file_name in os.listdir(path):
if lang == "go":
# Compile with the grpc plugin
command_out_path = "plugins=grpc"
# Allow to specify import_prefix for complete vendoring of dependencies
if go_import_prefix:
command_out_path += ",import_prefix=%s" % go_import_prefix
# Map the .proto files to go packages
if len(go_package_mappings) >= 1:
command_out_path += ",%s" % ",".join(go_package_mappings)
# Combine the output with all other output options
command_out_path = "%s:%s" % (command_out_path, os.path.abspath(out_path))
else:
command_out_path = os.path.abspath(out_path)
item_path = os.path.join(path, proto_file_name)
if os.path.isfile(item_path):
command = """{0} --proto_path="{1}" --{2}_out="{3}" "{4}\"""".format(
protoc_path,
tmp_path,
lang,
command_out_path,
item_path
)
call(command, shell=True)
elif os.path.isdir(item_path):
compile_directories(item_path)
walk_directory(proto_path)
# Compile =)
if desc_file:
root_package_file_path = os.path.join(tmp_path, "POGOProtos.proto")
with open(root_package_file_path, 'a') as root_package_file:
walk_files(root_package_file, proto_path, "POGOProtos", created_packages)
command = """{0} --include_imports --proto_path="{1}" --descriptor_set_out="{2}" "{3}\"""".format(
protoc_path,
tmp_path,
os.path.abspath(out_path + "/POGOProtos.desc"),
os.path.join(tmp_path, "POGOProtos.proto")
)
call(command, shell=True)
else:
if lang == "go":
if go_root_package:
mapper = (lambda m: """M{0}={1}""".format(m[1], os.path.join(go_root_package, m[0])))
else:
mapper = (lambda m: """M{0}={1}""".format(m[1], m[0]))
go_package_mappings = map(mapper, package_mappings)
compile_directories(tmp_path)
compile_helper.finish_compile(out_path, lang)
print("Done!")