Apply pylint to conv_linker_config.py

1. Run pyformat scripts/conv_linker_config.py -s 4 --force_quote_type none -i to fix formatting
2. Annotate #pylint: disable=import-error for linker_config_pb2 since it
will be provided by soong

Test: m conv_linker_config
Test: pylint --rcfile tools/repohooks/tools/pylintrc
build/soong/scripts/conv_linker_config.py
Bug: 195738175

Change-Id: I791576cf65cb053f68c804f8ec5c2fc22976fdb4
This commit is contained in:
Spandan Das
2021-08-25 22:58:46 +00:00
parent 7c16dabfa5
commit 1d4bfd8ca4

View File

@@ -20,178 +20,181 @@ import collections
import json import json
import os import os
import linker_config_pb2 import linker_config_pb2 #pylint: disable=import-error
from google.protobuf.descriptor import FieldDescriptor from google.protobuf.descriptor import FieldDescriptor
from google.protobuf.json_format import ParseDict from google.protobuf.json_format import ParseDict
from google.protobuf.text_format import MessageToString from google.protobuf.text_format import MessageToString
def Proto(args): def Proto(args):
json_content = '' json_content = ''
with open(args.source) as f: with open(args.source) as f:
for line in f: for line in f:
if not line.lstrip().startswith('//'): if not line.lstrip().startswith('//'):
json_content += line json_content += line
obj = json.loads(json_content, object_pairs_hook=collections.OrderedDict) obj = json.loads(json_content, object_pairs_hook=collections.OrderedDict)
pb = ParseDict(obj, linker_config_pb2.LinkerConfig()) pb = ParseDict(obj, linker_config_pb2.LinkerConfig())
with open(args.output, 'wb') as f: with open(args.output, 'wb') as f:
f.write(pb.SerializeToString()) f.write(pb.SerializeToString())
def Print(args): def Print(args):
with open(args.source, 'rb') as f: with open(args.source, 'rb') as f:
pb = linker_config_pb2.LinkerConfig() pb = linker_config_pb2.LinkerConfig()
pb.ParseFromString(f.read()) pb.ParseFromString(f.read())
print(MessageToString(pb)) print(MessageToString(pb))
def SystemProvide(args): def SystemProvide(args):
pb = linker_config_pb2.LinkerConfig() pb = linker_config_pb2.LinkerConfig()
with open(args.source, 'rb') as f: with open(args.source, 'rb') as f:
pb.ParseFromString(f.read()) pb.ParseFromString(f.read())
libraries = args.value.split() libraries = args.value.split()
def IsInLibPath(lib_name): def IsInLibPath(lib_name):
lib_path = os.path.join(args.system, 'lib', lib_name) lib_path = os.path.join(args.system, 'lib', lib_name)
lib64_path = os.path.join(args.system, 'lib64', lib_name) lib64_path = os.path.join(args.system, 'lib64', lib_name)
return os.path.exists(lib_path) or os.path.islink(lib_path) or os.path.exists(lib64_path) or os.path.islink(lib64_path) return os.path.exists(lib_path) or os.path.islink(
lib_path) or os.path.exists(lib64_path) or os.path.islink(
lib64_path)
installed_libraries = list(filter(IsInLibPath, libraries)) installed_libraries = [lib for lib in libraries if IsInLibPath(lib)]
for item in installed_libraries: for item in installed_libraries:
if item not in getattr(pb, 'provideLibs'): if item not in getattr(pb, 'provideLibs'):
getattr(pb, 'provideLibs').append(item) getattr(pb, 'provideLibs').append(item)
with open(args.output, 'wb') as f: with open(args.output, 'wb') as f:
f.write(pb.SerializeToString()) f.write(pb.SerializeToString())
def Append(args): def Append(args):
pb = linker_config_pb2.LinkerConfig() pb = linker_config_pb2.LinkerConfig()
with open(args.source, 'rb') as f: with open(args.source, 'rb') as f:
pb.ParseFromString(f.read()) pb.ParseFromString(f.read())
if getattr(type(pb), args.key).DESCRIPTOR.label == FieldDescriptor.LABEL_REPEATED: if getattr(type(pb),
for value in args.value.split(): args.key).DESCRIPTOR.label == FieldDescriptor.LABEL_REPEATED:
getattr(pb, args.key).append(value) for value in args.value.split():
else: getattr(pb, args.key).append(value)
setattr(pb, args.key, args.value) else:
setattr(pb, args.key, args.value)
with open(args.output, 'wb') as f:
f.write(pb.SerializeToString())
with open(args.output, 'wb') as f:
f.write(pb.SerializeToString())
def Merge(args): def Merge(args):
pb = linker_config_pb2.LinkerConfig() pb = linker_config_pb2.LinkerConfig()
for other in args.input: for other in args.input:
with open(other, 'rb') as f: with open(other, 'rb') as f:
pb.MergeFromString(f.read()) pb.MergeFromString(f.read())
with open(args.out, 'wb') as f:
f.write(pb.SerializeToString())
with open(args.out, 'wb') as f:
f.write(pb.SerializeToString())
def GetArgParser(): def GetArgParser():
parser = argparse.ArgumentParser() parser = argparse.ArgumentParser()
subparsers = parser.add_subparsers() subparsers = parser.add_subparsers()
parser_proto = subparsers.add_parser( parser_proto = subparsers.add_parser(
'proto', help='Convert the input JSON configuration file into protobuf.') 'proto',
parser_proto.add_argument( help='Convert the input JSON configuration file into protobuf.')
'-s', parser_proto.add_argument(
'--source', '-s',
required=True, '--source',
type=str, required=True,
help='Source linker configuration file in JSON.') type=str,
parser_proto.add_argument( help='Source linker configuration file in JSON.')
'-o', parser_proto.add_argument(
'--output', '-o',
required=True, '--output',
type=str, required=True,
help='Target path to create protobuf file.') type=str,
parser_proto.set_defaults(func=Proto) help='Target path to create protobuf file.')
parser_proto.set_defaults(func=Proto)
print_proto = subparsers.add_parser( print_proto = subparsers.add_parser(
'print', help='Print configuration in human-readable text format.') 'print', help='Print configuration in human-readable text format.')
print_proto.add_argument( print_proto.add_argument(
'-s', '-s',
'--source', '--source',
required=True, required=True,
type=str, type=str,
help='Source linker configuration file in protobuf.') help='Source linker configuration file in protobuf.')
print_proto.set_defaults(func=Print) print_proto.set_defaults(func=Print)
system_provide_libs = subparsers.add_parser( system_provide_libs = subparsers.add_parser(
'systemprovide', help='Append system provide libraries into the configuration.') 'systemprovide',
system_provide_libs.add_argument( help='Append system provide libraries into the configuration.')
'-s', system_provide_libs.add_argument(
'--source', '-s',
required=True, '--source',
type=str, required=True,
help='Source linker configuration file in protobuf.') type=str,
system_provide_libs.add_argument( help='Source linker configuration file in protobuf.')
'-o', system_provide_libs.add_argument(
'--output', '-o',
required=True, '--output',
type=str, required=True,
help='Target linker configuration file to write in protobuf.') type=str,
system_provide_libs.add_argument( help='Target linker configuration file to write in protobuf.')
'--value', system_provide_libs.add_argument(
required=True, '--value',
type=str, required=True,
help='Values of the libraries to append. If there are more than one it should be separated by empty space') type=str,
system_provide_libs.add_argument( help='Values of the libraries to append. If there are more than one '
'--system', 'it should be separated by empty space'
required=True, )
type=str, system_provide_libs.add_argument(
help='Path of the system image.') '--system', required=True, type=str, help='Path of the system image.')
system_provide_libs.set_defaults(func=SystemProvide) system_provide_libs.set_defaults(func=SystemProvide)
append = subparsers.add_parser( append = subparsers.add_parser(
'append', help='Append value(s) to given key.') 'append', help='Append value(s) to given key.')
append.add_argument( append.add_argument(
'-s', '-s',
'--source', '--source',
required=True, required=True,
type=str, type=str,
help='Source linker configuration file in protobuf.') help='Source linker configuration file in protobuf.')
append.add_argument( append.add_argument(
'-o', '-o',
'--output', '--output',
required=True, required=True,
type=str, type=str,
help='Target linker configuration file to write in protobuf.') help='Target linker configuration file to write in protobuf.')
append.add_argument( append.add_argument('--key', required=True, type=str, help='.')
'--key', append.add_argument(
required=True, '--value',
type=str, required=True,
help='.') type=str,
append.add_argument( help='Values of the libraries to append. If there are more than one'
'--value', 'it should be separated by empty space'
required=True, )
type=str, append.set_defaults(func=Append)
help='Values of the libraries to append. If there are more than one it should be separated by empty space')
append.set_defaults(func=Append)
append = subparsers.add_parser( append = subparsers.add_parser('merge', help='Merge configurations')
'merge', help='Merge configurations') append.add_argument(
append.add_argument( '-o',
'-o', '--out',
'--out', required=True,
required=True, type=str,
type=str, help='Output linker configuration file to write in protobuf.')
help='Ouptut linker configuration file to write in protobuf.') append.add_argument(
append.add_argument( '-i',
'-i', '--input',
'--input', nargs='+',
nargs='+', type=str,
type=str, help='Linker configuration files to merge.')
help='Linker configuration files to merge.') append.set_defaults(func=Merge)
append.set_defaults(func=Merge)
return parser return parser
def main(): def main():
args = GetArgParser().parse_args() args = GetArgParser().parse_args()
args.func(args) args.func(args)
if __name__ == '__main__': if __name__ == '__main__':
main() main()