Example #1
0
def rename_path(old_path, new_path=None, new_fullpath=None, force=False,
                  always_copy=False, always_move=False, create_dirs=True):
    """Moves the file to a new path.

    If it is on the same partition,
        it will be moved (unless always_copy is True)
    If it is on a different partition,
        it will be copied.
    If the target file already exists,
        it will raise OSError unless force is True.
    """
    test_mode = Config['test_mode']
    
    old_path = os.path.abspath(old_path)

    if always_copy and always_move:
        raise ValueError("Both always_copy and always_move "
                         "cannot be specified")

    if (new_path is None and new_fullpath is None) or \
       (new_path is not None and new_fullpath is not None):
        raise ValueError("Specify only new_dir or new_fullpath")

    if new_path is not None:
        old_dir, old_filename = os.path.split(old_path)

        # Join new filepath to old one (to handle realtive dirs)
        new_dir = os.path.abspath(os.path.join(old_dir, new_path))

        # Join new filename onto new filepath
        new_fullpath = os.path.join(new_dir, old_filename)

    else:
        old_dir, old_filename = os.path.split(old_path)

        # Join new filepath to old one (to handle realtive dirs)
        new_fullpath = os.path.abspath(os.path.join(old_dir, new_fullpath))

        new_dir = os.path.dirname(new_fullpath)

    if len(Config['move_files_fullpath_replacements']) > 0:
        log.debug("Before custom full path replacements: %s" % (new_fullpath))
        new_fullpath = applyCustomFullpathReplacements(new_fullpath)
        new_dir = os.path.dirname(new_fullpath)

    getattr(log, "info" if test_mode or Config['select_first'] else "debug")(
             "Moving:\n   ** %s\n   => %s" % (old_path, new_fullpath))
    if test_mode:
        return new_fullpath

    if create_dirs:
        log.debug("Creating directory %s" % new_dir)
        try:
            os.makedirs(new_dir)
        except OSError, e:
            if e.errno != 17:
                raise
Example #2
0
    def generate_dirname(self):
        if callable(self._dirname_key):
            key = self._dirname_key()
        else:
            key = self._dirname_key
        try:
            formatstr = Config[key]
        except KeyError:
            raise NotImplementedError(key)

        dirname = formatstr % self.__dict__

        if Config['move_files_lowercase_destination']:
            dirname = dirname.lower()
        
        if len(Config['move_files_fullpath_replacements']) > 0:
            log.info("Before custom output replacements: %s" % dirname)
            dirname = applyCustomFullpathReplacements(dirname)
        
        dirname = makeValidFilename(dirname, directory=True)
        log.debug("Generated path '%s' for %s" % (dirname, self))
        return dirname