Optimize the filesystem scanning slightly

master
Bradlee Speice 2013-01-10 10:17:34 -05:00
parent 944593be94
commit d4d3ad450a
1 changed files with 15 additions and 17 deletions

View File

@ -43,7 +43,7 @@ class Archive (models.Model):
"Scan the archive's root filesystem and add any new songs without adding metadata, delete songs that exist no more" "Scan the archive's root filesystem and add any new songs without adding metadata, delete songs that exist no more"
#This method is implemented since the other scan methods all need to use the same code #This method is implemented since the other scan methods all need to use the same code
#DRY FTW #DRY FTW
import re, os import re, os, itertools
from django.core.exceptions import ObjectDoesNotExist from django.core.exceptions import ObjectDoesNotExist
from Melodia.melodia_settings import SUPPORTED_AUDIO_EXTENSIONS from Melodia.melodia_settings import SUPPORTED_AUDIO_EXTENSIONS
from Melodia.melodia_settings import HASH_FUNCTION as hash from Melodia.melodia_settings import HASH_FUNCTION as hash
@ -60,23 +60,21 @@ class Archive (models.Model):
#Add new songs #Add new songs
for dirname, dirnames, filenames in os.walk(self.root_folder): for dirname, dirnames, filenames in os.walk(self.root_folder):
#For each filename #For each filename that is supported
for filename in filenames: for filename in itertools.ifilter(lambda filename: re.match(regex, filename), filenames):
#If the filename is a supported audio extension #Make sure that `filename` is in the database
if re.match(regex, filename): try:
#Make sure that `filename` is in the database rel_url = os.path.join(dirname, filename)
try: full_url = os.path.abspath(rel_url)
rel_url = os.path.join(dirname, filename) self.songs.get(url = full_url)
full_url = os.path.abspath(rel_url)
self.songs.get(url = full_url)
except ObjectDoesNotExist, e: except ObjectDoesNotExist, e:
#Song needs to be added to database #Song needs to be added to database
rel_url = os.path.join(dirname, filename) rel_url = os.path.join(dirname, filename)
full_url = os.path.abspath(rel_url) full_url = os.path.abspath(rel_url)
new_song = Song(url = full_url) new_song = Song(url = full_url)
new_song.save() new_song.save()
self.songs.add(new_song) self.songs.add(new_song)
def _update_song_metadata(self, use_echonest = False, progress_callback = lambda x, y: None): def _update_song_metadata(self, use_echonest = False, progress_callback = lambda x, y: None):
"""Scan every song in this archive (database only) and make sure all songs are correct """Scan every song in this archive (database only) and make sure all songs are correct