def _check_tmpdir(tmpdir): if not os.path.exists(tmpdir): os.mkdir(tmpdir) else: if not os.path.isdir(tmpdir): raise EnosFilePathError("%s is not a directory" % tmpdir) else: pass
def _set_resultdir(name=None): """Set or get the directory to store experiment results. Looks at the `name` and create the directory if it doesn"t exist or returns it in other cases. If the name is `None`, then the function generates an unique name for the results directory. Finally, it links the directory to `SYMLINK_NAME`. Args: name (str): file path to an existing directory. It could be weather an absolute or a relative to the current working directory. Returns: the file path of the results directory. """ # Compute file path of results directory resultdir_name = name or "enos_" + datetime.today().isoformat() resultdir_path = os.path.abspath(resultdir_name) # Raise error if a related file exists if os.path.isfile(resultdir_path): raise EnosFilePathError( resultdir_path, "Result directory cannot be created due " "to existing file %s" % resultdir_path, ) # Create the result directory if it does not exist if not os.path.isdir(resultdir_path): os.mkdir(resultdir_path) logger.info("Generate results directory %s" % resultdir_path) # Symlink the result directory with the "cwd/current" directory link_path = SYMLINK_NAME if os.path.lexists(link_path): os.remove(link_path) try: os.symlink(resultdir_path, link_path) logger.info("Symlink %s to %s" % (resultdir_path, link_path)) except OSError: # An harmless error can occur due to a race condition when # multiple regions are simultaneously deployed logger.warning("Symlink %s to %s failed" % (resultdir_path, link_path)) return resultdir_path