def _get_changes(source: Folder, dst: Folder, session): changes = list() for file in source.files: dst_file = dst.child_by_name(file.name, session) if dst_file is None: dst_file = File(name=file.name, host=dst.host, folder=dst, hash=file.hash, mtime=file.mtime, size=file.size) obj = session.query(File).filter(and_(File.hash == file.hash, File.host == dst.host)).first() if obj is None: change = Change(type='COPY', source=file, target=dst_file) else: change = Change(type='LCOPY', source=obj, target=dst_file) logger.info('Adding change: %s' % str(change)) changes.append(change) elif file.hash == dst_file.hash: pass else: if file.mtime > dst_file.mtime: change = Change(type='REPLACE', source=file, target=dst_file) else: change = Change(type='CONFLICT', source=file, target=dst_file) logger.info('Adding change: %s' % str(change)) changes.append(change) for folder in source.folders: dst_folder = dst.child_by_name(folder.name, session) if dst_folder is None: dst_folder = Folder(name=folder.name, parent=dst, host=dst.host) changes.append(Change(type='COPY', source=folder, target=dst_folder)) else: changes += ChangeSet._get_changes(source=folder, dst=dst_folder, session=session) for file in dst.files: src_file = source.child_by_name(file.name, session) if src_file is None: change = Change(type='DELETE', source='', target=file) logger.info('Adding change: %s' % str(change)) changes.append(change) for folder in dst.folders: src_folder = source.child_by_name(folder.name, session) if src_folder is None: change = Change(type='DELETE', source='', target=folder) logger.info('Adding change: %s' % str(change)) changes.append(change) return changes
def main(args=sys.argv[1:]): """ main :) @param args: the args used for the parser, just useful for testing """ parser = argparse.ArgumentParser(description='Configuration tool') parser.add_argument('-d', '--database', type=str, metavar='"Connection String"', required=True) parser.add_argument('--debug', action='store_true') parser.add_argument('--source', type=str, metavar='URI', required=True) parser.add_argument('--dst', type=str, metavar='URI', required=True) parser.add_argument('--format', type=str, metavar='FORMAT', default='<SOURCE>::<TYPE>::<TARGET>') args = parser.parse_args(args) database = create_engine(args.database, echo=False) session_maker = sessionmaker(bind=database) session = session_maker() database_logging.DBSession = session_maker() if args.debug: logging.basicConfig(level=logging.DEBUG, handlers=(database_logging.SQLAlchemyHandler(), logging.StreamHandler())) logger.debug('Tool started with following arguments: %s' % args) else: logging.basicConfig(level=logging.INFO, handlers=(database_logging.SQLAlchemyHandler(), logging.StreamHandler())) try: source = Folder.by_uri(args.source, session) logger.info('Source: %s', source) dst = Folder.by_uri(args.dst, session) logger.info('Target: %s', dst) changes = ChangeSet(source=source, dst=dst, session=session) print('%sGB' % (changes.get_size() / 2 ** 30)) except Exception as e: logger.exception(e) return -1 finally: session.close_all() return 0
def _add_child(self, path: Path): """ Adds a Directory to the Database @param path: a pathlib Path object """ logger.info('Adding Item: [%r]' % path) session = database.get_session(new_engine=False) parent = Folder.by_uri('%s::%s' % (config.HOSTNAME, path.parent), session) if path.is_dir(): child = parent.add_folder(path.name) self._queue.put(path) elif path.is_file(): child = parent.add_file(name=path.name, mtime=datetime.fromtimestamp(path.stat().st_mtime), size=path.stat().st_size, fhash=None) else: session.close() raise RuntimeError('%s is neither File nor Folder' % path) session.add(child) session.commit() session.close()
def get_roles(configfile): """ @param configfile: The Path of the xml configfile @return a callable that creates the described role """ #TODO: Dummy Implementation roles = list() #roles.append(partial(hasher.run, 3, 120)) session = database.get_global_session() #host = Host.by_name('konsti-desktop', database.get_global_session()) #folder = host.add_root('/media/konsti/Storage/makeMKV', database.get_global_session()) #folder = host.add_root('/home/konsti/Code', database.get_global_session()) folder = Folder.by_uri('konsti-desktop::/home/konsti/Code', session) #session.add(host) #session.add(folder) #session.commit() roles.append(partial(scanner.run, folder, 300)) print(roles) return roles