forked from AeonLucid/POGOProtos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
compile.py
executable file
·89 lines (69 loc) · 2.54 KB
/
compile.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
#!/usr/bin/env python
import argparse
import fnmatch
import ntpath
import sys
import os
import shutil
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")
args = parser.parse_args()
# Set defaults
lang = args.lang or "csharp"
out_path = args.out_path or "out"
default_out_path = out_path == "out"
# Determine where to store
proj_root = os.path.abspath("../")
proto_path = os.path.abspath("src/")
out_path = os.path.abspath(out_path)
tmp_out_path = out_path
# Output dir is actually different csharp because we modify it before compiling.
if lang == "csharp":
tmp_out_path = os.path.join(tmp_out_path, "POGOProtos")
if not default_out_path:
print 'Can we remove "%s"?' % tmp_out_path
may_remove = compile_helper.query_yes_no("Please answer.", default="no")
else:
may_remove = True
if may_remove and os.path.exists(tmp_out_path):
shutil.rmtree(tmp_out_path)
# If any of the protoc builds fail, we continue on but exit with a non-0 exit
# code so that any automation (such as CI testing) can be made aware of the
# failure.
exit_codes = []
# Find protofiles and compile
for root, dirnames, filenames in os.walk(proto_path):
protos = fnmatch.filter(filenames, '*.proto')
relative_out_path = None
for filename in protos:
relative_out_path = None
proto_file = os.path.join(root, filename)
relative_file_path = proto_file.replace(proto_path, "")
relative_path = relative_file_path.replace(ntpath.basename(proto_file), "")
if lang == "csharp":
destination_path = os.path.abspath(out_path + relative_path)
else:
destination_path = os.path.abspath(out_path)
if relative_out_path is None:
relative_out_path = os.path.abspath(out_path + relative_path)
if not os.path.exists(destination_path):
os.makedirs(destination_path)
print("Compiling " + relative_file_path + "..")
command = """{0} --proto_path="{1}" --{2}_out="{3}" "{4}\"""".format(
protoc_path,
proto_path,
lang,
destination_path,
os.path.abspath(proto_file)
)
exit_codes.append(call(command, shell=True))
compile_helper.finish_compile(out_path, lang)
print("Done!")
if any(c != 0 for c in exit_codes):
sys.exit(1)