summaryrefslogtreecommitdiffstats
path: root/tools/dump-package-stats
blob: d11e72746102bfdbfaea4b3b53f8628055a9f1dd (plain)
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
#!/bin/bash
#
# Copyright (C) 2007 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.
#

PROGNAME=`basename $0`

function fail ()
{
    if [ ! -z "$@" ]
    then
        echo "$PROGNAME: ERROR: $@" >&2
    fi
    echo "$PROGNAME: ERROR: failed." >&2
    exit 1
}

function usage ()
{
    cat << HERE
usage: $PROGNAME <.jar/.apk-file-list>
    Dumps a summary of the compressed and uncompressed sizes of various
    types of files in each package.  Emits one line per package.
    Packages must be zipfiles, readable using "unzip".

    Example output line:

        filesize=642684 all=603288/919304 dex=119529/353815 name="out/App.apk"

    filesize: the size of the package on disk
    name: the name of the package as passed to $PROGNAME

    These fields are presented as <uncompressed bytes>/<compressed bytes>:

        all: the sum of all entries in the package
        dex: the sum of all "*.dex" entries in the package
HERE
    exit 1
}

if [ $# -lt 1 ]
then
    usage
fi

UNAME=`uname`
if [ "x$UNAME" = "xDarwin" ]
then
    statArgs="-f %z"
elif [ "x$UNAME" = "xLinux" ]
then
    statArgs="-c %s"
else
    fail "Unknown uname $UNAME"
fi

function printFileSize ()
{
    stat $statArgs $1
}

for file
do
    if [ ! -f "$file" ]
    then
        fail "$file doesn't exist or isn't a file"
    fi
    unzip -lv "$file" | awk '
        BEGIN {
          total_compressed = 0;
          total_uncompressed = 0;
          dex_compressed = 0;
          dex_uncompressed = 0;
        }

        # Make sure the output of unzip -lv looks like something we expect.
        #
        NR == "1" {
            if ($1 != "Archive:") {
                print "'$PROGNAME': ERROR: Unexpected zip listing format" > \
                        "/dev/stderr";
                print "'$PROGNAME': ERROR: Line 1 is \"" $0 "\"" > \
                        "/dev/stderr";
                failed = 1;
                exit 1;
            }
        }
        NR == "2" {
            if (NF != "8" ||
                $1 != "Length" ||
                $2 != "Method" ||
                $3 != "Size" ||
                ($4 != "Ratio" && $4 != "Cmpr") ||
                $5 != "Date" ||
                $6 != "Time" ||
                $7 != "CRC-32" ||
                $8 != "Name")
            {
                print "'$PROGNAME': ERROR: Unexpected zip listing format" > \
                        "/dev/stderr";
                print "'$PROGNAME': ERROR: Line 2 is \"" $0 "\"" > \
                        "/dev/stderr";
                failed = 1;
                exit 1;
            } else {
                saw_listing = 1;
            }
        }

        # Only look for lines where the ratio is the fourth column;
        # this filters out the header and footer.
        #
        $4 ~ /%$/ {
            uncompressed = $1;
            compressed = $3;
            if ($0 ~ /.dex$/) {
                dex_compressed += compressed;
                dex_uncompressed += uncompressed;
            }
            total_compressed += compressed;
            total_uncompressed += uncompressed;
        }
        { next }

        END {
            if (!failed && saw_listing) {
                print "filesize='$(printFileSize "$file")'",
                      "all=" total_compressed "/" total_uncompressed,
                      "dex=" dex_compressed "/" dex_uncompressed,
                      "name=\"'"$file"'\"";
            } else {
                exit 1;
            }
        }
    '
    if [ $? -ne 0 ]
    then
        fail "Could not get stats for $file"
    fi
done