forked from google/grr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
makefile.py
106 lines (86 loc) · 3.25 KB
/
makefile.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
#!/usr/bin/env python
"""A script to prepare the source tree for building."""
# This script must have no special requirements because it wont be able to
# import any GRR stuff until the protos are built.
import logging
import os
import subprocess
from grr.lib import flags
parser = flags.PARSER
parser.add_argument(
"--clean",
action="store_true",
default=False,
help="Clean compiled protos.")
parser.add_argument(
"--python_out",
default=os.path.dirname(os.path.abspath(__file__)),
help="Where to put compiled protos. Default: next to source files.")
args = parser.parse_args()
def Clean():
"""Clean out compiled protos."""
# Find all the compiled proto files and unlink them.
for (root, _, files) in os.walk(args.python_out):
for filename in files:
full_filename = os.path.join(root, filename)
if full_filename.endswith("_pb2.py") or full_filename.endswith(
"_pb2.pyc"):
os.unlink(full_filename)
def MakeProto(python_out):
"""Make sure our protos have been compiled to python libraries."""
# Start running from one directory above the grr directory which is found by
# this scripts's location as __file__.
cwd = os.path.dirname(os.path.abspath(__file__))
# Find all the .proto files.
protos_to_compile = []
for (root, _, files) in os.walk(cwd):
for filename in files:
full_filename = os.path.join(root, filename)
if full_filename.endswith(".proto"):
proto_stat = os.stat(full_filename)
compiled_name = full_filename.rsplit(".", 1)[0] + "_pb2.py"
pb2_path = os.path.join(args.python_out,
os.path.relpath(compiled_name, cwd))
try:
pb2_stat = os.stat(pb2_path)
if pb2_stat.st_mtime >= proto_stat.st_mtime:
continue
except (OSError, IOError):
pass
protos_to_compile.append(full_filename)
if protos_to_compile:
# Find the protoc compiler.
protoc = os.environ.get("PROTOC", "protoc")
try:
output = subprocess.check_output([protoc, "--version"])
except (IOError, OSError):
raise RuntimeError("Unable to launch %s protoc compiler. Please "
"set the PROTOC environment variable.", protoc)
if "3.3.0" not in output:
raise RuntimeError("Incompatible protoc compiler detected. "
"We need 3.3.0 not %s" % output)
for proto in protos_to_compile:
logging.info("Compiling %s", proto)
# The protoc compiler is too dumb to deal with full paths - it expects a
# relative path from the current working directory.
subprocess.check_call(
[
protoc,
# Write the python files next to the .proto files.
"--python_out",
python_out,
# Standard include paths.
# We just bring google/proto/descriptor.proto with us to make it
# easier to install.
"--proto_path=.",
"--proto_path=grr",
"--proto_path=grr/proto",
os.path.relpath(proto, cwd)
],
cwd=cwd)
if __name__ == "__main__":
root_logger = logging.getLogger()
root_logger.setLevel(logging.INFO)
if args.clean:
Clean()
MakeProto(args.python_out)