| Ed Tanous | 1ace1bd | 2018-11-01 12:15:57 -0700 | [diff] [blame] | 1 | import subprocess | 
|  | 2 | import tempfile | 
|  | 3 | import os | 
|  | 4 | from os.path import join, getsize | 
| Michael Shepos | b84cc50 | 2019-01-20 21:09:05 -0600 | [diff] [blame^] | 5 | import argparse | 
| Ed Tanous | 1ace1bd | 2018-11-01 12:15:57 -0700 | [diff] [blame] | 6 |  | 
| Michael Shepos | b84cc50 | 2019-01-20 21:09:05 -0600 | [diff] [blame^] | 7 | # Set command line arguments | 
|  | 8 | parser = argparse.ArgumentParser( | 
|  | 9 | formatter_class=argparse.ArgumentDefaultsHelpFormatter) | 
| Ed Tanous | 1ace1bd | 2018-11-01 12:15:57 -0700 | [diff] [blame] | 10 |  | 
| Michael Shepos | b84cc50 | 2019-01-20 21:09:05 -0600 | [diff] [blame^] | 11 | parser.add_argument("-b", "--build_dir", | 
|  | 12 | dest="BUILD_DIR", | 
|  | 13 | default="/home/ed/openbmc-openbmc", | 
|  | 14 | help="Build directory path.") | 
|  | 15 |  | 
|  | 16 | parser.add_argument("-s", "--squashfs_file", | 
|  | 17 | dest="SQUASHFS_FILE", | 
|  | 18 | default="/build/tmp/deploy/images/wolfpass" + | 
|  | 19 | "/intel-platforms-wolfpass.squashfs-xz", | 
|  | 20 | help="Squashfs file.") | 
|  | 21 |  | 
|  | 22 | args = parser.parse_args() | 
|  | 23 |  | 
| Ed Tanous | 1ace1bd | 2018-11-01 12:15:57 -0700 | [diff] [blame] | 24 | # files below this size wont be attempted | 
|  | 25 | FILE_SIZE_LIMIT = 0 | 
|  | 26 |  | 
| Michael Shepos | b84cc50 | 2019-01-20 21:09:05 -0600 | [diff] [blame^] | 27 | SQUASHFS = args.BUILD_DIR + args.SQUASHFS_FILE | 
| Ed Tanous | 1ace1bd | 2018-11-01 12:15:57 -0700 | [diff] [blame] | 28 |  | 
|  | 29 | original_size = getsize(SQUASHFS) | 
| Michael Shepos | b84cc50 | 2019-01-20 21:09:05 -0600 | [diff] [blame^] | 30 | print ("squashfs size: {}".format(original_size)) | 
| Ed Tanous | 1ace1bd | 2018-11-01 12:15:57 -0700 | [diff] [blame] | 31 |  | 
|  | 32 | results = [] | 
|  | 33 |  | 
|  | 34 | with tempfile.TemporaryDirectory() as tempremovedfile: | 
|  | 35 | with tempfile.TemporaryDirectory() as tempsquashfsdir: | 
|  | 36 | print("writing to " + tempsquashfsdir) | 
|  | 37 | command = ["unsquashfs", "-d", | 
|  | 38 | os.path.join(tempsquashfsdir, "squashfs-root"),  SQUASHFS] | 
|  | 39 | print(" ".join(command)) | 
|  | 40 | subprocess.check_call(command) | 
|  | 41 | squashfsdir = tempsquashfsdir + "/squashfs-root" | 
|  | 42 |  | 
|  | 43 | files_to_test = [] | 
|  | 44 | for root, dirs, files in os.walk(squashfsdir): | 
|  | 45 | for name in files + dirs: | 
|  | 46 | filepath = os.path.join(root, name) | 
|  | 47 | if not os.path.islink(filepath): | 
|  | 48 | # ensure files/dirs can be renamed | 
| Michael Shepos | b84cc50 | 2019-01-20 21:09:05 -0600 | [diff] [blame^] | 49 | os.chmod(filepath, 0o711) | 
| Ed Tanous | 1ace1bd | 2018-11-01 12:15:57 -0700 | [diff] [blame] | 50 | if getsize(filepath) > FILE_SIZE_LIMIT: | 
|  | 51 | files_to_test.append(filepath) | 
|  | 52 |  | 
|  | 53 | print("{} files to attempt removing".format(len(files_to_test))) | 
|  | 54 |  | 
|  | 55 | for filepath in files_to_test: | 
|  | 56 | name = os.path.basename(filepath) | 
|  | 57 | newname = os.path.join(tempremovedfile, name) | 
|  | 58 | os.rename(filepath, newname) | 
|  | 59 | with tempfile.TemporaryDirectory() as newsquashfsroot: | 
|  | 60 | subprocess.check_output( | 
| Michael Shepos | b84cc50 | 2019-01-20 21:09:05 -0600 | [diff] [blame^] | 61 | ["mksquashfs", tempsquashfsdir, | 
|  | 62 | newsquashfsroot + "/test", "-comp", "xz"]) | 
| Ed Tanous | 1ace1bd | 2018-11-01 12:15:57 -0700 | [diff] [blame] | 63 |  | 
|  | 64 | results.append((filepath.replace(squashfsdir, ""), | 
| Michael Shepos | b84cc50 | 2019-01-20 21:09:05 -0600 | [diff] [blame^] | 65 | original_size - | 
|  | 66 | getsize(newsquashfsroot + "/test"))) | 
|  | 67 |  | 
| Ed Tanous | 1ace1bd | 2018-11-01 12:15:57 -0700 | [diff] [blame] | 68 | os.rename(newname, filepath) | 
|  | 69 |  | 
|  | 70 | print("{:>6} of {}".format(len(results), len(files_to_test))) | 
|  | 71 |  | 
|  | 72 | results.sort(key=lambda x: x[1], reverse=True) | 
|  | 73 |  | 
|  | 74 | with open("results.txt", 'w') as result_file: | 
|  | 75 | for filepath, size in results: | 
|  | 76 | result = "{:>10}: {}".format(size, filepath) | 
|  | 77 | print(result) | 
|  | 78 | result_file.write(result + "\n") |