Ed Tanous | 6762aea | 2019-01-24 11:06:09 -0800 | [diff] [blame] | 1 | #!/usr/bin/python3 |
| 2 | |
Michael Shepos | b84cc50 | 2019-01-20 21:09:05 -0600 | [diff] [blame] | 3 | import argparse |
Patrick Williams | a3db66b | 2022-12-04 16:27:08 -0600 | [diff] [blame] | 4 | import os |
Ed Tanous | d97f2f1 | 2022-02-03 19:29:34 -0800 | [diff] [blame] | 5 | import shutil |
Patrick Williams | a3db66b | 2022-12-04 16:27:08 -0600 | [diff] [blame] | 6 | import subprocess |
Ed Tanous | d97f2f1 | 2022-02-03 19:29:34 -0800 | [diff] [blame] | 7 | import sys |
Patrick Williams | a3db66b | 2022-12-04 16:27:08 -0600 | [diff] [blame] | 8 | import tempfile |
| 9 | from multiprocessing import Pool, cpu_count |
| 10 | from os.path import getsize |
Ed Tanous | 1ace1bd | 2018-11-01 12:15:57 -0700 | [diff] [blame] | 11 | |
Michael Shepos | b84cc50 | 2019-01-20 21:09:05 -0600 | [diff] [blame] | 12 | # Set command line arguments |
| 13 | parser = argparse.ArgumentParser( |
Patrick Williams | a3db66b | 2022-12-04 16:27:08 -0600 | [diff] [blame] | 14 | formatter_class=argparse.ArgumentDefaultsHelpFormatter |
| 15 | ) |
Ed Tanous | 1ace1bd | 2018-11-01 12:15:57 -0700 | [diff] [blame] | 16 | |
Patrick Williams | a3db66b | 2022-12-04 16:27:08 -0600 | [diff] [blame] | 17 | parser.add_argument( |
| 18 | "-b", |
| 19 | "--build_dir", |
| 20 | dest="BUILD_DIR", |
| 21 | default="/home/ed/openbmc-openbmc", |
| 22 | help="Build directory path.", |
| 23 | ) |
Michael Shepos | b84cc50 | 2019-01-20 21:09:05 -0600 | [diff] [blame] | 24 | |
Patrick Williams | a3db66b | 2022-12-04 16:27:08 -0600 | [diff] [blame] | 25 | parser.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 Shepos | b84cc50 | 2019-01-20 21:09:05 -0600 | [diff] [blame] | 33 | |
Patrick Williams | a3db66b | 2022-12-04 16:27:08 -0600 | [diff] [blame] | 34 | parser.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 Tanous | d97f2f1 | 2022-02-03 19:29:34 -0800 | [diff] [blame] | 42 | |
Michael Shepos | b84cc50 | 2019-01-20 21:09:05 -0600 | [diff] [blame] | 43 | args = parser.parse_args() |
| 44 | |
Ed Tanous | 1ace1bd | 2018-11-01 12:15:57 -0700 | [diff] [blame] | 45 | # files below this size wont be attempted |
| 46 | FILE_SIZE_LIMIT = 0 |
| 47 | |
Ed Tanous | d97f2f1 | 2022-02-03 19:29:34 -0800 | [diff] [blame] | 48 | SQUASHFS = args.SQUASHFS_FILE |
| 49 | if not os.path.isabs(args.SQUASHFS_FILE): |
| 50 | SQUASHFS = args.BUILD_DIR + args.SQUASHFS_FILE |
Ed Tanous | 1ace1bd | 2018-11-01 12:15:57 -0700 | [diff] [blame] | 51 | |
| 52 | original_size = getsize(SQUASHFS) |
Ed Tanous | b9cc276 | 2022-02-04 15:18:40 -0800 | [diff] [blame] | 53 | print("squashfs size: {}".format(original_size)) |
Ed Tanous | 1ace1bd | 2018-11-01 12:15:57 -0700 | [diff] [blame] | 54 | |
| 55 | results = [] |
| 56 | |
Ed Tanous | 1ace1bd | 2018-11-01 12:15:57 -0700 | [diff] [blame] | 57 | |
Ed Tanous | d97f2f1 | 2022-02-03 19:29:34 -0800 | [diff] [blame] | 58 | def get_unsquash_results(filepath): |
| 59 | with tempfile.TemporaryDirectory() as newsquashfsroot: |
| 60 | input_path = os.path.join(newsquashfsroot, "input") |
Patrick Williams | a3db66b | 2022-12-04 16:27:08 -0600 | [diff] [blame] | 61 | shutil.copytree( |
| 62 | squashfsdir, |
| 63 | input_path, |
| 64 | symlinks=True, |
| 65 | ignore_dangling_symlinks=True, |
| 66 | ) |
Ed Tanous | d97f2f1 | 2022-02-03 19:29:34 -0800 | [diff] [blame] | 67 | 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 Williams | a3db66b | 2022-12-04 16:27:08 -0600 | [diff] [blame] | 73 | [ |
| 74 | "mksquashfs", |
| 75 | input_path, |
| 76 | newsquashfsroot + "/test", |
| 77 | "-comp", |
| 78 | "xz", |
| 79 | "-processors", |
| 80 | "1", |
| 81 | ] |
| 82 | ) |
Ed Tanous | 1ace1bd | 2018-11-01 12:15:57 -0700 | [diff] [blame] | 83 | |
Patrick Williams | a3db66b | 2022-12-04 16:27:08 -0600 | [diff] [blame] | 84 | return ( |
| 85 | filepath.replace(squashfsdir, ""), |
| 86 | original_size - getsize(newsquashfsroot + "/test"), |
| 87 | ) |
Ed Tanous | 1ace1bd | 2018-11-01 12:15:57 -0700 | [diff] [blame] | 88 | |
Ed Tanous | 1ace1bd | 2018-11-01 12:15:57 -0700 | [diff] [blame] | 89 | |
Ed Tanous | d97f2f1 | 2022-02-03 19:29:34 -0800 | [diff] [blame] | 90 | with tempfile.TemporaryDirectory() as tempsquashfsdir: |
| 91 | print("writing to " + tempsquashfsdir) |
| 92 | squashfsdir = os.path.join(tempsquashfsdir, "squashfs-root") |
Patrick Williams | a3db66b | 2022-12-04 16:27:08 -0600 | [diff] [blame] | 93 | # squashfsdir = os.path.join("/tmp", "squashfs-root") |
Ed Tanous | d97f2f1 | 2022-02-03 19:29:34 -0800 | [diff] [blame] | 94 | command = ["unsquashfs", "-d", squashfsdir, SQUASHFS] |
| 95 | print(" ".join(command)) |
| 96 | subprocess.check_call(command) |
Michael Shepos | b84cc50 | 2019-01-20 21:09:05 -0600 | [diff] [blame] | 97 | |
Ed Tanous | d97f2f1 | 2022-02-03 19:29:34 -0800 | [diff] [blame] | 98 | 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 Williams | a3db66b | 2022-12-04 16:27:08 -0600 | [diff] [blame] | 105 | os.path.relpath(filepath, squashfsdir) |
| 106 | ) |
Ed Tanous | 1ace1bd | 2018-11-01 12:15:57 -0700 | [diff] [blame] | 107 | |
Ed Tanous | d97f2f1 | 2022-02-03 19:29:34 -0800 | [diff] [blame] | 108 | 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 Williams | a3db66b | 2022-12-04 16:27:08 -0600 | [diff] [blame] | 112 | for i, res in enumerate( |
| 113 | p.imap_unordered(get_unsquash_results, files_to_test) |
| 114 | ): |
Ed Tanous | d97f2f1 | 2022-02-03 19:29:34 -0800 | [diff] [blame] | 115 | results.append(res) |
Patrick Williams | a3db66b | 2022-12-04 16:27:08 -0600 | [diff] [blame] | 116 | sys.stderr.write( |
| 117 | "\rdone {:.1f}%".format(100 * (i / len(files_to_test))) |
| 118 | ) |
Ed Tanous | 1ace1bd | 2018-11-01 12:15:57 -0700 | [diff] [blame] | 119 | |
| 120 | results.sort(key=lambda x: x[1], reverse=True) |
| 121 | |
Patrick Williams | a3db66b | 2022-12-04 16:27:08 -0600 | [diff] [blame] | 122 | with open("results.txt", "w") as result_file: |
Ed Tanous | 1ace1bd | 2018-11-01 12:15:57 -0700 | [diff] [blame] | 123 | for filepath, size in results: |
| 124 | result = "{:>10}: {}".format(size, filepath) |
| 125 | print(result) |
| 126 | result_file.write(result + "\n") |