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