Test coverage report: ftp_deploy.utils.ftp

# ftp_deploy.utils.ftp: 54 total statements, 23.5% covered

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

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

Stats: 12 executed, 39 missed, 3 excluded, 30 ignored

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