Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- import argparse
- import ConfigParser
- import sys
- from sqlalchemy import create_engine
- from sqlalchemy.orm import sessionmaker
- class FakeSecHead(object):
- def __init__(self, fp):
- self.fp = fp
- self.sechead = '[dcache]\n'
- def readline(self):
- if self.sechead:
- try:
- return self.sechead
- finally:
- self.sechead = None
- else:
- return self.fp.readline()
- if __name__ == '__main__':
- PARSER = argparse.ArgumentParser()
- PARSER.add_argument('-d', '--dry-run', action='store_true')
- PARSER.add_argument('poolnames',
- type=str,
- action='store',
- help='List of pool names separated by a comma')
- ARGS = PARSER.parse_args()
- poolnames = ARGS.poolnames.split(',')
- # read dcache configuration
- config = ConfigParser.ConfigParser()
- config.readfp(FakeSecHead(open('/etc/dcache/dcache.conf')))
- user = config.get('dcache', 'dcache.db.user')
- password = config.get('dcache', 'dcache.db.password')
- host = config.get('dcache', 'dcache.db.host')
- host = host.split(',')[0] # Consider only the first host
- url = 'postgresql://%(user)s:%(password)s@%(host)s/chimera' % locals()
- # db connection
- engine = create_engine(url)
- Session = sessionmaker(bind=engine)
- session = Session()
- try:
- for poolname in poolnames:
- print 'Checking pool', poolname
- query = '''select count(1)
- from t_locationinfo
- where t_locationinfo.iLocation=:poolname'''
- n = session.execute(query, {'poolname': poolname}).fetchone()[0]
- print n, 'files found in t_locationinfo'
- if n > 0:
- fname = '.'.join((poolname, 'csv'))
- f = open(fname, 'w+')
- query = '''select inumber2path(t1.inumber), t2.isize, t1.inumber
- from t_locationinfo t1, t_inodes t2
- where iLocation=:poolname
- and t1.inumber = t2.inumber
- and not exists (select 1 from t_locationinfo t2
- where t1.inumber = t2.inumber
- and t1.ilocation != t2.ilocation)'''
- n = 0
- for path, size, inumber in session.execute(query, {'poolname': poolname}):
- f.write(','.join([path, str(size), str(inumber)]) + '\n')
- n += 1
- f.close()
- print n, 'Unique files found on the pool'
- print 'File %(fname)s created' % locals()
- query = 'delete from t_locationinfo where iLocation=:poolname'
- if not ARGS.dry_run:
- rowcount = session.execute(query, {'poolname': poolname}).rowcount
- session.commit()
- print rowcount, 'files deleted from t_locationinfo'
- else:
- print 'dry_run:', query.replace(':poolname', "'" + poolname + "'")
- query = '''select count(1)
- from t_locationinfo_trash
- where iLocation=:poolname'''
- n = session.execute(query, {'poolname': poolname}).fetchone()[0]
- print n, 'files found in t_locationinfo_trash'
- if n > 0:
- query = 'delete from t_locationinfo_trash where iLocation=:poolname'
- if not ARGS.dry_run:
- rowcount = session.execute(query, {'poolname': poolname}).rowcount
- session.commit()
- print rowcount, 'files deleted from t_locationinfo_trash'
- else:
- print 'dry_run:', query.replace(':poolname', "'" + poolname + "'")
- finally:
- session.close()
- sys.exit(0)
Add Comment
Please, Sign In to add comment