Permalink
Browse files

Safer caching.

By verifying that the cache file is referring to the
correct file path, in case of hash coliision.
  • Loading branch information...
1 parent 3742f36 commit bdf57e5577f00b3fc8940c16ad1588839e0a4164 @mythmon committed Jul 7, 2010
Showing with 21 additions and 17 deletions.
  1. +12 −6 playlistsync.py
  2. +9 −11 songfile.py
View
18 playlistsync.py
@@ -5,6 +5,7 @@
from progress import ProgressBar
from optparse import OptionParser
from mutagen.mp3 import HeaderNotFoundError
+import json
import unicodedata
cacheDir = os.path.join(os.getcwdu(), 'cache')
@@ -22,9 +23,14 @@ def getMetaData(songPath):
try:
f = open(cachePath, 'r')
- jsonData = f.read()
- song.fromJson(jsonData)
+ jsonData = json.load(f)
+ if jsonData['path'] == songPath:
+ song.fromJson(jsonData)
+ else:
+ print "Hash collision!"
+ raise LookupError()
except Exception as e:
+ print e
try:
song.load(songPath)
f = open(cachePath,'w')
@@ -36,7 +42,7 @@ def getMetaData(songPath):
return song
-def main(plistpath, dest):
+def main(plistpath, dest, flat=False, quiet=False, verbose=False):
plist = open(plistpath)
srcset = set()
destset = set()
@@ -125,7 +131,7 @@ def main(plistpath, dest):
data['album'] = sanitize(data['album'])
data['title'] = sanitize(data['title'])
newPath = ""
- if options.flat == False:
+ if flat == False:
artistDir = u"{0[root]}/{0[artist]}".format(data)
albumDir = artistDir + u"/{0[album]}".format(data)
newPath = albumDir + u"/{0[track]:0>2} {0[title]}.mp3".format(data)
@@ -159,7 +165,7 @@ def main(plistpath, dest):
data['album'] = sanitize(data['album'])
data['title'] = sanitize(data['title'])
newFile = ""
- if options.flat == False:
+ if flat == False:
artistDir = u"{0[root]}/{0[artist]}".format(data)
albumDir = artistDir + u"/{0[album]}".format(data)
newFile = albumDir + u"/{0[track]:0>2} {0[title]}.mp3".format(data)
@@ -194,4 +200,4 @@ def main(plistpath, dest):
plistpath = args[0]
dest = args[1]
- main(plistpath,dest)
+ main(plistpath,dest,flat=options.flat)
View
20 songfile.py
@@ -44,18 +44,16 @@ def toJson(self):
return j
# From json
- def fromJson(self, jsonString):
- data = json.loads(jsonString)
+ def fromJson(self, jsonData):
+ if int(jsonData['cache_version']) != CACHE_VERSION:
+ print jsonData['cache_version'], CACHE_VERSION
+ raise CacheVersionError(jsonData['cache_version'], CACHE_VERSION)
- if int(data['cache_version']) != CACHE_VERSION:
- print data['cache_version'], CACHE_VERSION
- raise CacheVersionError(data['cache_version'], CACHE_VERSION)
-
- self.mp3path = data['path']
- self.artist = [data['artist']]
- self.album = [data['album']]
- self.title = [data['title']]
- self.track = [data['track']]
+ self.mp3path = jsonData['path']
+ self.artist = [jsonData['artist']]
+ self.album = [jsonData['album']]
+ self.title = [jsonData['title']]
+ self.track = [jsonData['track']]
# ignore the path, just worry about the artist,album,title, etc
def __eq__(self, other):

0 comments on commit bdf57e5

Please sign in to comment.