def make_numbered_dir_with_cleanup(root, prefix, keep, lock_timeout):
e = None<EOL>for i in range(<NUM_LIT:10>):<EOL><INDENT>try:<EOL><INDENT>p = make_numbered_dir(root, prefix)<EOL>lock_path = create_cleanup_lock(p)<EOL>register_cleanup_lock_removal(lock_path)<EOL><DEDENT>except Exception as exc:<EOL><INDENT>e = exc<EOL><DEDENT>else:<EOL><INDENT>consider_lock_dead_if_created_before = p.stat().st_mtime - lock_timeout<EOL>cleanup_numbered_dir(<EOL>root=root,<EOL>prefix=prefix,<EOL>keep=keep,<EOL>consider_lock_dead_if_created_before=consider_lock_dead_if_created_before,<EOL>)<EOL>return p<EOL><DEDENT><DEDENT>assert e is not None<EOL>raise e<EOL>
creates a numbered dir with a cleanup lock and removes old ones