post_process_props.py revision dad2ab4df403125873940d50c80f2db8217b4916
1#!/usr/bin/env python
2#
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
19# See PROP_NAME_MAX and PROP_VALUE_MAX system_properties.h.
20# The constants in system_properties.h includes the termination NUL,
21# so we decrease the values by 1 here.
22PROP_NAME_MAX = 31
23PROP_VALUE_MAX = 91
24
25# Put the modifications that you need to make into the /system/build.prop into this
26# function. The prop object has get(name) and put(name,value) methods.
27def mangle_build_prop(prop):
28  pass
29
30# Put the modifications that you need to make into the /default.prop into this
31# function. The prop object has get(name) and put(name,value) methods.
32def mangle_default_prop(prop):
33  # If ro.debuggable is 1, then enable adb on USB by default
34  # (this is for userdebug builds)
35  if prop.get("ro.debuggable") == "1":
36    val = prop.get("persist.sys.usb.config")
37    if val == "":
38      val = "adb"
39    else:
40      val = val + ",adb"
41    prop.put("persist.sys.usb.config", val)
42  # UsbDeviceManager expects a value here.  If it doesn't get it, it will
43  # default to "adb". That might not the right policy there, but it's better
44  # to be explicit.
45  if not prop.get("persist.sys.usb.config"):
46    prop.put("persist.sys.usb.config", "none");
47
48def validate(prop):
49  """Validate the properties.
50
51  Returns:
52    True if nothing is wrong.
53  """
54  check_pass = True
55  buildprops = prop.to_dict()
56  dev_build = buildprops.get("ro.build.version.incremental",
57                             "").startswith("eng")
58  for key, value in buildprops.iteritems():
59    # Check build properties' length.
60    if len(key) > PROP_NAME_MAX:
61      check_pass = False
62      sys.stderr.write("error: %s cannot exceed %d bytes: " %
63                       (key, PROP_NAME_MAX))
64      sys.stderr.write("%s (%d)\n" % (key, len(key)))
65    if len(value) > PROP_VALUE_MAX:
66      # If dev build, show a warning message, otherwise fail the
67      # build with error message
68      if dev_build:
69        sys.stderr.write("warning: %s exceeds %d bytes: " %
70                         (key, PROP_VALUE_MAX))
71        sys.stderr.write("%s (%d)\n" % (value, len(value)))
72        sys.stderr.write("warning: This will cause the %s " % key)
73        sys.stderr.write("property return as empty at runtime\n")
74      else:
75        check_pass = False
76        sys.stderr.write("error: %s cannot exceed %d bytes: " %
77                         (key, PROP_VALUE_MAX))
78        sys.stderr.write("%s (%d)\n" % (value, len(value)))
79  return check_pass
80
81class PropFile:
82
83  def __init__(self, lines):
84    self.lines = [s.strip() for s in lines]
85
86  def to_dict(self):
87    props = {}
88    for line in self.lines:
89      if not line or line.startswith("#"):
90        continue
91      key, value = line.split("=", 1)
92      props[key] = value
93    return props
94
95  def get(self, name):
96    key = name + "="
97    for line in self.lines:
98      if line.startswith(key):
99        return line[len(key):]
100    return ""
101
102  def put(self, name, value):
103    key = name + "="
104    for i in range(0,len(self.lines)):
105      if self.lines[i].startswith(key):
106        self.lines[i] = key + value
107        return
108    self.lines.append(key + value)
109
110  def write(self, f):
111    f.write("\n".join(self.lines))
112    f.write("\n")
113
114def main(argv):
115  filename = argv[1]
116  f = open(filename)
117  lines = f.readlines()
118  f.close()
119
120  properties = PropFile(lines)
121
122  if filename.endswith("/build.prop"):
123    mangle_build_prop(properties)
124  elif filename.endswith("/default.prop"):
125    mangle_default_prop(properties)
126  else:
127    sys.stderr.write("bad command line: " + str(argv) + "\n")
128    sys.exit(1)
129
130  if not validate(properties):
131    sys.exit(1)
132
133  f = open(filename, 'w+')
134  properties.write(f)
135  f.close()
136
137if __name__ == "__main__":
138  main(sys.argv)
139