| 1 |
|
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
|
|---|
| 22 |
|
|---|
| 23 |
|
|---|
| 24 |
|
|---|
| 25 |
|
|---|
| 26 |
|
|---|
| 27 |
|
|---|
| 28 |
|
|---|
| 29 |
|
|---|
| 30 |
|
|---|
| 31 |
|
|---|
| 32 |
|
|---|
| 33 |
|
|---|
| 34 |
""" |
|---|
| 35 |
This module implements a mock version of the standard library's |
|---|
| 36 |
`ftplib.py` module. Some code is taken from there. |
|---|
| 37 |
|
|---|
| 38 |
Not all functionality is implemented, only that what is used to |
|---|
| 39 |
run the unit tests. |
|---|
| 40 |
""" |
|---|
| 41 |
|
|---|
| 42 |
import ftplib |
|---|
| 43 |
import StringIO |
|---|
| 44 |
|
|---|
| 45 |
DEBUG = 0 |
|---|
| 46 |
|
|---|
| 47 |
|
|---|
| 48 |
|
|---|
| 49 |
|
|---|
| 50 |
mock_files = {} |
|---|
| 51 |
|
|---|
| 52 |
def content_of(path): |
|---|
| 53 |
return mock_files[path].getvalue() |
|---|
| 54 |
|
|---|
| 55 |
|
|---|
| 56 |
class MockFile(StringIO.StringIO): |
|---|
| 57 |
""" |
|---|
| 58 |
Mock class for the file objects _contained in_ `_FTPFile` objects |
|---|
| 59 |
(not `_FTPFile` objects themselves!). |
|---|
| 60 |
|
|---|
| 61 |
Unless `StringIO.StringIO` instances, `MockFile` objects can be |
|---|
| 62 |
queried for their contents after they have been closed. |
|---|
| 63 |
""" |
|---|
| 64 |
def __init__(self, path, content=''): |
|---|
| 65 |
global mock_files |
|---|
| 66 |
mock_files[path] = self |
|---|
| 67 |
StringIO.StringIO.__init__(self, content) |
|---|
| 68 |
|
|---|
| 69 |
def getvalue(self): |
|---|
| 70 |
if not self.closed: |
|---|
| 71 |
return StringIO.StringIO.getvalue(self) |
|---|
| 72 |
else: |
|---|
| 73 |
return self._value_after_close |
|---|
| 74 |
|
|---|
| 75 |
def close(self): |
|---|
| 76 |
if not self.closed: |
|---|
| 77 |
self._value_after_close = StringIO.StringIO.getvalue(self) |
|---|
| 78 |
StringIO.StringIO.close(self) |
|---|
| 79 |
|
|---|
| 80 |
|
|---|
| 81 |
class MockSocket(object): |
|---|
| 82 |
""" |
|---|
| 83 |
Mock class which is used to return something from |
|---|
| 84 |
`MockSession.transfercmd`. |
|---|
| 85 |
""" |
|---|
| 86 |
def __init__(self, path, mock_file_content=''): |
|---|
| 87 |
if DEBUG: |
|---|
| 88 |
print 'File content: *%s*' % mock_file_content |
|---|
| 89 |
self.file_path = path |
|---|
| 90 |
self.mock_file_content = mock_file_content |
|---|
| 91 |
|
|---|
| 92 |
def makefile(self, mode): |
|---|
| 93 |
return MockFile(self.file_path, self.mock_file_content) |
|---|
| 94 |
|
|---|
| 95 |
def close(self): |
|---|
| 96 |
pass |
|---|
| 97 |
|
|---|
| 98 |
|
|---|
| 99 |
class MockSession(object): |
|---|
| 100 |
""" |
|---|
| 101 |
Mock class which works like `ftplib.FTP` for the purpose of the |
|---|
| 102 |
unit tests. |
|---|
| 103 |
""" |
|---|
| 104 |
|
|---|
| 105 |
current_dir = '/home/sschwarzer' |
|---|
| 106 |
|
|---|
| 107 |
|
|---|
| 108 |
dir_contents = { |
|---|
| 109 |
'/': """\ |
|---|
| 110 |
drwxr-xr-x 2 45854 200 512 May 4 2000 home""", |
|---|
| 111 |
|
|---|
| 112 |
'/home': """\ |
|---|
| 113 |
drwxr-sr-x 2 45854 200 512 May 4 2000 sschwarzer |
|---|
| 114 |
-rw-r--r-- 1 45854 200 4605 Jan 19 1970 older |
|---|
| 115 |
-rw-r--r-- 1 45854 200 4605 Jan 19 2020 newer |
|---|
| 116 |
lrwxrwxrwx 1 45854 200 21 Jan 19 2002 link -> sschwarzer/index.html |
|---|
| 117 |
lrwxrwxrwx 1 45854 200 15 Jan 19 2002 bad_link -> python/bad_link""", |
|---|
| 118 |
|
|---|
| 119 |
'/home/python': """\ |
|---|
| 120 |
lrwxrwxrwx 1 45854 200 7 Jan 19 2002 link_link -> ../link |
|---|
| 121 |
lrwxrwxrwx 1 45854 200 14 Jan 19 2002 bad_link -> /home/bad_link""", |
|---|
| 122 |
|
|---|
| 123 |
'/home/sschwarzer': """\ |
|---|
| 124 |
total 14 |
|---|
| 125 |
drwxr-sr-x 2 45854 200 512 May 4 2000 chemeng |
|---|
| 126 |
drwxr-sr-x 2 45854 200 512 Jan 3 17:17 download |
|---|
| 127 |
drwxr-sr-x 2 45854 200 512 Jul 30 17:14 image |
|---|
| 128 |
-rw-r--r-- 1 45854 200 4604 Jan 19 23:11 index.html |
|---|
| 129 |
drwxr-sr-x 2 45854 200 512 May 29 2000 os2 |
|---|
| 130 |
lrwxrwxrwx 2 45854 200 6 May 29 2000 osup -> ../os2 |
|---|
| 131 |
drwxr-sr-x 2 45854 200 512 May 25 2000 publications |
|---|
| 132 |
drwxr-sr-x 2 45854 200 512 Jan 20 16:12 python |
|---|
| 133 |
drwxr-sr-x 6 45854 200 512 Sep 20 1999 scios2""", |
|---|
| 134 |
|
|---|
| 135 |
|
|---|
| 136 |
'.': """\ |
|---|
| 137 |
|
|---|
| 138 |
, |
|---|
| 139 |
|
|---|
| 140 |
|
|---|
| 141 |
|
|---|
| 142 |
'sschwarzer': "", |
|---|
| 143 |
|
|---|
| 144 |
'/home/msformat': """\ |
|---|
| 145 |
10-23-01 03:25PM <DIR> WindowsXP |
|---|
| 146 |
12-07-01 02:05PM <DIR> XPLaunch |
|---|
| 147 |
07-17-00 02:08PM 12266720 abcd.exe |
|---|
| 148 |
07-17-00 02:08PM 89264 O2KKeys.exe""", |
|---|
| 149 |
|
|---|
| 150 |
'/home/msformat/XPLaunch': """\ |
|---|
| 151 |
10-23-01 03:25PM <DIR> WindowsXP |
|---|
| 152 |
12-07-01 02:05PM <DIR> XPLaunch |
|---|
| 153 |
12-07-01 02:05PM <DIR> empty |
|---|
| 154 |
07-17-00 02:08PM 12266720 abcd.exe |
|---|
| 155 |
07-17-00 02:08PM 89264 O2KKeys.exe""", |
|---|
| 156 |
|
|---|
| 157 |
'/home/msformat/XPLaunch/empty': "total 0", |
|---|
| 158 |
} |
|---|
| 159 |
|
|---|
| 160 |
|
|---|
| 161 |
mock_file_content = '' |
|---|
| 162 |
|
|---|
| 163 |
def __init__(self, host='', user='', password=''): |
|---|
| 164 |
self.closed = 0 |
|---|
| 165 |
|
|---|
| 166 |
|
|---|
| 167 |
self._transfercmds = 0 |
|---|
| 168 |
|
|---|
| 169 |
def _remove_trailing_slash(self, path): |
|---|
| 170 |
if path != '/' and path.endswith('/'): |
|---|
| 171 |
path = path[:-1] |
|---|
| 172 |
return path |
|---|
| 173 |
|
|---|
| 174 |
def voidcmd(self, cmd): |
|---|
| 175 |
if DEBUG: |
|---|
| 176 |
print cmd |
|---|
| 177 |
if cmd == 'STAT': |
|---|
| 178 |
return 'MockSession server awaiting your commands ;-)' |
|---|
| 179 |
elif cmd.startswith('TYPE '): |
|---|
| 180 |
return |
|---|
| 181 |
elif cmd.startswith('SITE CHMOD'): |
|---|
| 182 |
raise ftplib.error_perm("502 command not implemented") |
|---|
| 183 |
else: |
|---|
| 184 |
raise ftplib.error_perm |
|---|
| 185 |
|
|---|
| 186 |
def pwd(self): |
|---|
| 187 |
return self.current_dir |
|---|
| 188 |
|
|---|
| 189 |
def cwd(self, path): |
|---|
| 190 |
path = self._remove_trailing_slash(path) |
|---|
| 191 |
self.current_dir = path |
|---|
| 192 |
|
|---|
| 193 |
def dir(self, path, callback=None): |
|---|
| 194 |
"Provide a callback function with each line of a directory listing." |
|---|
| 195 |
if DEBUG: |
|---|
| 196 |
print 'dir: %s' % path |
|---|
| 197 |
path = self._remove_trailing_slash(path) |
|---|
| 198 |
if not self.dir_contents.has_key(path): |
|---|
| 199 |
raise ftplib.error_perm |
|---|
| 200 |
dir_lines = self.dir_contents[path].split('\n') |
|---|
| 201 |
for line in dir_lines: |
|---|
| 202 |
if callback is None: |
|---|
| 203 |
print line |
|---|
| 204 |
else: |
|---|
| 205 |
callback(line) |
|---|
| 206 |
|
|---|
| 207 |
def voidresp(self): |
|---|
| 208 |
assert self._transfercmds == 1 |
|---|
| 209 |
self._transfercmds = self._transfercmds - 1 |
|---|
| 210 |
return '2xx' |
|---|
| 211 |
|
|---|
| 212 |
def transfercmd(self, cmd): |
|---|
| 213 |
""" |
|---|
| 214 |
Return a `MockSocket` object whose `makefile` method will |
|---|
| 215 |
return a mock file object. |
|---|
| 216 |
""" |
|---|
| 217 |
if DEBUG: |
|---|
| 218 |
print cmd |
|---|
| 219 |
|
|---|
| 220 |
cmd, path = cmd.split() |
|---|
| 221 |
path = self._remove_trailing_slash(path) |
|---|
| 222 |
if self.dir_contents.has_key(path): |
|---|
| 223 |
raise ftplib.error_perm |
|---|
| 224 |
|
|---|
| 225 |
|
|---|
| 226 |
if (cmd, path) == ('RETR', 'notthere'): |
|---|
| 227 |
raise ftplib.error_perm |
|---|
| 228 |
assert self._transfercmds == 0 |
|---|
| 229 |
self._transfercmds = self._transfercmds + 1 |
|---|
| 230 |
return MockSocket(path, self.mock_file_content) |
|---|
| 231 |
|
|---|
| 232 |
def close(self): |
|---|
| 233 |
if not self.closed: |
|---|
| 234 |
self.closed = 1 |
|---|
| 235 |
assert self._transfercmds == 0 |
|---|
| 236 |
|
|---|