diff --git a/file_list.py b/file_list.py index 3ed489a..ad66402 100755 --- a/file_list.py +++ b/file_list.py @@ -24,6 +24,12 @@ 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("--haschanges",action="store_true",dest="haschanges",default=False, + help="Do not change anything, return True and exit code 1 if DB needs update. Exit code 0 if all intact.") + parser.add_argument("--hasdeletions",action="store_true",dest="hasdeletions",default=False, + help="Do not change anything, return True and exit code 1 if DB needs update. Exit code 0 if all intact.") + parser.add_argument("--hasadditions",action="store_true",dest="hasadditions",default=False, + help="Do not change anything, return True and exit code 1 if DB needs update. Exit code 0 if all intact.") parser.add_argument("-l",action="store_true",dest="symlinks",default=False, help="Follow symbolic links [%(default)s]") parser.add_argument("--dup",action="store_true",dest="duplicate",default=False, @@ -74,6 +80,49 @@ def delete_nonexisting(sqlfile,options): conn.commit() return +def has_changes(options): + conn=sqlite3.connect(options.sqlfile) + conn.text_factory=str + db=conn.cursor() + if options.haschanges: + options.changed=True + if options.hasdeletions or options.haschanges: + has_changes_deleted(db) + if options.hasadditions or options.haschanges: + has_changes_additions(db,options) + +def has_changes_deleted(db): + db.execute('SELECT file FROM list') + for row in db: + if not os.path.exists(row[0]): + print('True') + sys.exit(1) + return + +def has_changes_additions(db,options): + for path,dirs,files in os.walk(options.startpath,followlinks=options.symlinks): + dirs=clean_dirs(dirs) + db_files=get_folder_contents(db,os.path.abspath(path)+'/') + if not options.symlinks: + files=clean_syms(files,path) + for file in files: + filename=os.path.abspath(os.path.join(path,file)) + if file==options.sqlfile: + continue + #if not is_listed(db,filename): + if file not in db_files: + print('True') + sys.exit(1) + else: + if options.changed: + ftime=os.path.getmtime(filename) + if not ftime_match(db,filename,ftime): + #file content changed + print('True') + sys.exit(1) + + return + def add_recurse(options): conn=sqlite3.connect(options.sqlfile) conn.text_factory=str @@ -226,6 +275,9 @@ def main(): if not os.path.exists(options.sqlfile): createdb(options.sqlfile); + if options.haschanges or options.hasadditions or options.hasdeletions: + has_changes(options) + sys.exit(0) if len(options.search)>0 and not options.match: searchdb(options.sqlfile,options.search) sys.exit(0)