summaryrefslogtreecommitdiff
path: root/tools/post_process_props.py
blob: b2210416c3e2c2366adb67d05ac8f044ab718c6f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#!/usr/bin/env python3
#
# Copyright (C) 2009 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import sys

# Usage: post_process_props.py file.prop [blacklist_key, ...]
# Blacklisted keys are removed from the property file, if present

# See PROP_VALUE_MAX in system_properties.h.
# The constant in system_properties.h includes the terminating NUL,
# so we decrease the value by 1 here.
PROP_VALUE_MAX = 91

# 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_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_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_list.get("persist.sys.usb.config"):
    prop_list.put("persist.sys.usb.config", "none");

def validate(prop_list):
  """Validate the properties.

  Returns:
    True if nothing is wrong.
  """
  check_pass = True
  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: " %
                       (p.name, PROP_VALUE_MAX))
      sys.stderr.write("%s (%d)\n" % (p.value, len(p.value)))
  return check_pass

class Prop:

  def __init__(self, name, value, comment=None):
    self.name = name.strip()
    self.value = value.strip()
    self.comment = comment

  @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):
    return next((p.value for p in self.props if p.name == name), "")

  def put(self, name, 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):
    self.props = [p for p in self.props if p.name != name]

  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]

  if not filename.endswith("/build.prop"):
    sys.stderr.write("bad command line: " + str(argv) + "\n")
    sys.exit(1)

  props = PropList(filename)
  mangle_build_prop(props)
  if not validate(props):
    sys.exit(1)

  # Drop any blacklisted keys
  for key in argv[2:]:
    props.delete(key)

  props.write(filename)

if __name__ == "__main__":
  main(sys.argv)