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