1from __future__ import print_function, division
2import os
3import sys
4
5# Internal build script for generating debugging test .so size.
6# Usage:
7#     python libsize.py file.so save.txt -- displays the size of file.so and, if save.txt exists, compares it to the
8#                                           size in it, then overwrites save.txt with the new size for future runs.
9
10if len(sys.argv) != 3:
11    sys.exit("Invalid arguments: usage: python libsize.py file.so save.txt")
12
13lib = sys.argv[1]
14save = sys.argv[2]
15
16if not os.path.exists(lib):
17    sys.exit("Error: requested file ({}) does not exist".format(lib))
18
19libsize = os.path.getsize(lib)
20
21print("------", os.path.basename(lib), "file size:", libsize, end='')
22
23if os.path.exists(save):
24    with open(save) as sf:
25        oldsize = int(sf.readline())
26
27    if oldsize > 0:
28        change = libsize - oldsize
29        if change == 0:
30            print(" (no change)")
31        else:
32            print(" (change of {:+} bytes = {:+.2%})".format(change, change / oldsize))
33else:
34    print()
35
36with open(save, 'w') as sf:
37    sf.write(str(libsize))
38
39