blob: 295f8f611c2c418abf2a1961e1c6d1d454fb6248 [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):
Ying Wang35123212014-02-11 20:44:09 -080030 pass
Joe Onorato9197a482011-06-08 16:04:14 -070031
Ying Wang35123212014-02-11 20:44:09 -080032# Put the modifications that you need to make into the /default.prop into this
Joe Onorato9197a482011-06-08 16:04:14 -070033# function. The prop object has get(name) and put(name,value) methods.
34def mangle_default_prop(prop):
35 # If ro.debuggable is 1, then enable adb on USB by default
36 # (this is for userdebug builds)
37 if prop.get("ro.debuggable") == "1":
38 val = prop.get("persist.sys.usb.config")
Oreste Salerno420e3412015-05-20 16:44:48 +000039 if "adb" not in val:
40 if val == "":
41 val = "adb"
42 else:
43 val = val + ",adb"
44 prop.put("persist.sys.usb.config", val)
Joe Onorato8ad4bb12012-05-02 14:36:57 -070045 # 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 Onorato9197a482011-06-08 16:04:14 -070050
Ying Wang35123212014-02-11 20:44:09 -080051def 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 Wang35123212014-02-11 20:44:09 -080059 for key, value in buildprops.iteritems():
60 # Check build properties' length.
61 if len(value) > PROP_VALUE_MAX:
Ying Wang38df1012015-02-04 15:10:59 -080062 check_pass = False
63 sys.stderr.write("error: %s cannot exceed %d bytes: " %
64 (key, PROP_VALUE_MAX))
65 sys.stderr.write("%s (%d)\n" % (value, len(value)))
Ying Wang35123212014-02-11 20:44:09 -080066 return check_pass
67
Joe Onorato9197a482011-06-08 16:04:14 -070068class PropFile:
Yu Liu115c66b2014-02-10 19:20:36 -080069
Joe Onorato9197a482011-06-08 16:04:14 -070070 def __init__(self, lines):
Ying Wang35123212014-02-11 20:44:09 -080071 self.lines = [s.strip() for s in lines]
72
73 def to_dict(self):
74 props = {}
Yu Liu115c66b2014-02-10 19:20:36 -080075 for line in self.lines:
Ying Wang35123212014-02-11 20:44:09 -080076 if not line or line.startswith("#"):
Yu Liu115c66b2014-02-10 19:20:36 -080077 continue
Ying Wang5a7ad032014-04-15 11:36:46 -070078 if "=" in line:
79 key, value = line.split("=", 1)
80 props[key] = value
Ying Wang35123212014-02-11 20:44:09 -080081 return props
Joe Onorato9197a482011-06-08 16:04:14 -070082
83 def get(self, name):
84 key = name + "="
85 for line in self.lines:
86 if line.startswith(key):
87 return line[len(key):]
88 return ""
89
90 def put(self, name, value):
91 key = name + "="
92 for i in range(0,len(self.lines)):
93 if self.lines[i].startswith(key):
94 self.lines[i] = key + value
95 return
96 self.lines.append(key + value)
97
Jeff Sharkey26d22f72014-03-18 17:20:10 -070098 def delete(self, name):
99 key = name + "="
100 self.lines = [ line for line in self.lines if not line.startswith(key) ]
101
Joe Onorato9197a482011-06-08 16:04:14 -0700102 def write(self, f):
103 f.write("\n".join(self.lines))
104 f.write("\n")
105
106def main(argv):
107 filename = argv[1]
108 f = open(filename)
109 lines = f.readlines()
110 f.close()
111
112 properties = PropFile(lines)
Ying Wang35123212014-02-11 20:44:09 -0800113
Joe Onorato9197a482011-06-08 16:04:14 -0700114 if filename.endswith("/build.prop"):
115 mangle_build_prop(properties)
116 elif filename.endswith("/default.prop"):
117 mangle_default_prop(properties)
118 else:
119 sys.stderr.write("bad command line: " + str(argv) + "\n")
120 sys.exit(1)
121
Ying Wang35123212014-02-11 20:44:09 -0800122 if not validate(properties):
123 sys.exit(1)
124
Jeff Sharkey26d22f72014-03-18 17:20:10 -0700125 # Drop any blacklisted keys
126 for key in argv[2:]:
127 properties.delete(key)
128
Mike Lockwood5b65ee42011-06-08 19:06:43 -0700129 f = open(filename, 'w+')
130 properties.write(f)
Joe Onorato9197a482011-06-08 16:04:14 -0700131 f.close()
132
133if __name__ == "__main__":
134 main(sys.argv)