Advertisement
Guest User

Untitled

a guest
Apr 25th, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.41 KB | None | 0 0
  1. '''
  2. This script to reset migrations and delete database for this project.
  3. NOTE: This file must placed into the django project folder and run it by type "python reset.py"
  4. '''
  5. import os, shutil, glob
  6. # Stopping paramter
  7. check=True
  8. # loop for input corection
  9. while check==True:
  10. # check whether user want to continue or not
  11. print('This will delete all migraions include your database.')
  12. con = input('Do you want to continue [y/n]:')
  13. # alaysis the answer
  14. if con=='n':
  15. # quit
  16. print('Thank you or using reset script.')
  17. check=False
  18. elif con=='y':
  19. # get the project path
  20. BASE_DIR = os.path.dirname(os.path.abspath(__file__))
  21. # get a list of all files and folders the project contains
  22. content = os.listdir(BASE_DIR)
  23. # create a new list for folders only
  24. dirs=list()
  25. # filter the list of all content to get the folders only
  26. for dir in content:
  27. if os.path.isdir(os.path.join(BASE_DIR, dir)):
  28. dirs.append(dir)
  29. # clean up all folders
  30. for dir in dirs:
  31. # clean __pycache__ in all folders
  32. try:
  33. shutil.rmtree(dir+'/'+'__pycache__')
  34. print("Folder __pycache__ in "+ dir + ' has been removed!')
  35. except:
  36. pass
  37. # clean __pycache__ in all migrations folders
  38. try:
  39. shutil.rmtree(dir+'/'+'migrations'+'/'+'__pycache__')
  40. print("Folder __pycache__ in "+ dir + ' /migations has been removed!')
  41. except:
  42. pass
  43. #delete all migrations files except __init__.py
  44. try:
  45. FolderPath=os.path.join(BASE_DIR,dir,'migrations')
  46. FolderContent=os.listdir(FolderPath)
  47. for file in FolderContent:
  48. if not file=='__init__.py':
  49. os.remove(os.path.join(FolderPath,file))
  50. print(FolderPath+file+' has been removed')
  51. except:
  52. pass
  53. # delete .sqlite3 file
  54. try:
  55. for file in glob.glob("*.sqlite3"):
  56. os.remove(file)
  57. print(file+' has been removed')
  58. except:
  59. pass
  60. print('\nYour project has been reset.\nPlease migrate again before use.')
  61. check=False
  62. else:
  63. print("Invalid input. please input 'y' or 'n'.\n")
  64. quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement