Refactor a little bit and do the validation on also default.prop.

Change-Id: I76c2344d9033eadede7048689a879c9a69a05b7f
diff --git a/tools/post_process_props.py b/tools/post_process_props.py
index 32146e3..5d1b350 100755
--- a/tools/post_process_props.py
+++ b/tools/post_process_props.py
@@ -16,32 +16,17 @@
 
 import sys
 
+# See PROP_VALUE_MAX system_properties.h.
+# PROP_VALUE_MAX in system_properties.h includes the termination NUL,
+# so we decrease it 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):
-  buildprops=prop.buildprops
-  check_pass=True
-  for key in buildprops:
-    # Check build properties' length.
-    # Terminator(\0) added into the provided value of properties
-    # Total length (including terminator) will be no greater that PROP_VALUE_MAX(92).
-    if len(buildprops[key]) > 91:
-      # If dev build, show a warning message, otherwise fail the build with error message
-      if prop.get("ro.build.version.incremental").startswith("eng"):
-        sys.stderr.write("warning: " + key + " exceeds 91 symbols: ")
-        sys.stderr.write(buildprops[key])
-        sys.stderr.write("(" + str(len(buildprops[key])) + ") \n")
-        sys.stderr.write("warning: This will cause the " + key + " ")
-        sys.stderr.write("property return as empty at runtime\n")
-      else:
-        check_pass=False
-        sys.stderr.write("error: " + key + " cannot exceed 91 symbols: ")
-        sys.stderr.write(buildprops[key])
-        sys.stderr.write("(" + str(len(buildprops[key])) + ") \n")
-  if not check_pass:
-    sys.exit(1)
+  pass
 
-# 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 /default.prop into this
 # function. The prop object has get(name) and put(name,value) methods.
 def mangle_default_prop(prop):
   # If ro.debuggable is 1, then enable adb on USB by default
@@ -59,20 +44,47 @@
   if not prop.get("persist.sys.usb.config"):
     prop.put("persist.sys.usb.config", "none");
 
+def validate(prop):
+  """Validate the properties.
+
+  Returns:
+    True if nothing is wrong.
+  """
+  check_pass = True
+  buildprops = prop.to_dict()
+  dev_build = buildprops.get("ro.build.version.incremental",
+                             "").startswith("eng")
+  for key, value in buildprops.iteritems():
+    # Check build properties' length.
+    if len(value) > PROP_VALUE_MAX:
+      # If dev build, show a warning message, otherwise fail the
+      # build with error message
+      if dev_build:
+        sys.stderr.write("warning: %s exceeds %d bytes: " %
+                         (key, PROP_VALUE_MAX))
+        sys.stderr.write("%s (%d)\n" % (value, len(value)))
+        sys.stderr.write("warning: This will cause the %s " % key)
+        sys.stderr.write("property return as empty at runtime\n")
+      else:
+        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)))
+  return check_pass
+
 class PropFile:
 
-  buildprops={}
-
   def __init__(self, lines):
-    self.lines = [s[:-1] for s in lines]
+    self.lines = [s.strip() for s in lines]
+
+  def to_dict(self):
+    props = {}
     for line in self.lines:
-      line=line.strip()
-      if not line.strip() or line.startswith("#"):
+      if not line or line.startswith("#"):
         continue
-      index=line.find("=")
-      key=line[0:index]
-      value=line[index+1:]
-      self.buildprops[key]=value
+      key, value = line.split("=", 1)
+      props[key] = value
+    return props
 
   def get(self, name):
     key = name + "="
@@ -100,6 +112,7 @@
   f.close()
 
   properties = PropFile(lines)
+
   if filename.endswith("/build.prop"):
     mangle_build_prop(properties)
   elif filename.endswith("/default.prop"):
@@ -108,6 +121,9 @@
     sys.stderr.write("bad command line: " + str(argv) + "\n")
     sys.exit(1)
 
+  if not validate(properties):
+    sys.exit(1)
+
   f = open(filename, 'w+')
   properties.write(f)
   f.close()