Last active
January 6, 2020 11:14
-
-
Save marcelschmidtdev/35c273cf07157a5e495595bdc87d4910 to your computer and use it in GitHub Desktop.
Python script which deletes empty folders and meta files in Unity projects. Can be used as git hook.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/c/Python27/python | |
# Used as post-checkout and pre-commit git hook | |
import os | |
import sys | |
import msvcrt | |
deleted_files = [] | |
def is_directory_empty(path): | |
return os.listdir(path) == [] or (len(os.listdir(path)) == 1 and os.path.isfile(os.path.join(path, '.DS_Store'))) | |
def cleanup_directory(path): | |
global deleted_files | |
print ' ' + path | |
result = [] | |
all_files = os.listdir(path) | |
for filename in all_files: | |
full_file_path = os.path.join(path, filename) | |
basename, extension = os.path.splitext(filename) | |
if extension == '.meta': | |
meta_subject = os.path.join(path, basename) | |
if not os.path.exists(meta_subject): | |
print 'did not find ' + meta_subject + ', deleting .meta file' | |
deleted_files.append(full_file_path) | |
os.remove(full_file_path) | |
elif os.path.isdir(meta_subject): | |
cleanup_directory(meta_subject) | |
if is_directory_empty(meta_subject): | |
print meta_subject + ' is empty, deleting directory and .meta file' | |
ds_file = os.path.join(meta_subject, '.DS_Store') | |
if os.path.isfile(ds_file): | |
os.remove(ds_file) | |
deleted_files.append(ds_file) | |
os.rmdir(meta_subject) | |
os.remove(full_file_path) | |
deleted_files.append(meta_subject) | |
deleted_files.append(full_file_path) | |
if __name__ == '__main__': | |
assets_path = 'Assets' | |
deleted_files = [] | |
cleanup_directory(assets_path) | |
if len(deleted_files) > 0: | |
print 'Cleaning unused meta files from ' + assets_path | |
print '' | |
print '---------------------------------------------------------------' | |
print '' | |
print 'Deleted the following meta files and directories:' | |
print '' | |
for filename in deleted_files: | |
print filename |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment