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