added searching and folder options
This commit is contained in:
@@ -20,12 +20,16 @@ def setup_options():
|
||||
help="Modify changed files [%(default)s]")
|
||||
parser.add_argument("-d",action="store_true",dest="delete",default=False,
|
||||
help="Delete non-existing entries [%(default)s]")
|
||||
parser.add_argument("-f",action="store",dest="sqlfile",default=SQLFILE,
|
||||
help="SQL file name to use [%(default)s]")
|
||||
parser.add_argument("-l",action="store_true",dest="symlinks",default=False,
|
||||
help="Follow symbolic links [%(default)s]")
|
||||
parser.add_argument("-m",type=int,dest="minsize",default=MINSIZE,
|
||||
help="Minimum pixel width/height of stored image [%(default)s]")
|
||||
parser.add_argument("-r",action="store_true",dest="random",default=False,
|
||||
help="Create randomized files for landscape and portrait images [%(default)s]")
|
||||
parser.add_argument("-s",type=str,dest="search",default=False,
|
||||
help="Search list based on path pattern")
|
||||
parser.add_argument("--dup",action="store_true",dest="duplicate",default=False,
|
||||
help="Return a list of duplicate files, based on hashes. This option will flip the 'Add new files' option. [%(default)s]")
|
||||
parser.add_argument("--del",action="store_true",dest="deleteFiles",default=False,
|
||||
@@ -34,14 +38,16 @@ def setup_options():
|
||||
help="Return a list of small files, smaller than -m INT. This option will flip the 'Add new files' option. [%(default)s]")
|
||||
parser.add_argument("-x",action="append",dest="exclude",default=[],
|
||||
help="Exclude folder name from the lists. This option may be issued several times")
|
||||
parser.add_argument('startpath', action="store",default='.', nargs='?')
|
||||
|
||||
options=parser.parse_args()
|
||||
BADDIRS.extend(options.exclude)
|
||||
if options.duplicate or options.searchsmall:
|
||||
options.add=not options.add
|
||||
return options
|
||||
|
||||
def createdb():
|
||||
conn=sqlite3.connect(SQLFILE)
|
||||
def createdb(sqlfile):
|
||||
conn=sqlite3.connect(sqlfile)
|
||||
db=conn.cursor()
|
||||
conn.text_factory=str
|
||||
db.execute('CREATE TABLE list (id INTEGER PRIMARY KEY AUTOINCREMENT,\
|
||||
@@ -50,8 +56,8 @@ def createdb():
|
||||
conn.commit()
|
||||
return
|
||||
|
||||
def delete_nonexisting():
|
||||
conn=sqlite3.connect(SQLFILE)
|
||||
def delete_nonexisting(sqlfile):
|
||||
conn=sqlite3.connect(sqlfile)
|
||||
conn.text_factory=str
|
||||
#conn.row_factory=sqlite3.Row
|
||||
db=conn.cursor()
|
||||
@@ -78,10 +84,10 @@ def delete_files(files):
|
||||
return
|
||||
|
||||
def add_recurse(options):
|
||||
conn=sqlite3.connect(SQLFILE)
|
||||
conn=sqlite3.connect(options.sqlfile)
|
||||
conn.text_factory=str
|
||||
db=conn.cursor()
|
||||
for path,dirs,files in os.walk('.',followlinks=options.symlinks):
|
||||
for path,dirs,files in os.walk(options.startpath,followlinks=options.symlinks):
|
||||
dirs=clean_dirs(dirs)
|
||||
if not options.symlinks:
|
||||
files=clean_syms(files)
|
||||
@@ -127,8 +133,8 @@ def add_single(conn,filename,change=False,hash=None,minsize=0):
|
||||
conn.commit()
|
||||
return
|
||||
|
||||
def random_lists():
|
||||
conn=sqlite3.connect(SQLFILE)
|
||||
def random_lists(sqlfile):
|
||||
conn=sqlite3.connect(sqlfile)
|
||||
conn.text_factory=str
|
||||
db=conn.cursor()
|
||||
db.execute('SELECT file FROM list WHERE portrait=0')
|
||||
@@ -174,6 +180,19 @@ def get_dims(filename):
|
||||
out, err = p.communicate()
|
||||
return (out.strip().split('x'))
|
||||
|
||||
def searchdb(sqlfile,needle):
|
||||
conn=sqlite3.connect(sqlfile)
|
||||
conn.text_factory=str
|
||||
db=conn.cursor()
|
||||
dbh=conn.cursor()
|
||||
db.execute("SELECT file,width,height,date FROM list WHERE file LIKE ? ORDER BY file",('%'+needle+'%',))
|
||||
results=[]
|
||||
flist=[]
|
||||
for row in db:
|
||||
results.append(row)
|
||||
flist.append(('search',results))
|
||||
return flist
|
||||
|
||||
def clean_dirs(dirs):
|
||||
for s in BADDIRS:
|
||||
if s in dirs:
|
||||
@@ -212,8 +231,8 @@ def confirm(prompt=None, resp=False):
|
||||
if ans == 'n' or ans == 'N':
|
||||
return False
|
||||
|
||||
def find_duplicates():
|
||||
conn=sqlite3.connect(SQLFILE)
|
||||
def find_duplicates(sqlfile):
|
||||
conn=sqlite3.connect(sqlfile)
|
||||
conn.text_factory=str
|
||||
db=conn.cursor()
|
||||
dbh=conn.cursor()
|
||||
@@ -230,8 +249,8 @@ def find_duplicates():
|
||||
duphash.sort(key=lambda file: file[1][0])
|
||||
return duphash
|
||||
|
||||
def find_smalls(minsize):
|
||||
conn=sqlite3.connect(SQLFILE)
|
||||
def find_smalls(minsize,sqlfile):
|
||||
conn=sqlite3.connect(sqlfile)
|
||||
conn.text_factory=str
|
||||
db=conn.cursor()
|
||||
db.execute("SELECT file,width,height FROM list WHERE width < ? OR height < ?",(minsize,minsize))
|
||||
@@ -264,32 +283,33 @@ def print_dup_structure(files):
|
||||
|
||||
def main():
|
||||
options=setup_options();
|
||||
if not os.path.exists(SQLFILE):
|
||||
createdb();
|
||||
if not os.path.exists(options.sqlfile):
|
||||
createdb(options.sqlfile);
|
||||
if options.search:
|
||||
print_structure(searchdb(options.sqlfile,options.search))
|
||||
sys.exit(0)
|
||||
if options.delete:
|
||||
print('Deleting entries...')
|
||||
delete_nonexisting()
|
||||
delete_nonexisting(options.sqlfile)
|
||||
if options.add or options.changed:
|
||||
print('Adding entries...')
|
||||
add_recurse(options)
|
||||
if options.random:
|
||||
print('Random lists...')
|
||||
random_lists()
|
||||
random_lists(options.sqlfile)
|
||||
if options.duplicate:
|
||||
files=find_duplicates()
|
||||
files=find_duplicates(options.sqlfile)
|
||||
print_dup_structure(files)
|
||||
if options.searchsmall:
|
||||
files=find_smalls(options.minsize)
|
||||
files=find_smalls(options.minsize,options.sqlfile)
|
||||
if options.deleteFiles:
|
||||
if len(files[0][1])>0:
|
||||
delete_files(files)
|
||||
delete_nonexisting()
|
||||
delete_nonexisting(options.sqlfile)
|
||||
else:
|
||||
print_structure(files)
|
||||
#print(files)
|
||||
|
||||
|
||||
|
||||
sys.exit(0)
|
||||
|
||||
main()
|
||||
|
||||
Reference in New Issue
Block a user