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 |
| 5 | |
| 6 | # TODO |
| 7 | # - Make build directory an input parameter |
| 8 | # - Make squashfs file an input parameter |
| 9 | # - Fix 80 char violations and run through pep8 |
| 10 | |
| 11 | BUILD_DIR = "/home/ed/openbmc-openbmc" |
| 12 | # files below this size wont be attempted |
| 13 | FILE_SIZE_LIMIT = 0 |
| 14 | |
| 15 | SQUASHFS = BUILD_DIR + \ |
| 16 | "/build/tmp/deploy/images/wolfpass/intel-platforms-wolfpass.squashfs-xz" |
| 17 | |
| 18 | original_size = getsize(SQUASHFS) |
| 19 | print("squashfs size: ".format(original_size)) |
| 20 | |
| 21 | results = [] |
| 22 | |
| 23 | with tempfile.TemporaryDirectory() as tempremovedfile: |
| 24 | with tempfile.TemporaryDirectory() as tempsquashfsdir: |
| 25 | print("writing to " + tempsquashfsdir) |
| 26 | command = ["unsquashfs", "-d", |
| 27 | os.path.join(tempsquashfsdir, "squashfs-root"), SQUASHFS] |
| 28 | print(" ".join(command)) |
| 29 | subprocess.check_call(command) |
| 30 | squashfsdir = tempsquashfsdir + "/squashfs-root" |
| 31 | |
| 32 | files_to_test = [] |
| 33 | for root, dirs, files in os.walk(squashfsdir): |
| 34 | for name in files + dirs: |
| 35 | filepath = os.path.join(root, name) |
| 36 | if not os.path.islink(filepath): |
| 37 | # ensure files/dirs can be renamed |
| 38 | os.chmod(filepath,0o711) |
| 39 | if getsize(filepath) > FILE_SIZE_LIMIT: |
| 40 | files_to_test.append(filepath) |
| 41 | |
| 42 | print("{} files to attempt removing".format(len(files_to_test))) |
| 43 | |
| 44 | for filepath in files_to_test: |
| 45 | name = os.path.basename(filepath) |
| 46 | newname = os.path.join(tempremovedfile, name) |
| 47 | os.rename(filepath, newname) |
| 48 | with tempfile.TemporaryDirectory() as newsquashfsroot: |
| 49 | subprocess.check_output( |
| 50 | ["mksquashfs", tempsquashfsdir, newsquashfsroot + "/test", "-comp", "xz"]) |
| 51 | |
| 52 | results.append((filepath.replace(squashfsdir, ""), |
| 53 | original_size - getsize(newsquashfsroot + "/test"))) |
| 54 | os.rename(newname, filepath) |
| 55 | |
| 56 | print("{:>6} of {}".format(len(results), len(files_to_test))) |
| 57 | |
| 58 | results.sort(key=lambda x: x[1], reverse=True) |
| 59 | |
| 60 | with open("results.txt", 'w') as result_file: |
| 61 | for filepath, size in results: |
| 62 | result = "{:>10}: {}".format(size, filepath) |
| 63 | print(result) |
| 64 | result_file.write(result + "\n") |