Ed Tanous | 6762aea | 2019-01-24 11:06:09 -0800 | [diff] [blame] | 1 | #!/usr/bin/python3 |
| 2 | |
Ed Tanous | 1ace1bd | 2018-11-01 12:15:57 -0700 | [diff] [blame] | 3 | import subprocess |
| 4 | import tempfile |
| 5 | import os |
| 6 | from os.path import join, getsize |
Michael Shepos | b84cc50 | 2019-01-20 21:09:05 -0600 | [diff] [blame] | 7 | import argparse |
Ed Tanous | d97f2f1 | 2022-02-03 19:29:34 -0800 | [diff] [blame] | 8 | from multiprocessing import Pool, cpu_count |
| 9 | import shutil |
| 10 | import sys |
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( |
Ed Tanous | b9cc276 | 2022-02-04 15:18:40 -0800 | [diff] [blame] | 14 | formatter_class=argparse.ArgumentDefaultsHelpFormatter) |
Ed Tanous | 1ace1bd | 2018-11-01 12:15:57 -0700 | [diff] [blame] | 15 | |
Michael Shepos | b84cc50 | 2019-01-20 21:09:05 -0600 | [diff] [blame] | 16 | parser.add_argument("-b", "--build_dir", |
| 17 | dest="BUILD_DIR", |
| 18 | default="/home/ed/openbmc-openbmc", |
| 19 | help="Build directory path.") |
| 20 | |
| 21 | parser.add_argument("-s", "--squashfs_file", |
| 22 | dest="SQUASHFS_FILE", |
| 23 | default="/build/tmp/deploy/images/wolfpass" + |
| 24 | "/intel-platforms-wolfpass.squashfs-xz", |
| 25 | help="Squashfs file.") |
| 26 | |
Ed Tanous | d97f2f1 | 2022-02-03 19:29:34 -0800 | [diff] [blame] | 27 | parser.add_argument("-t", "--threads", |
| 28 | dest="threads", |
| 29 | default=int(cpu_count()), |
| 30 | type=int, |
| 31 | help="Number of threads to use (defaults to cpu count)") |
| 32 | |
Michael Shepos | b84cc50 | 2019-01-20 21:09:05 -0600 | [diff] [blame] | 33 | args = parser.parse_args() |
| 34 | |
Ed Tanous | 1ace1bd | 2018-11-01 12:15:57 -0700 | [diff] [blame] | 35 | # files below this size wont be attempted |
| 36 | FILE_SIZE_LIMIT = 0 |
| 37 | |
Ed Tanous | d97f2f1 | 2022-02-03 19:29:34 -0800 | [diff] [blame] | 38 | SQUASHFS = args.SQUASHFS_FILE |
| 39 | if not os.path.isabs(args.SQUASHFS_FILE): |
| 40 | SQUASHFS = args.BUILD_DIR + args.SQUASHFS_FILE |
Ed Tanous | 1ace1bd | 2018-11-01 12:15:57 -0700 | [diff] [blame] | 41 | |
| 42 | original_size = getsize(SQUASHFS) |
Ed Tanous | b9cc276 | 2022-02-04 15:18:40 -0800 | [diff] [blame] | 43 | print("squashfs size: {}".format(original_size)) |
Ed Tanous | 1ace1bd | 2018-11-01 12:15:57 -0700 | [diff] [blame] | 44 | |
| 45 | results = [] |
| 46 | |
Ed Tanous | 1ace1bd | 2018-11-01 12:15:57 -0700 | [diff] [blame] | 47 | |
Ed Tanous | d97f2f1 | 2022-02-03 19:29:34 -0800 | [diff] [blame] | 48 | def get_unsquash_results(filepath): |
| 49 | with tempfile.TemporaryDirectory() as newsquashfsroot: |
| 50 | input_path = os.path.join(newsquashfsroot, "input") |
| 51 | shutil.copytree(squashfsdir, input_path, symlinks=True, |
| 52 | ignore_dangling_symlinks=True) |
| 53 | file_to_remove = os.path.join(input_path, filepath) |
| 54 | try: |
| 55 | os.remove(file_to_remove) |
| 56 | except IsADirectoryError: |
| 57 | shutil.rmtree(file_to_remove) |
| 58 | subprocess.check_output( |
| 59 | ["mksquashfs", input_path, |
| 60 | newsquashfsroot + "/test", "-comp", "xz", '-processors', '1']) |
Ed Tanous | 1ace1bd | 2018-11-01 12:15:57 -0700 | [diff] [blame] | 61 | |
Ed Tanous | d97f2f1 | 2022-02-03 19:29:34 -0800 | [diff] [blame] | 62 | return ((filepath.replace(squashfsdir, ""), |
| 63 | original_size - |
| 64 | getsize(newsquashfsroot + "/test"))) |
Ed Tanous | 1ace1bd | 2018-11-01 12:15:57 -0700 | [diff] [blame] | 65 | |
Ed Tanous | 1ace1bd | 2018-11-01 12:15:57 -0700 | [diff] [blame] | 66 | |
Ed Tanous | d97f2f1 | 2022-02-03 19:29:34 -0800 | [diff] [blame] | 67 | with tempfile.TemporaryDirectory() as tempsquashfsdir: |
| 68 | print("writing to " + tempsquashfsdir) |
| 69 | squashfsdir = os.path.join(tempsquashfsdir, "squashfs-root") |
| 70 | #squashfsdir = os.path.join("/tmp", "squashfs-root") |
| 71 | command = ["unsquashfs", "-d", squashfsdir, SQUASHFS] |
| 72 | print(" ".join(command)) |
| 73 | subprocess.check_call(command) |
Michael Shepos | b84cc50 | 2019-01-20 21:09:05 -0600 | [diff] [blame] | 74 | |
Ed Tanous | d97f2f1 | 2022-02-03 19:29:34 -0800 | [diff] [blame] | 75 | files_to_test = [] |
| 76 | for root, dirs, files in os.walk(squashfsdir): |
| 77 | for name in files + dirs: |
| 78 | filepath = os.path.join(root, name) |
| 79 | if not os.path.islink(filepath): |
| 80 | if getsize(filepath) > FILE_SIZE_LIMIT: |
| 81 | files_to_test.append( |
| 82 | os.path.relpath(filepath, squashfsdir)) |
Ed Tanous | 1ace1bd | 2018-11-01 12:15:57 -0700 | [diff] [blame] | 83 | |
Ed Tanous | d97f2f1 | 2022-02-03 19:29:34 -0800 | [diff] [blame] | 84 | print("{} files to attempt removing".format(len(files_to_test))) |
| 85 | |
| 86 | print("Using {} threads".format(args.threads)) |
| 87 | with Pool(args.threads) as p: |
| 88 | for i, res in enumerate(p.imap_unordered(get_unsquash_results, files_to_test)): |
| 89 | results.append(res) |
| 90 | sys.stderr.write('\rdone {:.1f}%'.format( |
| 91 | 100 * (i/len(files_to_test)))) |
Ed Tanous | 1ace1bd | 2018-11-01 12:15:57 -0700 | [diff] [blame] | 92 | |
| 93 | results.sort(key=lambda x: x[1], reverse=True) |
| 94 | |
| 95 | with open("results.txt", 'w') as result_file: |
| 96 | for filepath, size in results: |
| 97 | result = "{:>10}: {}".format(size, filepath) |
| 98 | print(result) |
| 99 | result_file.write(result + "\n") |