ftp_deploy.utils.core: 76 total statements, 59.4% covered

Generated: Thu 2013-12-19 21:13 GMT

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

Stats: 41 executed, 28 missed, 7 excluded, 41 ignored

  1. import re
  2. import pycurl
  3. import certifi
  4. import StringIO
  5. from .ftp import ftp_check
  6. from .repo import bitbucket_check
  7. from ftp_deploy.conf import *
  8. class service_check(object):
  9. """Service check class, what group together all checking points. Return 'fails' and 'message' lists"""
  10. def __init__(self, service):
  11. self.service = service
  12. self.message = list()
  13. self.fails = [False, False, False, False]
  14. def check_log(self):
  15. """Check logs"""
  16. if self.service.pk is not None:
  17. log_fail = self.service.log_set.all().filter(status=False).filter(skip=False).count()
  18. if log_fail:
  19. self.message.append('<b>Log</b>: Deploy Fails(%d)' % log_fail)
  20. self.fails[0] = True
  21. def check_repo(self):
  22. """Check repositories connection, along with POST Hook"""
  23. if self.service.repo_source == 'bb':
  24. bb = bitbucket_check(BITBUCKET_SETTINGS['username'], BITBUCKET_SETTINGS['password'], self.service)
  25. bb_fail, bb_fail_message = bb.check_all()
  26. if bb_fail:
  27. self.message.append(bb_fail_message)
  28. self.fails[1] = True
  29. hook_fail, hook_fail_message = bb.check_hook_exist()
  30. if hook_fail:
  31. self.message.append(hook_fail_message)
  32. self.fails[2] = True
  33. def check_ftp(self):
  34. """Check FTP connection"""
  35. ftp = ftp_check(self.service.ftp_host, self.service.ftp_username, self.service.ftp_password, self.service.ftp_path)
  36. ftp_fail, ftp_fail_message = ftp.check_all()
  37. ftp.quit()
  38. if ftp_fail:
  39. self.message.append(ftp_fail_message)
  40. self.fails[3] = True
  41. def check_all(self):
  42. self.check_log()
  43. self.check_repo()
  44. self.check_ftp()
  45. return self.fails, self.message
  46. class commits_parser(object):
  47. """Commit parser for list of commits. Take commits dictionary captured from payload"""
  48. def __init__(self, commits):
  49. self.commits = commits
  50. def commits_info(self):
  51. """Return commits details list in format [message,author,raw_node]"""
  52. output = list()
  53. [output.append([commit['message'], commit['author'], commit['raw_node']]) for commit in reversed(self.commits)]
  54. return output
  55. def email_list(self):
  56. """Return email list from raw_author, limited to unique emails"""
  57. output = list()
  58. for commit in self.commits:
  59. email = re.search('%s(.*)%s' % ('<', '>'), commit['raw_author']).group(1)
  60. output.append(email) if email not in output else False
  61. return output
  62. def file_diff(self):
  63. """Return files list grouped by added, modified and removed. Respect order of commits"""
  64. added, removed, modified = list(), list(), list()
  65. for commit in self.commits:
  66. for file in commit['files']:
  67. if file['type'] == 'added':
  68. added.append(file['file']) if file['file'] not in added else False
  69. removed.remove(file['file']) if file['file'] in removed else False
  70. elif file['type'] == 'modified':
  71. modified.append(file['file']) if file['file'] not in modified and file['file'] not in added else False
  72. elif file['type'] == 'removed':
  73. removed.append(file['file']) if file['file'] not in removed else False
  74. added.remove(file['file']) if file['file'] in added else False
  75. modified.remove(file['file']) if file['file'] in modified else False
  76. return added, modified, removed
  77. class absolute_url(object):
  78. """Build absolute url to root url whthout trailing slash"""
  79. def __init__(self, request):
  80. self.request = request
  81. def build(self):
  82. return self.request.build_absolute_uri('/')[:-1]
  83. class LockError(Exception):
  84. """Exception if service is locked"""
  85. def __str__(self):
  86. return 'Deploy failed because service is Locked!'