Joe Onorato | 9197a48 | 2011-06-08 16:04:14 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # |
| 3 | # Copyright (C) 2009 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 sys |
| 18 | |
Jeff Sharkey | 26d22f7 | 2014-03-18 17:20:10 -0700 | [diff] [blame] | 19 | # Usage: post_process_props.py file.prop [blacklist_key, ...] |
| 20 | # Blacklisted keys are removed from the property file, if present |
| 21 | |
Ying Wang | 3512321 | 2014-02-11 20:44:09 -0800 | [diff] [blame] | 22 | # See PROP_VALUE_MAX system_properties.h. |
| 23 | # PROP_VALUE_MAX in system_properties.h includes the termination NUL, |
| 24 | # so we decrease it by 1 here. |
| 25 | PROP_VALUE_MAX = 91 |
| 26 | |
Joe Onorato | 9197a48 | 2011-06-08 16:04:14 -0700 | [diff] [blame] | 27 | # Put the modifications that you need to make into the /system/build.prop into this |
| 28 | # function. The prop object has get(name) and put(name,value) methods. |
| 29 | def mangle_build_prop(prop): |
Ying Wang | 3512321 | 2014-02-11 20:44:09 -0800 | [diff] [blame] | 30 | pass |
Joe Onorato | 9197a48 | 2011-06-08 16:04:14 -0700 | [diff] [blame] | 31 | |
Ying Wang | 3512321 | 2014-02-11 20:44:09 -0800 | [diff] [blame] | 32 | # Put the modifications that you need to make into the /default.prop into this |
Joe Onorato | 9197a48 | 2011-06-08 16:04:14 -0700 | [diff] [blame] | 33 | # function. The prop object has get(name) and put(name,value) methods. |
| 34 | def mangle_default_prop(prop): |
| 35 | # If ro.debuggable is 1, then enable adb on USB by default |
| 36 | # (this is for userdebug builds) |
| 37 | if prop.get("ro.debuggable") == "1": |
| 38 | val = prop.get("persist.sys.usb.config") |
| 39 | if val == "": |
| 40 | val = "adb" |
| 41 | else: |
| 42 | val = val + ",adb" |
| 43 | prop.put("persist.sys.usb.config", val) |
Joe Onorato | 8ad4bb1 | 2012-05-02 14:36:57 -0700 | [diff] [blame] | 44 | # UsbDeviceManager expects a value here. If it doesn't get it, it will |
| 45 | # default to "adb". That might not the right policy there, but it's better |
| 46 | # to be explicit. |
| 47 | if not prop.get("persist.sys.usb.config"): |
| 48 | prop.put("persist.sys.usb.config", "none"); |
Joe Onorato | 9197a48 | 2011-06-08 16:04:14 -0700 | [diff] [blame] | 49 | |
Ying Wang | 3512321 | 2014-02-11 20:44:09 -0800 | [diff] [blame] | 50 | def validate(prop): |
| 51 | """Validate the properties. |
| 52 | |
| 53 | Returns: |
| 54 | True if nothing is wrong. |
| 55 | """ |
| 56 | check_pass = True |
| 57 | buildprops = prop.to_dict() |
| 58 | dev_build = buildprops.get("ro.build.version.incremental", |
| 59 | "").startswith("eng") |
| 60 | for key, value in buildprops.iteritems(): |
| 61 | # Check build properties' length. |
| 62 | if len(value) > PROP_VALUE_MAX: |
| 63 | # If dev build, show a warning message, otherwise fail the |
| 64 | # build with error message |
| 65 | if dev_build: |
| 66 | sys.stderr.write("warning: %s exceeds %d bytes: " % |
| 67 | (key, PROP_VALUE_MAX)) |
| 68 | sys.stderr.write("%s (%d)\n" % (value, len(value))) |
| 69 | sys.stderr.write("warning: This will cause the %s " % key) |
| 70 | sys.stderr.write("property return as empty at runtime\n") |
| 71 | else: |
| 72 | check_pass = False |
| 73 | sys.stderr.write("error: %s cannot exceed %d bytes: " % |
| 74 | (key, PROP_VALUE_MAX)) |
| 75 | sys.stderr.write("%s (%d)\n" % (value, len(value))) |
| 76 | return check_pass |
| 77 | |
Joe Onorato | 9197a48 | 2011-06-08 16:04:14 -0700 | [diff] [blame] | 78 | class PropFile: |
Yu Liu | 115c66b | 2014-02-10 19:20:36 -0800 | [diff] [blame] | 79 | |
Joe Onorato | 9197a48 | 2011-06-08 16:04:14 -0700 | [diff] [blame] | 80 | def __init__(self, lines): |
Ying Wang | 3512321 | 2014-02-11 20:44:09 -0800 | [diff] [blame] | 81 | self.lines = [s.strip() for s in lines] |
| 82 | |
| 83 | def to_dict(self): |
| 84 | props = {} |
Yu Liu | 115c66b | 2014-02-10 19:20:36 -0800 | [diff] [blame] | 85 | for line in self.lines: |
Ying Wang | 3512321 | 2014-02-11 20:44:09 -0800 | [diff] [blame] | 86 | if not line or line.startswith("#"): |
Yu Liu | 115c66b | 2014-02-10 19:20:36 -0800 | [diff] [blame] | 87 | continue |
Ying Wang | 5a7ad03 | 2014-04-15 11:36:46 -0700 | [diff] [blame] | 88 | if "=" in line: |
| 89 | key, value = line.split("=", 1) |
| 90 | props[key] = value |
Ying Wang | 3512321 | 2014-02-11 20:44:09 -0800 | [diff] [blame] | 91 | return props |
Joe Onorato | 9197a48 | 2011-06-08 16:04:14 -0700 | [diff] [blame] | 92 | |
| 93 | def get(self, name): |
| 94 | key = name + "=" |
| 95 | for line in self.lines: |
| 96 | if line.startswith(key): |
| 97 | return line[len(key):] |
| 98 | return "" |
| 99 | |
| 100 | def put(self, name, value): |
| 101 | key = name + "=" |
| 102 | for i in range(0,len(self.lines)): |
| 103 | if self.lines[i].startswith(key): |
| 104 | self.lines[i] = key + value |
| 105 | return |
| 106 | self.lines.append(key + value) |
| 107 | |
Jeff Sharkey | 26d22f7 | 2014-03-18 17:20:10 -0700 | [diff] [blame] | 108 | def delete(self, name): |
| 109 | key = name + "=" |
| 110 | self.lines = [ line for line in self.lines if not line.startswith(key) ] |
| 111 | |
Joe Onorato | 9197a48 | 2011-06-08 16:04:14 -0700 | [diff] [blame] | 112 | def write(self, f): |
| 113 | f.write("\n".join(self.lines)) |
| 114 | f.write("\n") |
| 115 | |
| 116 | def main(argv): |
| 117 | filename = argv[1] |
| 118 | f = open(filename) |
| 119 | lines = f.readlines() |
| 120 | f.close() |
| 121 | |
| 122 | properties = PropFile(lines) |
Ying Wang | 3512321 | 2014-02-11 20:44:09 -0800 | [diff] [blame] | 123 | |
Joe Onorato | 9197a48 | 2011-06-08 16:04:14 -0700 | [diff] [blame] | 124 | if filename.endswith("/build.prop"): |
| 125 | mangle_build_prop(properties) |
| 126 | elif filename.endswith("/default.prop"): |
| 127 | mangle_default_prop(properties) |
| 128 | else: |
| 129 | sys.stderr.write("bad command line: " + str(argv) + "\n") |
| 130 | sys.exit(1) |
| 131 | |
Ying Wang | 3512321 | 2014-02-11 20:44:09 -0800 | [diff] [blame] | 132 | if not validate(properties): |
| 133 | sys.exit(1) |
| 134 | |
Jeff Sharkey | 26d22f7 | 2014-03-18 17:20:10 -0700 | [diff] [blame] | 135 | # Drop any blacklisted keys |
| 136 | for key in argv[2:]: |
| 137 | properties.delete(key) |
| 138 | |
Mike Lockwood | 5b65ee4 | 2011-06-08 19:06:43 -0700 | [diff] [blame] | 139 | f = open(filename, 'w+') |
| 140 | properties.write(f) |
Joe Onorato | 9197a48 | 2011-06-08 16:04:14 -0700 | [diff] [blame] | 141 | f.close() |
| 142 | |
| 143 | if __name__ == "__main__": |
| 144 | main(sys.argv) |