dotpercent-files.py (2975B)
1 #!/usr/bin/env python 2 # -*- coding: utf-8 -*- 3 import os 4 import sys 5 import sqlite3 6 7 # Common functions 8 9 def removePrefix(fileName): 10 while fileName[0:2] == ".%": 11 fileName = fileName[2:] 12 return fileName 13 14 def removePrefixPath(path): 15 return '/'.join([removePrefix(component) for component in path.split('/')]) 16 17 def prefixedExists(path): 18 components = path.split('/') 19 prefixedPaths = ('/'.join(components[0:i] + ['.%' + component for component in components[i:]]) for i in reversed(xrange(len(components)))) 20 return any((os.path.exists(prefixedPath) for prefixedPath in prefixedPaths)) 21 22 # Code for this utility 23 24 prefix = '.%' 25 26 def help(): 27 print 'usage: %s database.db --vrac ./dbl/vrac-1 ./dbl/vrac-2 ./dbl/vrac-3 --tri ./dbl/tri-1 ./dbl/tri-2 ./dbl/tri-3' % sys.argv[0] 28 sys.exit(1) 29 30 vracs = [] 31 tris = [] 32 33 db = sys.argv[1] 34 35 if len(sys.argv) < 6: 36 help() 37 38 state=None 39 for arg in sys.argv[2:]: 40 if arg == '-h' or arg == '--help': 41 help() 42 elif arg == '--vrac': 43 state = "vrac" 44 elif arg == '--tri': 45 state = "tri" 46 elif state == 'tri': 47 if arg[-1:] == '/': 48 tris.append(arg) 49 else: 50 tris.append(arg + '/') 51 elif state == 'vrac': 52 if arg[-1:] == '/': 53 vracs.append(arg) 54 else: 55 vracs.append(arg + '/') 56 else: 57 help() 58 59 print 'vracs=%s' % ', '.join(vracs) 60 print 'tris=%s' % ', '.join(tris) 61 62 connection = sqlite3.connect(db) 63 connection.text_factory = str # For utf-8 file names… 64 cursor = connection.cursor() 65 66 cursor.execute("create temp table hashesVrac(id, hash);") 67 for path in vracs: 68 likepath=('' + path).replace('%', '%%') + '%'; 69 cursor.execute("insert into hashesVrac select rowid,size||'#'||md5||'#'||sha1 from files where path like ?;", (likepath,)) 70 71 cursor.execute("create temp table hashesTri(id, hash);") 72 for path in tris: 73 likepath=('' + path).replace('%', '%%') + '%'; 74 cursor.execute("insert into hashesTri select rowid,size||'#'||md5||'#'||sha1 from files where path like ?;", (likepath,)) 75 76 cursor.execute("create index i_hashesTri_hash on hashesTri(hash);") 77 cursor.execute("create index i_hashesVrac_hash on hashesVrac(hash);") 78 79 for fpath, in cursor.execute("select (select path from files where rowid == hashesVrac.id) as path from hashesVrac where hashesVrac.hash in (select hash from hashesTri);"): 80 dest = '%s/%s%s' % (os.path.dirname(fpath), prefix, os.path.basename(fpath),) 81 if prefixedExists(fpath) and not os.path.exists(fpath): 82 pass # Already moved 83 elif not os.path.exists(fpath): 84 print "# Couldn't hide %s as %s: source doesn't exist" % (fpath, dest,) 85 print "i-have-not-moved-because-no-source -i -- '%s' '%s'" % (fpath.replace("'", "'\\''"), dest.replace("'", "'\\''"),) 86 elif os.path.exists(dest): 87 print "# Couldn't hide %s as %s: destination exists" % (fpath, dest,) 88 print "i-have-not-moved-because-dest-exists -i -- '%s' '%s'" % (fpath.replace("'", "'\\''"), dest.replace("'", "'\\''"),) 89 else: 90 print "i-have-moved -i -- '%s' '%s'" % (fpath.replace("'", "'\\''"), dest.replace("'", "'\\''"),) 91 os.rename(fpath, dest)