def main(): # Parse our config Conf = load_config('gallerpy.conf') started = time.time() # Work out where we're going if len(sys.argv) == 2: walkdir = os.path.abspath(sys.argv[1]) elif 'root_local' in Conf: walkdir = os.path.abspath(Conf['root_local']) else: walkdir = os.path.abspath('.') walklen = len(os.sep) + len(walkdir) if 'thumbs_local' not in Conf: Conf['thumbs_local'] = os.path.join(walkdir, 'thumbs') if 'resized_local' not in Conf: Conf['resized_local'] = os.path.join(walkdir, '_resized') # Make sure our thumbs dir exists if not os.path.exists(Conf['thumbs_local']): print "ERROR: %s doesn't exist!" % (Conf['thumbs_local']) sys.exit(1) # Change to the root dir and off we go os.chdir(walkdir) os.umask(0000) made = 0 thumbs = {} for root, dirs, files in os.walk(walkdir): for hide in Conf['hide_dirs']: if hide in dirs: dirs.remove(hide) dirs.sort() files.sort() if root == walkdir: continue print '> Entering %s' % (root[walklen:]) newthumbs, _, images, warns = generate_thumbnails(Conf, root[walklen:], files, sizes=0) for warning in warns: print warning made += newthumbs for img in images: thumbs[img[5]] = None # Done print print 'Generated %d thumbnail(s) in %.1fs' % (made, time.time() - started) # Now clean up any missing thumbs deadthumbs = 0 for filename in os.listdir(Conf['thumbs_local']): if filename in thumbs: continue filepath = os.path.join(Conf['thumbs_local'], filename) if not os.path.isfile(filepath): continue os.remove(filepath) deadthumbs += 1 if deadthumbs: print 'Removed %d stale thumbnails' % (deadthumbs)
def UpdateThumbs(image_name): # Ask dircache for a list of files files = dircache.listdir(Paths['current']) if UsingSCGI: if Paths['current'] in CACHE: if files is CACHE[Paths['current']][0]: return CACHE[Paths['current']][1] # Get a sorted list of filenames lfiles = list(files) if Conf['sort_alphabetically']: temp = [(f.lower(), f) for f in lfiles] temp.sort() lfiles = [f[1] for f in temp] del temp else: lfiles.sort() # Initialise the data structure data = { 'dirs': [], 'images': [], } if Paths['current'] == '.': try: lfiles.remove(Conf['folder_image']) except ValueError: pass # If they want just a single image, we only have to update 1-3 thumbs... # unless we're using SCGI, then we should always update the whole # directory to save time later. if image_name and not UsingSCGI: for i in range(len(lfiles)): if lfiles[i] == image_name: newfiles = [] # Search backwards for a valid image for the prev link if i > 0: for j in range(i-1, -1, -1): if IMAGE_RE.search(lfiles[j]): newfiles.append(lfiles[j]) break # This image newfiles.append(lfiles[i]) # Search forwards for a valid image for the next link if i < (len(lfiles) - 1): for j in range(i+1, len(lfiles)): if IMAGE_RE.search(lfiles[j]): newfiles.append(lfiles[j]) break lfiles = newfiles break # Time to generate stuff! newthumbs, data['dirs'], data['images'], warnings = generate_thumbnails(Conf, Paths['current'], lfiles) # If it's not the root dir, add '..' to the list of dirs if Paths['current'] != '.': data['dirs'].insert(0, '..') # If we had any warnings, stick them into the errors thing Warnings.extend(warnings) # If we're using SCGI, cache the info if UsingSCGI: CACHE[Paths['current']] = [files, data] # Throw the info back return data