def archive_recursive(path): for root, dirs, files in os.walk(path): leafs = dirs + files while leafs: name = leafs.pop(0) cpath = os.path.join(root, name) skip = ignored(cpath) if not skip: skip = illegal.search(cpath) is not None if skip: log(err, 'Illegal characters in %s', cpath) elif os.path.isfile(cpath): skip = 0 > os.path.getsize(cpath) > maxsize if skip: log(warn, 'Skipping: too large: %s', cpath) if skip: log(note, 'Ingored %s', cpath) if os.path.isdir(cpath): dirs.remove(name) else: files.remove(name) if not os.path.isdir(cpath): if not isarchive(cpath): archive(cpath, path, settings.volume.cabinet.root) else: pass#archived(cpath), cpath
def archived(path): """ Read archive date from path. """ dates = [] parts = delimiter.split(path) year, month, day = None, None, None while parts: part = parts.pop(0) if part.isdigit(): if not year: if len(part)==4: year = part else: log(warn, "Ignored %s in <%s>", part, path) else: if len(part) == 1: part = '0'+part if not month: if not (0 < int(part) <= 12): log(note, "Illegal month number %s after year %s <%s>", part, year, path) month = part else: if not (0 < int(part) <= 31): log(note, "Illegal day number %s for %s-%s <%s>", part, year, month, path) day = part if year and month and day: dates.append((year, month, day)) year, month, day = None, None, None return dates
def archive(path, root=None, archive_root=None): #datetuple = datetime.datetime.now().timetuple()[:3] # or use mtime try: st = os.stat(path) except: log(warn, "Skipping unreadable %s", path) return assert st datetuple = map(int, str(datetime.date.fromtimestamp( st.st_mtime)).split('-')) date = dict(itertools.izip( ('year','month','day'), datetuple)) ro = 0 if not root.startswith('.'): ro = len(root) newpath = os.path.join( archive_root, settings.rsr.archive_format % date, settings.rsr.archive_prefix + path[ro:]) if not settings.rsr.dry_run: assert os.path.exists(path), path assert os.path.isfile(path), path target_dir = os.path.dirname(newpath) #print path, newpath, target_dir if not os.path.exists(target_dir): os.makedirs(target_dir) os.rename(path, newpath) else: log(debug, 'Toarchive %s -> %s', path, newpath)
else: pass#archived(cpath), cpath if __name__ == '__main__': prsr = optparse.OptionParser(usage=usage_descr) for a,k in options_spec: if isinstance(a, tuple): prsr.add_option(*a, **k) else: prsr.add_option(a, **k) opts, args = prsr.parse_args() settings.rsr.override(opts) if not args: args = [os.getcwd()] log(note, "Running from: %s", args[0]) for path in args: if not os.path.exists(path): log(warn, "Path does not exist: %s", path) else: archive_recursive(path)