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