Guest User

Untitled

a guest
Oct 24th, 2017
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.78 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. import argparse
  4. import ConfigParser
  5. import sys
  6.  
  7. from sqlalchemy import create_engine
  8. from sqlalchemy.orm import sessionmaker
  9.  
  10.  
  11. class FakeSecHead(object):
  12. def __init__(self, fp):
  13. self.fp = fp
  14. self.sechead = '[dcache]\n'
  15.  
  16. def readline(self):
  17. if self.sechead:
  18. try:
  19. return self.sechead
  20. finally:
  21. self.sechead = None
  22. else:
  23. return self.fp.readline()
  24.  
  25.  
  26. if __name__ == '__main__':
  27.  
  28. PARSER = argparse.ArgumentParser()
  29. PARSER.add_argument('-d', '--dry-run', action='store_true')
  30. PARSER.add_argument('poolnames',
  31. type=str,
  32. action='store',
  33. help='List of pool names separated by a comma')
  34. ARGS = PARSER.parse_args()
  35. poolnames = ARGS.poolnames.split(',')
  36.  
  37. # read dcache configuration
  38. config = ConfigParser.ConfigParser()
  39. config.readfp(FakeSecHead(open('/etc/dcache/dcache.conf')))
  40. user = config.get('dcache', 'dcache.db.user')
  41. password = config.get('dcache', 'dcache.db.password')
  42. host = config.get('dcache', 'dcache.db.host')
  43. host = host.split(',')[0] # Consider only the first host
  44. url = 'postgresql://%(user)s:%(password)s@%(host)s/chimera' % locals()
  45.  
  46. # db connection
  47. engine = create_engine(url)
  48. Session = sessionmaker(bind=engine)
  49. session = Session()
  50. try:
  51. for poolname in poolnames:
  52.  
  53. print 'Checking pool', poolname
  54. query = '''select count(1)
  55. from t_locationinfo
  56. where t_locationinfo.iLocation=:poolname'''
  57. n = session.execute(query, {'poolname': poolname}).fetchone()[0]
  58. print n, 'files found in t_locationinfo'
  59.  
  60. if n > 0:
  61. fname = '.'.join((poolname, 'csv'))
  62. f = open(fname, 'w+')
  63. query = '''select inumber2path(t1.inumber), t2.isize, t1.inumber
  64. from t_locationinfo t1, t_inodes t2
  65. where iLocation=:poolname
  66. and t1.inumber = t2.inumber
  67. and not exists (select 1 from t_locationinfo t2
  68. where t1.inumber = t2.inumber
  69. and t1.ilocation != t2.ilocation)'''
  70. n = 0
  71. for path, size, inumber in session.execute(query, {'poolname': poolname}):
  72. f.write(','.join([path, str(size), str(inumber)]) + '\n')
  73. n += 1
  74. f.close()
  75. print n, 'Unique files found on the pool'
  76. print 'File %(fname)s created' % locals()
  77.  
  78. query = 'delete from t_locationinfo where iLocation=:poolname'
  79. if not ARGS.dry_run:
  80. rowcount = session.execute(query, {'poolname': poolname}).rowcount
  81. session.commit()
  82. print rowcount, 'files deleted from t_locationinfo'
  83. else:
  84. print 'dry_run:', query.replace(':poolname', "'" + poolname + "'")
  85.  
  86. query = '''select count(1)
  87. from t_locationinfo_trash
  88. where iLocation=:poolname'''
  89. n = session.execute(query, {'poolname': poolname}).fetchone()[0]
  90. print n, 'files found in t_locationinfo_trash'
  91.  
  92. if n > 0:
  93. query = 'delete from t_locationinfo_trash where iLocation=:poolname'
  94. if not ARGS.dry_run:
  95. rowcount = session.execute(query, {'poolname': poolname}).rowcount
  96. session.commit()
  97. print rowcount, 'files deleted from t_locationinfo_trash'
  98. else:
  99. print 'dry_run:', query.replace(':poolname', "'" + poolname + "'")
  100. finally:
  101. session.close()
  102.  
  103. sys.exit(0)
Add Comment
Please, Sign In to add comment