def update(self, create=None): if not os.path.isdir(self.directory) and not self.ask_create(create): print_(u'Skipping creation of {0}'.format( displayable_path(self.directory))) return converter = self.converter() for (item, action) in self.items_action(): dest = self.destination(item) path = self.get_path(item) if action == self.MOVE: print_(u'>{0} -> {1}'.format(displayable_path(path), displayable_path(dest))) util.mkdirall(dest) util.move(path, dest) util.prune_dirs(os.path.dirname(path), root=self.directory) self.set_path(item, dest) item.store() item.write(path=dest) elif action == self.WRITE: print_(u'*{0}'.format(displayable_path(path))) item.write(path=path) elif action == self.ADD: print_(u'+{0}'.format(displayable_path(dest))) converter.submit(item) elif action == self.REMOVE: print_(u'-{0}'.format(displayable_path(path))) self.remove_item(item) item.store() for item, dest in converter.as_completed(): self.set_path(item, dest) item.store() converter.shutdown()
def move_file(self, dest, copy=False, link=False): """Moves or copies the item's file, updating the path value if the move succeeds. If a file exists at ``dest``, then it is slightly modified to be unique. """ if not util.samefile(self.path, dest): dest = util.unique_path(dest) if copy: util.copy(self.path, dest) plugins.send("item_copied", item=self, source=self.path, destination=dest) elif link: util.link(self.path, dest) plugins.send("item_linked", item=self, source=self.path, destination=dest) else: plugins.send("before_item_moved", item=self, source=self.path, destination=dest) util.move(self.path, dest) plugins.send("item_moved", item=self, source=self.path, destination=dest) # Either copying or moving succeeded, so update the stored path. self.path = dest
def set_art(self, path, copy=True): """Sets the album's cover art to the image at the given path. The image is copied (or moved) into place, replacing any existing art. Sends an 'art_set' event with `self` as the sole argument. """ path = bytestring_path(path) oldart = self.artpath artdest = self.art_destination(path) if oldart and samefile(path, oldart): # Art already set. return elif samefile(path, artdest): # Art already in place. self.artpath = path return # Normal operation. if oldart == artdest: util.remove(oldart) artdest = util.unique_path(artdest) if copy: util.copy(path, artdest) else: util.move(path, artdest) self.artpath = artdest plugins.send('art_set', album=self)
def move_art(self, copy=False, link=False): """Move or copy any existing album art so that it remains in the same directory as the items. """ old_art = self.artpath if not old_art: return new_art = self.art_destination(old_art) if new_art == old_art: return new_art = util.unique_path(new_art) log.debug(u'moving album art {0} to {1}', util.displayable_path(old_art), util.displayable_path(new_art)) if copy: util.copy(old_art, new_art) elif link: util.link(old_art, new_art) else: util.move(old_art, new_art) self.artpath = new_art # Prune old path when moving. if not copy: util.prune_dirs(os.path.dirname(old_art), self._db.directory)
def update(self, create=None): if not os.path.isdir(self.directory) and not self.ask_create(create): print_(u'Skipping creation of {0}' .format(displayable_path(self.directory))) return converter = self.converter() for (item, action) in self.items_action(): dest = self.destination(item) path = self.get_path(item) if action == self.MOVE: print_(u'>{0} -> {1}'.format(displayable_path(path), displayable_path(dest))) util.mkdirall(dest) util.move(path, dest) util.prune_dirs(os.path.dirname(path), root=self.directory) self.set_path(item, dest) item.store() item.write(path=dest) elif action == self.WRITE: print_(u'*{0}'.format(displayable_path(path))) item.write(path=path) elif action == self.ADD: print_(u'+{0}'.format(displayable_path(dest))) converter.submit(item) elif action == self.REMOVE: print_(u'-{0}'.format(displayable_path(path))) self.remove_item(item) item.store() for item, dest in converter.as_completed(): self.set_path(item, dest) item.store() converter.shutdown()
def move(self, copy=False, basedir=None): """Moves (or copies) all items to their destination. Any album art moves along with them. basedir overrides the library base directory for the destination. """ basedir = basedir or self._library.directory # Move items. items = list(self.items()) for item in items: item.move(self._library, copy, basedir=basedir) newdir = os.path.dirname(items[0].path) # Move art. old_art = self.artpath if old_art: new_art = self.art_destination(old_art, newdir) if new_art != old_art: if copy: util.copy(old_art, new_art) else: util.move(old_art, new_art) self.artpath = new_art if not copy: # Prune old path. util.prune_dirs(os.path.dirname(old_art), self._library.directory) # Store new item paths. We do this at the end to avoid # locking the database for too long while files are copied. for item in items: self._library.store(item)
def set_art(self, path, copy=True): """Sets the album's cover art to the image at the given path. The image is copied (or moved) into place, replacing any existing art. """ path = bytestring_path(path) oldart = self.artpath artdest = self.art_destination(path) if oldart and samefile(path, oldart): # Art already set. return elif samefile(path, artdest): # Art already in place. self.artpath = path return # Normal operation. if oldart == artdest: util.remove(oldart) artdest = util.unique_path(artdest) if copy: util.copy(path, artdest) else: util.move(path, artdest) self.artpath = artdest
def convert_item(dest_dir, keep_new, path_formats, command, ext): while True: item = yield dest = item.destination(basedir=dest_dir, path_formats=path_formats) # When keeping the new file in the library, we first move the # current (pristine) file to the destination. We'll then copy it # back to its old path or transcode it to a new path. if keep_new: original = dest converted = replace_ext(item.path, ext) else: original = item.path dest = replace_ext(dest, ext) converted = dest # Ensure that only one thread tries to create directories at a # time. (The existence check is not atomic with the directory # creation inside this function.) with _fs_lock: util.mkdirall(dest) if os.path.exists(util.syspath(dest)): log.info(u'Skipping {0} (target file exists)'.format( util.displayable_path(item.path) )) continue if keep_new: log.info(u'Moving to {0}'. format(util.displayable_path(original))) util.move(item.path, original) if not should_transcode(item): # No transcoding necessary. log.info(u'Copying {0}'.format(util.displayable_path(item.path))) util.copy(original, converted) else: try: encode(command, original, converted) except subprocess.CalledProcessError: continue # Write tags from the database to the converted file. item.write(path=converted) if keep_new: # If we're keeping the transcoded file, read it again (after # writing) to get new bitrate, duration, etc. item.path = converted item.read() item.store() # Store new path and audio data. if config['convert']['embed']: album = item.get_album() if album and album.artpath: embed_item(item, album.artpath, itempath=converted) plugins.send('after_convert', item=item, dest=dest, keepnew=keep_new)
def convert_item(dest_dir, keep_new, path_formats): while True: item = yield dest = _destination(dest_dir, item, keep_new, path_formats) if os.path.exists(util.syspath(dest)): log.info(u'Skipping {0} (target file exists)'.format( util.displayable_path(item.path) )) continue # Ensure that only one thread tries to create directories at a # time. (The existence check is not atomic with the directory # creation inside this function.) with _fs_lock: util.mkdirall(dest) # When keeping the new file in the library, we first move the # current (pristine) file to the destination. We'll then copy it # back to its old path or transcode it to a new path. if keep_new: log.info(u'Moving to {0}'. format(util.displayable_path(dest))) util.move(item.path, dest) if not should_transcode(item): # No transcoding necessary. log.info(u'Copying {0}'.format(util.displayable_path(item.path))) if keep_new: util.copy(dest, item.path) else: util.copy(item.path, dest) else: if keep_new: _, ext = get_format() item.path = os.path.splitext(item.path)[0] + ext encode(dest, item.path) else: encode(item.path, dest) # Write tags from the database to the converted file. if not keep_new: item.path = dest item.write() # If we're keeping the transcoded file, read it again (after # writing) to get new bitrate, duration, etc. if keep_new: item.read() item.store() # Store new path and audio data. if config['convert']['embed']: album = item.get_album() if album: artpath = album.artpath if artpath: _embed(artpath, [item])
def _displace(fn): """Move a file aside using a timestamp suffix so a new file can be put in its place. """ util.move( fn, u'{0}.old.{1}'.format(fn, int(time.time())), True )
def convert_item(dest_dir, keep_new, path_formats): while True: item = yield dest = _destination(dest_dir, item, keep_new, path_formats) if os.path.exists(util.syspath(dest)): log.info(u'Skipping {0} (target file exists)'.format( util.displayable_path(item.path))) continue # Ensure that only one thread tries to create directories at a # time. (The existence check is not atomic with the directory # creation inside this function.) with _fs_lock: util.mkdirall(dest) # When keeping the new file in the library, we first move the # current (pristine) file to the destination. We'll then copy it # back to its old path or transcode it to a new path. if keep_new: log.info(u'Moving to {0}'.format(util.displayable_path(dest))) util.move(item.path, dest) if not should_transcode(item): # No transcoding necessary. log.info(u'Copying {0}'.format(util.displayable_path(item.path))) if keep_new: util.copy(dest, item.path) else: util.copy(item.path, dest) else: if keep_new: _, ext = get_format() item.path = os.path.splitext(item.path)[0] + ext encode(dest, item.path) else: encode(item.path, dest) # Write tags from the database to the converted file. if not keep_new: item.path = dest item.write() # If we're keeping the transcoded file, read it again (after # writing) to get new bitrate, duration, etc. if keep_new: item.read() item.store() # Store new path and audio data. if config['convert']['embed']: album = item.get_album() if album: artpath = album.artpath if artpath: _embed(artpath, [item])
def move(self, dest, copy=False): """Moves or copies the item's file, updating the path value if the move succeeds. """ if copy: util.copy(self.path, dest) else: util.move(self.path, dest) # Either copying or moving succeeded, so update the stored path. self.path = dest
def move(self, dest, copy=False): """Moves or copies the item's file, updating the path value if the move succeeds. If a file exists at ``dest``, then it is slightly modified to be unique. """ if not util.samefile(self.path, dest): dest = util.unique_path(dest) if copy: util.copy(self.path, dest) else: util.move(self.path, dest) # Either copying or moving succeeded, so update the stored path. self.path = dest
def update(self, create=None): if (not os.path.isdir(syspath(self.directory)) and not self.ask_create(create)): print_(u'Skipping creation of {0}' .format(displayable_path(self.directory))) return converter = self.converter() for (item, actions) in self.items_actions(): dest = self.destination(item) path = self.get_path(item) for action in actions: if action == self.MOVE: print_(u'>{0} -> {1}'.format(displayable_path(path), displayable_path(dest))) util.mkdirall(dest) util.move(path, dest) util.prune_dirs(os.path.dirname(path), root=self.directory) self.set_path(item, dest) item.store() path = dest elif action == self.WRITE: print_(u'*{0}'.format(displayable_path(path))) item.write(path=path) elif action == self.SYNC_ART: print_(u'~{0}'.format(displayable_path(path))) self.sync_art(item, path) elif action == self.ADD: print_(u'+{0}'.format(displayable_path(dest))) converter.submit(item) elif action == self.REMOVE: print_(u'-{0}'.format(displayable_path(path))) self.remove_item(item) item.store() for item, dest in converter.as_completed(): self.set_path(item, dest) item.store() converter.shutdown() for (album, actions) in self.albums_actions(): for action in actions: dest_dir = self.album_destination(album) if action == self.COPY_ART: path = album.artpath dest = album.art_destination(path, dest_dir) util.copy(path, dest, replace=True) print_(u'$~{0}'.format(displayable_path(dest))) print_(u'$!{0}'.format(displayable_path(path)))
def move(self, library, copy=False, in_album=False, basedir=None): """Move the item to its designated location within the library directory (provided by destination()). Subdirectories are created as needed. If the operation succeeds, the item's path field is updated to reflect the new location. If copy is True, moving the file is copied rather than moved. If in_album is True, then the track is treated as part of an album even if it does not yet have an album_id associated with it. (This allows items to be moved before they are added to the database, a performance optimization.) basedir overrides the library base directory for the destination. Passes on appropriate exceptions if directories cannot be created or moving/copying fails. Note that one should almost certainly call store() and library.save() after this method in order to keep on-disk data consistent. """ dest = library.destination(self, in_album=in_album, basedir=basedir) # Create necessary ancestry for the move. util.mkdirall(dest) if not samefile(self.path, dest): if copy: util.copy(self.path, dest) else: util.move(self.path, dest) # Either copying or moving succeeded, so update the stored path. old_path = self.path self.path = dest # Prune vacated directory. if not copy: util.prune_dirs(os.path.dirname(old_path), library.directory)
def move_art(self, copy=False): """Move or copy any existing album art so that it remains in the same directory as the items. """ old_art = self.artpath if not old_art: return new_art = self.art_destination(old_art) if new_art == old_art: return log.debug('moving album art %s to %s' % (old_art, new_art)) if copy: util.copy(old_art, new_art) else: util.move(old_art, new_art) self.artpath = new_art # Prune old path when moving. if not copy: util.prune_dirs(os.path.dirname(old_art), self._library.directory)
def convert_item(dest_dir, keep_new, path_formats, format, pretend=False): command, ext = get_format(format) item, original, converted = None, None, None while True: item = yield (item, original, converted) dest = item.destination(basedir=dest_dir, path_formats=path_formats) # When keeping the new file in the library, we first move the # current (pristine) file to the destination. We'll then copy it # back to its old path or transcode it to a new path. if keep_new: original = dest converted = item.path if should_transcode(item, format): converted = replace_ext(converted, ext) else: original = item.path if should_transcode(item, format): dest = replace_ext(dest, ext) converted = dest # Ensure that only one thread tries to create directories at a # time. (The existence check is not atomic with the directory # creation inside this function.) if not pretend: with _fs_lock: util.mkdirall(dest) if os.path.exists(util.syspath(dest)): log.info(u'Skipping {0} (target file exists)'.format( util.displayable_path(item.path) )) continue if keep_new: if pretend: log.info(u'mv {0} {1}'.format( util.displayable_path(item.path), util.displayable_path(original), )) else: log.info(u'Moving to {0}'.format( util.displayable_path(original)) ) util.move(item.path, original) if should_transcode(item, format): try: encode(command, original, converted, pretend) except subprocess.CalledProcessError: continue else: if pretend: log.info(u'cp {0} {1}'.format( util.displayable_path(original), util.displayable_path(converted), )) else: # No transcoding necessary. log.info(u'Copying {0}'.format( util.displayable_path(item.path)) ) util.copy(original, converted) if pretend: continue # Write tags from the database to the converted file. item.try_write(path=converted) if keep_new: # If we're keeping the transcoded file, read it again (after # writing) to get new bitrate, duration, etc. item.path = converted item.read() item.store() # Store new path and audio data. if config['convert']['embed']: album = item.get_album() if album and album.artpath: embed_item(item, album.artpath, itempath=converted) if keep_new: plugins.send('after_convert', item=item, dest=dest, keepnew=True) else: plugins.send('after_convert', item=item, dest=converted, keepnew=False)
def convert_item(self, dest_dir, keep_new, path_formats, fmt, pretend=False, link=False, hardlink=False): """A pipeline thread that converts `Item` objects from a library. """ command, ext = get_format(fmt) item, original, converted = None, None, None while True: item = yield (item, original, converted) dest = item.destination(basedir=dest_dir, path_formats=path_formats) # When keeping the new file in the library, we first move the # current (pristine) file to the destination. We'll then copy it # back to its old path or transcode it to a new path. if keep_new: original = dest converted = item.path if should_transcode(item, fmt): converted = replace_ext(converted, ext) else: original = item.path if should_transcode(item, fmt): dest = replace_ext(dest, ext) converted = dest # Ensure that only one thread tries to create directories at a # time. (The existence check is not atomic with the directory # creation inside this function.) if not pretend: with _fs_lock: util.mkdirall(dest) if os.path.exists(util.syspath(dest)): self._log.info(u'Skipping {0} (target file exists)', util.displayable_path(item.path)) continue if keep_new: if pretend: self._log.info(u'mv {0} {1}', util.displayable_path(item.path), util.displayable_path(original)) else: self._log.info(u'Moving to {0}', util.displayable_path(original)) util.move(item.path, original) if should_transcode(item, fmt): linked = False try: self.encode(command, original, converted, pretend) except subprocess.CalledProcessError: continue else: linked = link or hardlink if pretend: msg = 'ln' if hardlink else ('ln -s' if link else 'cp') self._log.info(u'{2} {0} {1}', util.displayable_path(original), util.displayable_path(converted), msg) else: # No transcoding necessary. msg = 'Hardlinking' if hardlink \ else ('Linking' if link else 'Copying') self._log.info(u'{1} {0}', util.displayable_path(item.path), msg) if hardlink: util.hardlink(original, converted) elif link: util.link(original, converted) else: util.copy(original, converted) if pretend: continue id3v23 = self.config['id3v23'].as_choice([True, False, 'inherit']) if id3v23 == 'inherit': id3v23 = None # Write tags from the database to the converted file. item.try_write(path=converted, id3v23=id3v23) if keep_new: # If we're keeping the transcoded file, read it again (after # writing) to get new bitrate, duration, etc. item.path = converted item.read() item.store() # Store new path and audio data. if self.config['embed'] and not linked: album = item._cached_album if album and album.artpath: self._log.debug(u'embedding album art from {}', util.displayable_path(album.artpath)) art.embed_item(self._log, item, album.artpath, itempath=converted, id3v23=id3v23) if keep_new: plugins.send('after_convert', item=item, dest=dest, keepnew=True) else: plugins.send('after_convert', item=item, dest=converted, keepnew=False)
def convert_item(self, dest_dir, keep_new, path_formats, fmt, pretend=False): command, ext = get_format(fmt) item, original, converted = None, None, None while True: item = yield (item, original, converted) dest = item.destination(basedir=dest_dir, path_formats=path_formats) # When keeping the new file in the library, we first move the # current (pristine) file to the destination. We'll then copy it # back to its old path or transcode it to a new path. if keep_new: original = dest converted = item.path if should_transcode(item, fmt): converted = replace_ext(converted, ext) else: original = item.path if should_transcode(item, fmt): dest = replace_ext(dest, ext) converted = dest # Ensure that only one thread tries to create directories at a # time. (The existence check is not atomic with the directory # creation inside this function.) if not pretend: with _fs_lock: util.mkdirall(dest) if os.path.exists(util.syspath(dest)): self._log.info("Skipping {0} (target file exists)", util.displayable_path(item.path)) continue if keep_new: if pretend: self._log.info("mv {0} {1}", util.displayable_path(item.path), util.displayable_path(original)) else: self._log.info("Moving to {0}", util.displayable_path(original)) util.move(item.path, original) if should_transcode(item, fmt): try: self.encode(command, original, converted, pretend) except subprocess.CalledProcessError: continue else: if pretend: self._log.info("cp {0} {1}", util.displayable_path(original), util.displayable_path(converted)) else: # No transcoding necessary. self._log.info("Copying {0}", util.displayable_path(item.path)) util.copy(original, converted) if pretend: continue # Write tags from the database to the converted file. item.try_write(path=converted) if keep_new: # If we're keeping the transcoded file, read it again (after # writing) to get new bitrate, duration, etc. item.path = converted item.read() item.store() # Store new path and audio data. if self.config["embed"]: album = item.get_album() if album and album.artpath: self._log.debug("embedding album art from {}", util.displayable_path(album.artpath)) art.embed_item(self._log, item, album.artpath, itempath=converted) if keep_new: plugins.send("after_convert", item=item, dest=dest, keepnew=True) else: plugins.send("after_convert", item=item, dest=converted, keepnew=False)
def test_successful_move(self): util.move(self.path, self.dest)
def test_unsuccessful_move(self): with self.assertRaises(util.FilesystemError): util.move(self.path, self.otherpath)
def test_self_move(self): util.move(self.path, self.path) self.assertExists(self.path)
def test_successful_move(self): util.move(self.path, self.dest) self.assertExists(self.dest) self.assertNotExists(self.path)
def test_unsuccessful_move(self): with self.assertRaises(OSError): util.move(self.path, self.otherpath)
def convert_item(dest_dir, keep_new, path_formats): while True: item = yield dest = _destination(dest_dir, item, keep_new, path_formats) if os.path.exists(util.syspath(dest)): log.info(u'Skipping {0} (target file exists)'.format( util.displayable_path(item.path))) continue # Ensure that only one thread tries to create directories at a # time. (The existence check is not atomic with the directory # creation inside this function.) with _fs_lock: util.mkdirall(dest) # When keeping the new file in the library, we first move the # current (pristine) file to the destination. We'll then copy it # back to its old path or transcode it to a new path. if keep_new: log.info(u'Moving to {0}'.format(util.displayable_path(dest))) util.move(item.path, dest) original = dest _, ext = get_format() converted = os.path.splitext(item.path)[0] + ext else: original = item.path converted = dest if not should_transcode(item): # No transcoding necessary. log.info(u'Copying {0}'.format(util.displayable_path(item.path))) util.copy(original, converted) else: try: encode(original, converted) except subprocess.CalledProcessError: continue # Write tags from the database to the converted file. item.write(path=converted) if keep_new: # If we're keeping the transcoded file, read it again (after # writing) to get new bitrate, duration, etc. item.path = converted item.read() item.store() # Store new path and audio data. if config['convert']['embed']: album = item.get_album() if album: artpath = album.artpath if artpath: try: _embed(artpath, [converted]) except IOError as exc: log.warn( u'could not embed cover art in {0}: {1}'.format( util.displayable_path(item.path), exc)) plugins.send('after_convert', item=item, dest=dest, keepnew=keep_new)