Felix Krull avatar Felix Krull committed b33e973

Fix collectiongain bug with directories that have non-ASCII characters.

Comments (0)

Files changed (1)

rgain/script/collectiongain.py

 # -*- coding: utf-8 -*-
+# kate: indent-width 4; indent-mode python
 #
 # Copyright (c) 2009, 2010 Felix Krull <f_krull@gmx.de>
 #
                 f.close()
             except NameError:
                 pass
-    
+
     return files
 
 
 def write_cache(cache_file, files):
     cache_dir = os.path.dirname(cache_file)
-    
+
     try:
         if not os.path.isdir(cache_dir):
             os.makedirs(cache_dir, 0755)
         tags = mutagen.File(properpath)
     except Exception, exc:
         raise Error(u"%s: error - %s" % (filepath, exc))
-    
+
     if ext == ".mp3":
         if "TALB" in tags:
             album = tags["TALB"].text[0]
             album = None
     else:
         album = tags.get("album", [""])[0]
-    
+
     if album:
         if ext == ".mp3":
             artist = None
             artist = tags.get("albumartist") or tags.get("artist")
             if artist:
                 artist = artist[0]
-        
+
         if not artist:
             artist = u""
         album_id = u"%s - %s" % (artist, album)
     else:
         album_id = None
-    
+
     return album_id
 
 
                                   sys.getfilesystemencoding())
             properpath = os.path.join(dirpath, filename)
             mtime = os.path.getmtime(properpath)
-            
+
             # check the cache
             if filepath in cache:
                 cache[filepath] = True
                 if mtime <= record[1]:
                     # the file's still ok
                     continue
-            
+
             ext = os.path.splitext(filename)[1]
             if ext in supported_formats:
                 i += 1
             albums.setdefault(album_id, []).append(filepath)
         else:
             single_tracks.append(filepath)
-    
+
     # purge anything that's marked as processed, if desired
     if not ignore_cache:
         for album_id, album_files in albums.items():
                     break
             if not keep:
                 del albums[album_id]
-        
+
         for filepath in single_tracks[:]:
             if files[filepath][2]:
                 single_tracks.remove(filepath)
-    
+
     return albums, single_tracks
 
 
         if not dry_run:
             update_cache(files, music_dir, single_tracks, None)
         print
-    
+
     for album_id, album_files in albums.iteritems():
         print ou(u"%s:" % album_id),
         do_gain((os.path.join(music_dir, path) for path in album_files),
 def do_collectiongain(music_dir, ref_level=89, force=False, dry_run=False,
                       mp3_format="ql", ignore_cache=False):
     music_dir = un(music_dir, sys.getfilesystemencoding())
-    
+
     music_abspath = os.path.abspath(music_dir)
-    musicpath_hash = md5(music_abspath).hexdigest()
+    musicpath_hash = md5(music_abspath.encode("utf-8")).hexdigest()
     cache_file = os.path.join(os.path.expanduser("~"), ".cache",
                               "collectiongain-cache.%s" % musicpath_hash)
-    
+
     # load the cache
     files = read_cache(cache_file)
-    
+
     # yeah, side-effects are bad, I know
     validate_cache(files)
     cache = dict.fromkeys(files.iterkeys(), False)
-    
+
     print "Collecting files ..."
-    
+
     # whenever this part is stopped (KeyboardInterrupt/other exception), the
     # cache is written to disk so all progress persists
     try:
                 del files[filepath]
         # hopefully gets rid of at least one huge data structure
         del cache
-    
+
         albums, single_tracks = transform_cache(files, ignore_cache)
         print
-        
+
         # gain everything that has survived the cleansing
         do_gain_all(music_dir, albums, single_tracks, files, ref_level, force,
                   dry_run, mp3_format)
     finally:
         validate_cache(files)
         write_cache(cache_file, files)
-    
+
     print "All finished."
 
 
 def collectiongain_options():
     opts = common_options()
-    
+
     opts.add_option("--ignore-cache", help="Don't trust implicit assumptions "
                     "about what was already done, instead check all files for "
                     "Replay Gain data explicitly.", dest="ignore_cache",
                     action="store_true")
-    
+
     opts.set_defaults(ignore_cache=False)
-    
+
     opts.set_usage("%prog [options] MUSIC_DIR")
     opts.set_description("Calculate Replay Gain for a large set of audio files "
                          "without asking many questions. This program "
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.