blob: 891587f0c6823a008a01dcd0f271b59cb7046dde [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
Joe Onorato88155422012-07-23 13:51:44 -070019import os
20import re
21import sys
22
Anthony King74dcc712015-10-31 15:06:38 -040023
24def iteritems(obj):
25 if hasattr('iteritems'):
26 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
32 if key in ("PRODUCT_MODEL"):
33 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 King74dcc712015-10-31 15:06:38 -040054 variables = list(variables.items())
Joe Onorato88155422012-07-23 13:51:44 -070055 variables.sort(lambda a, b: cmp(a[0], b[0]))
56 return ("<table id='variables'>"
57 + "\n".join([ "<tr><th>%(key)s</th><td>%(val)s</td></tr>" % { "key": key, "val": val }
58 for key,val in variables])
59 +"</table>")
60
61def linkify_inherit(variables, text, func_name):
62 groups = re.split("(\\$\\(call " + func_name + ",.*\\))", text)
63 result = ""
64 for i in range(0,len(groups)/2):
65 i = i * 2
66 result = result + groups[i]
67 s = groups[i+1]
68 href = s.split(",", 1)[1].strip()[:-1]
69 href = href.replace("$(SRC_TARGET_DIR)", "build/target")
70 href = ("../" * variables["FILE"].count("/")) + href + ".html"
71 result = result + "<a href=\"%s\">%s</a>" % (href,s)
72 result = result + groups[-1]
73 return result
74
75def render_original(variables, text):
76 text = linkify_inherit(variables, text, "inherit-product")
77 text = linkify_inherit(variables, text, "inherit-product-if-exists")
78 return text
79
80def read_file(fn):
81 f = file(fn)
82 text = f.read()
83 f.close()
84 return text
85
86def main(argv):
87 # read the variables
88 lines = sys.stdin.readlines()
89 variables = parse_variables(lines)
90
91 # format the variables
92 variables = [break_lines(key,val) for key,val in variables]
93
94 # now it's a dict
95 variables = dict(variables)
96
97 sorted_vars = (
98 "PRODUCT_COPY_FILES",
99 "PRODUCT_PACKAGES",
100 "PRODUCT_LOCALES",
Joe Onorato88155422012-07-23 13:51:44 -0700101 "PRODUCT_PROPERTY_OVERRIDES",
102 )
103
104 for key in sorted_vars:
105 variables[key] = sort_lines(variables[key])
106
107 # the original file
108 original = read_file(variables["FILE"])
109
110 # formatting
111 values = dict(variables)
112 values.update({
113 "variables": render_variables(variables),
114 "original": render_original(variables, original),
115 })
Anthony King74dcc712015-10-31 15:06:38 -0400116 print("""<html>
Joe Onorato88155422012-07-23 13:51:44 -0700117
118
119<head>
120 <title>%(FILE)s</title>
121 <style type="text/css">
122 body {
123 font-family: Helvetica, Arial, sans-serif;
124 padding-bottom: 20px;
125 }
126 #variables {
127 border-collapse: collapse;
128 }
129 #variables th, #variables td {
130 vertical-align: top;
131 text-align: left;
132 border-top: 1px solid #c5cdde;
133 border-bottom: 1px solid #c5cdde;
134 padding: 2px 10px 2px 10px;
135 }
136 #variables th {
137 font-size: 10pt;
138 background-color: #e2ecff
139 }
140 #variables td {
141 background-color: #ebf2ff;
142 white-space: pre;
143 font-size: 10pt;
144 }
145 #original {
146 background-color: #ebf2ff;
147 border-top: 1px solid #c5cdde;
148 border-bottom: 1px solid #c5cdde;
149 padding: 2px 10px 2px 10px;
150 white-space: pre;
151 font-size: 10pt;
152 }
153 </style>
154</head>
155<body>
156<h1>%(FILE)s</h1>
157<a href="#Original">Original</a>
158<a href="#Variables">Variables</a>
159<h2><a name="Original"></a>Original</h2>
160<div id="original">%(original)s</div>
161<h2><a name="Variables"></a>Variables</h2>
162%(variables)s
163</body>
164</html>
Anthony King74dcc712015-10-31 15:06:38 -0400165""" % values)
Joe Onorato88155422012-07-23 13:51:44 -0700166
167if __name__ == "__main__":
168 main(sys.argv)