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 | 1ace1bd | 2018-11-01 12:15:57 -0700 | [diff] [blame] | 8 | |
Michael Shepos | b84cc50 | 2019-01-20 21:09:05 -0600 | [diff] [blame] | 9 | # Set command line arguments |
| 10 | parser = argparse.ArgumentParser( |
| 11 | formatter_class=argparse.ArgumentDefaultsHelpFormatter) |
Ed Tanous | 1ace1bd | 2018-11-01 12:15:57 -0700 | [diff] [blame] | 12 | |
Michael Shepos | b84cc50 | 2019-01-20 21:09:05 -0600 | [diff] [blame] | 13 | parser.add_argument("-b", "--build_dir", |
| 14 | dest="BUILD_DIR", |
| 15 | default="/home/ed/openbmc-openbmc", |
| 16 | help="Build directory path.") |
| 17 | |
| 18 | parser.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 | |
| 24 | args = parser.parse_args() |
| 25 | |
Ed Tanous | 1ace1bd | 2018-11-01 12:15:57 -0700 | [diff] [blame] | 26 | # files below this size wont be attempted |
| 27 | FILE_SIZE_LIMIT = 0 |
| 28 | |
Michael Shepos | b84cc50 | 2019-01-20 21:09:05 -0600 | [diff] [blame] | 29 | SQUASHFS = args.BUILD_DIR + args.SQUASHFS_FILE |
Ed Tanous | 1ace1bd | 2018-11-01 12:15:57 -0700 | [diff] [blame] | 30 | |
| 31 | original_size = getsize(SQUASHFS) |
Michael Shepos | b84cc50 | 2019-01-20 21:09:05 -0600 | [diff] [blame] | 32 | print ("squashfs size: {}".format(original_size)) |
Ed Tanous | 1ace1bd | 2018-11-01 12:15:57 -0700 | [diff] [blame] | 33 | |
| 34 | results = [] |
| 35 | |
| 36 | with 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 Shepos | b84cc50 | 2019-01-20 21:09:05 -0600 | [diff] [blame] | 51 | os.chmod(filepath, 0o711) |
Ed Tanous | 1ace1bd | 2018-11-01 12:15:57 -0700 | [diff] [blame] | 52 | 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 Shepos | b84cc50 | 2019-01-20 21:09:05 -0600 | [diff] [blame] | 63 | ["mksquashfs", tempsquashfsdir, |
| 64 | newsquashfsroot + "/test", "-comp", "xz"]) |
Ed Tanous | 1ace1bd | 2018-11-01 12:15:57 -0700 | [diff] [blame] | 65 | |
| 66 | results.append((filepath.replace(squashfsdir, ""), |
Michael Shepos | b84cc50 | 2019-01-20 21:09:05 -0600 | [diff] [blame] | 67 | original_size - |
| 68 | getsize(newsquashfsroot + "/test"))) |
| 69 | |
Ed Tanous | 1ace1bd | 2018-11-01 12:15:57 -0700 | [diff] [blame] | 70 | os.rename(newname, filepath) |
| 71 | |
| 72 | print("{:>6} of {}".format(len(results), len(files_to_test))) |
| 73 | |
| 74 | results.sort(key=lambda x: x[1], reverse=True) |
| 75 | |
| 76 | with 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") |