import ftplib import re import string import os import os.path import sys class ftpBuConf: def getConf(self,f): """\ Reads a configuration file f. The values will be stored in the following variables: self.username : Username to the FTP server. self.password : Password to the FTP server. self.extlst : List of all the extensions of the files that must be downloaded. self.dirlst : List of all the folders to download and their corresponding place. """ self.dirlst = {} self.extlst = {} self.conf = file(f, "r") l = self.conf.readlines() #Regular expression that will be used later on verifyLine = re.compile("download .*? to .*? only .*?\n") if "local folder is " != l[0][0:16]: raise invalidConfiguration("Local folder must be defined in the first line.") else: root = l[0][16:-1] if not re.match("log in as .*? with password .*? to .*?", l[1]): raise invalidConfiguration("Log in information must be defined in the second line.") u, r = l[1].split(" with ") self.username = u[10:] p, d = r.split(" to ") self.password = p[9:] self.domain = d[:-1] for line in l[2:]: if verifyLine.match(line[0:-1]): raise invalidConfiguration("Incorrect format in line " + `l.index(line) + 1` + " of file " + os.path.abspath(f)) key, rest = line[9:-1].split(" to ") fol, ext = rest.split(" only ") exts = ext.replace(",", "|") self.dirlst[key] = os.path.join(root,fol) self.extlst[key] = exts class invalidConfiguration(Exception): def __init__(self, msg): self.value = "Invalid Configuration: " + msg def __str__(self): return repr(self.value) conf = ftpBuConf() if len(sys.argv) > 1: conf.getConf(sys.argv[1]) else: raise invalidConfiguration("Unknown file: No configuration file was given.") try: f = ftplib.FTP() f.connect(conf.domain) f.login(conf.username, conf.password) for dir in conf.dirlst.keys(): regex = re.compile(conf.extlst[dir]) lst = f.nlst(dir) lst = [x for x in lst if regex.search(string.lower(x)) != None] f.cwd(dir) for fn in lst: s = os.path.join(conf.dirlst[dir], fn) fl = file(s, "wb") f.retrbinary("retr " + fn, fl.write) print s fl.close() finally: f.quit()