()
| 344 | |
| 345 | |
| 346 | def main(): |
| 347 | username = '' |
| 348 | password = '' |
| 349 | account = '' |
| 350 | try: |
| 351 | opts, args = getopt.getopt(sys.argv[1:], 'vqu:p:a:') |
| 352 | except getopt.GetoptError, msg: |
| 353 | log('%s\n%s' % (msg, __doc__), abort=True) |
| 354 | for opt, val in opts: |
| 355 | if opt == '-v': globals['verbose'] += 1 |
| 356 | if opt == '-q': globals['verbose'] = 0 |
| 357 | if opt == '-u': username = val |
| 358 | if opt == '-p': password = val |
| 359 | if opt == '-a': account = val |
| 360 | |
| 361 | if not args: |
| 362 | log('No action given\n' + __doc__, abort=True) |
| 363 | |
| 364 | action = args[0] |
| 365 | if action not in ('store', 'retrieve', 'remove', 'info'): |
| 366 | log('Unknown action: %s\n%s' % (action, __doc__), abort=True) |
| 367 | |
| 368 | if len(args) == 1: |
| 369 | log('Missing hostname\n' + __doc__, abort=True) |
| 370 | |
| 371 | args[1] = args[1].split(':') |
| 372 | host = args[1][0] |
| 373 | if len(args[1]) == 1: |
| 374 | port = 21 |
| 375 | else: |
| 376 | port = int(args[1][1]) |
| 377 | |
| 378 | remotedir = '/' |
| 379 | if len(args) > 2: |
| 380 | remotedir = os.path.normpath(args[2]) |
| 381 | if not remotedir.startswith('/'): |
| 382 | log('Invalid remotedir, must start with /', abort=True) |
| 383 | |
| 384 | localdir = os.getcwd() |
| 385 | if len(args) > 3: |
| 386 | if action not in ('store', 'retrieve'): |
| 387 | log('Too many arguments\n%s' % __doc__, abort=True) |
| 388 | localdir = os.path.abspath(args[3]) |
| 389 | if not os.path.isdir(localdir): |
| 390 | log('localdir does not exist: %s' % localdir, abort=True) |
| 391 | |
| 392 | if len(args) > 4: |
| 393 | log('Too many arguments\n%s' % __doc__, abort=True) |
| 394 | |
| 395 | if not username: |
| 396 | username = 'anonymous' |
| 397 | elif not password and globals['verbose']: |
| 398 | password = getpass.getpass('FTP Password: ') |
| 399 | |
| 400 | ftp = ftplib.FTP() |
| 401 | if globals['verbose'] > 2: |
| 402 | ftp.set_debuglevel(globals['verbose']-2) |
| 403 | ftp.connect(host, port) |
no test coverage detected