def _file_archive_ensure_dir(parent_dir): """ Ensure that the parent directory for an archive exists. If a file exists where a directory is needed, then rename it (see bug 256376). @param parent_dir: path of parent directory @type parent_dir: str """ for parent in iter_parents(parent_dir): # Use lstat because a symlink to a directory might point # to a directory outside of the config archive, making # it an unsuitable parent. try: parent_st = os.lstat(parent) except OSError: pass else: if not stat.S_ISDIR(parent_st.st_mode): _file_archive_rotate(parent) break try: os.makedirs(parent_dir) except OSError: pass