#! /usr/bin/env python ############################################################################## # This script is a random signature generator. It reads all files in the # directory given as a parameter and selects one of the files from that dir # and copies it to $HOME/.signature. The names of the files does not matter, # and all files in the dir are considered. ############################################################################## import os, sys import os.path import whrandom, shutil def usage (): """Prints usage info and exits.""" print "usage: %s directory" % sys.argv[0] print " where dir contains the possible signature files." sys.exit ( 1 ) # home dir try: home = os.getenv("HOME") except: # ouch, failed. well, use hardcoded home = '/home/chakie/' # did we get enough parameters? if len ( sys.argv ) != 2: # nope usage () # get the dir dir = sys.argv [1] # ok, so is it really a dir if not os.path.isdir ( dir ): # nope usage () # no files yet files = [] # get all files in the dir for entry in os.listdir ( dir ): # is it a file? if os.path.isfile ( dir + '/' + entry ): # yep, add it files.append ( dir + '/' + entry ) # did we get any files if len ( files ) == 0: # no files? print "no possible signature files in %s" % dir sys.exit ( 0 ) # get the random index and the file file = files [ whrandom.randint ( 0, len ( files ) -1 ) ] # open the signature file shutil.copyfile ( file, home + '/.signature' ) print "new signature: %s" % file