def copy(tree, source_filename): """ Copy file in tree, show a progress bar during operations, and return the sha1 sum of copied file. """ #_, ext = os.path.splitext(source_filename) filehash = sha1() with printer.progress(os.path.getsize(source_filename)) as update: with open(source_filename, 'rb') as fsource: with NamedTemporaryFile(dir=os.path.join(tree, '.kolekto', 'movies'), delete=False) as fdestination: # Copy the source into the temporary destination: while True: buf = fsource.read(10 * 1024) if not buf: break filehash.update(buf) fdestination.write(buf) update(len(buf)) # Rename the file to its final name or raise an error if # the file already exists: dest = os.path.join(tree, '.kolekto', 'movies', filehash.hexdigest()) if os.path.exists(dest): raise IOError('This file already exists in tree (%s)' % filehash.hexdigest()) else: os.rename(fdestination.name, dest) return filehash.hexdigest()
def run(self, args, config): mdb = self.get_metadata_db(args.tree) mds = MovieDatasource(config.subsections('datasource'), args.tree, self.profile.object_class) if args.input is None: # Refresh all movies if printer.ask('Would you like to refresh all movies?', default=True): with printer.progress(mdb.count(), task=True) as update: for movie_hash, movie in list(mdb.itermovies()): movie = mds.refresh(movie) mdb.save(movie_hash, movie) printer.verbose('Saved {hash}', hash=movie_hash) update(1) else: movie_hash = get_hash(args.input) try: movie = mdb.get(movie_hash) except KeyError: printer.p('Unknown movie hash.') return else: movie = mds.refresh(movie) show(movie) if printer.ask('Would you like to save the movie?', default=True): mdb.save(movie_hash, movie) printer.p('Saved.')
def link(tree, source_filename, symlink=False): filehash = sha1() with printer.progress(os.path.getsize(source_filename)) as update: with open(source_filename, 'rb') as fsource: # Copy the source into the temporary destination: while True: buf = fsource.read(10 * 1024) if not buf: break filehash.update(buf) update(len(buf)) # Hardlink the file or raise an error if the file already exists: dest = os.path.join(tree, '.kolekto', 'movies', filehash.hexdigest()) if os.path.exists(dest): raise IOError('This file already exists in tree (%s)' % filehash.hexdigest()) else: if symlink: os.symlink(source_filename, dest) else: os.link(source_filename, dest) return filehash.hexdigest()
def run(self, args, config): mdb = self.get_metadata_db(args.tree) mds = MovieDatasource(config.subsections('datasource'), args.tree, self.profile.object_class) if args.dry_run: printer.p('Dry run: I will not create or delete any link') # Create the list of links that must exists on the fs: db_links = {} with printer.progress(mdb.count(), task=True) as update: for movie_hash, movie in mdb.itermovies(): movie = mds.attach(movie_hash, movie) for view in config.subsections('view'): for pattern in view.get('pattern'): for result in format_all(pattern, movie): filename = os.path.join(view.args, result) if filename in db_links: printer.p('Warning: duplicate link {link}', link=filename) else: db_links[filename] = movie_hash update(1) # Create the list of links already existing on the fs: fs_links = {} for view in config.subsections('view'): view_links = walk_links(os.path.join(args.tree, view.args), prefix=view.args, linkbase=os.path.join(args.tree, '.kolekto', 'movies')) fs_links.update(view_links) db_links = set(db_links.iteritems()) fs_links = set(fs_links.iteritems()) links_to_delete = fs_links - db_links links_to_create = db_links - fs_links printer.p('Found {rem} links to delete, {add} links to create', rem=len(links_to_delete), add=len(links_to_create)) dirs_to_cleanup = set() # Delete the old links: for filename, link in links_to_delete: printer.verbose('Deleting {file}', file=filename) if not args.dry_run: os.remove(os.path.join(args.tree, filename)) while filename: filename = os.path.split(filename)[0] dirs_to_cleanup.add(filename) dirs_to_cleanup.discard('') # Avoid to delete view roots # Delete empty directories: for directory in dirs_to_cleanup: if not args.dry_run: try: os.rmdir(os.path.join(args.tree, directory)) except OSError, err: if err.errno != 39: # Ignore "Directory not empty" error raise else: printer.verbose('Deleted directory {dir}', dir=directory)