blob: 31b7a78d58d7d0dbf43dcd70c488dceafebf151b [file] [log] [blame]
Joe Onorato9197a482011-06-08 16:04:14 -07001#!/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
17import sys
18
Jeff Sharkey26d22f72014-03-18 17:20:10 -070019# Usage: post_process_props.py file.prop [blacklist_key, ...]
20# Blacklisted keys are removed from the property file, if present
21
Elliott Hughes05c1a2a2017-02-28 10:04:23 -080022# 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 Wang35123212014-02-11 20:44:09 -080025PROP_VALUE_MAX = 91
26
Joe Onorato9197a482011-06-08 16:04:14 -070027# Put the modifications that you need to make into the /system/build.prop into this
28# function. The prop object has get(name) and put(name,value) methods.
29def mangle_build_prop(prop):
Jerry Zhang16956532016-10-18 00:01:27 +000030 # If ro.debuggable is 1, then enable adb on USB by default
31 # (this is for userdebug builds)
32 if prop.get("ro.debuggable") == "1":
33 val = prop.get("persist.sys.usb.config")
34 if "adb" not in val:
35 if val == "":
36 val = "adb"
37 else:
38 val = val + ",adb"
39 prop.put("persist.sys.usb.config", val)
Joe Onorato8ad4bb12012-05-02 14:36:57 -070040 # 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.
43 if not prop.get("persist.sys.usb.config"):
44 prop.put("persist.sys.usb.config", "none");
Joe Onorato9197a482011-06-08 16:04:14 -070045
Ying Wang35123212014-02-11 20:44:09 -080046def validate(prop):
47 """Validate the properties.
48
49 Returns:
50 True if nothing is wrong.
51 """
52 check_pass = True
53 buildprops = prop.to_dict()
Ying Wang35123212014-02-11 20:44:09 -080054 for key, value in buildprops.iteritems():
55 # Check build properties' length.
Tom Cherry47c4eb42017-10-12 09:20:14 -070056 if len(value) > PROP_VALUE_MAX and not key.startswith("ro."):
Ying Wang38df1012015-02-04 15:10:59 -080057 check_pass = False
58 sys.stderr.write("error: %s cannot exceed %d bytes: " %
59 (key, PROP_VALUE_MAX))
60 sys.stderr.write("%s (%d)\n" % (value, len(value)))
Ying Wang35123212014-02-11 20:44:09 -080061 return check_pass
62
Joe Onorato9197a482011-06-08 16:04:14 -070063class PropFile:
Yu Liu115c66b2014-02-10 19:20:36 -080064
Joe Onorato9197a482011-06-08 16:04:14 -070065 def __init__(self, lines):
Ying Wang35123212014-02-11 20:44:09 -080066 self.lines = [s.strip() for s in lines]
67
68 def to_dict(self):
69 props = {}
Yu Liu115c66b2014-02-10 19:20:36 -080070 for line in self.lines:
Ying Wang35123212014-02-11 20:44:09 -080071 if not line or line.startswith("#"):
Yu Liu115c66b2014-02-10 19:20:36 -080072 continue
Ying Wang5a7ad032014-04-15 11:36:46 -070073 if "=" in line:
74 key, value = line.split("=", 1)
75 props[key] = value
Ying Wang35123212014-02-11 20:44:09 -080076 return props
Joe Onorato9197a482011-06-08 16:04:14 -070077
78 def get(self, name):
79 key = name + "="
80 for line in self.lines:
81 if line.startswith(key):
82 return line[len(key):]
83 return ""
84
85 def put(self, name, value):
86 key = name + "="
87 for i in range(0,len(self.lines)):
88 if self.lines[i].startswith(key):
89 self.lines[i] = key + value
90 return
91 self.lines.append(key + value)
92
Jeff Sharkey26d22f72014-03-18 17:20:10 -070093 def delete(self, name):
94 key = name + "="
95 self.lines = [ line for line in self.lines if not line.startswith(key) ]
96
Joe Onorato9197a482011-06-08 16:04:14 -070097 def write(self, f):
98 f.write("\n".join(self.lines))
99 f.write("\n")
100
101def main(argv):
102 filename = argv[1]
103 f = open(filename)
104 lines = f.readlines()
105 f.close()
106
107 properties = PropFile(lines)
Ying Wang35123212014-02-11 20:44:09 -0800108
Joe Onorato9197a482011-06-08 16:04:14 -0700109 if filename.endswith("/build.prop"):
110 mangle_build_prop(properties)
Joe Onorato9197a482011-06-08 16:04:14 -0700111 else:
112 sys.stderr.write("bad command line: " + str(argv) + "\n")
113 sys.exit(1)
114
Ying Wang35123212014-02-11 20:44:09 -0800115 if not validate(properties):
116 sys.exit(1)
117
Jeff Sharkey26d22f72014-03-18 17:20:10 -0700118 # Drop any blacklisted keys
119 for key in argv[2:]:
120 properties.delete(key)
121
Mike Lockwood5b65ee42011-06-08 19:06:43 -0700122 f = open(filename, 'w+')
123 properties.write(f)
Joe Onorato9197a482011-06-08 16:04:14 -0700124 f.close()
125
126if __name__ == "__main__":
127 main(sys.argv)