cleanup.py 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. #!/usr/bin/python3
  2. """ Maintenance up the database used by dirnotes and c_dirnotes
  3. - view the history of file comments
  4. - delete the history of file comments
  5. """
  6. import sqlite3, os, sys
  7. import logging
  8. VERSION = "0.1"
  9. dbname = "~/.dirnotes.db"
  10. dbname = os.path.expanduser(dbname)
  11. def main(summary, view, delete):
  12. try:
  13. d = sqlite3.connect(dbname)
  14. except sqlite3.OperationalError:
  15. print("database not found, or permission-locked; cleanup aborted")
  16. return
  17. c = d.cursor()
  18. if summary:
  19. c = d.cursor()
  20. total,distinct = c.execute("select count(name),count(distinct name) from dirnotes;").fetchone()
  21. print(f"this database has entries for {distinct} files, and {total-distinct} history entries")
  22. if view or delete:
  23. c.execute("select distinct name from dirnotes;")
  24. for f in c:
  25. # note, f is a tuple of (name,)
  26. c1 = d.cursor()
  27. if view:
  28. history = c1.execute("select comment_date,author,comment from dirnotes where name=? order by comment_date desc",f).fetchall()
  29. if len(history)>1:
  30. print(f"filename: {f[0]} has a history of comments:")
  31. first = True
  32. for lin in history:
  33. print(f"{'->' if first else ' '} date: {lin[0]} author: {lin[1]}\n comment: {lin[2]}")
  34. first = False
  35. if delete:
  36. most_recent = c1.execute("select comment_date from dirnotes where name=? order by comment_date desc",f).fetchone()[0]
  37. logging.info(f"working on file: {f} with most-recent-date of {most_recent}")
  38. c1.execute("delete from dirnotes where name=? and comment_date <> ?",(f[0],most_recent))
  39. if delete:
  40. total,distinct = c.execute("select count(name),count(distinct name) from dirnotes;").fetchone()
  41. print(f"this database has been cleaned to {distinct} files, and {total-distinct} history entries")
  42. d.commit()
  43. d.close()
  44. view = False
  45. delete = False
  46. summary = True # default
  47. if __name__ == "__main__":
  48. logging.basicConfig(filename="/tmp/dirnotes_clear.log", level=logging.DEBUG)
  49. logging.info("starting the db mainenance tool")
  50. if len(sys.argv)>1:
  51. if "-h" in sys.argv or "--help" in sys.argv:
  52. print(f"Usage: {sys.argv[0]} maintenance for the database used by dirnotes & c_dirnotes")
  53. print(" -d delete comment history")
  54. print(" -s view comment summary")
  55. print(" -v view comment history")
  56. sys.exit(1)
  57. if "-v" in sys.argv:
  58. view = True
  59. if "-s" in sys.argv:
  60. summary = True
  61. if "-d" in sys.argv:
  62. delete = True
  63. if delete:
  64. print("Cleanup of the dirnotes database located at ~/.dirnotes.db")
  65. c = input("Proceed? (Y/N)")
  66. if c in ("yY"):
  67. main(summary, view, delete)
  68. else:
  69. print("nothing done")
  70. else:
  71. main(summary, view, delete)