1#!/usr/bin/python
2
3#
4# Copyright (C) 2015 The Android Open Source Project
5#
6# Licensed under the Apache License, Version 2.0 (the "License");
7# you may not use this file except in compliance with the License.
8# You may obtain a copy of the License at
9#
10#      http://www.apache.org/licenses/LICENSE-2.0
11#
12# Unless required by applicable law or agreed to in writing, software
13# distributed under the License is distributed on an "AS IS" BASIS,
14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15# See the License for the specific language governing permissions and
16# limitations under the License.
17#
18
19"""Tool to generate vnames.json file for kythe from Android repo
20manifest. Needs to be run from repo root."""
21
22import xml.etree.ElementTree
23import json
24
25doc = xml.etree.ElementTree.parse('.repo/manifests/default.xml')
26manifest = doc.getroot()
27# fallback patterns to be added to tail of vnames.json
28tail = json.loads("""[
29  {
30    "pattern": "bazel-out/[^/]+/(.*)",
31    "vname": {
32      "corpus": "googleplex-android",
33      "root": "GENERATED/studio/bazel",
34      "path": "@1@"
35    }
36  },
37  {
38    "pattern": "(.*)",
39    "vname": {
40      "corpus": "googleplex-android",
41      "path": "@1@"
42    }
43  }
44]
45""")
46
47vnames = []
48# manifest xml contains <project path="" name=""> tags
49# we convert these into vname patterns that kythe understands.
50for project in manifest.findall('project'):
51    node = {}
52    node['pattern'] = "%s/(.*)" % project.get('path')
53    vname = {}
54    vname['corpus'] = 'googleplex-android'
55    vname['root'] = project.get('name')
56    vname['path'] = '@1@'
57    node['vname'] = vname
58    vnames.append(node);
59
60# add fallback vname patterns to end of json list.
61vnames.extend(tail)
62
63# print the json vnames list to stdout
64# can be used for debugging or pipe to vnames.json.
65print(json.dumps(vnames, indent=2, separators=(',', ':')))
66