forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
protoc_java.py
executable file
·68 lines (52 loc) · 1.64 KB
/
protoc_java.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
#!/usr/bin/env python
# Copyright (c) 2012 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
"""Generate java source files from protobuf files.
Usage:
protoc_java.py {protoc} {proto_path} {java_out} {proto_runtime} \
{stamp_file} {proto_files}
This is a helper file for the genproto_java action in protoc_java.gypi.
It performs the following steps:
1. Deletes all old sources (ensures deleted classes are not part of new jars).
2. Creates source directory.
3. Generates Java files using protoc.
4. Creates a new stamp file.
proto_runtime must be one of 'nano' and 'lite'.
"""
import os
import shutil
import subprocess
import sys
def main(argv):
if len(argv) < 6:
usage()
return 1
protoc_path, proto_path, java_out, proto_runtime, stamp_file = argv[1:6]
proto_files = argv[6:]
# Delete all old sources.
if os.path.exists(java_out):
shutil.rmtree(java_out)
# Create source directory.
os.makedirs(java_out)
# Figure out which runtime to use.
if proto_runtime == 'nano':
out_arg = '--javanano_out=optional_field_style=reftypes,' + \
'store_unknown_fields=true:' + java_out
elif proto_runtime == 'lite':
out_arg = '--java_out=' + java_out
else:
usage()
return 1
# Generate Java files using protoc.
ret = subprocess.call(
[protoc_path, '--proto_path', proto_path, out_arg] + proto_files)
if ret == 0:
# Create a new stamp file.
with file(stamp_file, 'a'):
os.utime(stamp_file, None)
return ret
def usage():
print(__doc__);
if __name__ == '__main__':
sys.exit(main(sys.argv))