1# Copyright 2015 gRPC authors.
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#     http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14"""Buildgen .proto files list plugin.
15
16This parses the list of targets from the yaml build file, and creates
17a list called "protos" that contains all of the proto file names.
18
19"""
20
21import re
22
23
24def mako_plugin(dictionary):
25    """The exported plugin code for list_protos.
26
27  Some projects generators may want to get the full list of unique .proto files
28  that are being included in a project. This code extracts all files referenced
29  in any library or target that ends in .proto, and builds and exports that as
30  a list called "protos".
31
32  """
33
34    libs = dictionary.get('libs', [])
35    targets = dictionary.get('targets', [])
36
37    proto_re = re.compile('(.*)\\.proto')
38
39    protos = set()
40    for lib in libs:
41        for src in lib.get('src', []):
42            m = proto_re.match(src)
43            if m:
44                protos.add(m.group(1))
45    for tgt in targets:
46        for src in tgt.get('src', []):
47            m = proto_re.match(src)
48            if m:
49                protos.add(m.group(1))
50
51    protos = sorted(protos)
52
53    dictionary['protos'] = protos
54