Jiyong Park | ae55638 | 2020-05-20 18:33:43 +0900 | [diff] [blame^] | 1 | #!/usr/bin/env python3 |
Joe Onorato | 9197a48 | 2011-06-08 16:04:14 -0700 | [diff] [blame] | 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 | |
Jiyong Park | ae55638 | 2020-05-20 18:33:43 +0900 | [diff] [blame^] | 27 | # Put the modifications that you need to make into the */build.prop into this |
| 28 | # function. |
| 29 | def mangle_build_prop(prop_list): |
Jerry Zhang | 1695653 | 2016-10-18 00:01:27 +0000 | [diff] [blame] | 30 | # If ro.debuggable is 1, then enable adb on USB by default |
| 31 | # (this is for userdebug builds) |
Jiyong Park | ae55638 | 2020-05-20 18:33:43 +0900 | [diff] [blame^] | 32 | if prop_list.get("ro.debuggable") == "1": |
| 33 | val = prop_list.get("persist.sys.usb.config") |
Jerry Zhang | 1695653 | 2016-10-18 00:01:27 +0000 | [diff] [blame] | 34 | if "adb" not in val: |
| 35 | if val == "": |
| 36 | val = "adb" |
| 37 | else: |
| 38 | val = val + ",adb" |
Jiyong Park | ae55638 | 2020-05-20 18:33:43 +0900 | [diff] [blame^] | 39 | prop_list.put("persist.sys.usb.config", val) |
Joe Onorato | 8ad4bb1 | 2012-05-02 14:36:57 -0700 | [diff] [blame] | 40 | # UsbDeviceManager expects a value here. If it doesn't get it, it will |
| 41 | # default to "adb". That might not the right policy there, but it's better |
| 42 | # to be explicit. |
Jiyong Park | ae55638 | 2020-05-20 18:33:43 +0900 | [diff] [blame^] | 43 | if not prop_list.get("persist.sys.usb.config"): |
| 44 | prop_list.put("persist.sys.usb.config", "none"); |
Joe Onorato | 9197a48 | 2011-06-08 16:04:14 -0700 | [diff] [blame] | 45 | |
Jiyong Park | ae55638 | 2020-05-20 18:33:43 +0900 | [diff] [blame^] | 46 | def validate(prop_list): |
Ying Wang | 3512321 | 2014-02-11 20:44:09 -0800 | [diff] [blame] | 47 | """Validate the properties. |
| 48 | |
| 49 | Returns: |
| 50 | True if nothing is wrong. |
| 51 | """ |
| 52 | check_pass = True |
Jiyong Park | ae55638 | 2020-05-20 18:33:43 +0900 | [diff] [blame^] | 53 | for p in prop_list.get_all(): |
| 54 | if len(p.value) > PROP_VALUE_MAX and not p.name.startswith("ro."): |
Ying Wang | 38df101 | 2015-02-04 15:10:59 -0800 | [diff] [blame] | 55 | check_pass = False |
| 56 | sys.stderr.write("error: %s cannot exceed %d bytes: " % |
Jiyong Park | ae55638 | 2020-05-20 18:33:43 +0900 | [diff] [blame^] | 57 | (p.name, PROP_VALUE_MAX)) |
| 58 | sys.stderr.write("%s (%d)\n" % (p.value, len(p.value))) |
Ying Wang | 3512321 | 2014-02-11 20:44:09 -0800 | [diff] [blame] | 59 | return check_pass |
| 60 | |
Jiyong Park | ae55638 | 2020-05-20 18:33:43 +0900 | [diff] [blame^] | 61 | class Prop: |
Yu Liu | 115c66b | 2014-02-10 19:20:36 -0800 | [diff] [blame] | 62 | |
Jiyong Park | ae55638 | 2020-05-20 18:33:43 +0900 | [diff] [blame^] | 63 | def __init__(self, name, value, comment=None): |
| 64 | self.name = name.strip() |
| 65 | self.value = value.strip() |
| 66 | self.comment = comment |
Ying Wang | 3512321 | 2014-02-11 20:44:09 -0800 | [diff] [blame] | 67 | |
Jiyong Park | ae55638 | 2020-05-20 18:33:43 +0900 | [diff] [blame^] | 68 | @staticmethod |
| 69 | def from_line(line): |
| 70 | line = line.rstrip('\n') |
| 71 | if line.startswith("#"): |
| 72 | return Prop("", "", line) |
| 73 | elif "=" in line: |
| 74 | name, value = line.split("=", 1) |
| 75 | return Prop(name, value) |
| 76 | else: |
| 77 | # don't fail on invalid line |
| 78 | # TODO(jiyong) make this a hard error |
| 79 | return Prop("", "", line) |
| 80 | |
| 81 | def is_comment(self): |
| 82 | return self.comment != None |
| 83 | |
| 84 | def __str__(self): |
| 85 | if self.is_comment(): |
| 86 | return self.comment |
| 87 | else: |
| 88 | return self.name + "=" + self.value |
| 89 | |
| 90 | class PropList: |
| 91 | |
| 92 | def __init__(self, filename): |
| 93 | with open(filename) as f: |
| 94 | self.props = [Prop.from_line(l) |
| 95 | for l in f.readlines() if l.strip() != ""] |
| 96 | |
| 97 | def get_all(self): |
| 98 | return [p for p in self.props if not p.is_comment()] |
Joe Onorato | 9197a48 | 2011-06-08 16:04:14 -0700 | [diff] [blame] | 99 | |
| 100 | def get(self, name): |
Jiyong Park | ae55638 | 2020-05-20 18:33:43 +0900 | [diff] [blame^] | 101 | return next((p.value for p in self.props if p.name == name), "") |
Joe Onorato | 9197a48 | 2011-06-08 16:04:14 -0700 | [diff] [blame] | 102 | |
| 103 | def put(self, name, value): |
Jiyong Park | ae55638 | 2020-05-20 18:33:43 +0900 | [diff] [blame^] | 104 | index = next((i for i,p in enumerate(self.props) if p.name == name), -1) |
| 105 | if index == -1: |
| 106 | self.props.append(Prop(name, value)) |
| 107 | else: |
| 108 | self.props[index].value = value |
Joe Onorato | 9197a48 | 2011-06-08 16:04:14 -0700 | [diff] [blame] | 109 | |
Jeff Sharkey | 26d22f7 | 2014-03-18 17:20:10 -0700 | [diff] [blame] | 110 | def delete(self, name): |
Jiyong Park | ae55638 | 2020-05-20 18:33:43 +0900 | [diff] [blame^] | 111 | self.props = [p for p in self.props if p.name != name] |
Jeff Sharkey | 26d22f7 | 2014-03-18 17:20:10 -0700 | [diff] [blame] | 112 | |
Jiyong Park | ae55638 | 2020-05-20 18:33:43 +0900 | [diff] [blame^] | 113 | def write(self, filename): |
| 114 | with open(filename, 'w+') as f: |
| 115 | for p in self.props: |
| 116 | f.write(str(p) + "\n") |
Joe Onorato | 9197a48 | 2011-06-08 16:04:14 -0700 | [diff] [blame] | 117 | |
| 118 | def main(argv): |
| 119 | filename = argv[1] |
Joe Onorato | 9197a48 | 2011-06-08 16:04:14 -0700 | [diff] [blame] | 120 | |
Jiyong Park | ae55638 | 2020-05-20 18:33:43 +0900 | [diff] [blame^] | 121 | if not filename.endswith("/build.prop"): |
Joe Onorato | 9197a48 | 2011-06-08 16:04:14 -0700 | [diff] [blame] | 122 | sys.stderr.write("bad command line: " + str(argv) + "\n") |
| 123 | sys.exit(1) |
| 124 | |
Jiyong Park | ae55638 | 2020-05-20 18:33:43 +0900 | [diff] [blame^] | 125 | props = PropList(filename) |
| 126 | mangle_build_prop(props) |
| 127 | if not validate(props): |
Ying Wang | 3512321 | 2014-02-11 20:44:09 -0800 | [diff] [blame] | 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:]: |
Jiyong Park | ae55638 | 2020-05-20 18:33:43 +0900 | [diff] [blame^] | 132 | props.delete(key) |
Jeff Sharkey | 26d22f7 | 2014-03-18 17:20:10 -0700 | [diff] [blame] | 133 | |
Jiyong Park | ae55638 | 2020-05-20 18:33:43 +0900 | [diff] [blame^] | 134 | props.write(filename) |
Joe Onorato | 9197a48 | 2011-06-08 16:04:14 -0700 | [diff] [blame] | 135 | |
| 136 | if __name__ == "__main__": |
| 137 | main(sys.argv) |