def _safe_dir_name(dir_name, need_dir=True):
    replace_try = ["", "-src", "_core"]
    for replace in replace_try:
        check = dir_name.replace(replace, "")
        if env.safe_exists(check):
            return check
    # still couldn't find it, it's a nasty one
    for check_part in (dir_name.split("-")[0].split("_")[0],
                       dir_name.split("-")[-1].split("_")[-1],
                       dir_name.split(".")[0],
                       dir_name.lower().split(".")[0]):
        with settings(hide('warnings', 'running', 'stdout', 'stderr'),
                      warn_only=True):
            dirs = env.safe_run_output("ls -d1 *%s*/" % check_part).split("\n")
            dirs = [x for x in dirs if "cannot access" not in x and "No such" not in x]
        if len(dirs) == 1 and dirs[0]:
            return dirs[0]
    dirs = env.safe_run_output("find * -type d -maxdepth 0").split("\n")
    if len(dirs) == 1 and dirs[0]:
        return dirs[0]
    if need_dir:
        raise ValueError("Could not find directory %s" % dir_name)
dataset/ETHPy150Open chapmanb/cloudbiolinux/cloudbio/custom/shared.py/_safe_dir_name/original