1#!/bin/bash
2#
3# Copyright (C) 2007 The Android Open Source Project
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9#      http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16#
17
18PROGNAME=`basename $0`
19
20function fail ()
21{
22    if [ ! -z "$@" ]
23    then
24        echo "$PROGNAME: ERROR: $@" >&2
25    fi
26    echo "$PROGNAME: ERROR: failed." >&2
27    exit 1
28}
29
30function usage ()
31{
32    cat << HERE
33usage: $PROGNAME <.jar/.apk-file-list>
34    Dumps a summary of the compressed and uncompressed sizes of various
35    types of files in each package.  Emits one line per package.
36    Packages must be zipfiles, readable using "unzip".
37
38    Example output line:
39
40        filesize=642684 all=603288/919304 dex=119529/353815 name="out/App.apk"
41
42    filesize: the size of the package on disk
43    name: the name of the package as passed to $PROGNAME
44
45    These fields are presented as <uncompressed bytes>/<compressed bytes>:
46
47        all: the sum of all entries in the package
48        dex: the sum of all "*.dex" entries in the package
49HERE
50    exit 1
51}
52
53if [ $# -lt 1 ]
54then
55    usage
56fi
57
58UNAME=`uname`
59if [ "x$UNAME" = "xDarwin" ]
60then
61    statArgs="-f %z"
62elif [ "x$UNAME" = "xLinux" ]
63then
64    statArgs="-c %s"
65else
66    fail "Unknown uname $UNAME"
67fi
68
69function printFileSize ()
70{
71    stat $statArgs $1
72}
73
74for file
75do
76    if [ ! -f "$file" ]
77    then
78        fail "$file doesn't exist or isn't a file"
79    fi
80    unzip -lv "$file" | awk '
81        BEGIN {
82          total_compressed = 0;
83          total_uncompressed = 0;
84          dex_compressed = 0;
85          dex_uncompressed = 0;
86        }
87
88        # Make sure the output of unzip -lv looks like something we expect.
89        #
90        NR == "1" {
91            if ($1 != "Archive:") {
92                print "'$PROGNAME': ERROR: Unexpected zip listing format" > \
93                        "/dev/stderr";
94                print "'$PROGNAME': ERROR: Line 1 is \"" $0 "\"" > \
95                        "/dev/stderr";
96                failed = 1;
97                exit 1;
98            }
99        }
100        NR == "2" {
101            if (NF != "8" ||
102                $1 != "Length" ||
103                $2 != "Method" ||
104                $3 != "Size" ||
105                ($4 != "Ratio" && $4 != "Cmpr") ||
106                $5 != "Date" ||
107                $6 != "Time" ||
108                $7 != "CRC-32" ||
109                $8 != "Name")
110            {
111                print "'$PROGNAME': ERROR: Unexpected zip listing format" > \
112                        "/dev/stderr";
113                print "'$PROGNAME': ERROR: Line 2 is \"" $0 "\"" > \
114                        "/dev/stderr";
115                failed = 1;
116                exit 1;
117            } else {
118                saw_listing = 1;
119            }
120        }
121
122        # Only look for lines where the ratio is the fourth column;
123        # this filters out the header and footer.
124        #
125        $4 ~ /%$/ {
126            uncompressed = $1;
127            compressed = $3;
128            if ($0 ~ /.dex$/) {
129                dex_compressed += compressed;
130                dex_uncompressed += uncompressed;
131            }
132            total_compressed += compressed;
133            total_uncompressed += uncompressed;
134        }
135        { next }
136
137        END {
138            if (!failed && saw_listing) {
139                print "filesize='$(printFileSize "$file")'",
140                      "all=" total_compressed "/" total_uncompressed,
141                      "dex=" dex_compressed "/" dex_uncompressed,
142                      "name=\"'"$file"'\"";
143            } else {
144                exit 1;
145            }
146        }
147    '
148    if [ $? -ne 0 ]
149    then
150        fail "Could not get stats for $file"
151    fi
152done
153