def do_move(mode_dir, ext_done, del_dir_dst, dir_src, bn_dir_src, dir_dst, dir_tmp): """ - Move path directory to a temporary dir in 'dir_tmp' - Delete 'file.ext_done' files - Move path directory to 'dir_dst' """ dpath, tdir, tpath=do_common(mode_dir, bn_dir_src, del_dir_dst, dir_src, dir_dst, dir_tmp) if not mode_dir: ## next, delete all files with extension 'ext_done' code, maybe_files=get_root_files(tpath) if not code.startswith("ok"): rmdir(tdir) raise Exception("Can't get the filenames of temp dir: %s" % tpath) def _cmp(path): return path.endswith(ext_done) liste=filter(_cmp, maybe_files) logging.debug("Deleting '%s' files with extension '%s'" % (len(liste), ext_done)) for f in liste: code, _=rm(f) if not code.startswith("ok"): logging.warning("Can't delete file '%s'... aborting" % f) rmdir(tdir) return ## last, move to final destination logging.debug("Moving '%s' to final dir '%s'" % (tpath, dpath)) code, _=move(tpath, dpath) rmdir(tdir) logging.debug("Removed temp dir: %s" % tdir) if not code.startswith("ok"): raise Exception("Can't move '%s' to directory" % dpath) logging.info("Processed directory: %s" % dir_src)
def process(src_file, dst_file, enable_delete): """ 1. read file, extract URL 2. send "start" 3. send each line 4. send "end" 5. move/delete source file """ code, contents=file_contents(src_file) if not code.startswith("ok"): return ("error", "file/invalid") try: contents=contents.strip() lines=contents.split("\n") except: return ("error", "data/invalid") ############################################### try: stdout({"sp": src_file, "code":"begin"}) for line in lines: stdout({"code": "line", "line": line}) stdout({"sp": src_file, "code":"end"}) except: raise BrokenPipe("Broken Pipe") ############################################### if enable_delete: code, _msg=rm(src_file) if not code.startswith("ok"): logging.error("Can't delete '%s'" % src_file) return ("error", "file/delete") return ("ok", None) ### well then, we need to move the source_file code, _=move(src_file, dst_file) return (code, "file/move")
def do_common(mode_dir, bn_dir_src, del_dir_dst, dir_src, dir_dst, dir_tmp): dpath=maybe_del_dir(mode_dir, del_dir_dst, bn_dir_src, dir_dst) try: tdir=tempfile.mkdtemp(dir=dir_tmp) except: raise Exception("Can't create temporary directory") tpath=os.path.join(tdir, bn_dir_src) ## rename if mode_dir: tpath=os.path.splitext(tpath)[0] logging.debug("Moving to temp dir: %s => %s" % (dir_src, tpath)) code, _=move(dir_src, tpath) if not code.startswith("ok"): rm(tdir) raise Exception("Can't move '%s' in a temp directory" % tpath) return dpath, tdir, tpath