|
1 | 1 | import logging |
2 | 2 | import math |
3 | 3 | import os |
4 | | -import subprocess |
5 | 4 | import time |
6 | 5 | from pathlib import Path |
7 | 6 | from threading import Thread |
@@ -31,32 +30,26 @@ def total_folder_size(startup: bool = False, root_dir: str = "/tmp") -> str: |
31 | 30 |
|
32 | 31 | running_total: int = 0 |
33 | 32 | if not startup: |
34 | | - for file in os.listdir(root_dir): |
35 | | - if "firefox" in file or ".xpi" in file or "owpm" in file or "Temp" in file: |
36 | | - path = os.path.join(root_dir, file) |
37 | | - try: |
38 | | - running_total += int( |
39 | | - subprocess.check_output(["du", "-s", "-b", path]) |
40 | | - .split()[0] |
41 | | - .decode("utf-8") |
42 | | - ) |
43 | | - except: |
44 | | - pass |
| 33 | + for dirpath, dirnames, filenames in os.walk(root_dir): |
| 34 | + for file in filenames: |
| 35 | + if ( |
| 36 | + "firefox" in file |
| 37 | + or ".xpi" in file |
| 38 | + or "owpm" in file |
| 39 | + or "Temp" in file |
| 40 | + ): |
| 41 | + path = os.path.join(dirpath, file) |
| 42 | + # skip if it is symbolic link |
| 43 | + if not os.path.islink(path): |
| 44 | + running_total += os.path.getsize(path) |
45 | 45 | return f"Currently using: {convert_size(running_total)} of storage on disk..." |
46 | 46 |
|
47 | | - for file in os.listdir(root_dir): |
48 | | - path = os.path.join(root_dir, file) |
49 | | - try: |
50 | | - running_total += int( |
51 | | - subprocess.check_output( |
52 | | - ["du", "-s", "-b", path], stderr=subprocess.DEVNULL |
53 | | - ) |
54 | | - .split()[0] |
55 | | - .decode("utf-8") |
56 | | - ) |
57 | | - except: |
58 | | - pass |
59 | | - |
| 47 | + for dirpath, dirnames, filenames in os.walk(root_dir): |
| 48 | + for file in filenames: |
| 49 | + path = os.path.join(dirpath, file) |
| 50 | + # skip if it is symbolic link |
| 51 | + if not os.path.islink(path): |
| 52 | + running_total += os.path.getsize(path) |
60 | 53 | return f"Readable files in {root_dir} folder take up {convert_size(running_total)} of storage on disk at start time..." |
61 | 54 |
|
62 | 55 |
|
@@ -99,11 +92,14 @@ def profile_size_exceeds_max_size( |
99 | 92 |
|
100 | 93 | readable_max_dir_size = convert_size(max_dir_size) |
101 | 94 |
|
102 | | - dir_size = int( |
103 | | - subprocess.check_output(["du", "-s", "-b", profile_path]) |
104 | | - .split()[0] |
105 | | - .decode("utf-8") |
106 | | - ) |
| 95 | + dir_size = 0 |
| 96 | + for dirpath, dirnames, filenames in os.walk(profile_path): |
| 97 | + for file in filenames: |
| 98 | + path = os.path.join(dirpath, file) |
| 99 | + # skip if it is symbolic link |
| 100 | + if not os.path.islink(path): |
| 101 | + dir_size += os.path.getsize(path) |
| 102 | + |
107 | 103 | readable_dir_size = convert_size(dir_size) |
108 | 104 |
|
109 | 105 | if dir_size < max_dir_size: |
|
0 commit comments