blob: 397526f05e21bbf41256ec520cd398c680c0f200 [file] [log] [blame]
Jiyong Parkae556382020-05-20 18:33:43 +09001#!/usr/bin/env python3
Joe Onorato9197a482011-06-08 16:04:14 -07002#
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
Jiyong Parkd721e872020-06-22 17:30:57 +090019# Usage: post_process_props.py file.prop [disallowed_key, ...]
20# Disallowed keys are removed from the property file, if present
Jeff Sharkey26d22f72014-03-18 17:20:10 -070021
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
Jiyong Parkae556382020-05-20 18:33:43 +090027# Put the modifications that you need to make into the */build.prop into this
28# function.
29def mangle_build_prop(prop_list):
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)
Jiyong Parkd721e872020-06-22 17:30:57 +090032 if prop_list.get_value("ro.debuggable") == "1":
33 val = prop_list.get_value("persist.sys.usb.config")
Jerry Zhang16956532016-10-18 00:01:27 +000034 if "adb" not in val:
35 if val == "":
36 val = "adb"
37 else:
38 val = val + ",adb"
Jiyong Parkae556382020-05-20 18:33:43 +090039 prop_list.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.
Jiyong Parkd721e872020-06-22 17:30:57 +090043 if not prop_list.get_value("persist.sys.usb.config"):
Jiyong Parkae556382020-05-20 18:33:43 +090044 prop_list.put("persist.sys.usb.config", "none");
Joe Onorato9197a482011-06-08 16:04:14 -070045
Jiyong Parkae556382020-05-20 18:33:43 +090046def validate(prop_list):
Ying Wang35123212014-02-11 20:44:09 -080047 """Validate the properties.
48
Jiyong Parkd721e872020-06-22 17:30:57 +090049 If the value of a sysprop exceeds the max limit (91), it's an error, unless
50 the sysprop is a read-only one.
51
52 Checks if there is no optional prop assignments.
53
Ying Wang35123212014-02-11 20:44:09 -080054 Returns:
55 True if nothing is wrong.
56 """
57 check_pass = True
Jiyong Parkd721e872020-06-22 17:30:57 +090058 for p in prop_list.get_all_props():
Jiyong Parkae556382020-05-20 18:33:43 +090059 if len(p.value) > PROP_VALUE_MAX and not p.name.startswith("ro."):
Ying Wang38df1012015-02-04 15:10:59 -080060 check_pass = False
61 sys.stderr.write("error: %s cannot exceed %d bytes: " %
Jiyong Parkae556382020-05-20 18:33:43 +090062 (p.name, PROP_VALUE_MAX))
63 sys.stderr.write("%s (%d)\n" % (p.value, len(p.value)))
Jiyong Parkd721e872020-06-22 17:30:57 +090064
65 if p.is_optional():
66 check_pass = False
67 sys.stderr.write("error: found unresolved optional prop assignment:\n")
68 sys.stderr.write(str(p) + "\n")
69
Ying Wang35123212014-02-11 20:44:09 -080070 return check_pass
71
Jiyong Parkd721e872020-06-22 17:30:57 +090072def override_optional_props(prop_list):
73 """Override a?=b with a=c, if the latter exists
74
75 Overriding is done by deleting a?=b
76 When there are a?=b and a?=c, then only the last one survives
77 When there are a=b and a=c, then it's an error.
78
79 Returns:
80 True if the override was successful
81 """
82 success = True
83 for name in prop_list.get_all_names():
84 props = prop_list.get_props(name)
85 optional_props = [p for p in props if p.is_optional()]
86 overriding_props = [p for p in props if not p.is_optional()]
87 if len(overriding_props) > 1:
88 # duplicated props are allowed when the all have the same value
89 if all(overriding_props[0].value == p.value for p in overriding_props):
90 continue
91 success = False
92 sys.stderr.write("error: found duplicate sysprop assignments:\n")
93 for p in overriding_props:
94 sys.stderr.write("%s\n" % str(p))
95 elif len(overriding_props) == 1:
96 for p in optional_props:
97 p.delete("overridden by %s" % str(overriding_props[0]))
98 else:
99 if len(optional_props) > 1:
100 for p in optional_props[:-1]:
101 p.delete("overridden by %s" % str(optional_props[-1]))
102 # Make the last optional one as non-optional
103 optional_props[-1].optional = False
104
105 return success
106
Jiyong Parkae556382020-05-20 18:33:43 +0900107class Prop:
Yu Liu115c66b2014-02-10 19:20:36 -0800108
Jiyong Parkd721e872020-06-22 17:30:57 +0900109 def __init__(self, name, value, optional=False, comment=None):
Jiyong Parkae556382020-05-20 18:33:43 +0900110 self.name = name.strip()
111 self.value = value.strip()
Jiyong Parkd721e872020-06-22 17:30:57 +0900112 if comment != None:
113 self.comments = [comment]
114 else:
115 self.comments = []
116 self.optional = optional
Ying Wang35123212014-02-11 20:44:09 -0800117
Jiyong Parkae556382020-05-20 18:33:43 +0900118 @staticmethod
119 def from_line(line):
120 line = line.rstrip('\n')
121 if line.startswith("#"):
Jiyong Parkd721e872020-06-22 17:30:57 +0900122 return Prop("", "", comment=line)
123 elif "?=" in line:
124 name, value = line.split("?=", 1)
125 return Prop(name, value, optional=True)
Jiyong Parkae556382020-05-20 18:33:43 +0900126 elif "=" in line:
127 name, value = line.split("=", 1)
Jiyong Parkd721e872020-06-22 17:30:57 +0900128 return Prop(name, value, optional=False)
Jiyong Parkae556382020-05-20 18:33:43 +0900129 else:
130 # don't fail on invalid line
131 # TODO(jiyong) make this a hard error
Jiyong Parkd721e872020-06-22 17:30:57 +0900132 return Prop("", "", comment=line)
Jiyong Parkae556382020-05-20 18:33:43 +0900133
134 def is_comment(self):
Jiyong Parkd721e872020-06-22 17:30:57 +0900135 return bool(self.comments and not self.name)
136
137 def is_optional(self):
138 return (not self.is_comment()) and self.optional
139
140 def make_as_comment(self):
141 # Prepend "#" to the last line which is the prop assignment
142 if not self.is_comment():
143 assignment = str(self).rsplit("\n", 1)[-1]
144 self.comments.append("#" + assignment)
145 self.name = ""
146 self.value = ""
147
148 def delete(self, reason):
149 self.comments.append("# Removed by post_process_props.py because " + reason)
150 self.make_as_comment()
Jiyong Parkae556382020-05-20 18:33:43 +0900151
152 def __str__(self):
Jiyong Parkd721e872020-06-22 17:30:57 +0900153 assignment = []
154 if not self.is_comment():
155 operator = "?=" if self.is_optional() else "="
156 assignment.append(self.name + operator + self.value)
157 return "\n".join(self.comments + assignment)
Jiyong Parkae556382020-05-20 18:33:43 +0900158
159class PropList:
160
161 def __init__(self, filename):
162 with open(filename) as f:
163 self.props = [Prop.from_line(l)
164 for l in f.readlines() if l.strip() != ""]
165
Jiyong Parkd721e872020-06-22 17:30:57 +0900166 def get_all_props(self):
Jiyong Parkae556382020-05-20 18:33:43 +0900167 return [p for p in self.props if not p.is_comment()]
Joe Onorato9197a482011-06-08 16:04:14 -0700168
Jiyong Parkd721e872020-06-22 17:30:57 +0900169 def get_all_names(self):
170 return set([p.name for p in self.get_all_props()])
171
172 def get_props(self, name):
173 return [p for p in self.get_all_props() if p.name == name]
174
175 def get_value(self, name):
176 # Caution: only the value of the first sysprop having the name is returned.
Jiyong Parkae556382020-05-20 18:33:43 +0900177 return next((p.value for p in self.props if p.name == name), "")
Joe Onorato9197a482011-06-08 16:04:14 -0700178
179 def put(self, name, value):
Jiyong Parkd721e872020-06-22 17:30:57 +0900180 # Note: when there is an optional prop for the name, its value isn't changed.
181 # Instead a new non-optional prop is appended, which will override the
182 # optional prop. Otherwise, the new value might be overridden by an existing
183 # non-optional prop of the same name.
184 index = next((i for i,p in enumerate(self.props)
185 if p.name == name and not p.is_optional()), -1)
Jiyong Parkae556382020-05-20 18:33:43 +0900186 if index == -1:
Jiyong Parkd721e872020-06-22 17:30:57 +0900187 self.props.append(Prop(name, value,
188 comment="# Auto-added by post_process_props.py"))
Jiyong Parkae556382020-05-20 18:33:43 +0900189 else:
Jiyong Parkd721e872020-06-22 17:30:57 +0900190 self.props[index].comments.append(
191 "# Value overridden by post_process_props.py. Original value: %s" %
192 self.props[index].value)
Jiyong Parkae556382020-05-20 18:33:43 +0900193 self.props[index].value = value
Joe Onorato9197a482011-06-08 16:04:14 -0700194
Jiyong Parkae556382020-05-20 18:33:43 +0900195 def write(self, filename):
196 with open(filename, 'w+') as f:
197 for p in self.props:
198 f.write(str(p) + "\n")
Joe Onorato9197a482011-06-08 16:04:14 -0700199
200def main(argv):
201 filename = argv[1]
Joe Onorato9197a482011-06-08 16:04:14 -0700202
Jiyong Parkae556382020-05-20 18:33:43 +0900203 if not filename.endswith("/build.prop"):
Joe Onorato9197a482011-06-08 16:04:14 -0700204 sys.stderr.write("bad command line: " + str(argv) + "\n")
205 sys.exit(1)
206
Jiyong Parkae556382020-05-20 18:33:43 +0900207 props = PropList(filename)
208 mangle_build_prop(props)
Jiyong Parkd721e872020-06-22 17:30:57 +0900209 if not override_optional_props(props):
210 sys.exit(1)
Jiyong Parkae556382020-05-20 18:33:43 +0900211 if not validate(props):
Ying Wang35123212014-02-11 20:44:09 -0800212 sys.exit(1)
213
Jiyong Parkd721e872020-06-22 17:30:57 +0900214 # Drop any disallowed keys
Jeff Sharkey26d22f72014-03-18 17:20:10 -0700215 for key in argv[2:]:
Jiyong Parkd721e872020-06-22 17:30:57 +0900216 for p in props.get_props(key):
217 p.delete("%s is a disallowed key" % key)
Jeff Sharkey26d22f72014-03-18 17:20:10 -0700218
Jiyong Parkae556382020-05-20 18:33:43 +0900219 props.write(filename)
Joe Onorato9197a482011-06-08 16:04:14 -0700220
221if __name__ == "__main__":
222 main(sys.argv)