Refactor post_process_props.py

The script is refactored to not parse lines everytime a prop is looked
up or updated.

Also it is now built using python_binary_host.

Bug: 117892318
Test: m

Change-Id: I1677ca5aa919c54b7b0740fdc52e1399009f9a12
This commit is contained in:
Jiyong Park
2020-05-20 18:33:43 +09:00
parent 58dd353a07
commit ae55638f75
3 changed files with 81 additions and 58 deletions

View File

@@ -25,7 +25,7 @@ endif
BUILDINFO_SH := build/make/tools/buildinfo.sh BUILDINFO_SH := build/make/tools/buildinfo.sh
BUILDINFO_COMMON_SH := build/make/tools/buildinfo_common.sh BUILDINFO_COMMON_SH := build/make/tools/buildinfo_common.sh
POST_PROCESS_PROPS :=$= build/make/tools/post_process_props.py POST_PROCESS_PROPS := $(HOST_OUT_EXECUTABLES)/post_process_props$(HOST_EXECUTABLE_SUFFIX)
# Generates a set of sysprops common to all partitions to a file. # Generates a set of sysprops common to all partitions to a file.
# $(1): Partition name # $(1): Partition name

View File

@@ -24,3 +24,16 @@ python_binary_host {
}, },
}, },
} }
python_binary_host {
name: "post_process_props",
srcs: ["post_process_props.py"],
version: {
py2: {
enabled: false,
},
py3: {
enabled: true,
},
},
}

View File

@@ -1,4 +1,4 @@
#!/usr/bin/env python #!/usr/bin/env python3
# #
# Copyright (C) 2009 The Android Open Source Project # Copyright (C) 2009 The Android Open Source Project
# #
@@ -24,104 +24,114 @@ import sys
# so we decrease the value by 1 here. # so we decrease the value by 1 here.
PROP_VALUE_MAX = 91 PROP_VALUE_MAX = 91
# Put the modifications that you need to make into the /system/build.prop into this # Put the modifications that you need to make into the */build.prop into this
# function. The prop object has get(name) and put(name,value) methods. # function.
def mangle_build_prop(prop): def mangle_build_prop(prop_list):
# If ro.debuggable is 1, then enable adb on USB by default # If ro.debuggable is 1, then enable adb on USB by default
# (this is for userdebug builds) # (this is for userdebug builds)
if prop.get("ro.debuggable") == "1": if prop_list.get("ro.debuggable") == "1":
val = prop.get("persist.sys.usb.config") val = prop_list.get("persist.sys.usb.config")
if "adb" not in val: if "adb" not in val:
if val == "": if val == "":
val = "adb" val = "adb"
else: else:
val = val + ",adb" val = val + ",adb"
prop.put("persist.sys.usb.config", val) prop_list.put("persist.sys.usb.config", val)
# UsbDeviceManager expects a value here. If it doesn't get it, it will # UsbDeviceManager expects a value here. If it doesn't get it, it will
# default to "adb". That might not the right policy there, but it's better # default to "adb". That might not the right policy there, but it's better
# to be explicit. # to be explicit.
if not prop.get("persist.sys.usb.config"): if not prop_list.get("persist.sys.usb.config"):
prop.put("persist.sys.usb.config", "none"); prop_list.put("persist.sys.usb.config", "none");
def validate(prop): def validate(prop_list):
"""Validate the properties. """Validate the properties.
Returns: Returns:
True if nothing is wrong. True if nothing is wrong.
""" """
check_pass = True check_pass = True
buildprops = prop.to_dict() for p in prop_list.get_all():
for key, value in buildprops.iteritems(): if len(p.value) > PROP_VALUE_MAX and not p.name.startswith("ro."):
# Check build properties' length.
if len(value) > PROP_VALUE_MAX and not key.startswith("ro."):
check_pass = False check_pass = False
sys.stderr.write("error: %s cannot exceed %d bytes: " % sys.stderr.write("error: %s cannot exceed %d bytes: " %
(key, PROP_VALUE_MAX)) (p.name, PROP_VALUE_MAX))
sys.stderr.write("%s (%d)\n" % (value, len(value))) sys.stderr.write("%s (%d)\n" % (p.value, len(p.value)))
return check_pass return check_pass
class PropFile: class Prop:
def __init__(self, lines): def __init__(self, name, value, comment=None):
self.lines = [s.strip() for s in lines] self.name = name.strip()
self.value = value.strip()
self.comment = comment
def to_dict(self): @staticmethod
props = {} def from_line(line):
for line in self.lines: line = line.rstrip('\n')
if not line or line.startswith("#"): if line.startswith("#"):
continue return Prop("", "", line)
if "=" in line: elif "=" in line:
key, value = line.split("=", 1) name, value = line.split("=", 1)
props[key] = value return Prop(name, value)
return props else:
# don't fail on invalid line
# TODO(jiyong) make this a hard error
return Prop("", "", line)
def is_comment(self):
return self.comment != None
def __str__(self):
if self.is_comment():
return self.comment
else:
return self.name + "=" + self.value
class PropList:
def __init__(self, filename):
with open(filename) as f:
self.props = [Prop.from_line(l)
for l in f.readlines() if l.strip() != ""]
def get_all(self):
return [p for p in self.props if not p.is_comment()]
def get(self, name): def get(self, name):
key = name + "=" return next((p.value for p in self.props if p.name == name), "")
for line in self.lines:
if line.startswith(key):
return line[len(key):]
return ""
def put(self, name, value): def put(self, name, value):
key = name + "=" index = next((i for i,p in enumerate(self.props) if p.name == name), -1)
for i in range(0,len(self.lines)): if index == -1:
if self.lines[i].startswith(key): self.props.append(Prop(name, value))
self.lines[i] = key + value else:
return self.props[index].value = value
self.lines.append(key + value)
def delete(self, name): def delete(self, name):
key = name + "=" self.props = [p for p in self.props if p.name != name]
self.lines = [ line for line in self.lines if not line.startswith(key) ]
def write(self, f): def write(self, filename):
f.write("\n".join(self.lines)) with open(filename, 'w+') as f:
f.write("\n") for p in self.props:
f.write(str(p) + "\n")
def main(argv): def main(argv):
filename = argv[1] filename = argv[1]
f = open(filename)
lines = f.readlines()
f.close()
properties = PropFile(lines) if not filename.endswith("/build.prop"):
if filename.endswith("/build.prop"):
mangle_build_prop(properties)
else:
sys.stderr.write("bad command line: " + str(argv) + "\n") sys.stderr.write("bad command line: " + str(argv) + "\n")
sys.exit(1) sys.exit(1)
if not validate(properties): props = PropList(filename)
mangle_build_prop(props)
if not validate(props):
sys.exit(1) sys.exit(1)
# Drop any blacklisted keys # Drop any blacklisted keys
for key in argv[2:]: for key in argv[2:]:
properties.delete(key) props.delete(key)
f = open(filename, 'w+') props.write(filename)
properties.write(f)
f.close()
if __name__ == "__main__": if __name__ == "__main__":
main(sys.argv) main(sys.argv)