|
17 | 17 | import fnmatch |
18 | 18 | import os |
19 | 19 | import shutil |
| 20 | +import sys |
20 | 21 | import zlib |
21 | 22 | from pathlib import Path |
22 | 23 |
|
@@ -54,52 +55,67 @@ def crc32(fp: str) -> int: |
54 | 55 | return checksum |
55 | 56 |
|
56 | 57 |
|
57 | | -def iterate(fp: str, blacklist: list[str]) -> None: |
| 58 | +def clean_iterate(root: str, fp: str, blacklist: list[str]) -> None: |
58 | 59 | """ |
59 | | - Iterate through all files and determine what is necessary |
| 60 | + Iterate through all files and delete what is unnecessary |
60 | 61 | Args: |
| 62 | + root (str): Root directory, used in generating skunkcrafts files |
61 | 63 | fp (str): Folder to iterate through. |
62 | 64 | blacklist (list[str]): Files to skip. |
63 | 65 | """ |
64 | | - for files in os.listdir(fp): |
| 66 | + for file_name in os.listdir(fp): |
65 | 67 | skip = False |
66 | 68 | for item in blacklist: |
67 | | - if fnmatch.fnmatch(files, item) or files.startswith("."): |
| 69 | + if fnmatch.fnmatch(file_name, item) or file_name.startswith("."): |
68 | 70 | skip = True |
69 | 71 | break |
70 | 72 |
|
71 | | - file_path = os.path.join(fp, files) |
| 73 | + file_path = os.path.join(fp, file_name) |
72 | 74 | if skip: |
73 | | - if files == "skunkcrafts_updater.cfg" or files == "skunkcrafts_updater_beta.cfg": |
74 | | - print("Preserving file " + file_path) # skip |
| 75 | + if file_name == "skunkcrafts_updater.cfg" or file_name == "skunkcrafts_updater_beta.cfg": |
| 76 | + print("Preserving file", file_path) # skip |
75 | 77 | elif Path.is_dir(Path(file_path)): |
76 | | - print("Clearing directory " + file_path) |
| 78 | + print("Clearing directory", file_path) |
77 | 79 | shutil.rmtree(os.path.join(os.curdir, file_path)) |
78 | 80 | else: |
79 | | - print("Clearing " + file_path) |
| 81 | + print("Clearing", file_path) |
80 | 82 | os.remove(os.path.join(os.curdir, file_path)) |
81 | 83 |
|
82 | 84 | elif os.path.isdir(file_path): |
83 | | - iterate(file_path, blacklist) |
| 85 | + clean_iterate(root, file_path, blacklist) |
84 | 86 | else: |
85 | | - file_path_out = file_path.replace("workdir/", "", 1) |
| 87 | + file_path_out = file_path.replace(root + "/", "", 1) |
86 | 88 | whitelist_file.write(f"{file_path_out}|{str(crc32(file_path))}\n") |
87 | 89 | filesize_file.write(f"{file_path_out}|{str(size(file_path))}\n") |
88 | 90 |
|
89 | 91 |
|
90 | 92 | def main(): |
91 | | - fp = Path.joinpath(Path(os.curdir), "workdir") |
92 | 93 | blacklist: list[str] = [] |
93 | 94 |
|
| 95 | + input_folder = sys.argv[1] |
| 96 | + output_file = sys.argv[2] |
| 97 | + |
94 | 98 | if not blacklist_file.is_file(): |
95 | 99 | print("Ignore file does not exist! Extra files may be added!") |
96 | 100 | else: |
97 | 101 | blacklist = blacklist_file.open().read().splitlines() |
98 | 102 |
|
99 | | - iterate(fp.name, blacklist) |
| 103 | + clean_iterate(input_folder, input_folder, blacklist) |
100 | 104 |
|
101 | 105 | whitelist_file.close() |
102 | 106 | filesize_file.close() |
103 | 107 |
|
| 108 | + outputs = output_file.split(".", 1) |
| 109 | + |
| 110 | + if outputs[1] == "tar.gz": |
| 111 | + outputs[1] = "gztar" |
| 112 | + elif outputs[1] == "tar.bz": |
| 113 | + outputs[1] = "bztar" |
| 114 | + elif outputs[1] == "tar.xz": |
| 115 | + outputs[1] = "xztar" |
| 116 | + |
| 117 | + print("Generating", output_file) |
| 118 | + shutil.make_archive(outputs[0], outputs[1], input_folder) |
| 119 | + |
104 | 120 |
|
105 | 121 | main() |
0 commit comments