blob: 32829c1c9fc44421dabd1ec03bd17fe26e6e7789 [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
Jiyong Park0b4fccb2020-06-26 17:38:00 +090017import argparse
Joe Onorato9197a482011-06-08 16:04:14 -070018import sys
19
Jiyong Parkd721e872020-06-22 17:30:57 +090020# Usage: post_process_props.py file.prop [disallowed_key, ...]
21# Disallowed keys are removed from the property file, if present
Jeff Sharkey26d22f72014-03-18 17:20:10 -070022
Elliott Hughes05c1a2a2017-02-28 10:04:23 -080023# See PROP_VALUE_MAX in system_properties.h.
24# The constant in system_properties.h includes the terminating NUL,
25# so we decrease the value by 1 here.
Ying Wang35123212014-02-11 20:44:09 -080026PROP_VALUE_MAX = 91
27
Jiyong Parkae556382020-05-20 18:33:43 +090028# Put the modifications that you need to make into the */build.prop into this
29# function.
30def mangle_build_prop(prop_list):
Jerry Zhang16956532016-10-18 00:01:27 +000031 # If ro.debuggable is 1, then enable adb on USB by default
32 # (this is for userdebug builds)
Jiyong Parkd721e872020-06-22 17:30:57 +090033 if prop_list.get_value("ro.debuggable") == "1":
34 val = prop_list.get_value("persist.sys.usb.config")
Jerry Zhang16956532016-10-18 00:01:27 +000035 if "adb" not in val:
36 if val == "":
37 val = "adb"
38 else:
39 val = val + ",adb"
Jiyong Parkae556382020-05-20 18:33:43 +090040 prop_list.put("persist.sys.usb.config", val)
Justin Yun07ceaa72021-04-02 16:29:06 +090041
Justin Yun23d52432023-11-10 16:31:04 +090042def validate_grf_props(prop_list):
Justin Yun07ceaa72021-04-02 16:29:06 +090043 """Validate GRF properties if exist.
44
Justin Yun23d52432023-11-10 16:31:04 +090045 If ro.board.first_api_level is defined, check if its value is valid.
Justin Yun07ceaa72021-04-02 16:29:06 +090046
47 Returns:
48 True if the GRF properties are valid.
49 """
50 grf_api_level = prop_list.get_value("ro.board.first_api_level")
51 board_api_level = prop_list.get_value("ro.board.api_level")
52
Justin Yun23d52432023-11-10 16:31:04 +090053 if grf_api_level and board_api_level:
54 grf_api_level = int(grf_api_level)
Justin Yun870ea2e2023-04-06 16:28:12 +090055 board_api_level = int(board_api_level)
56 if board_api_level < grf_api_level:
Justin Yun23d52432023-11-10 16:31:04 +090057 sys.stderr.write("error: ro.board.api_level(%d) must not be less than "
Justin Yun870ea2e2023-04-06 16:28:12 +090058 "ro.board.first_api_level(%d)\n"
59 % (board_api_level, grf_api_level))
60 return False
61
Justin Yun07ceaa72021-04-02 16:29:06 +090062 return True
Joe Onorato9197a482011-06-08 16:04:14 -070063
Jiyong Parkae556382020-05-20 18:33:43 +090064def validate(prop_list):
Ying Wang35123212014-02-11 20:44:09 -080065 """Validate the properties.
66
Jiyong Parkd721e872020-06-22 17:30:57 +090067 If the value of a sysprop exceeds the max limit (91), it's an error, unless
68 the sysprop is a read-only one.
69
70 Checks if there is no optional prop assignments.
71
Ying Wang35123212014-02-11 20:44:09 -080072 Returns:
73 True if nothing is wrong.
74 """
75 check_pass = True
Jiyong Parkd721e872020-06-22 17:30:57 +090076 for p in prop_list.get_all_props():
Jiyong Parkae556382020-05-20 18:33:43 +090077 if len(p.value) > PROP_VALUE_MAX and not p.name.startswith("ro."):
Ying Wang38df1012015-02-04 15:10:59 -080078 check_pass = False
79 sys.stderr.write("error: %s cannot exceed %d bytes: " %
Jiyong Parkae556382020-05-20 18:33:43 +090080 (p.name, PROP_VALUE_MAX))
81 sys.stderr.write("%s (%d)\n" % (p.value, len(p.value)))
Jiyong Parkd721e872020-06-22 17:30:57 +090082
83 if p.is_optional():
84 check_pass = False
85 sys.stderr.write("error: found unresolved optional prop assignment:\n")
86 sys.stderr.write(str(p) + "\n")
87
Ying Wang35123212014-02-11 20:44:09 -080088 return check_pass
89
Jiyong Park0b4fccb2020-06-26 17:38:00 +090090def override_optional_props(prop_list, allow_dup=False):
Jiyong Parkd721e872020-06-22 17:30:57 +090091 """Override a?=b with a=c, if the latter exists
92
93 Overriding is done by deleting a?=b
94 When there are a?=b and a?=c, then only the last one survives
95 When there are a=b and a=c, then it's an error.
96
97 Returns:
98 True if the override was successful
99 """
100 success = True
101 for name in prop_list.get_all_names():
102 props = prop_list.get_props(name)
103 optional_props = [p for p in props if p.is_optional()]
104 overriding_props = [p for p in props if not p.is_optional()]
105 if len(overriding_props) > 1:
106 # duplicated props are allowed when the all have the same value
107 if all(overriding_props[0].value == p.value for p in overriding_props):
Jiyong Park24d9cad2020-06-30 11:41:23 +0900108 for p in optional_props:
109 p.delete("overridden by %s" % str(overriding_props[0]))
Jiyong Parkd721e872020-06-22 17:30:57 +0900110 continue
Jiyong Park0b4fccb2020-06-26 17:38:00 +0900111 # or if dup is explicitly allowed for compat reason
112 if allow_dup:
113 # this could left one or more optional props unresolved.
114 # Convert them into non-optional because init doesn't understand ?=
115 # syntax
116 for p in optional_props:
117 p.optional = False
118 continue
119
Jiyong Parkd721e872020-06-22 17:30:57 +0900120 success = False
121 sys.stderr.write("error: found duplicate sysprop assignments:\n")
122 for p in overriding_props:
123 sys.stderr.write("%s\n" % str(p))
124 elif len(overriding_props) == 1:
125 for p in optional_props:
126 p.delete("overridden by %s" % str(overriding_props[0]))
127 else:
128 if len(optional_props) > 1:
129 for p in optional_props[:-1]:
130 p.delete("overridden by %s" % str(optional_props[-1]))
131 # Make the last optional one as non-optional
132 optional_props[-1].optional = False
133
134 return success
135
Jiyong Parkae556382020-05-20 18:33:43 +0900136class Prop:
Yu Liu115c66b2014-02-10 19:20:36 -0800137
Jiyong Parkd721e872020-06-22 17:30:57 +0900138 def __init__(self, name, value, optional=False, comment=None):
Jiyong Parkae556382020-05-20 18:33:43 +0900139 self.name = name.strip()
140 self.value = value.strip()
Jiyong Parkd721e872020-06-22 17:30:57 +0900141 if comment != None:
142 self.comments = [comment]
143 else:
144 self.comments = []
145 self.optional = optional
Ying Wang35123212014-02-11 20:44:09 -0800146
Jiyong Parkae556382020-05-20 18:33:43 +0900147 @staticmethod
148 def from_line(line):
149 line = line.rstrip('\n')
150 if line.startswith("#"):
Jiyong Parkd721e872020-06-22 17:30:57 +0900151 return Prop("", "", comment=line)
152 elif "?=" in line:
153 name, value = line.split("?=", 1)
154 return Prop(name, value, optional=True)
Jiyong Parkae556382020-05-20 18:33:43 +0900155 elif "=" in line:
156 name, value = line.split("=", 1)
Jiyong Parkd721e872020-06-22 17:30:57 +0900157 return Prop(name, value, optional=False)
Jiyong Parkae556382020-05-20 18:33:43 +0900158 else:
159 # don't fail on invalid line
160 # TODO(jiyong) make this a hard error
Jiyong Parkd721e872020-06-22 17:30:57 +0900161 return Prop("", "", comment=line)
Jiyong Parkae556382020-05-20 18:33:43 +0900162
163 def is_comment(self):
Jiyong Parkd721e872020-06-22 17:30:57 +0900164 return bool(self.comments and not self.name)
165
166 def is_optional(self):
167 return (not self.is_comment()) and self.optional
168
169 def make_as_comment(self):
170 # Prepend "#" to the last line which is the prop assignment
171 if not self.is_comment():
172 assignment = str(self).rsplit("\n", 1)[-1]
173 self.comments.append("#" + assignment)
174 self.name = ""
175 self.value = ""
176
177 def delete(self, reason):
178 self.comments.append("# Removed by post_process_props.py because " + reason)
179 self.make_as_comment()
Jiyong Parkae556382020-05-20 18:33:43 +0900180
181 def __str__(self):
Jiyong Parkd721e872020-06-22 17:30:57 +0900182 assignment = []
183 if not self.is_comment():
184 operator = "?=" if self.is_optional() else "="
185 assignment.append(self.name + operator + self.value)
186 return "\n".join(self.comments + assignment)
Jiyong Parkae556382020-05-20 18:33:43 +0900187
188class PropList:
189
190 def __init__(self, filename):
191 with open(filename) as f:
192 self.props = [Prop.from_line(l)
193 for l in f.readlines() if l.strip() != ""]
194
Jiyong Parkd721e872020-06-22 17:30:57 +0900195 def get_all_props(self):
Jiyong Parkae556382020-05-20 18:33:43 +0900196 return [p for p in self.props if not p.is_comment()]
Joe Onorato9197a482011-06-08 16:04:14 -0700197
Jiyong Parkd721e872020-06-22 17:30:57 +0900198 def get_all_names(self):
199 return set([p.name for p in self.get_all_props()])
200
201 def get_props(self, name):
202 return [p for p in self.get_all_props() if p.name == name]
203
204 def get_value(self, name):
205 # Caution: only the value of the first sysprop having the name is returned.
Jiyong Parkae556382020-05-20 18:33:43 +0900206 return next((p.value for p in self.props if p.name == name), "")
Joe Onorato9197a482011-06-08 16:04:14 -0700207
208 def put(self, name, value):
Jiyong Parkd721e872020-06-22 17:30:57 +0900209 # Note: when there is an optional prop for the name, its value isn't changed.
210 # Instead a new non-optional prop is appended, which will override the
211 # optional prop. Otherwise, the new value might be overridden by an existing
212 # non-optional prop of the same name.
213 index = next((i for i,p in enumerate(self.props)
214 if p.name == name and not p.is_optional()), -1)
Jiyong Parkae556382020-05-20 18:33:43 +0900215 if index == -1:
Jiyong Parkd721e872020-06-22 17:30:57 +0900216 self.props.append(Prop(name, value,
217 comment="# Auto-added by post_process_props.py"))
Jiyong Parkae556382020-05-20 18:33:43 +0900218 else:
Jiyong Parkd721e872020-06-22 17:30:57 +0900219 self.props[index].comments.append(
220 "# Value overridden by post_process_props.py. Original value: %s" %
221 self.props[index].value)
Jiyong Parkae556382020-05-20 18:33:43 +0900222 self.props[index].value = value
Joe Onorato9197a482011-06-08 16:04:14 -0700223
Jiyong Parkae556382020-05-20 18:33:43 +0900224 def write(self, filename):
225 with open(filename, 'w+') as f:
226 for p in self.props:
227 f.write(str(p) + "\n")
Joe Onorato9197a482011-06-08 16:04:14 -0700228
229def main(argv):
Jiyong Park0b4fccb2020-06-26 17:38:00 +0900230 parser = argparse.ArgumentParser(description="Post-process build.prop file")
231 parser.add_argument("--allow-dup", dest="allow_dup", action="store_true",
232 default=False)
233 parser.add_argument("filename")
234 parser.add_argument("disallowed_keys", metavar="KEY", type=str, nargs="*")
Justin Yun07ceaa72021-04-02 16:29:06 +0900235 parser.add_argument("--sdk-version", type=int, required=True)
Jiyong Park0b4fccb2020-06-26 17:38:00 +0900236 args = parser.parse_args()
Joe Onorato9197a482011-06-08 16:04:14 -0700237
Jiyong Park0b4fccb2020-06-26 17:38:00 +0900238 if not args.filename.endswith("/build.prop"):
Joe Onorato9197a482011-06-08 16:04:14 -0700239 sys.stderr.write("bad command line: " + str(argv) + "\n")
240 sys.exit(1)
241
Jiyong Park0b4fccb2020-06-26 17:38:00 +0900242 props = PropList(args.filename)
Jiyong Parkae556382020-05-20 18:33:43 +0900243 mangle_build_prop(props)
Jiyong Park0b4fccb2020-06-26 17:38:00 +0900244 if not override_optional_props(props, args.allow_dup):
Jiyong Parkd721e872020-06-22 17:30:57 +0900245 sys.exit(1)
Justin Yun23d52432023-11-10 16:31:04 +0900246 if not validate_grf_props(props):
Justin Yun07ceaa72021-04-02 16:29:06 +0900247 sys.exit(1)
Jiyong Parkae556382020-05-20 18:33:43 +0900248 if not validate(props):
Ying Wang35123212014-02-11 20:44:09 -0800249 sys.exit(1)
250
Jiyong Parkd721e872020-06-22 17:30:57 +0900251 # Drop any disallowed keys
Jiyong Park0b4fccb2020-06-26 17:38:00 +0900252 for key in args.disallowed_keys:
Jiyong Parkd721e872020-06-22 17:30:57 +0900253 for p in props.get_props(key):
254 p.delete("%s is a disallowed key" % key)
Jeff Sharkey26d22f72014-03-18 17:20:10 -0700255
Jiyong Park0b4fccb2020-06-26 17:38:00 +0900256 props.write(args.filename)
Joe Onorato9197a482011-06-08 16:04:14 -0700257
258if __name__ == "__main__":
259 main(sys.argv)