blob: cfacb02063e5bf4f08dcbdd3201ae4af9f0c543c [file] [log] [blame]
Ed Tanous6762aea2019-01-24 11:06:09 -08001#!/usr/bin/python3
2
Ed Tanous1ace1bd2018-11-01 12:15:57 -07003import subprocess
4import tempfile
5import os
6from os.path import join, getsize
Michael Sheposb84cc502019-01-20 21:09:05 -06007import argparse
Ed Tanous1ace1bd2018-11-01 12:15:57 -07008
Michael Sheposb84cc502019-01-20 21:09:05 -06009# Set command line arguments
10parser = argparse.ArgumentParser(
Ed Tanousb9cc2762022-02-04 15:18:40 -080011 formatter_class=argparse.ArgumentDefaultsHelpFormatter)
Ed Tanous1ace1bd2018-11-01 12:15:57 -070012
Michael Sheposb84cc502019-01-20 21:09:05 -060013parser.add_argument("-b", "--build_dir",
14 dest="BUILD_DIR",
15 default="/home/ed/openbmc-openbmc",
16 help="Build directory path.")
17
18parser.add_argument("-s", "--squashfs_file",
19 dest="SQUASHFS_FILE",
20 default="/build/tmp/deploy/images/wolfpass" +
21 "/intel-platforms-wolfpass.squashfs-xz",
22 help="Squashfs file.")
23
24args = parser.parse_args()
25
Ed Tanous1ace1bd2018-11-01 12:15:57 -070026# files below this size wont be attempted
27FILE_SIZE_LIMIT = 0
28
Michael Sheposb84cc502019-01-20 21:09:05 -060029SQUASHFS = args.BUILD_DIR + args.SQUASHFS_FILE
Ed Tanous1ace1bd2018-11-01 12:15:57 -070030
31original_size = getsize(SQUASHFS)
Ed Tanousb9cc2762022-02-04 15:18:40 -080032print("squashfs size: {}".format(original_size))
Ed Tanous1ace1bd2018-11-01 12:15:57 -070033
34results = []
35
36with tempfile.TemporaryDirectory() as tempremovedfile:
37 with tempfile.TemporaryDirectory() as tempsquashfsdir:
38 print("writing to " + tempsquashfsdir)
39 command = ["unsquashfs", "-d",
40 os.path.join(tempsquashfsdir, "squashfs-root"), SQUASHFS]
41 print(" ".join(command))
42 subprocess.check_call(command)
43 squashfsdir = tempsquashfsdir + "/squashfs-root"
44
45 files_to_test = []
46 for root, dirs, files in os.walk(squashfsdir):
47 for name in files + dirs:
48 filepath = os.path.join(root, name)
49 if not os.path.islink(filepath):
50 # ensure files/dirs can be renamed
Michael Sheposb84cc502019-01-20 21:09:05 -060051 os.chmod(filepath, 0o711)
Ed Tanous1ace1bd2018-11-01 12:15:57 -070052 if getsize(filepath) > FILE_SIZE_LIMIT:
53 files_to_test.append(filepath)
54
55 print("{} files to attempt removing".format(len(files_to_test)))
56
57 for filepath in files_to_test:
58 name = os.path.basename(filepath)
59 newname = os.path.join(tempremovedfile, name)
60 os.rename(filepath, newname)
61 with tempfile.TemporaryDirectory() as newsquashfsroot:
62 subprocess.check_output(
Michael Sheposb84cc502019-01-20 21:09:05 -060063 ["mksquashfs", tempsquashfsdir,
64 newsquashfsroot + "/test", "-comp", "xz"])
Ed Tanous1ace1bd2018-11-01 12:15:57 -070065
66 results.append((filepath.replace(squashfsdir, ""),
Michael Sheposb84cc502019-01-20 21:09:05 -060067 original_size -
68 getsize(newsquashfsroot + "/test")))
69
Ed Tanous1ace1bd2018-11-01 12:15:57 -070070 os.rename(newname, filepath)
71
72 print("{:>6} of {}".format(len(results), len(files_to_test)))
73
74results.sort(key=lambda x: x[1], reverse=True)
75
76with open("results.txt", 'w') as result_file:
77 for filepath, size in results:
78 result = "{:>10}: {}".format(size, filepath)
79 print(result)
80 result_file.write(result + "\n")