forked from TerraFirmaCraft/TerraFirmaCraft
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cleanResources.py
33 lines (28 loc) · 1.19 KB
/
cleanResources.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import os
def clean_generated_resources(path='src/main/resources') -> None:
"""
Recursively removes all autogenerated resources, as identified by the inserted comment.
Removes empty directories
Use to figure out if resources are missing / duplicated / disorganized
If you accidentally run this, run generateResources.py to replace removed files, the check the git diff for any differences
:param path: the initial path to search through
"""
for subdir in os.listdir(path):
sub_path = os.path.join(path, subdir)
if os.path.isfile(sub_path):
# File, check if valid and then delete
if subdir.endswith('.json'):
delete = False
with open(sub_path, 'r') as file:
if '"__comment": "Generated by generateResources.py' in file.read():
delete = True
if delete:
os.remove(sub_path)
else:
# Folder, search recursively
clean_generated_resources(sub_path)
if not os.listdir(path):
# Delete empty folder
os.rmdir(path)
if __name__ == '__main__':
clean_generated_resources()