1 
 2 import sys, os, glob, shutil, blaster, logging
 3 
 4 class Downloader:
 5 
 6   def __init__(self, options, argv):
 7     self.options = options
 8     self.blaster = blaster.Blaster(options.server, 3412, argv)
 9 
10   def run(self):
11     logging.info("login as %s", self.options.login)
12     self.blaster.login(self.options.login, self.options.password, None)
13     logging.info("sessionId: %s", self.blaster.sessionId)
14     logging.info("serverIOR: %s", self.blaster.serverIOR)
15     try:
16       for file in glob.glob(os.path.abspath(self.options.files)):
17         try:
18           logging.info("Downloading %s", file)
19           content = open(file, "rb").read()
20           address, oid = os.path.split(file)
21           if self.options.move:
22             logging.info("backup %s to %s", oid, self.options.move)
23             shutil.copy(file, self.options.move)
24           logging.info("removing %s", file)
25           os.remove(file)
26           rqos = self.blaster.send(self.options.receiver, address, oid, content)
27           logging.info("%s", rqos)
28         except:
29           logging.exception("fail to download file %s", file)
30     finally:
31       logging.info("logout")
32       self.blaster.logout()
33     
34 def setupLog(options):
35   from warnings import filterwarnings
36   filterwarnings("ignore", category = DeprecationWarning, module = "logging")
37   hdlr = logging.StreamHandler()
38   fmt = logging.Formatter("%(asctime)s %(levelname)s %(message)s")
39   hdlr.setFormatter(fmt)
40   logging.getLogger().addHandler(hdlr)
41   logging.getLogger().setLevel(logging.INFO)
42   
43 def main(argv):
44   from optparse import OptionParser
45   parser = OptionParser("usage: \%prog [-f <filemask>] [-s <server>] [-l <login>] [-r <receiver>]")
46   parser.add_option("-f", "--files", dest="files", help="files mask", default="*.*")
47   parser.add_option("-s", "--server", dest="server", help="auth server host", default="localhost")
48   parser.add_option("-l", "--login", dest="login", help="login", default="")
49   parser.add_option("-p", "--password", dest="password", help="password", default="")
50   parser.add_option("-r", "--receiver", dest="receiver", help="message receiver", default="Diasoft5NT")
51   parser.add_option("-m", "--move", dest="move", help="move to directory", default="")
52   (options, args) = parser.parse_args(argv)
53   setupLog(options)
54   downloader = Downloader(options, [])
55   downloader.run()
56 
57 if __name__ == '__main__':
58   main(sys.argv)


syntax highlighted by Code2HTML, v. 0.9.1