Нацарапано не профита ради, но языка освоения для, так что интересует мнение знающих людей. Вроде работает и тэги должным образом проставляет.
#!/usr/bin/env python
#_*_coding: utf-8_*_
"""splits .iso.wv images to .flack tracks"""
import sys, os, subprocess, shutil
class DstDir:
def __init__ (self, path, img, cue='', src='', dirs=None, other=None):
self.path = path
self.img = img
self.cue = cue
self.src = src
if dirs == None:
self.dirs = []
else:
self.dirs = dirs
if other == None:
self.other = []
else:
self.other = other
self.tracks = []
self.album = ''
def makeFlac (self):
cuebreakpoints = subprocess.Popen(["cuebreakpoints", self.cue],
stdout=subprocess.PIPE)
cmd = ["shnsplit", "-O", "always", "-i", "wv", "-o",
"flac flac -V -8 -e -p -o %f -",
"-t", "-%n", self.src]
shnsplit = subprocess.Popen(cmd,
stdin=cuebreakpoints.stdout,
stdout=subprocess.PIPE,
cwd=self.path)
shnsplit.wait()
def tagFlack (self):
subprocess.Popen(["recode", "cp1251..utf8"],
stdin=open(self.cue),
stdout=open(self.path + "/tmp.cue", "w")).wait()
self.cue = self.path + "/tmp.cue"
cmd = ["cueprint", "-n", "1", "-t", "%T", self.cue]
self.album = subprocess.Popen(cmd,
stdout=subprocess.PIPE).communicate()[0]
try:
year = subprocess.Popen(["grep", "REM DATE", self.cue],
stdout=subprocess.PIPE).communicate()[0].split()[2]
except IndexError:
year = "0000"
tag_names = ("ARRANGER=%A\nCOMPOSER=%C\nGENRE=%G\nMESSAGE=%M\n" +
"TRACKNUMBER=%n\nARTIST=%p\nTITLE=%t\nALBUM=%T\nDATE=" +
str(year) + "\n\n")
cmd = ["cueprint", "-t", tag_names, self.cue]
cueprint = subprocess.Popen(cmd, stdout=subprocess.PIPE)
cueprint.wait()
tracks = {}
n = 1
for i in cueprint.communicate()[0].split("\n\n")[:-1]:
tracks[n] = i
name = "%s/%02d.flac" % (self.path, n)
new_name = tracks[n].split("\n")[6].split("=")[1]
new_name = new_name.replace("/", "aka")
new_name = "%s/%d-%s.flac" % (self.path, n, new_name)
metaflac = subprocess.Popen(["metaflac", "--import-tags-from=-",
name],
stdin=subprocess.PIPE,
stdout=subprocess.PIPE)
actual_tags = ''
for line in tracks[n].split("\n"):
if line[-1] != "=":
actual_tags += line + "\n"
metaflac.communicate(actual_tags)
metaflac.wait()
os.rename(name, new_name)
self.tracks.append(new_name)
n += 1
self.album = year + "-" + self.album
def moveFiles(self, album=''):
print "moving files"
if album == '': album = self.album
dst = self.path + "/../" + album + "/"
if os.path.exists(dst):
dst = "%s%s/" % (dst, self.img.split("/")[-1][:-7])
os.mkdir(dst)
for dir in self.dirs:
shutil.copytree(dir, "%s%s"
% (dst, dir.split("/")[-1]))
for track in self.tracks:
print "shutil.move(%s,%s)" % (track, dst)
shutil.move(track, dst)
for item in self.other:
shutil.copy(item,"%s%s"
% (dst, item.split("/")[-1]))
os.remove(self.cue)
def getContent(dst_dir, cur_dir, files):
while files != []:
dst_dir.other.append(cur_dir + "/" + files.pop())
for cur_file in dst_dir.other:
if os.path.isdir(cur_file):
dst_dir.other.remove(cur_file)
dst_dir.dirs.append(cur_file)
for cur_file in dst_dir.other:
if dst_dir.cue != '': break
if cur_file[-4:] == ".cue":
dst_dir.cue = cur_file
for cur_file in dst_dir.other:
if dst_dir.src != '': break
if cur_file[-3:] == ".wv":
dst_dir.other.remove(cur_file)
dst_dir.src = cur_file
######################################MAIN######################################
if not os.path.isdir(sys.argv[1]): raise AssertionError, "Not directory!"
work_dir = os.path.normpath(sys.argv[1])
work_list = []
for cur_dir in os.walk(work_dir):
for cur_file in cur_dir[2]:
if cur_file[-7:] == ".iso.wv":
work_list.append(cur_dir[0] + "/" + cur_file)
if work_list == []: sys.exit()
for img in work_list:
tmp_dir = os.path.dirname(img) + "/tmp"
mnt_dir = tmp_dir + "/mnt"
os.mkdir(tmp_dir)
os.mkdir(mnt_dir)
if subprocess.call(["sudo", "mount", "-o",
"loop", "-o", "ro", img, mnt_dir]) == 0:
else:
raise RuntimeError, "Cant mount image!"
try:
WDir = DstDir(tmp_dir, img)
os.path.walk(mnt_dir, getContent, WDir)
WDir.makeFlac()
WDir.tagFlack()
WDir.moveFiles()
finally: #pass
if subprocess.call(["sudo", "umount", mnt_dir]) !=0:
raise RuntimeError, "Cant umount image!"
os.rmdir(mnt_dir)
os.rmdir(tmp_dir)
os.remove(img)