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 | |
Elliott Hughes | 05c1a2a | 2017-02-28 10:04:23 -0800 | [diff] [blame] | 22 | # See PROP_VALUE_MAX in system_properties.h. |
| 23 | # The constant in system_properties.h includes the terminating NUL, |
| 24 | # so we decrease the value by 1 here. |
Ying Wang | 3512321 | 2014-02-11 20:44:09 -0800 | [diff] [blame] | 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 | |
Jaekyun Seok | 8bee120 | 2017-01-31 20:07:02 +0900 | [diff] [blame] | 32 | # Put the modifications that you need to make into /vendor/default.prop and |
| 33 | # /odm/default.prop into this function. The prop object has get(name) and |
| 34 | # put(name,value) methods. |
| 35 | def mangle_default_prop_override(prop): |
| 36 | pass |
| 37 | |
Hung-ying Tyan | f829b40 | 2017-05-01 21:56:26 +0800 | [diff] [blame^] | 38 | # Put the modifications that you need to make into the /system/etc/prop.default into this |
Joe Onorato | 9197a48 | 2011-06-08 16:04:14 -0700 | [diff] [blame] | 39 | # function. The prop object has get(name) and put(name,value) methods. |
| 40 | def mangle_default_prop(prop): |
Jerry Zhang | 1695653 | 2016-10-18 00:01:27 +0000 | [diff] [blame] | 41 | # If ro.debuggable is 1, then enable adb on USB by default |
| 42 | # (this is for userdebug builds) |
| 43 | if prop.get("ro.debuggable") == "1": |
| 44 | val = prop.get("persist.sys.usb.config") |
| 45 | if "adb" not in val: |
| 46 | if val == "": |
| 47 | val = "adb" |
| 48 | else: |
| 49 | val = val + ",adb" |
| 50 | prop.put("persist.sys.usb.config", val) |
Joe Onorato | 8ad4bb1 | 2012-05-02 14:36:57 -0700 | [diff] [blame] | 51 | # UsbDeviceManager expects a value here. If it doesn't get it, it will |
| 52 | # default to "adb". That might not the right policy there, but it's better |
| 53 | # to be explicit. |
| 54 | if not prop.get("persist.sys.usb.config"): |
| 55 | prop.put("persist.sys.usb.config", "none"); |
Joe Onorato | 9197a48 | 2011-06-08 16:04:14 -0700 | [diff] [blame] | 56 | |
Ying Wang | 3512321 | 2014-02-11 20:44:09 -0800 | [diff] [blame] | 57 | def validate(prop): |
| 58 | """Validate the properties. |
| 59 | |
| 60 | Returns: |
| 61 | True if nothing is wrong. |
| 62 | """ |
| 63 | check_pass = True |
| 64 | buildprops = prop.to_dict() |
Ying Wang | 3512321 | 2014-02-11 20:44:09 -0800 | [diff] [blame] | 65 | for key, value in buildprops.iteritems(): |
| 66 | # Check build properties' length. |
| 67 | if len(value) > PROP_VALUE_MAX: |
Ying Wang | 38df101 | 2015-02-04 15:10:59 -0800 | [diff] [blame] | 68 | check_pass = False |
| 69 | sys.stderr.write("error: %s cannot exceed %d bytes: " % |
| 70 | (key, PROP_VALUE_MAX)) |
| 71 | sys.stderr.write("%s (%d)\n" % (value, len(value))) |
Ying Wang | 3512321 | 2014-02-11 20:44:09 -0800 | [diff] [blame] | 72 | return check_pass |
| 73 | |
Joe Onorato | 9197a48 | 2011-06-08 16:04:14 -0700 | [diff] [blame] | 74 | class PropFile: |
Yu Liu | 115c66b | 2014-02-10 19:20:36 -0800 | [diff] [blame] | 75 | |
Joe Onorato | 9197a48 | 2011-06-08 16:04:14 -0700 | [diff] [blame] | 76 | def __init__(self, lines): |
Ying Wang | 3512321 | 2014-02-11 20:44:09 -0800 | [diff] [blame] | 77 | self.lines = [s.strip() for s in lines] |
| 78 | |
| 79 | def to_dict(self): |
| 80 | props = {} |
Yu Liu | 115c66b | 2014-02-10 19:20:36 -0800 | [diff] [blame] | 81 | for line in self.lines: |
Ying Wang | 3512321 | 2014-02-11 20:44:09 -0800 | [diff] [blame] | 82 | if not line or line.startswith("#"): |
Yu Liu | 115c66b | 2014-02-10 19:20:36 -0800 | [diff] [blame] | 83 | continue |
Ying Wang | 5a7ad03 | 2014-04-15 11:36:46 -0700 | [diff] [blame] | 84 | if "=" in line: |
| 85 | key, value = line.split("=", 1) |
| 86 | props[key] = value |
Ying Wang | 3512321 | 2014-02-11 20:44:09 -0800 | [diff] [blame] | 87 | return props |
Joe Onorato | 9197a48 | 2011-06-08 16:04:14 -0700 | [diff] [blame] | 88 | |
| 89 | def get(self, name): |
| 90 | key = name + "=" |
| 91 | for line in self.lines: |
| 92 | if line.startswith(key): |
| 93 | return line[len(key):] |
| 94 | return "" |
| 95 | |
| 96 | def put(self, name, value): |
| 97 | key = name + "=" |
| 98 | for i in range(0,len(self.lines)): |
| 99 | if self.lines[i].startswith(key): |
| 100 | self.lines[i] = key + value |
| 101 | return |
| 102 | self.lines.append(key + value) |
| 103 | |
Jeff Sharkey | 26d22f7 | 2014-03-18 17:20:10 -0700 | [diff] [blame] | 104 | def delete(self, name): |
| 105 | key = name + "=" |
| 106 | self.lines = [ line for line in self.lines if not line.startswith(key) ] |
| 107 | |
Joe Onorato | 9197a48 | 2011-06-08 16:04:14 -0700 | [diff] [blame] | 108 | def write(self, f): |
| 109 | f.write("\n".join(self.lines)) |
| 110 | f.write("\n") |
| 111 | |
| 112 | def main(argv): |
| 113 | filename = argv[1] |
| 114 | f = open(filename) |
| 115 | lines = f.readlines() |
| 116 | f.close() |
| 117 | |
| 118 | properties = PropFile(lines) |
Ying Wang | 3512321 | 2014-02-11 20:44:09 -0800 | [diff] [blame] | 119 | |
Joe Onorato | 9197a48 | 2011-06-08 16:04:14 -0700 | [diff] [blame] | 120 | if filename.endswith("/build.prop"): |
| 121 | mangle_build_prop(properties) |
Jaekyun Seok | 8bee120 | 2017-01-31 20:07:02 +0900 | [diff] [blame] | 122 | elif (filename.endswith("/vendor/default.prop") or |
| 123 | filename.endswith("/odm/default.prop")): |
| 124 | mangle_default_prop_override(properties) |
Hung-ying Tyan | f829b40 | 2017-05-01 21:56:26 +0800 | [diff] [blame^] | 125 | elif (filename.endswith("/default.prop") or # legacy |
| 126 | filename.endswith("/prop.default")): |
Joe Onorato | 9197a48 | 2011-06-08 16:04:14 -0700 | [diff] [blame] | 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) |