blob: 80fb85d7e16911dc2a692e0526559eb5e9db7ef5 [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
Brian Carlstromdad2ab42014-07-29 16:08:25 -070022# 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.
25PROP_NAME_MAX = 31
Ying Wang35123212014-02-11 20:44:09 -080026PROP_VALUE_MAX = 91
27
Joe Onorato9197a482011-06-08 16:04:14 -070028# 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.
30def mangle_build_prop(prop):
Ying Wang35123212014-02-11 20:44:09 -080031 pass
Joe Onorato9197a482011-06-08 16:04:14 -070032
Jaekyun Seok8bee1202017-01-31 20:07:02 +090033# Put the modifications that you need to make into /vendor/default.prop and
34# /odm/default.prop into this function. The prop object has get(name) and
35# put(name,value) methods.
36def mangle_default_prop_override(prop):
37 pass
38
Ying Wang35123212014-02-11 20:44:09 -080039# Put the modifications that you need to make into the /default.prop into this
Joe Onorato9197a482011-06-08 16:04:14 -070040# function. The prop object has get(name) and put(name,value) methods.
41def mangle_default_prop(prop):
Jerry Zhang16956532016-10-18 00:01:27 +000042 # If ro.debuggable is 1, then enable adb on USB by default
43 # (this is for userdebug builds)
44 if prop.get("ro.debuggable") == "1":
45 val = prop.get("persist.sys.usb.config")
46 if "adb" not in val:
47 if val == "":
48 val = "adb"
49 else:
50 val = val + ",adb"
51 prop.put("persist.sys.usb.config", val)
Joe Onorato8ad4bb12012-05-02 14:36:57 -070052 # UsbDeviceManager expects a value here. If it doesn't get it, it will
53 # default to "adb". That might not the right policy there, but it's better
54 # to be explicit.
55 if not prop.get("persist.sys.usb.config"):
56 prop.put("persist.sys.usb.config", "none");
Joe Onorato9197a482011-06-08 16:04:14 -070057
Ying Wang35123212014-02-11 20:44:09 -080058def validate(prop):
59 """Validate the properties.
60
61 Returns:
62 True if nothing is wrong.
63 """
64 check_pass = True
65 buildprops = prop.to_dict()
Ying Wang35123212014-02-11 20:44:09 -080066 for key, value in buildprops.iteritems():
67 # Check build properties' length.
Brian Carlstromdad2ab42014-07-29 16:08:25 -070068 if len(key) > PROP_NAME_MAX:
69 check_pass = False
70 sys.stderr.write("error: %s cannot exceed %d bytes: " %
71 (key, PROP_NAME_MAX))
72 sys.stderr.write("%s (%d)\n" % (key, len(key)))
Ying Wang35123212014-02-11 20:44:09 -080073 if len(value) > PROP_VALUE_MAX:
Ying Wang38df1012015-02-04 15:10:59 -080074 check_pass = False
75 sys.stderr.write("error: %s cannot exceed %d bytes: " %
76 (key, PROP_VALUE_MAX))
77 sys.stderr.write("%s (%d)\n" % (value, len(value)))
Ying Wang35123212014-02-11 20:44:09 -080078 return check_pass
79
Joe Onorato9197a482011-06-08 16:04:14 -070080class PropFile:
Yu Liu115c66b2014-02-10 19:20:36 -080081
Joe Onorato9197a482011-06-08 16:04:14 -070082 def __init__(self, lines):
Ying Wang35123212014-02-11 20:44:09 -080083 self.lines = [s.strip() for s in lines]
84
85 def to_dict(self):
86 props = {}
Yu Liu115c66b2014-02-10 19:20:36 -080087 for line in self.lines:
Ying Wang35123212014-02-11 20:44:09 -080088 if not line or line.startswith("#"):
Yu Liu115c66b2014-02-10 19:20:36 -080089 continue
Ying Wang5a7ad032014-04-15 11:36:46 -070090 if "=" in line:
91 key, value = line.split("=", 1)
92 props[key] = value
Ying Wang35123212014-02-11 20:44:09 -080093 return props
Joe Onorato9197a482011-06-08 16:04:14 -070094
95 def get(self, name):
96 key = name + "="
97 for line in self.lines:
98 if line.startswith(key):
99 return line[len(key):]
100 return ""
101
102 def put(self, name, value):
103 key = name + "="
104 for i in range(0,len(self.lines)):
105 if self.lines[i].startswith(key):
106 self.lines[i] = key + value
107 return
108 self.lines.append(key + value)
109
Jeff Sharkey26d22f72014-03-18 17:20:10 -0700110 def delete(self, name):
111 key = name + "="
112 self.lines = [ line for line in self.lines if not line.startswith(key) ]
113
Joe Onorato9197a482011-06-08 16:04:14 -0700114 def write(self, f):
115 f.write("\n".join(self.lines))
116 f.write("\n")
117
118def main(argv):
119 filename = argv[1]
120 f = open(filename)
121 lines = f.readlines()
122 f.close()
123
124 properties = PropFile(lines)
Ying Wang35123212014-02-11 20:44:09 -0800125
Joe Onorato9197a482011-06-08 16:04:14 -0700126 if filename.endswith("/build.prop"):
127 mangle_build_prop(properties)
Jaekyun Seok8bee1202017-01-31 20:07:02 +0900128 elif (filename.endswith("/vendor/default.prop") or
129 filename.endswith("/odm/default.prop")):
130 mangle_default_prop_override(properties)
Joe Onorato9197a482011-06-08 16:04:14 -0700131 elif filename.endswith("/default.prop"):
132 mangle_default_prop(properties)
133 else:
134 sys.stderr.write("bad command line: " + str(argv) + "\n")
135 sys.exit(1)
136
Ying Wang35123212014-02-11 20:44:09 -0800137 if not validate(properties):
138 sys.exit(1)
139
Jeff Sharkey26d22f72014-03-18 17:20:10 -0700140 # Drop any blacklisted keys
141 for key in argv[2:]:
142 properties.delete(key)
143
Mike Lockwood5b65ee42011-06-08 19:06:43 -0700144 f = open(filename, 'w+')
145 properties.write(f)
Joe Onorato9197a482011-06-08 16:04:14 -0700146 f.close()
147
148if __name__ == "__main__":
149 main(sys.argv)