ftp_deploy.utils.ftp: 54 total statements, 100.0% covered

Generated: Sun 2014-01-12 11:05 GMT

Source file: /var/www/service.dev/service/ftp_deploy/utils/ftp.py

Stats: 51 executed, 0 missed, 3 excluded, 29 ignored

  1. import os
  2. from ftplib import FTP
  3. from .decorators import check
  4. class ftp_connection(object):
  5. """FTP Connection helper. Provide methods to manage ftp resources"""
  6. def __init__(self, host, username, password, ftp_path):
  7. self.host = host
  8. self.username = username
  9. self.password = password
  10. self.ftp_path = self.encode(ftp_path)
  11. self.connected = 0
  12. def connect(self):
  13. """Initialize FTP Connection"""
  14. self.ftp = FTP(self.host)
  15. self.ftp.login(self.username, self.password)
  16. self.connected = 1
  17. def create_file(self, file_path, content):
  18. """Create file populated with 'content' and save to 'file_path' location"""
  19. file_path = self.encode(file_path)
  20. self.ftp.storbinary('STOR ' + self.ftp_path + file_path, content)
  21. def remove_file(self, file_path):
  22. """Remove file from 'file_path' location, and clear empty directories"""
  23. file_path = self.encode(file_path)
  24. self.ftp.delete(self.ftp_path + file_path)
  25. dirname = file_path.split('/')
  26. for i in xrange(len(dirname)):
  27. current = '/'.join(dirname[:-1 - i])
  28. try:
  29. self.ftp.rmd(self.ftp_path + current)
  30. except Exception, e:
  31. return False
  32. def make_dirs(self, file_path):
  33. """ Create FTP tree directories based on 'file_path'"""
  34. file_path = self.encode(file_path)
  35. dirname = os.path.dirname(file_path).split('/')
  36. for i in xrange(len(dirname)):
  37. current = '/'.join(dirname[:i + 1])
  38. try:
  39. self.ftp.dir(self.ftp_path + current)
  40. except Exception, e:
  41. self.ftp.mkd(self.ftp_path + current)
  42. def encode(self, content):
  43. """Encode path string"""
  44. return str(content).encode('ascii', 'ignore')
  45. def quit(self):
  46. """Close FTP Connection"""
  47. return self.ftp.quit() if self.connected else False
  48. class ftp_check(ftp_connection):
  49. """Check FTP Connection, return True if fail"""
  50. @check('FTP')
  51. def check_ftp_login(self):
  52. self.connect()
  53. @check('FTP')
  54. def check_ftp_path(self):
  55. self.ftp.cwd(self.ftp_path)
  56. self.ftp.cwd('/')
  57. def check_all(self):
  58. status = self.check_ftp_login()
  59. if status[0] == True:
  60. return status
  61. status = self.check_ftp_path()
  62. if status[0] == True:
  63. return status
  64. return False, ''