Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # -*- coding: utf-8 -*-
- """
- Simple script to check if every file on filesystem is also
- present on database
- @author Gabriele Tozzi <gabriele@tozzi.eu>
- @date 2011-10-29
- """
- import os, sys
- import getpass, argparse
- import re
- import MySQLdb
- class main:
- NAME = 'amarok_check_collection'
- VERSION = '0.1'
- def run(self):
- ''' Main enrty point '''
- # Parse command line
- usage = self.NAME + " [options] <path>"
- parser = argparse.ArgumentParser(usage=usage, description=self.NAME + ' ' + self.VERSION)
- parser.add_argument('path',
- help='the path to scan for')
- parser.add_argument("-o", "--host", dest="host", default="localhost",
- help="Mysql host")
- parser.add_argument("-u", "--user", dest="user", default=getpass.getuser(),
- help="Mysql user")
- parser.add_argument("-d", "--database", dest="db", default="amarok",
- help="Mysql database name")
- self.__args = parser.parse_args()
- # Connect to mysql
- self.__db = MySQLdb.connect(
- user = self.__args.user,
- passwd = getpass.getpass('MySql Password: '),
- host = self.__args.host,
- db = self.__args.db
- )
- # Set charset
- self.__db.set_character_set('utf8')
- # Read list of files known to amarok
- amarok = self.__getAmarokFiles()
- print "Found %s files in amarok database" % len(amarok)
- local = self.__getLocalFiles(self.__args.path)
- print "Found %s files in local path" % len(local)
- missing = []
- for f in local:
- if not f in amarok:
- missing.append(f)
- print "MISSED:", f
- print "%s missed files" % len(missing)
- return 0
- def __getAmarokFiles(self):
- ''' Return list of files indexed by amarok '''
- ret = []
- curs = self.__db.cursor(cursorclass=MySQLdb.cursors.DictCursor)
- q = '''
- SELECT
- urls.rpath AS path,
- devices.lastmountpoint AS base
- FROM urls
- LEFT JOIN devices
- ON urls.deviceid = devices.id
- '''
- curs.execute(q)
- while True:
- res = curs.fetchone()
- if not res:
- break
- ret.append(res['base'] + (res['path'] if res['path'][0]!='.' else \
- res['path'][1:]) )
- return ret
- def __getLocalFiles(self, path, ignore=re.compile("^.*\.(png|jpg|gif)$")):
- ''' Returns recursive list of files on path '''
- ret = []
- for f in os.listdir(path):
- ff = os.path.join(path,f)
- if os.path.isfile(ff):
- if not ignore.match(f):
- ret.append(ff)
- elif os.path.isdir(ff):
- ret.extend(self.__getLocalFiles(ff))
- return ret
- if __name__ == '__main__':
- app = main()
- ret = app.run()
- sys.exit(ret)
Add Comment
Please, Sign In to add comment