blob: 64af01db39d517540a8d75f69c10a6531f46d229 [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
Anthony Kingc713d762015-11-03 00:23:11 +000017import sys
Joe Onorato9197a482011-06-08 16:04:14 -070018
Anthony King12b55e82015-10-31 15:27:39 -040019
20def iteritems(obj):
21 if hasattr(obj, 'iteritems'):
22 return obj.iteritems()
23 return obj.items()
24
25
Jeff Sharkey26d22f72014-03-18 17:20:10 -070026# Usage: post_process_props.py file.prop [blacklist_key, ...]
27# Blacklisted keys are removed from the property file, if present
28
Brian Carlstromdad2ab42014-07-29 16:08:25 -070029# See PROP_NAME_MAX and PROP_VALUE_MAX system_properties.h.
30# The constants in system_properties.h includes the termination NUL,
31# so we decrease the values by 1 here.
32PROP_NAME_MAX = 31
Ying Wang35123212014-02-11 20:44:09 -080033PROP_VALUE_MAX = 91
34
Joe Onorato9197a482011-06-08 16:04:14 -070035# Put the modifications that you need to make into the /system/build.prop into this
36# function. The prop object has get(name) and put(name,value) methods.
Ricardo Cerqueiraab949b52014-01-03 02:46:15 +000037def mangle_build_prop(prop, overrides):
38 if len(overrides) == 0:
39 return
40 overridelist = overrides.replace(" ",",").split(",")
41 for proppair in overridelist:
42 values = proppair.split("=")
43 prop.put(values[0], values[1])
44
Ying Wang35123212014-02-11 20:44:09 -080045 pass
Joe Onorato9197a482011-06-08 16:04:14 -070046
Ying Wang35123212014-02-11 20:44:09 -080047# Put the modifications that you need to make into the /default.prop into this
Joe Onorato9197a482011-06-08 16:04:14 -070048# function. The prop object has get(name) and put(name,value) methods.
49def mangle_default_prop(prop):
Scott Mertz9e5f0772015-02-26 10:51:44 -080050 # If ro.adb.secure is not 1, then enable adb on USB by default
51 # (this is for eng builds)
52 if prop.get("ro.adb.secure") != "1":
Joe Onorato9197a482011-06-08 16:04:14 -070053 val = prop.get("persist.sys.usb.config")
54 if val == "":
55 val = "adb"
56 else:
57 val = val + ",adb"
58 prop.put("persist.sys.usb.config", val)
Joe Onorato8ad4bb12012-05-02 14:36:57 -070059 # UsbDeviceManager expects a value here. If it doesn't get it, it will
60 # default to "adb". That might not the right policy there, but it's better
61 # to be explicit.
62 if not prop.get("persist.sys.usb.config"):
Anthony Kingc713d762015-11-03 00:23:11 +000063 prop.put("persist.sys.usb.config", "none")
Joe Onorato9197a482011-06-08 16:04:14 -070064
Ying Wang35123212014-02-11 20:44:09 -080065def validate(prop):
66 """Validate the properties.
67
68 Returns:
69 True if nothing is wrong.
70 """
71 check_pass = True
72 buildprops = prop.to_dict()
Anthony King12b55e82015-10-31 15:27:39 -040073 for key, value in iteritems(buildprops):
Ying Wang35123212014-02-11 20:44:09 -080074 # Check build properties' length.
Brian Carlstromdad2ab42014-07-29 16:08:25 -070075 if len(key) > PROP_NAME_MAX:
76 check_pass = False
77 sys.stderr.write("error: %s cannot exceed %d bytes: " %
78 (key, PROP_NAME_MAX))
79 sys.stderr.write("%s (%d)\n" % (key, len(key)))
Ying Wang35123212014-02-11 20:44:09 -080080 if len(value) > PROP_VALUE_MAX:
Ying Wang38df1012015-02-04 15:10:59 -080081 check_pass = False
82 sys.stderr.write("error: %s cannot exceed %d bytes: " %
83 (key, PROP_VALUE_MAX))
84 sys.stderr.write("%s (%d)\n" % (value, len(value)))
Ying Wang35123212014-02-11 20:44:09 -080085 return check_pass
86
Joe Onorato9197a482011-06-08 16:04:14 -070087class PropFile:
Yu Liu115c66b2014-02-10 19:20:36 -080088
Joe Onorato9197a482011-06-08 16:04:14 -070089 def __init__(self, lines):
Ying Wang35123212014-02-11 20:44:09 -080090 self.lines = [s.strip() for s in lines]
91
92 def to_dict(self):
93 props = {}
Yu Liu115c66b2014-02-10 19:20:36 -080094 for line in self.lines:
Ying Wang35123212014-02-11 20:44:09 -080095 if not line or line.startswith("#"):
Yu Liu115c66b2014-02-10 19:20:36 -080096 continue
Ying Wang5a7ad032014-04-15 11:36:46 -070097 if "=" in line:
98 key, value = line.split("=", 1)
99 props[key] = value
Ying Wang35123212014-02-11 20:44:09 -0800100 return props
Joe Onorato9197a482011-06-08 16:04:14 -0700101
102 def get(self, name):
103 key = name + "="
104 for line in self.lines:
105 if line.startswith(key):
106 return line[len(key):]
107 return ""
108
109 def put(self, name, value):
110 key = name + "="
111 for i in range(0,len(self.lines)):
112 if self.lines[i].startswith(key):
113 self.lines[i] = key + value
114 return
115 self.lines.append(key + value)
116
Jeff Sharkey26d22f72014-03-18 17:20:10 -0700117 def delete(self, name):
118 key = name + "="
119 self.lines = [ line for line in self.lines if not line.startswith(key) ]
120
Joe Onorato9197a482011-06-08 16:04:14 -0700121 def write(self, f):
122 f.write("\n".join(self.lines))
123 f.write("\n")
124
125def main(argv):
126 filename = argv[1]
Ricardo Cerqueiraab949b52014-01-03 02:46:15 +0000127 if (len(argv) > 2):
128 extraargs = argv[2]
129 else:
130 extraargs = ""
Joe Onorato9197a482011-06-08 16:04:14 -0700131 f = open(filename)
132 lines = f.readlines()
133 f.close()
134
135 properties = PropFile(lines)
Ying Wang35123212014-02-11 20:44:09 -0800136
Joe Onorato9197a482011-06-08 16:04:14 -0700137 if filename.endswith("/build.prop"):
Ricardo Cerqueiraab949b52014-01-03 02:46:15 +0000138 mangle_build_prop(properties, extraargs)
Joe Onorato9197a482011-06-08 16:04:14 -0700139 elif filename.endswith("/default.prop"):
140 mangle_default_prop(properties)
141 else:
142 sys.stderr.write("bad command line: " + str(argv) + "\n")
143 sys.exit(1)
144
Ying Wang35123212014-02-11 20:44:09 -0800145 if not validate(properties):
146 sys.exit(1)
147
Jeff Sharkey26d22f72014-03-18 17:20:10 -0700148 # Drop any blacklisted keys
Ricardo Cerqueiraab949b52014-01-03 02:46:15 +0000149 for key in argv[3:]:
Jeff Sharkey26d22f72014-03-18 17:20:10 -0700150 properties.delete(key)
151
Mike Lockwood5b65ee42011-06-08 19:06:43 -0700152 f = open(filename, 'w+')
153 properties.write(f)
Joe Onorato9197a482011-06-08 16:04:14 -0700154 f.close()
155
156if __name__ == "__main__":
157 main(sys.argv)