|
| 1 | +# Copyright (C) 2026 The Android Open Source Project |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +#!/usr/bin/env python3 |
| 16 | + |
| 17 | +import os |
| 18 | +import sys |
| 19 | +import zipfile |
| 20 | +import tarfile |
| 21 | +import json |
| 22 | +import argparse |
| 23 | + |
| 24 | +# Hardcoded list of library extensions to track |
| 25 | +LIB_EXTENSIONS = ('.a', '.jar', '.so') |
| 26 | + |
| 27 | +def get_archive_content(path): |
| 28 | + """ |
| 29 | + Extracts metadata for library files within a compressed archive. |
| 30 | + Returns a list of dicts with 'name' and 'size' (uncompressed). |
| 31 | + """ |
| 32 | + content = [] |
| 33 | + |
| 34 | + # .aar and .zip are both handled by zipfile |
| 35 | + if path.endswith(('.zip', '.aar')): |
| 36 | + with zipfile.ZipFile(path, 'r') as z: |
| 37 | + for info in z.infolist(): |
| 38 | + # info.is_dir() is available in Python 3.6+ |
| 39 | + if not info.is_dir() and info.filename.endswith(LIB_EXTENSIONS): |
| 40 | + content.append({ |
| 41 | + "name": info.filename, |
| 42 | + "size": info.file_size |
| 43 | + }) |
| 44 | + |
| 45 | + # .tgz or .tar.gz handled by tarfile |
| 46 | + elif path.endswith(('.tgz', '.tar.gz')): |
| 47 | + with tarfile.open(path, 'r:gz') as t: |
| 48 | + for member in t.getmembers(): |
| 49 | + if member.isfile() and member.name.endswith(LIB_EXTENSIONS): |
| 50 | + content.append({ |
| 51 | + "name": member.name, |
| 52 | + "size": member.size |
| 53 | + }) |
| 54 | + else: |
| 55 | + raise ValueError(f"Unsupported archive format: {path}") |
| 56 | + |
| 57 | + return content |
| 58 | + |
| 59 | +def main(): |
| 60 | + parser = argparse.ArgumentParser(description="Dump sizes of library files inside archives as JSON.") |
| 61 | + parser.add_argument('archives', nargs='+', help='List of compressed archives (.aar, .tgz, .zip)') |
| 62 | + args = parser.parse_args() |
| 63 | + |
| 64 | + results = [] |
| 65 | + |
| 66 | + for archive_path in args.archives: |
| 67 | + if not os.path.exists(archive_path): |
| 68 | + print(f"Error: File not found: {archive_path}", file=sys.stderr) |
| 69 | + sys.exit(1) |
| 70 | + |
| 71 | + try: |
| 72 | + archive_full_size = os.path.getsize(archive_path) |
| 73 | + inner_content = get_archive_content(archive_path) |
| 74 | + |
| 75 | + # Sort the content list alphabetically by name |
| 76 | + inner_content.sort(key=lambda x: x['name']) |
| 77 | + |
| 78 | + results.append({ |
| 79 | + "name": os.path.basename(archive_path), |
| 80 | + "size": archive_full_size, |
| 81 | + "content": inner_content |
| 82 | + }) |
| 83 | + except Exception as e: |
| 84 | + print(f"Error processing {archive_path}: {e}", file=sys.stderr) |
| 85 | + sys.exit(1) |
| 86 | + |
| 87 | + # Sort the top-level results by archive name |
| 88 | + results.sort(key=lambda x: x['name']) |
| 89 | + |
| 90 | + # Output JSON to stdout |
| 91 | + print(json.dumps(results, indent=2)) |
| 92 | + |
| 93 | +if __name__ == "__main__": |
| 94 | + main() |
0 commit comments