]> diplodocus.org Git - flac-archive/commitdiff
Implement (and tested) do_write.
author <epg@pretzelnet.org> <>
Thu, 27 Dec 2012 02:50:16 +0000 (18:50 -0800)
committer <epg@pretzelnet.org> <>
Thu, 27 Dec 2012 02:50:16 +0000 (18:50 -0800)
rewrite-tags

index b20a8ea..c44a27b 100755 (executable)
@@ -1,6 +1,8 @@
 #! /usr/bin/python
 
 import os, sys
+import subprocess
+import tempfile
 from errno import EEXIST
 from subprocess import Popen, PIPE
 
@@ -34,6 +36,17 @@ def get_tags(fn):
 
     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 = Popen(command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
+    stdout, stderr = p.communicate()
+    status = p.wait()
+    if status != 0:
+        raise SubprocessError(status, command=command, stderr=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: