blob: 32a90bc1a4215d06397bcf7e3c6f423a6a8163ca [file] [log] [blame]
Joe Onorato9197a482011-06-08 16:04:14 -07001#!/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
Brian Carlstromdad2ab42014-07-29 16:08:25 -070019# 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
Ying Wang35123212014-02-11 20:44:09 -080023PROP_VALUE_MAX = 91
24
Joe Onorato9197a482011-06-08 16:04:14 -070025# 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):
Ying Wang35123212014-02-11 20:44:09 -080028 pass
Joe Onorato9197a482011-06-08 16:04:14 -070029
Ying Wang35123212014-02-11 20:44:09 -080030# Put the modifications that you need to make into the /default.prop into this
Joe Onorato9197a482011-06-08 16:04:14 -070031# 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)
Joe Onorato8ad4bb12012-05-02 14:36:57 -070042 # 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");
Joe Onorato9197a482011-06-08 16:04:14 -070047
Ying Wang35123212014-02-11 20:44:09 -080048def 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.
Brian Carlstromdad2ab42014-07-29 16:08:25 -070060 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)))
Ying Wang35123212014-02-11 20:44:09 -080065 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
Joe Onorato9197a482011-06-08 16:04:14 -070081class PropFile:
Yu Liu115c66b2014-02-10 19:20:36 -080082
Joe Onorato9197a482011-06-08 16:04:14 -070083 def __init__(self, lines):
Ying Wang35123212014-02-11 20:44:09 -080084 self.lines = [s.strip() for s in lines]
85
86 def to_dict(self):
87 props = {}
Yu Liu115c66b2014-02-10 19:20:36 -080088 for line in self.lines:
Ying Wang35123212014-02-11 20:44:09 -080089 if not line or line.startswith("#"):
Yu Liu115c66b2014-02-10 19:20:36 -080090 continue
Ying Wang35123212014-02-11 20:44:09 -080091 key, value = line.split("=", 1)
92 props[key] = value
93 return props
Joe Onorato9197a482011-06-08 16:04:14 -070094
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)
Ying Wang35123212014-02-11 20:44:09 -0800121
Joe Onorato9197a482011-06-08 16:04:14 -0700122 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
Ying Wang35123212014-02-11 20:44:09 -0800130 if not validate(properties):
131 sys.exit(1)
132
Mike Lockwood5b65ee42011-06-08 19:06:43 -0700133 f = open(filename, 'w+')
134 properties.write(f)
Joe Onorato9197a482011-06-08 16:04:14 -0700135 f.close()
136
137if __name__ == "__main__":
138 main(sys.argv)