def process(ctx, subdirs): topic=ctx["topic"] path=ctx["path"] always=ctx["always"] minfo=map(getmtime, subdirs) entries=map(partial(getchanges, ctx), minfo) for entry in entries: path, mtime, result=entry maybe_tr, _=result if maybe_tr=="tr" or always: output(topic, path, mtime)
def getsubdirs(path, max_entries=None): """ >>> print getsubdirs("/tmp") ... """ try: paths=os.listdir(path) if max_entries is not None: paths=paths[0:max_entries] paths=map(partial(os.path.join, path), paths) dirs=filter(os.path.isdir, paths) return ("ok", dirs) except Exception,e: return ("error", e)
def run( path=None ,polling_interval=None ,topic=None ,always=None ,**_ ): code, path=resolve_path(path) if not code.startswith("ok"): raise Exception("Can't resolve path...: %s" % path) def loginfo(path, state, *_): logging.info("Path state '%s': %s" % (path, state)) ctx={"topic": topic, "path": path, "always":always} ctx["_path"]={ "previous": "ok" ,"ch": partial(loginfo, path) } tm=transition_manager(ctx) ppid=os.getppid() logging.info("Process pid: %s" % os.getpid()) logging.info("Parent pid : %s" % ppid) logging.info("Starting loop...") while True: if os.getppid()!=ppid: logging.warning("Parent terminated... exiting") break code, maybe_subdirs=getsubdirs(path) tm.send(("_path", code)) if code.startswith("ok"): process(ctx, maybe_subdirs) start_time=time.time() while True: ir, _w, _e=select.select([sys.stdin], [], [], polling_interval) if len(ir): iline=sys.stdin.readline() sys.stdout.write(iline) elapsed_time = time.time() - start_time if elapsed_time > polling_interval: break
def run(primary_path=None, compare_path=None, dest_path=None, status_filename=None, check_path=None ,just_basename=None ,topic_name=None ,exts=None ,wait_status=None, polling_interval=None ,just_zppp=None, just_ppzp=None, just_com=None ,**_): if check_path is not None: ct=check_transition() if dest_path: code, dest_path=resolve_path(dest_path) if not code.startswith("ok"): raise Exception("can't destination path '%s'" % dest_path) logging.info("Creating (if necessary) destination path: %s" % dest_path) code, msg=mkdir_p(dest_path) if code!="ok": raise Exception("Can't create path: %s" % dest_path) code, primary_path=resolve_path(primary_path) if not code.startswith("ok"): raise Exception("can't resolve primary path '%s'" % primary_path) logging.info("Creating (if necessary) primary path: %s" % primary_path) mkdir_p(primary_path) code, compare_path=resolve_path(compare_path) if not code.startswith("ok"): raise Exception("can't resolve compare path '%s'" % compare_path) logging.info("Creating (if necessary) compare path: %s" % compare_path) mkdir_p(compare_path) if wait_status: status_path=os.path.join(primary_path, status_filename) logging.info("Using status file path: %s" % status_path) else: status_path=None ### context for logging etc. ctx={ "just_zppp": just_zppp ,"just_ppzp": just_ppzp ,"just_com": just_com ,"just_list": just_zppp or just_ppzp or just_com ,"pp": primary_path ,"zp": compare_path ,"sp": status_path ,"pp_log" :{"up": partial(ilog, primary_path) ,"down": partial(wlog, primary_path) } ,"zp_log" :{"up": partial(ilog, compare_path) ,"down": partial(wlog, compare_path) } ,"topic_name": topic_name ,"exts": exts } ctx["tm"]=transition_manager(ctx) ppid=os.getppid() logging.info("Process pid: %s" % os.getpid()) logging.info("Parent pid: %s" % ppid) logging.info("Starting loop...") while True: if os.getppid()!=ppid: logging.warning("Parent terminated... exiting") break if check_path is not None: try: exists=os.path.exists(check_path) except: exists=False maybe_tr, _=ct.send(exists) if maybe_tr=="tr" and exists: logging.info("Check path: passed") if maybe_tr=="tr" and not exists: logging.info("Check path: failed - skipping") else: ## fake 'exists' exists=True if exists: code, msg=check_if_ok(status_path, default="ok") maybe_process(ctx, code, msg, primary_path, compare_path, just_basename, dest_path) logging.debug("...sleeping for %s seconds" % polling_interval) sleep(polling_interval)