Thursday, September 09, 2004

A simple backup script

#backup.py
#
#backupdirs  - a list of complete paths of directories to backup
#excludes    - a list of subdirectories to be excluded
#backupdir   - the path to the directory where the backup should be written
#machinename - the name of the machine

backupdirs = []
excludes = []
backupdir = "."
machinename = "the_box"

from zipfile import ZipFile
import time

try:
    from itertools import chain
except:
    def chain(*iterators):
        for i in iterators:
            for j in i:
                yield j

def getAllFilesInPath(path):
    from os import listdir
    from os.path import isdir, join
    dirs = chain(*[getAllFilesInPath(join(path, filename))
           for filename in listdir(path)
           if isdir(join(path, filename)) and join(path, filename) not in excludes])
    files = [join(path,filename)
           for filename in listdir(path)
           if not isdir(join(path, filename))]
    return chain(files, dirs)
            

def backup():
    from os.path import join
    zf = ZipFile(time.strftime(join(backupdir,machinename+"%-d-%m-%Y.zip")),"w")
    for backupdir in backupdirs:
        for filename in getAllFilesInPath(backupdir):
            zf.write(filename)
    zf.close()

if __name__ == "__main__":
    backup()

Monday, September 06, 2004

Resizing images in python

# resize.py
# Simply put the file into the directory of images
# and run.
#
# A directory called resized is created, into which
# the resized images are put
#
# resizeFactor of 0.5 redueces the height and width
# by half

import Image
import glob
import os
from os.path import join

resizeFactor = 0.5

if not os.path.isdir("resized"):
    os.mkdir("resized")

for i in glob.glob("*.jpg"):
    im = Image.open(i)
    width, height = im.size
    print join("resized", i)
    im.resize((int(width*resizeFactor),
              int(height*resizeFactor))).save(join(os.curdir, "resized", i))