X-Git-Url: https://diplodocus.org/git/flac-archive/blobdiff_plain/f6aab846670f5a35837cd4ce15000859631f77e6..73112f3f050b8f78a6d03449c00a60c6a30f715e:/rewrite-tags diff --git a/rewrite-tags b/rewrite-tags index b20a8ea..a3b5eb6 100755 --- a/rewrite-tags +++ b/rewrite-tags @@ -1,8 +1,9 @@ #! /usr/bin/python import os, sys +import subprocess +import tempfile from errno import EEXIST -from subprocess import Popen, PIPE from flac_archive.tags import Tags @@ -19,21 +20,33 @@ class SubprocessError(Exception): Exception.__init__(self, msg % (abs(status), command_msg)) self.status = status self.command = command - self.stderr = stderr + self.stderr = ''.join(stderr) def get_tags(fn): tags = Tags() command = ['metaflac', '--no-utf8-convert', '--export-tags-to=-', fn] - p = Popen(command, stdout=PIPE) + p = subprocess.Popen(command, stdout=subprocess.PIPE, + stderr=subprocess.STDOUT) tags.load(p.stdout) status = p.wait() if status != 0: - raise SubprocessError(status, command=command, stderr=p.stderr) + raise SubprocessError(status, command=command, stderr=p.stdout) return tags +def rewrite_track_tags(track, tags, fn, tmp): + tmp.write('\n'.join(tags.track(track)) + '\n') + tmp.close() + command = ['metaflac', '--no-utf8-convert', '--dont-use-padding', + '--remove-all-tags', '--import-tags-from', tmp.name, fn] + p = subprocess.Popen(command, stdout=subprocess.PIPE, + stderr=subprocess.STDOUT) + status = p.wait() + if status != 0: + raise SubprocessError(status, command=command, stderr=p.stdout) + def do_read(filenames): # Use this mapping of tag names to sets of tag values to detect global tags. all_tags = {} @@ -93,8 +106,16 @@ def do_write(args): track = i + 1 fn = '%s/%s/%s.flac' % (artist, album, tags.make_filename(track)) if fn != old_fn: - print tags.track(track) - #os.rename(old_fn, fn) + os.rename(old_fn, fn) + tmp = tempfile.NamedTemporaryFile(delete=False) + try: + rewrite_track_tags(track, tags, fn, tmp) + finally: + try: + os.unlink(tmp.name) + except: + pass + return 0 def main(args): if len(args) < 3: