2011-06-09 07:04:14 +08:00
|
|
|
#!/usr/bin/env python
|
|
|
|
#
|
|
|
|
# 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
|
|
|
|
|
2014-03-19 08:20:10 +08:00
|
|
|
# Usage: post_process_props.py file.prop [blacklist_key, ...]
|
|
|
|
# Blacklisted keys are removed from the property file, if present
|
|
|
|
|
2017-03-01 02:04:23 +08:00
|
|
|
# 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.
|
2014-02-12 12:44:09 +08:00
|
|
|
PROP_VALUE_MAX = 91
|
|
|
|
|
2011-06-09 07:04:14 +08:00
|
|
|
# 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):
|
2016-10-18 08:01:27 +08:00
|
|
|
# If ro.debuggable is 1, then enable adb on USB by default
|
|
|
|
# (this is for userdebug builds)
|
|
|
|
if prop.get("ro.debuggable") == "1":
|
|
|
|
val = prop.get("persist.sys.usb.config")
|
|
|
|
if "adb" not in val:
|
|
|
|
if val == "":
|
|
|
|
val = "adb"
|
|
|
|
else:
|
|
|
|
val = val + ",adb"
|
|
|
|
prop.put("persist.sys.usb.config", val)
|
2012-05-03 05:36:57 +08:00
|
|
|
# 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.get("persist.sys.usb.config"):
|
|
|
|
prop.put("persist.sys.usb.config", "none");
|
2011-06-09 07:04:14 +08:00
|
|
|
|
2014-02-12 12:44:09 +08:00
|
|
|
def validate(prop):
|
|
|
|
"""Validate the properties.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
True if nothing is wrong.
|
|
|
|
"""
|
|
|
|
check_pass = True
|
|
|
|
buildprops = prop.to_dict()
|
|
|
|
for key, value in buildprops.iteritems():
|
|
|
|
# Check build properties' length.
|
2017-10-13 00:20:14 +08:00
|
|
|
if len(value) > PROP_VALUE_MAX and not key.startswith("ro."):
|
2015-02-05 07:10:59 +08:00
|
|
|
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)))
|
2014-02-12 12:44:09 +08:00
|
|
|
return check_pass
|
2014-02-11 11:20:36 +08:00
|
|
|
|
2014-02-12 12:44:09 +08:00
|
|
|
class PropFile:
|
2014-02-11 11:20:36 +08:00
|
|
|
|
2011-06-09 07:04:14 +08:00
|
|
|
def __init__(self, lines):
|
2014-02-12 12:44:09 +08:00
|
|
|
self.lines = [s.strip() for s in lines]
|
|
|
|
|
|
|
|
def to_dict(self):
|
|
|
|
props = {}
|
2014-02-11 11:20:36 +08:00
|
|
|
for line in self.lines:
|
2014-02-12 12:44:09 +08:00
|
|
|
if not line or line.startswith("#"):
|
2014-02-11 11:20:36 +08:00
|
|
|
continue
|
2014-04-16 02:36:46 +08:00
|
|
|
if "=" in line:
|
|
|
|
key, value = line.split("=", 1)
|
|
|
|
props[key] = value
|
2014-02-12 12:44:09 +08:00
|
|
|
return props
|
2011-06-09 07:04:14 +08:00
|
|
|
|
|
|
|
def get(self, name):
|
|
|
|
key = name + "="
|
|
|
|
for line in self.lines:
|
|
|
|
if line.startswith(key):
|
|
|
|
return line[len(key):]
|
|
|
|
return ""
|
|
|
|
|
|
|
|
def put(self, name, value):
|
|
|
|
key = name + "="
|
|
|
|
for i in range(0,len(self.lines)):
|
|
|
|
if self.lines[i].startswith(key):
|
|
|
|
self.lines[i] = key + value
|
|
|
|
return
|
|
|
|
self.lines.append(key + value)
|
|
|
|
|
2014-03-19 08:20:10 +08:00
|
|
|
def delete(self, name):
|
|
|
|
key = name + "="
|
|
|
|
self.lines = [ line for line in self.lines if not line.startswith(key) ]
|
|
|
|
|
2011-06-09 07:04:14 +08:00
|
|
|
def write(self, f):
|
|
|
|
f.write("\n".join(self.lines))
|
|
|
|
f.write("\n")
|
|
|
|
|
|
|
|
def main(argv):
|
|
|
|
filename = argv[1]
|
|
|
|
f = open(filename)
|
|
|
|
lines = f.readlines()
|
|
|
|
f.close()
|
|
|
|
|
|
|
|
properties = PropFile(lines)
|
2014-02-12 12:44:09 +08:00
|
|
|
|
2011-06-09 07:04:14 +08:00
|
|
|
if filename.endswith("/build.prop"):
|
|
|
|
mangle_build_prop(properties)
|
|
|
|
else:
|
|
|
|
sys.stderr.write("bad command line: " + str(argv) + "\n")
|
|
|
|
sys.exit(1)
|
|
|
|
|
2014-02-12 12:44:09 +08:00
|
|
|
if not validate(properties):
|
|
|
|
sys.exit(1)
|
|
|
|
|
2014-03-19 08:20:10 +08:00
|
|
|
# Drop any blacklisted keys
|
|
|
|
for key in argv[2:]:
|
|
|
|
properties.delete(key)
|
|
|
|
|
2011-06-09 10:06:43 +08:00
|
|
|
f = open(filename, 'w+')
|
|
|
|
properties.write(f)
|
2011-06-09 07:04:14 +08:00
|
|
|
f.close()
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main(sys.argv)
|