diff options
author | Chris Gross <chrisgross@google.com> | 2021-03-10 23:00:14 +0000 |
---|---|---|
committer | Gerrit Code Review <noreply-gerritcodereview@google.com> | 2021-03-10 23:00:14 +0000 |
commit | 5989878a55d2d34a1a8e5e16bb37349c4949758b (patch) | |
tree | 93d8e18c914c88f6555a10a863db4bf72d778ce5 /scripts/hiddenapi/merge_csv.py | |
parent | 31143d8dc32e15aeea395b1d467538d46027ce48 (diff) | |
parent | 622eac0b10f516bc59fe0d23db7f7cff52d44173 (diff) |
Merge changes from topic "SP1A.210222.001" into s-keystone-qcom-dev
* changes:
Fix the releax_check flag format verb.
Add non-fatal mode for verify_uses_libraries check.
Merge SP1A.210222.001
Diffstat (limited to 'scripts/hiddenapi/merge_csv.py')
-rwxr-xr-x | scripts/hiddenapi/merge_csv.py | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/scripts/hiddenapi/merge_csv.py b/scripts/hiddenapi/merge_csv.py new file mode 100755 index 000000000..5ad61b2f6 --- /dev/null +++ b/scripts/hiddenapi/merge_csv.py @@ -0,0 +1,66 @@ +#!/usr/bin/env python +# +# Copyright (C) 2018 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. +""" +Merge multiple CSV files, possibly with different columns. +""" + +import argparse +import csv +import io + +from zipfile import ZipFile + +args_parser = argparse.ArgumentParser(description='Merge given CSV files into a single one.') +args_parser.add_argument('--header', help='Comma separated field names; ' + 'if missing determines the header from input files.') +args_parser.add_argument('--zip_input', help='Treat files as ZIP archives containing CSV files to merge.', + action="store_true") +args_parser.add_argument('--output', help='Output file for merged CSV.', + default='-', type=argparse.FileType('w')) +args_parser.add_argument('files', nargs=argparse.REMAINDER) +args = args_parser.parse_args() + + +def dict_reader(input): + return csv.DictReader(input, delimiter=',', quotechar='|') + +csv_readers = [] +if not(args.zip_input): + for file in args.files: + csv_readers.append(dict_reader(open(file, 'r'))) +else: + for file in args.files: + with ZipFile(file) as zip: + for entry in zip.namelist(): + if entry.endswith('.uau'): + csv_readers.append(dict_reader(io.TextIOWrapper(zip.open(entry, 'r')))) + +headers = set() +if args.header: + fieldnames = args.header.split(',') +else: + # Build union of all columns from source files: + for reader in csv_readers: + headers = headers.union(reader.fieldnames) + fieldnames = sorted(headers) + +# Concatenate all files to output: +writer = csv.DictWriter(args.output, delimiter=',', quotechar='|', quoting=csv.QUOTE_MINIMAL, + dialect='unix', fieldnames=fieldnames) +writer.writeheader() +for reader in csv_readers: + for row in reader: + writer.writerow(row) |