• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python
2# Copyright (c) 2012 The Chromium Authors. All rights reserved.
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
6"""Generate java source files from protobufs
7
8Usage:
9    protoc_java.py {protoc} {proto_path} {java_out} {stamp_file} {proto_files}
10
11This is a helper file for the genproto_java action in protoc_java.gypi.
12
13It performs the following steps:
141. Deletes all old sources (ensures deleted classes are not part of new jars).
152. Creates source directory.
163. Generates Java files using protoc.
174. Creates a new stamp file.
18"""
19
20import os
21import shutil
22import subprocess
23import sys
24
25def main(argv):
26  if len(argv) < 5:
27    usage()
28    return 1
29
30  protoc_path, proto_path, java_out, stamp_file = argv[1:5]
31  proto_files = argv[5:]
32
33  # Delete all old sources
34  if os.path.exists(java_out):
35    shutil.rmtree(java_out)
36
37  # Create source directory
38  os.makedirs(java_out)
39
40  # Generate Java files using protoc
41  ret = subprocess.call(
42      [protoc_path, '--proto_path', proto_path, '--java_out', java_out]
43      + proto_files)
44
45  if ret == 0:
46    # Create a new stamp file
47    with file(stamp_file, 'a'):
48      os.utime(stamp_file, None)
49
50  return ret
51
52def usage():
53  print(__doc__);
54
55if __name__ == '__main__':
56  sys.exit(main(sys.argv))
57