-
Notifications
You must be signed in to change notification settings - Fork 0
/
dt_rename
executable file
·65 lines (55 loc) · 2.17 KB
/
dt_rename
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#!/usr/bin/env python
import argparse
import discogstool.libtags as libtags
import sys
import os
import filecmp
import shutil
import traceback
def process_file(path, args):
return libtags.rename_file(path, args.dest, args.verbose,
args.dryrun, args.move, True)
parser = argparse.ArgumentParser(description='Copy music files, renaming them')
parser.add_argument("-s", "--src", default="/Users/andrew/Music/iTunes/iTunes Media/Music/",
help="source directory")
parser.add_argument("-d", "--dest", required=True,
help="destination directory")
parser.add_argument("-y", "--dryrun", action="store_true",
help="Don't actually do anything")
parser.add_argument("-m", "--move", action="store_true",
help="Move items instead of copying them")
parser.add_argument("-p", "--prune", action="store_true",
help="Delete files under dest which aren't part of the set")
parser.add_argument("-v", "--verbose", action="store_true")
args = parser.parse_args(sys.argv[1:])
filelist = []
for root, dirs, files in os.walk(args.src):
for fname in files:
if fname.startswith("."):
continue
if fname[-3:].upper() in ["MP3", "M4A", "AAC", "MP4"]:
filename = os.path.abspath(os.path.join(root, fname))
try:
filelist.append(process_file(filename, args))
except Exception, e:
print "Failed to process", filename
traceback.print_exc()
elif args.verbose:
print "Skipping unsupported file", fname
filelist = frozenset(filelist)
if args.prune:
for root, dirs, files in os.walk(args.dest, topdown=False):
for fname in files:
filename = os.path.abspath(os.path.join(root, fname))
if filename not in filelist:
if args.verbose:
print "Deleting", filename
if not args.dryrun:
try:
os.remove(filename)
except Exception, e:
print "Couldn't delete", filename
if not os.listdir(root):
if args.verbose:
print "rmdir", root
os.rmdir(root)