]> diplodocus.org Git - flac-archive/blobdiff - rewrite-tags
untested attempt to restore embedded cover art
[flac-archive] / rewrite-tags
index c44a27b5a93f7e304ffc6894aa863cd0d2050f71..eec265e548186c784ed78a26fb8f5f274bc984d9 100755 (executable)
@@ -4,7 +4,6 @@ import os, sys
 import subprocess
 import tempfile
 from errno import EEXIST
-from subprocess import Popen, PIPE
 
 from flac_archive.tags import Tags
 
@@ -21,18 +20,19 @@ 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
 
@@ -41,11 +41,11 @@ def rewrite_track_tags(track, tags, fn, tmp):
     tmp.close()
     command = ['metaflac', '--no-utf8-convert', '--dont-use-padding',
                '--remove-all-tags', '--import-tags-from', tmp.name, fn]
-    p = Popen(command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
-    stdout, stderr = p.communicate()
+    p = subprocess.Popen(command, stdout=subprocess.PIPE,
+                         stderr=subprocess.STDOUT)
     status = p.wait()
     if status != 0:
-        raise SubprocessError(status, command=command, stderr=stdout)
+        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.
@@ -60,7 +60,7 @@ def do_read(filenames):
         # this check belongs in Tags
         if len(track_tags) != 1:
             sys.stderr.write('bogus TRACKNUMBER %s: %s\n' % (track_tags, fn))
-            return 3
+            return 4
         track = int(track_tags[0])
         for tag, values in tags._global.iteritems():
             # Makes no sense to save TRACKNUMBER in coll_tags.
@@ -74,11 +74,18 @@ def do_read(filenames):
                 coll_tags.set(tag, value, track)
     for tag, values in all_tags.iteritems():
         if len(values) == 1:
-            # Only one value for this tag, so add it to global tags.
-            coll_tags.set(tag, list(values)[0])
-            # And now remove it from each track tags.
+            # Only one value for this tag, but does that one value appear on
+            # all tracks?
             for track, tags in coll_tags._tracks.iteritems():
-                del tags[tag]
+                if not tag in tags:
+                    # Nope.
+                    break
+            else:
+                # Yep, so add it to global tags.
+                coll_tags.set(tag, list(values)[0])
+                # And now remove it from each track tags.
+                for track, tags in coll_tags._tracks.iteritems():
+                    del tags[tag]
     print '\n'.join(coll_tags.all())
     return 0
 
@@ -88,9 +95,13 @@ def do_write(args):
     if len(args) != len(tags):
         sys.stderr.write('expected %d flac files, got %d\n'
                          % (len(tags), len(args)))
-        return 4
-    artist = tags.get_path_safe('ARTIST')
+        return 2
+    artist = tags.get_path_safe('ALBUMARTIST')
+    if not artist:
+        artist = tags.get_path_safe('ARTIST')
     album = tags.get_path_safe('ALBUM')
+    if not album:
+        album = tags.get_path_safe('ALBUM', track=1)
     try:
         os.mkdir(artist)
     except OSError, e:
@@ -120,10 +131,14 @@ def do_write(args):
 def main(args):
     if len(args) < 3:
         return usage()
-    if args[1] == 'read':
-        return do_read(args[2:])
-    if args[1] == 'write':
-        return do_write(args[2:])
+    try:
+        if args[1] == 'read':
+            return do_read(args[2:])
+        if args[1] == 'write':
+            return do_write(args[2:])
+    except SubprocessError, e:
+        sys.stderr.write('%s\n%s\n' % (e.stderr, e))
+        return 3
     return usage()
 
 def usage():