Guest User

Untitled

a guest
Feb 28th, 2018
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.13 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3.  
  4. """
  5. Simple script to check if every file on filesystem is also
  6. present on database
  7.  
  8. @author Gabriele Tozzi <gabriele@tozzi.eu>
  9. @date   2011-10-29
  10. """
  11.  
  12. import os, sys
  13. import getpass, argparse
  14. import re
  15. import MySQLdb
  16.  
  17. class main:
  18.    
  19.     NAME = 'amarok_check_collection'
  20.     VERSION = '0.1'
  21.    
  22.     def run(self):
  23.         ''' Main enrty point '''
  24.        
  25.         # Parse command line
  26.         usage = self.NAME + " [options] <path>"
  27.         parser = argparse.ArgumentParser(usage=usage, description=self.NAME + ' ' + self.VERSION)
  28.         parser.add_argument('path',
  29.                    help='the path to scan for')
  30.         parser.add_argument("-o", "--host", dest="host", default="localhost",
  31.                   help="Mysql host")
  32.         parser.add_argument("-u", "--user", dest="user", default=getpass.getuser(),
  33.                   help="Mysql user")
  34.         parser.add_argument("-d", "--database", dest="db", default="amarok",
  35.                   help="Mysql database name")
  36.  
  37.         self.__args = parser.parse_args()
  38.  
  39.         # Connect to mysql
  40.         self.__db = MySQLdb.connect(
  41.             user = self.__args.user,
  42.             passwd = getpass.getpass('MySql Password: '),
  43.             host = self.__args.host,
  44.             db = self.__args.db
  45.         )
  46.         # Set charset
  47.         self.__db.set_character_set('utf8')
  48.        
  49.         # Read list of files known to amarok
  50.         amarok = self.__getAmarokFiles()
  51.         print "Found %s files in amarok database" % len(amarok)
  52.        
  53.         local = self.__getLocalFiles(self.__args.path)
  54.         print "Found %s files in local path" % len(local)
  55.        
  56.         missing = []
  57.         for f in local:
  58.             if not f in amarok:
  59.                 missing.append(f)
  60.                 print "MISSED:", f
  61.         print "%s missed files" % len(missing)
  62.        
  63.         return 0
  64.    
  65.     def __getAmarokFiles(self):
  66.         ''' Return list of files indexed by amarok '''
  67.         ret = []
  68.        
  69.         curs = self.__db.cursor(cursorclass=MySQLdb.cursors.DictCursor)
  70.         q = '''
  71.            SELECT
  72.                urls.rpath AS path,
  73.                devices.lastmountpoint AS base
  74.            FROM urls
  75.            LEFT JOIN devices
  76.                ON urls.deviceid = devices.id
  77.        '''
  78.         curs.execute(q)
  79.         while True:
  80.             res = curs.fetchone()
  81.             if not res:
  82.                 break
  83.             ret.append(res['base'] + (res['path'] if res['path'][0]!='.' else \
  84.                 res['path'][1:]) )
  85.         return ret
  86.    
  87.     def __getLocalFiles(self, path, ignore=re.compile("^.*\.(png|jpg|gif)$")):
  88.         ''' Returns recursive list of files on path '''
  89.         ret = []
  90.        
  91.         for f in os.listdir(path):
  92.             ff = os.path.join(path,f)
  93.             if os.path.isfile(ff):
  94.                 if not ignore.match(f):
  95.                     ret.append(ff)
  96.             elif os.path.isdir(ff):
  97.                 ret.extend(self.__getLocalFiles(ff))
  98.         return ret
  99.    
  100. if __name__ == '__main__':
  101.     app = main()
  102.     ret = app.run()
  103.     sys.exit(ret)
Add Comment
Please, Sign In to add comment