blob: 1433a9ac2895af0d51de85fa559bf79b6fcca9fa [file] [log] [blame]
Joe Onorato88155422012-07-23 13:51:44 -07001#!/usr/bin/env python
2#
3# Copyright (C) 2012 The Android Open Source Project
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
Anthony King74dcc712015-10-31 15:06:38 -040017from __future__ import print_function
18
Anthony Kingc713d762015-11-03 00:23:11 +000019from operator import itemgetter
Joe Onorato88155422012-07-23 13:51:44 -070020import re
21import sys
22
Anthony King74dcc712015-10-31 15:06:38 -040023
24def iteritems(obj):
Anthony Kingc713d762015-11-03 00:23:11 +000025 if hasattr(obj, 'iteritems'):
Anthony King74dcc712015-10-31 15:06:38 -040026 return obj.iteritems()
27 return obj.items()
28
29
Joe Onorato88155422012-07-23 13:51:44 -070030def break_lines(key, val):
31 # these don't get split
Anthony Kingc713d762015-11-03 00:23:11 +000032 if key in ("PRODUCT_MODEL",):
Joe Onorato88155422012-07-23 13:51:44 -070033 return (key,val)
34 return (key, "\n".join(val.split()))
35
36def split_line(line):
37 words = line.split("=", 1)
38 if len(words) == 1:
39 return (words[0], "")
40 else:
41 return (words[0], words[1])
42
43def sort_lines(text):
44 lines = text.split()
45 lines.sort()
46 return "\n".join(lines)
47
48def parse_variables(lines):
49 return [split_line(line) for line in lines if line.strip()]
50
51def render_variables(variables):
52 variables = dict(variables)
53 del variables["FILE"]
Anthony Kingc713d762015-11-03 00:23:11 +000054 variables = sorted(variables.items(), key=itemgetter(0))
Joe Onorato88155422012-07-23 13:51:44 -070055 return ("<table id='variables'>"
56 + "\n".join([ "<tr><th>%(key)s</th><td>%(val)s</td></tr>" % { "key": key, "val": val }
57 for key,val in variables])
58 +"</table>")
59
60def linkify_inherit(variables, text, func_name):
61 groups = re.split("(\\$\\(call " + func_name + ",.*\\))", text)
62 result = ""
63 for i in range(0,len(groups)/2):
64 i = i * 2
65 result = result + groups[i]
66 s = groups[i+1]
67 href = s.split(",", 1)[1].strip()[:-1]
68 href = href.replace("$(SRC_TARGET_DIR)", "build/target")
69 href = ("../" * variables["FILE"].count("/")) + href + ".html"
70 result = result + "<a href=\"%s\">%s</a>" % (href,s)
71 result = result + groups[-1]
72 return result
73
74def render_original(variables, text):
75 text = linkify_inherit(variables, text, "inherit-product")
76 text = linkify_inherit(variables, text, "inherit-product-if-exists")
77 return text
78
79def read_file(fn):
Anthony Kingc713d762015-11-03 00:23:11 +000080 f = open(fn)
Joe Onorato88155422012-07-23 13:51:44 -070081 text = f.read()
82 f.close()
83 return text
84
85def main(argv):
86 # read the variables
87 lines = sys.stdin.readlines()
88 variables = parse_variables(lines)
89
90 # format the variables
91 variables = [break_lines(key,val) for key,val in variables]
92
93 # now it's a dict
94 variables = dict(variables)
95
96 sorted_vars = (
97 "PRODUCT_COPY_FILES",
98 "PRODUCT_PACKAGES",
99 "PRODUCT_LOCALES",
Joe Onorato88155422012-07-23 13:51:44 -0700100 "PRODUCT_PROPERTY_OVERRIDES",
101 )
102
103 for key in sorted_vars:
104 variables[key] = sort_lines(variables[key])
105
106 # the original file
107 original = read_file(variables["FILE"])
108
109 # formatting
110 values = dict(variables)
111 values.update({
112 "variables": render_variables(variables),
113 "original": render_original(variables, original),
114 })
Anthony King74dcc712015-10-31 15:06:38 -0400115 print("""<html>
Joe Onorato88155422012-07-23 13:51:44 -0700116
117
118<head>
119 <title>%(FILE)s</title>
120 <style type="text/css">
121 body {
122 font-family: Helvetica, Arial, sans-serif;
123 padding-bottom: 20px;
124 }
125 #variables {
126 border-collapse: collapse;
127 }
128 #variables th, #variables td {
129 vertical-align: top;
130 text-align: left;
131 border-top: 1px solid #c5cdde;
132 border-bottom: 1px solid #c5cdde;
133 padding: 2px 10px 2px 10px;
134 }
135 #variables th {
136 font-size: 10pt;
137 background-color: #e2ecff
138 }
139 #variables td {
140 background-color: #ebf2ff;
141 white-space: pre;
142 font-size: 10pt;
143 }
144 #original {
145 background-color: #ebf2ff;
146 border-top: 1px solid #c5cdde;
147 border-bottom: 1px solid #c5cdde;
148 padding: 2px 10px 2px 10px;
149 white-space: pre;
150 font-size: 10pt;
151 }
152 </style>
153</head>
154<body>
155<h1>%(FILE)s</h1>
156<a href="#Original">Original</a>
157<a href="#Variables">Variables</a>
158<h2><a name="Original"></a>Original</h2>
159<div id="original">%(original)s</div>
160<h2><a name="Variables"></a>Variables</h2>
161%(variables)s
162</body>
163</html>
Anthony King74dcc712015-10-31 15:06:38 -0400164""" % values)
Joe Onorato88155422012-07-23 13:51:44 -0700165
166if __name__ == "__main__":
167 main(sys.argv)