Craig Tiller | 1b4e330 | 2015-12-17 16:35:00 -0800 | [diff] [blame] | 1 | #!/usr/bin/env python2.7 |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 2 | # Copyright 2015 gRPC authors. |
Craig Tiller | 1b4e330 | 2015-12-17 16:35:00 -0800 | [diff] [blame] | 3 | # |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 4 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | # you may not use this file except in compliance with the License. |
| 6 | # You may obtain a copy of the License at |
Craig Tiller | 1b4e330 | 2015-12-17 16:35:00 -0800 | [diff] [blame] | 7 | # |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
Craig Tiller | 1b4e330 | 2015-12-17 16:35:00 -0800 | [diff] [blame] | 9 | # |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | # See the License for the specific language governing permissions and |
| 14 | # limitations under the License. |
Craig Tiller | 1b4e330 | 2015-12-17 16:35:00 -0800 | [diff] [blame] | 15 | |
| 16 | |
| 17 | """Generates the appropriate build.json data for all the proto files.""" |
| 18 | import yaml |
| 19 | import collections |
| 20 | import os |
| 21 | import re |
| 22 | import sys |
| 23 | |
murgatroid99 | 0d4b13c | 2016-04-05 09:35:41 -0700 | [diff] [blame] | 24 | def update_deps(key, proto_filename, deps, deps_external, is_trans, visited): |
Sree Kuchibhotla | c819e50 | 2016-01-14 11:16:10 -0800 | [diff] [blame] | 25 | if not proto_filename in visited: |
| 26 | visited.append(proto_filename) |
| 27 | with open(proto_filename) as inp: |
| 28 | for line in inp: |
| 29 | imp = re.search(r'import "([^"]*)"', line) |
| 30 | if not imp: continue |
| 31 | imp_proto = imp.group(1) |
murgatroid99 | 0d4b13c | 2016-04-05 09:35:41 -0700 | [diff] [blame] | 32 | # This indicates an external dependency, which we should handle |
| 33 | # differently and not traverse recursively |
| 34 | if imp_proto.startswith('google/'): |
| 35 | if key not in deps_external: |
| 36 | deps_external[key] = [] |
| 37 | deps_external[key].append(imp_proto[:-6]) |
| 38 | continue |
Sree Kuchibhotla | c819e50 | 2016-01-14 11:16:10 -0800 | [diff] [blame] | 39 | if key not in deps: deps[key] = [] |
| 40 | deps[key].append(imp_proto[:-6]) |
| 41 | if is_trans: |
murgatroid99 | 0d4b13c | 2016-04-05 09:35:41 -0700 | [diff] [blame] | 42 | update_deps(key, imp_proto, deps, deps_external, is_trans, visited) |
Sree Kuchibhotla | c819e50 | 2016-01-14 11:16:10 -0800 | [diff] [blame] | 43 | |
Craig Tiller | 1b4e330 | 2015-12-17 16:35:00 -0800 | [diff] [blame] | 44 | def main(): |
Sree Kuchibhotla | c819e50 | 2016-01-14 11:16:10 -0800 | [diff] [blame] | 45 | proto_dir = os.path.abspath(os.path.dirname(sys.argv[0])) |
| 46 | os.chdir(os.path.join(proto_dir, '../..')) |
| 47 | |
Craig Tiller | 1b4e330 | 2015-12-17 16:35:00 -0800 | [diff] [blame] | 48 | deps = {} |
Sree Kuchibhotla | c819e50 | 2016-01-14 11:16:10 -0800 | [diff] [blame] | 49 | deps_trans = {} |
murgatroid99 | 0d4b13c | 2016-04-05 09:35:41 -0700 | [diff] [blame] | 50 | deps_external = {} |
| 51 | deps_external_trans = {} |
Sree Kuchibhotla | 80b6a47 | 2016-01-14 11:26:54 -0800 | [diff] [blame] | 52 | for root, dirs, files in os.walk('src/proto'): |
Craig Tiller | 1b4e330 | 2015-12-17 16:35:00 -0800 | [diff] [blame] | 53 | for f in files: |
| 54 | if f[-6:] != '.proto': continue |
| 55 | look_at = os.path.join(root, f) |
Sree Kuchibhotla | c819e50 | 2016-01-14 11:16:10 -0800 | [diff] [blame] | 56 | deps_for = look_at[:-6] |
murgatroid99 | 0d4b13c | 2016-04-05 09:35:41 -0700 | [diff] [blame] | 57 | # First level deps |
| 58 | update_deps(deps_for, look_at, deps, deps_external, False, []) |
| 59 | # Transitive deps |
| 60 | update_deps(deps_for, look_at, deps_trans, deps_external_trans, True, []) |
Craig Tiller | 1b4e330 | 2015-12-17 16:35:00 -0800 | [diff] [blame] | 61 | |
| 62 | json = { |
Sree Kuchibhotla | c819e50 | 2016-01-14 11:16:10 -0800 | [diff] [blame] | 63 | 'proto_deps': deps, |
murgatroid99 | 0d4b13c | 2016-04-05 09:35:41 -0700 | [diff] [blame] | 64 | 'proto_transitive_deps': deps_trans, |
| 65 | 'proto_external_deps': deps_external, |
| 66 | 'proto_transitive_external_deps': deps_external_trans |
Craig Tiller | 1b4e330 | 2015-12-17 16:35:00 -0800 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | print yaml.dump(json) |
| 70 | |
| 71 | if __name__ == '__main__': |
| 72 | main() |