import sys, os, fnmatch, string, re

#size_saved = 0
#size_saved = size_saved + 1

def clean_dir(path_now):
   size_saved = 0
   thumb_path = path_now + '/thumbs'
   files = get_dirs(thumb_path, 1, 1)
   for file in files:
      full_file       = path_now   + '/' + file
      full_thumb_file = thumb_path + '/' + file

      # we have a thumb without a picture
      if not os.access(full_file, os.F_OK) or file == 'Thumbs.db':
         size_saved += os.stat(full_thumb_file).st_size
         os.remove(full_thumb_file)
         print "extraneous file %s" % thumb_path + file

      # we have an out of date thumbnail (thumb newer than the image)
      elif os.stat(full_thumb_file).st_mtime < \
         os.stat(full_file).st_mtime:
         os.remove(full_thumb_file)
         print "file %s needs updating" % (thumb_path + '/' + file)

   # scan again for pictures that need a thumb
   image_matcher = re.compile('^.*\.(jpg|jpeg|png|gif|bmp)$', re.IGNORECASE)
   files = get_dirs(path_now, 1, 1)
   for file in files:
     full_file       = path_now   + '/' + file
     full_thumb_file = thumb_path + '/' + file

     if not os.access(full_thumb_file, os.F_OK):
        if image_matcher.match(file):
           print "file %s needs a thumb" % (full_file)
           os.system('convert "' + full_file + '" -strip -thumbnail \'200x150>\' miff:- | composite -gravity center  -  -size 200x150  xc:black miff:- "' + full_thumb_file + '"')
   return size_saved

def get_dirs(path_now, file_mode = 0, short_path = 0):
   result = []
   try:
      names = os.listdir(path_now)
   except os.error:
      sys.exit(1)

   for name in names:
      full_name = os.path.normpath(os.path.join(path_now, name))
      if (not file_mode and os.path.isdir(full_name)) or \
             (file_mode and os.path.isfile(full_name)):
          if short_path:
             result.append(name)
          else:
             result.append(full_name)
   return result

root = "/home/httpd/twiki/pub"

webs = get_dirs(root)

deleted_size = 0

for web in webs:
   topics = get_dirs(web)
   for topic in topics:
      if os.access(topic + '/thumbs', os.F_OK):
         deleted_size += clean_dir(topic)

print "deleted %d bytes" % deleted_size

-- MattWalsh - 28 Aug 2004

Topic revision: r1 - 28 Aug 2004 - MattWalsh
 
This site is powered by the TWiki collaboration platformCopyright © 2008-2012 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback