Ejemplo n.º 1
0
def rename(*args):
    albums, dir_list = scan(args)[:2]
    check_rename_sync(albums, dir_list)
    util.test_splits(dir_list)
    onsplit = Config['onsplit']
    for (dir_, files) in dir_list.items():
        dir_p = util.fsdecode(dir_)
        msg(consoleformat=u"from dir %(dir_p)s:",
            format="enter: %(dir_)r", dir_=dir_, dir_p=dir_p, loglevel=INFO)
        dstdirs = set()
        moves = []
        for file_ in files:
            src = file_.filename
            dst = util.fsencode(file_.format())
            src_p = util.fsdecode(src)
            dst_p = util.fsdecode(dst)
            if src == dst:
                msg(consoleformat=u"  skipping %(src_p)s, already named correctly",
                    format="skip: %(src)r",
                    src_p=src_p, src=src, loglevel=INFO)
                continue
            dstdir = os.path.split(dst)[0]
            if dstdir not in dstdirs and dstdir != dir_:
                try:
                    os.makedirs(dstdir)
                except OSError, e:
                    if e.errno != errno.EEXIST or not os.path.isdir(dstdir):
                        raise
                dstdirs.add(dstdir)
            msg(consoleformat=u"  %(src_p)s -> %(dst_p)s",
                format="move: %(src)r, %(dst)r",
                src=src, dst=dst, src_p=src_p, dst_p=dst_p, loglevel=INFO)
            util.move(src, dst)
        if len(dstdirs) == 1:
            dstdir = dstdirs.pop()
            for file_ in os.listdir(dir_):
                src = os.path.join(dir_, file_)
                dst = os.path.join(dstdir, file_)
                src_p = util.fsdecode(src)
                dst_p = util.fsdecode(dst)
                msg(consoleformat=u"  %(src_p)s -> %(dst_p)s",
                    format="move: %(src)r, %(dst)r", src=src, dst=dst,
                    src_p=src_p, dst_p=dst_p, loglevel=INFO)
                util.move(src, dst)
            while len(os.listdir(dir_)) == 0:
                dir_p = util.fsdecode(dir_)
                msg(consoleformat=u"  remove empty directory: %(dir_p)s",
                    format="rmdir: %(dir_)r",
                    dir_=dir_, dir_p=dir_p, loglevel=INFO)
                try:
                    os.rmdir(dir_)
                except Exception:
                    break
                newdir = os.path.split(dir_)[0]
                if newdir != dir_:
                    dir_ = newdir
                else:
                    break
        else:
            if onsplit == 'warn':
                msg(consoleformat=u"WARNING: tracks in %(dir_p)s were placed in different directories, other files may be left in the source directory",
                    format="split: %(dir_)r",
                    dir_=dir_, dir_p=dir_p, loglevel=WARNING)
Ejemplo n.º 2
0
def sync(*args):
    (album_list, dir_list) = scan(args)[:2]
    targettids = scan(Config['base'])[2]
    check_rename_sync(album_list, dir_list, targettids)
    sync_sets(album_list.values(), targettids)