Home | History | Annotate | Download | only in tools
      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 
     18 PROGNAME=`basename $0`
     19 
     20 function 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 
     30 function usage ()
     31 {
     32     cat << HERE
     33 usage: $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
     49 HERE
     50     exit 1
     51 }
     52 
     53 if [ $# -lt 1 ]
     54 then
     55     usage
     56 fi
     57 
     58 UNAME=`uname`
     59 if [ "x$UNAME" = "xDarwin" ]
     60 then
     61     statArgs="-f %z"
     62 elif [ "x$UNAME" = "xLinux" ]
     63 then
     64     statArgs="-c %s"
     65 else
     66     fail "Unknown uname $UNAME"
     67 fi
     68 
     69 function printFileSize ()
     70 {
     71     stat $statArgs $1
     72 }
     73 
     74 for file
     75 do
     76     if [ ! -f "$file" ]
     77     then
     78         fail "$file doesn't exist or isn't a file"
     79     fi
     80     unzip -lvq "$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 (NF != "8" ||
     92                 $1 != "Length" ||
     93                 $2 != "Method" ||
     94                 $3 != "Size" ||
     95                 ($4 != "Ratio" && $4 != "Cmpr") ||
     96                 $5 != "Date" ||
     97                 $6 != "Time" ||
     98                 $7 != "CRC-32" ||
     99                 $8 != "Name")
    100             {
    101                 print "'$PROGNAME': ERROR: Unexpected zip listing format" > \
    102                         "/dev/stderr";
    103                 print "'$PROGNAME': ERROR: Line 2 is \"" $0 "\"" > \
    104                         "/dev/stderr";
    105                 failed = 1;
    106                 exit 1;
    107             } else {
    108                 saw_listing = 1;
    109             }
    110         }
    111 
    112         # Only look for lines where the ratio is the fourth column;
    113         # this filters out the header and footer.
    114         #
    115         $4 ~ /%$/ {
    116             uncompressed = $1;
    117             compressed = $3;
    118             if ($0 ~ /.dex$/) {
    119                 dex_compressed += compressed;
    120                 dex_uncompressed += uncompressed;
    121             }
    122             total_compressed += compressed;
    123             total_uncompressed += uncompressed;
    124         }
    125         { next }
    126 
    127         END {
    128             if (!failed && saw_listing) {
    129                 print "filesize='$(printFileSize "$file")'",
    130                       "all=" total_compressed "/" total_uncompressed,
    131                       "dex=" dex_compressed "/" dex_uncompressed,
    132                       "name=\"'"$file"'\"";
    133             } else {
    134                 exit 1;
    135             }
    136         }
    137     '
    138     if [ $? -ne 0 ]
    139     then
    140         fail "Could not get stats for $file"
    141     fi
    142 done
    143