Local file and directory functions
| 89 | |
| 90 | |
| 91 | class localHandler: |
| 92 | """ Local file and directory functions""" |
| 93 | def __init__(self, ftp, root): |
| 94 | self.ftp = ftp |
| 95 | self.root = root |
| 96 | self.host = '' |
| 97 | |
| 98 | def storefile(self, src, dst): |
| 99 | fh = open(dst, 'wb') |
| 100 | self.ftp.retrbinary('RETR %s' % src, fh.write) |
| 101 | fh.close() |
| 102 | |
| 103 | def storetext(self, text, dst): |
| 104 | fh = open(dst, 'w') |
| 105 | fh.write(text) |
| 106 | fh.close() |
| 107 | |
| 108 | def readlines(self, path): |
| 109 | fh = open(path, 'r') |
| 110 | buffer = [line.strip() for line in fh.readlines()] |
| 111 | fh.close() |
| 112 | return buffer |
| 113 | |
| 114 | def list(self, dir, skip_mtime=False): |
| 115 | dirs = [] |
| 116 | files = {} |
| 117 | for name in os.listdir(dir): |
| 118 | path = os.path.join(dir, name) |
| 119 | if os.path.isdir(path): |
| 120 | dirs.append(name) |
| 121 | else: |
| 122 | if skip_mtime: mtime = 0 |
| 123 | else: mtime = os.path.getmtime(path) |
| 124 | files[name] = { |
| 125 | 'size': os.path.getsize(path), |
| 126 | 'mtime': mtime, |
| 127 | } |
| 128 | return (dirs, files) |
| 129 | |
| 130 | def makedir(self, path): |
| 131 | log('--> Create directory %s' % path, 2) |
| 132 | os.mkdir(path) |
| 133 | globals['status']['dirs_created'] += 1 |
| 134 | |
| 135 | def removefile(self, path): |
| 136 | log('--> Remove file %s' % path, 2) |
| 137 | os.remove(path) |
| 138 | globals['status']['files_removed'] += 1 |
| 139 | |
| 140 | def removedir(self, dir): |
| 141 | for name in os.listdir(dir): |
| 142 | path = os.path.join(dir, name) |
| 143 | if os.path.isdir(path): |
| 144 | self.removedir(path) |
| 145 | else: |
| 146 | self.removefile(path) |
| 147 | log('--> Remove directory %s' % dir, 2) |
| 148 | os.rmdir(dir) |