2020-06-07 16:16:35 +01:00
|
|
|
import os
|
|
|
|
import shutil
|
|
|
|
|
|
|
|
from pprint import pprint
|
2020-07-18 16:23:54 +01:00
|
|
|
"""Cleans all django-created files and compiled python. Used by the
|
|
|
|
pre-run.sh script which cleans and initialises everything before
|
|
|
|
running:
|
|
|
|
python databaseReset.py reset
|
|
|
|
|
|
|
|
Downloaded (June 2020) from:
|
|
|
|
https://groups.google.com/forum/#!topic/django-users/C8Q7CTpcChc
|
|
|
|
"""
|
2020-06-07 16:16:35 +01:00
|
|
|
|
|
|
|
# from https://groups.google.com/forum/#!topic/django-users/C8Q7CTpcChc
|
|
|
|
# Just put it in the folder where manage.py file is and run it.
|
|
|
|
|
|
|
|
folders = []
|
|
|
|
base_dir = os.path.dirname(os.path.realpath(__file__))
|
|
|
|
|
|
|
|
|
|
|
|
def get_directory_list():
|
|
|
|
global folders
|
|
|
|
global base_dir
|
|
|
|
|
|
|
|
for root, d_names, f_names in os.walk(base_dir):
|
|
|
|
for name in d_names:
|
|
|
|
folders.append(os.path.join(root, name))
|
|
|
|
folders = sorted(folders)
|
|
|
|
|
|
|
|
return folders
|
|
|
|
|
|
|
|
|
|
|
|
def delete_pycache():
|
|
|
|
global folders
|
|
|
|
|
|
|
|
for folder in folders:
|
|
|
|
if folder.endswith("__pycache__"):
|
|
|
|
shutil.rmtree(folder)
|
|
|
|
|
|
|
|
print("All __pycache__ files deleted.")
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
|
def delete_migrations():
|
|
|
|
global folders
|
|
|
|
|
|
|
|
for folder in folders:
|
|
|
|
if folder.endswith("migrations"):
|
|
|
|
for item in os.listdir(folder):
|
|
|
|
if not item.endswith("__init__.py"):
|
|
|
|
os.remove(os.path.join(folder, item))
|
|
|
|
|
|
|
|
print("All migration files deleted.")
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
|
def delete_sqlite3():
|
|
|
|
global base_dir
|
|
|
|
|
2020-06-20 23:08:34 +01:00
|
|
|
db_file = os.path.join(base_dir, "troggle.sqlite")
|
|
|
|
#print("troggle.sqlite: {}".format(db_file))
|
2020-06-07 16:16:35 +01:00
|
|
|
if os.path.exists(db_file):
|
2020-06-20 23:08:34 +01:00
|
|
|
try:
|
|
|
|
os.remove(db_file)
|
2022-11-23 10:48:39 +00:00
|
|
|
print(f"\n>>> troggle.sqlite: {db_file} DELETED\n")
|
2020-06-20 23:08:34 +01:00
|
|
|
except:
|
2022-11-23 10:48:39 +00:00
|
|
|
print(f"troggle.sqlite: {db_file} NOT deleted")
|
2020-06-07 16:16:35 +01:00
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
global folders
|
2022-11-23 10:48:39 +00:00
|
|
|
print(f"base directory used: {base_dir}")
|
2020-06-20 23:08:34 +01:00
|
|
|
|
2020-06-07 16:16:35 +01:00
|
|
|
|
|
|
|
try:
|
|
|
|
get_directory_list()
|
|
|
|
delete_pycache()
|
|
|
|
delete_migrations()
|
|
|
|
delete_sqlite3()
|
|
|
|
print("All cleanup operations performed successfully.")
|
|
|
|
|
|
|
|
except Exception as e:
|
|
|
|
print("There was some error! Aaargh. \n")
|
|
|
|
raise
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|