Advertisement
hungat9c

Untitled

May 16th, 2018
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.84 KB | None | 0 0
  1. # coding=utf-8
  2. # version 1
  3.  
  4. import logging,os, time,sys,psycopg2
  5. from datetime import datetime,timedelta
  6.  
  7. logger = logging.getLogger('SmartfileTest')
  8. console = logging.StreamHandler()
  9. console.setLevel(logging.DEBUG)
  10. logging.getLogger('SmartfileTest').addHandler(console)
  11. file_logger = logging.FileHandler('/usr/local/www/apache24/data/runtime.log',mode='w')
  12. NEW_FORMAT = '[%(asctime)s] = [%(levelname)s] - %(message)s'
  13. file_logger_format = logging.Formatter(NEW_FORMAT)
  14. file_logger.setFormatter(file_logger_format)
  15. logger.addHandler(file_logger)
  16. logger.setLevel(logging.DEBUG)
  17.  
  18.  
  19. # logging.basicConfig(level=logging.DEBUG,
  20. # filename='/var/www/wsgi-scripts/pyadmin/scripts/runtime.log',
  21. # filemode='w',
  22. # format='%(levelname)s - %(filename)s - %(funcName)s - %(lineno)s - %(message)s')
  23.  
  24. # log some stuff!
  25. logger.debug("This is a debug message!")
  26. logger.info("This is an info message!")
  27. logger.warning("This is a warning message")
  28.  
  29. import os, time
  30.  
  31. path_data = "/usr/local/www/apache24/data"
  32. path_data_data = "/usr/local/www/apache24/data/data"
  33. path_disagreement_without_image = "/usr/local/www/apache24/data/disagreement_without_image"
  34. path_excel_without_images = "/usr/local/www/apache24/data/excel_without_images"
  35. path_images = "/usr/local/www/apache24/data/images"
  36.  
  37. def flushdir(dir):
  38. now = time.time()
  39. fs = iter(os.listdir(dir))
  40. while True:
  41. try:
  42. f = fs.__next__()
  43. fullpath = os.path.join(dir,f)
  44. if os.stat(fullpath).st_mtime < (now - 2*86400):
  45. if os.path.isfile(fullpath):
  46. os.remove(fullpath)
  47. elif os.path.isdir(fullpath):
  48. flushdir(fullpath)
  49. except StopIteration:
  50. break
  51.  
  52. flushdir(path_data)
  53. flushdir(path_data_data)
  54. flushdir(path_disagreement_without_image)
  55. flushdir(path_excel_without_images)
  56. flushdir(path_images)
  57.  
  58. # drop table
  59.  
  60. def getConnection():
  61. conn = psycopg2.connect("host=localhost dbname=pyadmin user=postgres password=12345678")
  62. return conn
  63.  
  64. day_expire = datetime.today() - timedelta(days=2)
  65.  
  66.  
  67. conn = getConnection()
  68. cur = conn.cursor()
  69. cur.execute("select tablename from pg_tables where schemaname='public' and tablename ilike '%_20%'")
  70. tables = iter(cur.fetchall())
  71. conn.commit()
  72. cur.close()
  73. conn.close()
  74. import re
  75.  
  76.  
  77. while True:
  78. try:
  79. table = tables.__next__()[0]
  80. str_day= "-".join(table.split("_")[-3:])
  81. day = datetime.strptime(str_day,'%Y-%m-%d')
  82. if day < datetime.today()-timedelta(days=2):
  83. conn = getConnection()
  84. cur = conn.cursor()
  85. cur.execute(f"drop table {table}")
  86. conn.commit()
  87. cur.close()
  88. conn.close()
  89. except StopIteration:
  90. break
  91.  
  92. logger.info("Done!")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement