Joe Onorato | 8815542 | 2012-07-23 13:51:44 -0700 | [diff] [blame] | 1 | #!/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 | |
| 17 | import os |
| 18 | import re |
| 19 | import sys |
| 20 | |
| 21 | def break_lines(key, val): |
| 22 | # these don't get split |
| 23 | if key in ("PRODUCT_MODEL"): |
| 24 | return (key,val) |
| 25 | return (key, "\n".join(val.split())) |
| 26 | |
| 27 | def split_line(line): |
| 28 | words = line.split("=", 1) |
| 29 | if len(words) == 1: |
| 30 | return (words[0], "") |
| 31 | else: |
| 32 | return (words[0], words[1]) |
| 33 | |
| 34 | def sort_lines(text): |
| 35 | lines = text.split() |
| 36 | lines.sort() |
| 37 | return "\n".join(lines) |
| 38 | |
| 39 | def parse_variables(lines): |
| 40 | return [split_line(line) for line in lines if line.strip()] |
| 41 | |
| 42 | def render_variables(variables): |
| 43 | variables = dict(variables) |
| 44 | del variables["FILE"] |
| 45 | variables = list(variables.iteritems()) |
| 46 | variables.sort(lambda a, b: cmp(a[0], b[0])) |
| 47 | return ("<table id='variables'>" |
| 48 | + "\n".join([ "<tr><th>%(key)s</th><td>%(val)s</td></tr>" % { "key": key, "val": val } |
| 49 | for key,val in variables]) |
| 50 | +"</table>") |
| 51 | |
| 52 | def linkify_inherit(variables, text, func_name): |
| 53 | groups = re.split("(\\$\\(call " + func_name + ",.*\\))", text) |
| 54 | result = "" |
| 55 | for i in range(0,len(groups)/2): |
| 56 | i = i * 2 |
| 57 | result = result + groups[i] |
| 58 | s = groups[i+1] |
| 59 | href = s.split(",", 1)[1].strip()[:-1] |
| 60 | href = href.replace("$(SRC_TARGET_DIR)", "build/target") |
| 61 | href = ("../" * variables["FILE"].count("/")) + href + ".html" |
| 62 | result = result + "<a href=\"%s\">%s</a>" % (href,s) |
| 63 | result = result + groups[-1] |
| 64 | return result |
| 65 | |
| 66 | def render_original(variables, text): |
| 67 | text = linkify_inherit(variables, text, "inherit-product") |
| 68 | text = linkify_inherit(variables, text, "inherit-product-if-exists") |
| 69 | return text |
| 70 | |
| 71 | def read_file(fn): |
| 72 | f = file(fn) |
| 73 | text = f.read() |
| 74 | f.close() |
| 75 | return text |
| 76 | |
| 77 | def main(argv): |
| 78 | # read the variables |
| 79 | lines = sys.stdin.readlines() |
| 80 | variables = parse_variables(lines) |
| 81 | |
| 82 | # format the variables |
| 83 | variables = [break_lines(key,val) for key,val in variables] |
| 84 | |
| 85 | # now it's a dict |
| 86 | variables = dict(variables) |
| 87 | |
| 88 | sorted_vars = ( |
| 89 | "PRODUCT_COPY_FILES", |
| 90 | "PRODUCT_PACKAGES", |
| 91 | "PRODUCT_LOCALES", |
| 92 | "PRODUCT_FACTORY_RAMDISK_MODULES", |
| 93 | "PRODUCT_PROPERTY_OVERRIDES", |
| 94 | ) |
| 95 | |
| 96 | for key in sorted_vars: |
| 97 | variables[key] = sort_lines(variables[key]) |
| 98 | |
| 99 | # the original file |
| 100 | original = read_file(variables["FILE"]) |
| 101 | |
| 102 | # formatting |
| 103 | values = dict(variables) |
| 104 | values.update({ |
| 105 | "variables": render_variables(variables), |
| 106 | "original": render_original(variables, original), |
| 107 | }) |
| 108 | print """<html> |
| 109 | |
| 110 | |
| 111 | <head> |
| 112 | <title>%(FILE)s</title> |
| 113 | <style type="text/css"> |
| 114 | body { |
| 115 | font-family: Helvetica, Arial, sans-serif; |
| 116 | padding-bottom: 20px; |
| 117 | } |
| 118 | #variables { |
| 119 | border-collapse: collapse; |
| 120 | } |
| 121 | #variables th, #variables td { |
| 122 | vertical-align: top; |
| 123 | text-align: left; |
| 124 | border-top: 1px solid #c5cdde; |
| 125 | border-bottom: 1px solid #c5cdde; |
| 126 | padding: 2px 10px 2px 10px; |
| 127 | } |
| 128 | #variables th { |
| 129 | font-size: 10pt; |
| 130 | background-color: #e2ecff |
| 131 | } |
| 132 | #variables td { |
| 133 | background-color: #ebf2ff; |
| 134 | white-space: pre; |
| 135 | font-size: 10pt; |
| 136 | } |
| 137 | #original { |
| 138 | background-color: #ebf2ff; |
| 139 | border-top: 1px solid #c5cdde; |
| 140 | border-bottom: 1px solid #c5cdde; |
| 141 | padding: 2px 10px 2px 10px; |
| 142 | white-space: pre; |
| 143 | font-size: 10pt; |
| 144 | } |
| 145 | </style> |
| 146 | </head> |
| 147 | <body> |
| 148 | <h1>%(FILE)s</h1> |
| 149 | <a href="#Original">Original</a> |
| 150 | <a href="#Variables">Variables</a> |
| 151 | <h2><a name="Original"></a>Original</h2> |
| 152 | <div id="original">%(original)s</div> |
| 153 | <h2><a name="Variables"></a>Variables</h2> |
| 154 | %(variables)s |
| 155 | </body> |
| 156 | </html> |
| 157 | """ % values |
| 158 | |
| 159 | if __name__ == "__main__": |
| 160 | main(sys.argv) |