MCPcopy Create free account
hub / github.com/ActiveState/code / mirror

Function mirror

recipes/Python/576777_Simple_FTP_Mirror/recipe-576777.py:237–313  ·  view source on GitHub ↗
(src, dst, subdir='')

Source from the content-addressed store, hash-verified

235
236
237def mirror(src, dst, subdir=''):
238 src_path = os.path.normpath('%s/%s' % (src.root, subdir))
239 dst_path = os.path.normpath('%s/%s' % (dst.root, subdir))
240 log('Working on %s%s' % (src.host, src_path))
241
242 src_dirs, src_files = src.list(src_path)
243 if '.sfmstat' in src_files:
244 del src_files['.sfmstat']
245
246 globals['status']['dirs_total'] += len(src_dirs)
247 globals['status']['files_total'] += len(src_files)
248
249 dst_dirs, dst_files = dst.list(dst_path, True)
250 if '.sfmstat' in dst_files:
251 sfmstat = dst.readlines(os.path.join(dst_path, '.sfmstat'))
252 del dst_files['.sfmstat']
253 else:
254 if dst_path == dst.root and (dst_dirs or dst_files):
255 if globals['verbose']: abort = False
256 else: abort = True
257 log('New mirror, but target directory not empty!', abort=abort)
258 result = raw_input('Do you really want to replace this directory? [y|n]: ')
259 if result.lower() not in ('y', 'yes'):
260 log('Aborted', abort=True)
261 sfmstat = ['0 %s%s' % (src.host, src_path)]
262
263 last_updated, mirror_path = sfmstat[0].split(None, 1)
264 if mirror_path != (src.host + src_path):
265 if globals['verbose']: abort = False
266 else: abort = True
267 error = 'Mirror mismatch!\n%s already contains another mirror of %s' % (dst_path, mirror_path)
268 log(error, abort=abort)
269 result = raw_input('Do you really want to replace this mirror? [y|n]: ')
270 if result.lower() not in ('y', 'yes'):
271 log('Aborted', abort=True)
272 sfmstat = ['0 %s%s' % (src.host, src_path)]
273
274 for line in sfmstat[1:]:
275 mtime, file = line.split(None, 1)
276 if file in dst_files:
277 dst_files[file]['mtime'] = int(mtime)
278
279 for dir in dst_dirs:
280 if dir not in src_dirs:
281 path = os.path.join(dst_path, dir)
282 log('-> Remove directory %s' % path)
283 dst.removedir(path)
284
285 for file in dst_files:
286 if file not in src_files:
287 dst_file = os.path.join(dst_path, file)
288 log('-> Remove file %s: %s' % (dst_file, strfbytes(dst_files[file]['size'])))
289 dst.removefile(dst_file)
290
291 newstat = ['%i %s%s' % (int(time.time()), src.host, src_path)]
292 for file in src_files:
293 if file not in dst_files or src_files[file]['mtime'] > dst_files[file]['mtime'] or src_files[file]['size'] != dst_files[file]['size']:
294 src_file = os.path.join(src_path, file)

Callers 1

mainFunction · 0.85

Calls 14

strfbytesFunction · 0.85
logFunction · 0.70
listMethod · 0.45
readlinesMethod · 0.45
joinMethod · 0.45
lowerMethod · 0.45
splitMethod · 0.45
removedirMethod · 0.45
removefileMethod · 0.45
timeMethod · 0.45
storefileMethod · 0.45
appendMethod · 0.45

Tested by

no test coverage detected