1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
|
#!/usr/bin/env python3
# Copyright 2022 The Chromium Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
"""
Prints out du-style information about the files that will be deployed
for a given target
"""
import argparse
import os
import re
import sys
from typing import Iterable
from enum import Enum
def format_size(bytesize: float) -> str:
"""Convert bytes to human readable format.
Args:
bytesize: Number to humanize
Returns:
Size as string in human-readable format (e.g. 1.8MiB)
"""
if bytesize < 1024:
return f'{bytesize}B'
for suffix in 'BKMGTPEZY':
if bytesize < 1024:
break
bytesize /= 1024
return f'{bytesize:.1f}{suffix}iB' # pylint: disable=undefined-loop-variable
class FilesystemNode:
def __init__(self, path: str) -> None:
self.path = path
self.descendant_count = 0
try:
self.size = 0 if os.path.isdir(self.path) else os.path.getsize(self.path)
except FileNotFoundError:
print(f'{path} not found, please check that you have compiled '
'the target that generates this manifest.')
exit(1)
class Analysis(Enum):
FILE_COUNT = 'file_count'
SIZE = 'size'
def __str__(self):
return self.value
class SortOrder(Enum):
ASCENDING = 'ascending'
DESCENDING = 'descending'
def __str__(self):
return self.value
def compute_prefix_paths(path: str) -> Iterable[str]:
prefix = path.rpartition('/')[0]
while prefix:
yield prefix
prefix = prefix.rpartition('/')[0]
class ManifestAnalyzer:
def __init__(self) -> None:
self.path_map: dict[str, FilesystemNode] = dict()
def parse_manifest(self, manifest_path: str) -> None:
out_dir = re.match('out\/[^\/]+', manifest_path).group()
with open(manifest_path, 'r') as manifest:
for line in manifest:
relative_path = line.strip().partition('=')[2]
self.register_file(f'{out_dir}/{relative_path}')
def register_file(self, path: str) -> None:
if path in self.path_map:
return
leaf_node = FilesystemNode(path)
self.path_map[path] = leaf_node
for prefix in compute_prefix_paths(path):
if prefix in self.path_map:
parent_node = self.path_map[prefix]
else:
parent_node = FilesystemNode(prefix)
self.path_map[prefix] = parent_node
parent_node.descendant_count += 1
parent_node.size += leaf_node.size
def print_file_count(self, max_depth: int, sort_order: SortOrder) -> None:
sorted_nodes = sorted(self.path_map.values(),
key=lambda node: node.descendant_count,
reverse=sort_order == SortOrder.DESCENDING)
for node in sorted_nodes:
if node.descendant_count <= 0:
continue
depth = node.path.count('/')
if depth > max_depth:
continue
print(f'{node.descendant_count: >10}\t{node.path}')
def print_byte_size(self, max_depth: int, sort_order: SortOrder) -> None:
sorted_nodes = sorted(self.path_map.values(),
key=lambda node: node.size,
reverse=sort_order == SortOrder.DESCENDING)
for node in sorted_nodes:
depth = node.path.count('/')
if depth > max_depth:
continue
print(f'{format_size(node.size): >10}\t{node.path}')
def main():
parser = argparse.ArgumentParser(
description='Launches a long-running emulator that can '
'be re-used for multiple test runs.')
parser.add_argument(
'manifest_path',
type=str,
help='path to the .manifest '
'file. For example, the manifest for chrome/test:browser_tests can be '
'found at <out_dir>/gen/chrome/test/browser_tests/browser_tests.manifest')
parser.add_argument('--analysis',
type=Analysis,
choices=list(Analysis),
default=Analysis.SIZE,
help='which type of analysis to print')
parser.add_argument('--max-depth',
type=int,
default=sys.maxsize,
help='only print directories to the provided depth')
parser.add_argument(
'--sort-order',
type=SortOrder,
choices=list(SortOrder),
default=SortOrder.ASCENDING,
help='which order to use for sorting, defualts to ascending')
args = parser.parse_args()
analyzer = ManifestAnalyzer()
analyzer.parse_manifest(args.manifest_path)
if args.analysis == Analysis.FILE_COUNT:
analyzer.print_file_count(args.max_depth, args.sort_order)
else:
analyzer.print_byte_size(args.max_depth, args.sort_order)
if __name__ == '__main__':
main()
|