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