blob: db2dd7b1431456a53adad1cb26fc2dbd906fd345 [file] [log] [blame]
Ed Tanous6762aea2019-01-24 11:06:09 -08001#!/usr/bin/python3
2
Michael Sheposb84cc502019-01-20 21:09:05 -06003import argparse
Patrick Williamsa3db66b2022-12-04 16:27:08 -06004import os
Ed Tanousd97f2f12022-02-03 19:29:34 -08005import shutil
Patrick Williamsa3db66b2022-12-04 16:27:08 -06006import subprocess
Ed Tanousd97f2f12022-02-03 19:29:34 -08007import sys
Patrick Williamsa3db66b2022-12-04 16:27:08 -06008import tempfile
9from multiprocessing import Pool, cpu_count
10from os.path import getsize
Ed Tanous1ace1bd2018-11-01 12:15:57 -070011
Michael Sheposb84cc502019-01-20 21:09:05 -060012# Set command line arguments
13parser = argparse.ArgumentParser(
Patrick Williamsa3db66b2022-12-04 16:27:08 -060014 formatter_class=argparse.ArgumentDefaultsHelpFormatter
15)
Ed Tanous1ace1bd2018-11-01 12:15:57 -070016
Patrick Williamsa3db66b2022-12-04 16:27:08 -060017parser.add_argument(
18 "-b",
19 "--build_dir",
20 dest="BUILD_DIR",
21 default="/home/ed/openbmc-openbmc",
22 help="Build directory path.",
23)
Michael Sheposb84cc502019-01-20 21:09:05 -060024
Patrick Williamsa3db66b2022-12-04 16:27:08 -060025parser.add_argument(
26 "-s",
27 "--squashfs_file",
28 dest="SQUASHFS_FILE",
29 default="/build/tmp/deploy/images/wolfpass"
30 + "/intel-platforms-wolfpass.squashfs-xz",
31 help="Squashfs file.",
32)
Michael Sheposb84cc502019-01-20 21:09:05 -060033
Patrick Williamsa3db66b2022-12-04 16:27:08 -060034parser.add_argument(
35 "-t",
36 "--threads",
37 dest="threads",
38 default=int(cpu_count()),
39 type=int,
40 help="Number of threads to use (defaults to cpu count)",
41)
Ed Tanousd97f2f12022-02-03 19:29:34 -080042
Michael Sheposb84cc502019-01-20 21:09:05 -060043args = parser.parse_args()
44
Ed Tanous1ace1bd2018-11-01 12:15:57 -070045# files below this size wont be attempted
46FILE_SIZE_LIMIT = 0
47
Ed Tanousd97f2f12022-02-03 19:29:34 -080048SQUASHFS = args.SQUASHFS_FILE
49if not os.path.isabs(args.SQUASHFS_FILE):
50 SQUASHFS = args.BUILD_DIR + args.SQUASHFS_FILE
Ed Tanous1ace1bd2018-11-01 12:15:57 -070051
52original_size = getsize(SQUASHFS)
Ed Tanousb9cc2762022-02-04 15:18:40 -080053print("squashfs size: {}".format(original_size))
Ed Tanous1ace1bd2018-11-01 12:15:57 -070054
55results = []
56
Ed Tanous1ace1bd2018-11-01 12:15:57 -070057
Ed Tanousd97f2f12022-02-03 19:29:34 -080058def get_unsquash_results(filepath):
59 with tempfile.TemporaryDirectory() as newsquashfsroot:
60 input_path = os.path.join(newsquashfsroot, "input")
Patrick Williamsa3db66b2022-12-04 16:27:08 -060061 shutil.copytree(
62 squashfsdir,
63 input_path,
64 symlinks=True,
65 ignore_dangling_symlinks=True,
66 )
Ed Tanousd97f2f12022-02-03 19:29:34 -080067 file_to_remove = os.path.join(input_path, filepath)
68 try:
69 os.remove(file_to_remove)
70 except IsADirectoryError:
71 shutil.rmtree(file_to_remove)
72 subprocess.check_output(
Patrick Williamsa3db66b2022-12-04 16:27:08 -060073 [
74 "mksquashfs",
75 input_path,
76 newsquashfsroot + "/test",
77 "-comp",
78 "xz",
79 "-processors",
80 "1",
81 ]
82 )
Ed Tanous1ace1bd2018-11-01 12:15:57 -070083
Patrick Williamsa3db66b2022-12-04 16:27:08 -060084 return (
85 filepath.replace(squashfsdir, ""),
86 original_size - getsize(newsquashfsroot + "/test"),
87 )
Ed Tanous1ace1bd2018-11-01 12:15:57 -070088
Ed Tanous1ace1bd2018-11-01 12:15:57 -070089
Ed Tanousd97f2f12022-02-03 19:29:34 -080090with tempfile.TemporaryDirectory() as tempsquashfsdir:
91 print("writing to " + tempsquashfsdir)
92 squashfsdir = os.path.join(tempsquashfsdir, "squashfs-root")
Patrick Williamsa3db66b2022-12-04 16:27:08 -060093 # squashfsdir = os.path.join("/tmp", "squashfs-root")
Ed Tanousd97f2f12022-02-03 19:29:34 -080094 command = ["unsquashfs", "-d", squashfsdir, SQUASHFS]
95 print(" ".join(command))
96 subprocess.check_call(command)
Michael Sheposb84cc502019-01-20 21:09:05 -060097
Ed Tanousd97f2f12022-02-03 19:29:34 -080098 files_to_test = []
99 for root, dirs, files in os.walk(squashfsdir):
100 for name in files + dirs:
101 filepath = os.path.join(root, name)
102 if not os.path.islink(filepath):
103 if getsize(filepath) > FILE_SIZE_LIMIT:
104 files_to_test.append(
Patrick Williamsa3db66b2022-12-04 16:27:08 -0600105 os.path.relpath(filepath, squashfsdir)
106 )
Ed Tanous1ace1bd2018-11-01 12:15:57 -0700107
Ed Tanousd97f2f12022-02-03 19:29:34 -0800108 print("{} files to attempt removing".format(len(files_to_test)))
109
110 print("Using {} threads".format(args.threads))
111 with Pool(args.threads) as p:
Patrick Williamsa3db66b2022-12-04 16:27:08 -0600112 for i, res in enumerate(
113 p.imap_unordered(get_unsquash_results, files_to_test)
114 ):
Ed Tanousd97f2f12022-02-03 19:29:34 -0800115 results.append(res)
Patrick Williamsa3db66b2022-12-04 16:27:08 -0600116 sys.stderr.write(
117 "\rdone {:.1f}%".format(100 * (i / len(files_to_test)))
118 )
Ed Tanous1ace1bd2018-11-01 12:15:57 -0700119
120results.sort(key=lambda x: x[1], reverse=True)
121
Patrick Williamsa3db66b2022-12-04 16:27:08 -0600122with open("results.txt", "w") as result_file:
Ed Tanous1ace1bd2018-11-01 12:15:57 -0700123 for filepath, size in results:
124 result = "{:>10}: {}".format(size, filepath)
125 print(result)
126 result_file.write(result + "\n")