Advertisement
Guest User

Untitled

a guest
Jan 30th, 2016
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.14 KB | None | 0 0
  1. #!/usr/bin/python
  2. from sys import argv
  3. from os.path import basename
  4. from ftplib import FTP, error_perm, error_reply
  5. import configparser
  6. import netrc
  7.  
  8. def _parse_config(filename):
  9. config = configparser.ConfigParser({
  10. 'port': '0',
  11. 'user': '',
  12. 'password': '',
  13. 'use_netrc': 'false'})
  14. config.read(filename)
  15. c = config['FTP']
  16. if 'host' not in c:
  17. raise configparser.NoOptionError()
  18. if c.getboolean('use_netrc'):
  19. rc = netrc.netrc()
  20. data = rc.authenticators(c['host'])
  21. if data is not None:
  22. c['user'], c['password'] = data[1:]
  23. return c['host'], c['port'], c['user'], c['password']
  24.  
  25. def ftpush(roots, push_files, config, remove_mode=False):
  26. host, port, user, password = _parse_config(config)
  27. with FTP() as ftp:
  28. print('Connect to server...')
  29. ftp.connect(host, int(port))
  30. ftp.login(user, password)
  31. for root, files in zip(roots, push_files):
  32. ftp.cwd(root)
  33. print('Change working directory to %s' % root)
  34. for f in files:
  35. name = basename(f)
  36. if remove_mode:
  37. print('Start removing %s as %s' % (f, name))
  38. try:
  39. ftp.delete(name)
  40. print('Successful removed %s' % name)
  41. except error_perm:
  42. print('Cannot delete file %s: permission error' % name)
  43. except error_reply:
  44. print('Cannot delete file %s: server error' % name)
  45. else:
  46. print('Start pushing %s as %s' % (f, name))
  47. ftp.storbinary('STOR ' + name, open(f, 'rb'))
  48. print('File %s successful pushed' % name)
  49. print('Quit server...')
  50. ftp.quit()
  51.  
  52. if __name__ == '__main__':
  53. config = 'ftpush.ini'
  54. roots = []
  55. files = []
  56. remove_mode = False
  57. i = -1
  58. set_root = False
  59. args = argv[1:]
  60. if args[0] == '--help':
  61. print('''
  62. Tool that help you push files to FTP server from scripts
  63. %s [-r] [--config config] --set-root root [files] [...] -- HELP:
  64. -r -- enables remove mode(default: False) when files removing instead of uploading to server
  65. --set-root root -- set root directory(on ftp server through cwd command) for files next in argumets to push
  66. --config config -- set path to config file(default: ftpush.ini)
  67.  
  68. Example of config file:
  69. [FTP]
  70. host = localhost
  71. user = alextalker # Optional
  72. password = my_cool_password # Optional
  73. use_netrc = true # Optional, default: false
  74. port = 666 # Optional
  75. ''' % argv[0])
  76. exit()
  77. if args[0] == '-r':
  78. remove_mode = True
  79. args = args[1:]
  80. if args[0] == '--config':
  81. config = args[1]
  82. args = args[2:]
  83. for v in args:
  84. if set_root:
  85. set_root = False
  86. roots.append(v)
  87. files.append([])
  88. continue
  89. if v == '--set-root':
  90. set_root = True
  91. i += 1
  92. continue
  93. files[i].append(v)
  94. ftpush(roots, files, config, remove_mode)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement