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