blob: 7d6ef853e397486127a4e41295489925960f197c [file] [log] [blame]
Ed Tanous1ace1bd2018-11-01 12:15:57 -07001import subprocess
2import tempfile
3import os
4from os.path import join, getsize
Michael Sheposb84cc502019-01-20 21:09:05 -06005import argparse
Ed Tanous1ace1bd2018-11-01 12:15:57 -07006
Michael Sheposb84cc502019-01-20 21:09:05 -06007# Set command line arguments
8parser = argparse.ArgumentParser(
9 formatter_class=argparse.ArgumentDefaultsHelpFormatter)
Ed Tanous1ace1bd2018-11-01 12:15:57 -070010
Michael Sheposb84cc502019-01-20 21:09:05 -060011parser.add_argument("-b", "--build_dir",
12 dest="BUILD_DIR",
13 default="/home/ed/openbmc-openbmc",
14 help="Build directory path.")
15
16parser.add_argument("-s", "--squashfs_file",
17 dest="SQUASHFS_FILE",
18 default="/build/tmp/deploy/images/wolfpass" +
19 "/intel-platforms-wolfpass.squashfs-xz",
20 help="Squashfs file.")
21
22args = parser.parse_args()
23
Ed Tanous1ace1bd2018-11-01 12:15:57 -070024# files below this size wont be attempted
25FILE_SIZE_LIMIT = 0
26
Michael Sheposb84cc502019-01-20 21:09:05 -060027SQUASHFS = args.BUILD_DIR + args.SQUASHFS_FILE
Ed Tanous1ace1bd2018-11-01 12:15:57 -070028
29original_size = getsize(SQUASHFS)
Michael Sheposb84cc502019-01-20 21:09:05 -060030print ("squashfs size: {}".format(original_size))
Ed Tanous1ace1bd2018-11-01 12:15:57 -070031
32results = []
33
34with tempfile.TemporaryDirectory() as tempremovedfile:
35 with tempfile.TemporaryDirectory() as tempsquashfsdir:
36 print("writing to " + tempsquashfsdir)
37 command = ["unsquashfs", "-d",
38 os.path.join(tempsquashfsdir, "squashfs-root"), SQUASHFS]
39 print(" ".join(command))
40 subprocess.check_call(command)
41 squashfsdir = tempsquashfsdir + "/squashfs-root"
42
43 files_to_test = []
44 for root, dirs, files in os.walk(squashfsdir):
45 for name in files + dirs:
46 filepath = os.path.join(root, name)
47 if not os.path.islink(filepath):
48 # ensure files/dirs can be renamed
Michael Sheposb84cc502019-01-20 21:09:05 -060049 os.chmod(filepath, 0o711)
Ed Tanous1ace1bd2018-11-01 12:15:57 -070050 if getsize(filepath) > FILE_SIZE_LIMIT:
51 files_to_test.append(filepath)
52
53 print("{} files to attempt removing".format(len(files_to_test)))
54
55 for filepath in files_to_test:
56 name = os.path.basename(filepath)
57 newname = os.path.join(tempremovedfile, name)
58 os.rename(filepath, newname)
59 with tempfile.TemporaryDirectory() as newsquashfsroot:
60 subprocess.check_output(
Michael Sheposb84cc502019-01-20 21:09:05 -060061 ["mksquashfs", tempsquashfsdir,
62 newsquashfsroot + "/test", "-comp", "xz"])
Ed Tanous1ace1bd2018-11-01 12:15:57 -070063
64 results.append((filepath.replace(squashfsdir, ""),
Michael Sheposb84cc502019-01-20 21:09:05 -060065 original_size -
66 getsize(newsquashfsroot + "/test")))
67
Ed Tanous1ace1bd2018-11-01 12:15:57 -070068 os.rename(newname, filepath)
69
70 print("{:>6} of {}".format(len(results), len(files_to_test)))
71
72results.sort(key=lambda x: x[1], reverse=True)
73
74with open("results.txt", 'w') as result_file:
75 for filepath, size in results:
76 result = "{:>10}: {}".format(size, filepath)
77 print(result)
78 result_file.write(result + "\n")