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 main(env=os.environ, started=Started, scgi=0): global Started, UsingSCGI Started = started UsingSCGI = scgi t1 = time.time() # Some naughty globals global Conf, Paths, Warnings Paths = {} Warnings = [] # We need these global SCRIPT_FILENAME, SCRIPT_NAME SCRIPT_FILENAME = env.get('SCRIPT_FILENAME', None) SCRIPT_NAME = env.get('SCRIPT_NAME', None) # Find our config if SCRIPT_FILENAME is None or SCRIPT_NAME is None: return ShowError('CGI environment is broken!') config_file = os.path.join(os.path.dirname(SCRIPT_FILENAME), 'gallerpy.conf') if not os.path.isfile(config_file): return ShowError('config file is missing!') # Parse our config tc1 = time.time() Conf = load_config(config_file) tc2 = time.time() # Work out some paths if not ('thumbs_local' in Conf and 'thumbs_web' in Conf): Conf['thumbs_web'], Conf['thumbs_local'] = GetPaths('thumbs') if Conf['thumbs_local'] is None: return ShowError("Can't find your thumbnail directory!") if Conf['use_resized']: if not ('resized_local' in Conf and 'resized_web' in Conf): Conf['resized_web'], Conf['resized_local'] = GetPaths('_resized') if Conf['resized_local'] is None: return ShowError("Can't find your resized image directory!") Paths['folder_image'] = GetPaths(Conf['folder_image'])[0] or 'folder.png' Conf['template'] = os.path.join(os.path.dirname(SCRIPT_FILENAME), Conf['template']) # Work out what they're after path_info = env.get('PATH_INFO', None) or '.' # Don't want a starting or ending seperator if path_info.startswith('/'): path_info = path_info[1:] if path_info.endswith('/'): path_info = path_info[:-1] # If there's an image on the end, we want it image_name = None bits = list(path_info.split('/')) # See if they want a full image global FullImage FullImage = 0 if bits[-1] == '_full_': blah = bits.pop(-1) FullImage = 1 # See if they're after an image m = IMAGE_RE.search(bits[-1]) if m: image_name = bits.pop(-1) path_info = '/'.join(bits) or '.' # Don't let people go into hidden dirs if len(bits) > 0: if bits[-1] in Conf['hide_dirs']: return ShowError('Access denied: %s', path_info) # If we have a different local root, we need to change path if ('root_local' in Conf and 'root_web' in Conf): os.chdir(Conf['root_local']) # Check the path to make sure it's valid image_dir = GetPaths(path_info)[1] if image_dir is None: return ShowError('Path does not exist: %s', path_info) # We need to know what the current dir is Paths['current'] = path_info or '.' t2 = time.time() # Now that we've done all that, update the thumbnails data = UpdateThumbs(image_name) t3 = time.time() # If we have an image name, try to display it if image_name: tmpl = DisplayImage(data, image_name) # Or we could just display the directory else: tmpl = DisplayDir(data) # An error occurred if tmpl is None: return t4 = time.time() # Work out how long it took tmpl['elapsed'] = '%.3fs' % (time.time() - started) # If we had any warnings, add those if Warnings: tmpl['error'] = '<br />\n'.join(Warnings) else: tmpl.extract('show_error') t5 = time.time() # We are HTML! if Conf['show_header']: encoding = Conf.get('encoding', None) if encoding is not None: print 'Content-type: text/html; charset=%s' % (encoding) else: print 'Content-type: text/html' # If we're using GZIP, it might be time to squish if Conf['use_gzip'] and env.get('HTTP_ACCEPT_ENCODING', '').find('gzip') >= 0: import cStringIO import gzip zbuf = cStringIO.StringIO() gzip.GzipFile(mode='wb', fileobj=zbuf, compresslevel=Conf['use_gzip']).write(str(tmpl)) # Spit out our gzip headers print 'Content-Encoding: gzip' print 'Content-Length: %d' % (zbuf.tell()) print print zbuf.getvalue() # Just normal output please else: print print tmpl # Debug info if False: print 'startup: %.4fs<br />\n' % (t1 - Started) print 'startup(conf): %.4fs<br />\n' % (tc2 - tc1) print 'parse_env: %.4fs<br />\n' % (t2 - t1) print 'thumbs: %.4fs<br />\n' % (t3 - t2) print 'display: %.4fs<br />\n' % (t4 - t3) print 'finish_tmpl: %.4fs<br />\n' % (t5 - t4) print 'print_tmpl: %.4fs<br />\n' % (time.time() - t5)