def prepare_dir()

in utils/common_utils.py [0:0]


def prepare_dir(path, is_dir, allow_overwrite=False, clear_dir_if_exist=False, extra_info=None):
    """ to make dir if a dir or the parent dir of a file does not exist

    Args:
        path: can be a file path or a dir path.

    Returns:

    """
    if is_dir:
        if clear_dir_if_exist:
            allow_overwrite = True

        if not os.path.exists(path):
            os.makedirs(path)
        else:
            if not allow_overwrite:
                overwrite_option = input('The directory %s already exists, input "yes" to allow us to overwrite the directory contents and "no" to exit. (default:no): ' % path) \
                    if not extra_info else \
                    input('The directory %s already exists, %s, \ninput "yes" to allow us to operate and "no" to exit. (default:no): ' % (path, extra_info))
                if overwrite_option.lower() != 'yes':
                    exit(0)
            if (allow_overwrite or overwrite_option == 'yes') and clear_dir_if_exist:
                shutil.rmtree(path)
                logging.info('Clear dir %s...' % path)
                while os.path.exists(path):
                    time.sleep(0.3)
                os.makedirs(path)
    else:
        dir = os.path.dirname(path)
        if dir == '':       # when the path is only a file name, the dir would be empty and raise exception when making dir
            dir = '.'
        if not os.path.exists(dir):
            os.makedirs(dir)
        else:
            if os.path.exists(path) and allow_overwrite is False:
                overwrite_option = input('The file %s already exists, input "yes" to allow us to overwrite it or "no" to exit. (default:no): ' % path)
                if overwrite_option.lower() != 'yes':
                    exit(0)