Make sure the given directory exists and is empty. This function will create an empty directory if the directory doesn't exits, or it will clean all content under the directory. Hidden files and sub direcotries will be deleted if clear_hidden is True.
(dir, clear_hidden=True)
| 151 | |
| 152 | |
| 153 | def ensure_empty_dir(dir, clear_hidden=True): |
| 154 | """ |
| 155 | Make sure the given directory exists and is empty. |
| 156 | This function will create an empty directory if the directory doesn't exits, |
| 157 | or it will clean all content under the directory. Hidden files and sub |
| 158 | direcotries will be deleted if clear_hidden is True. |
| 159 | """ |
| 160 | if not os.path.exists(dir): |
| 161 | logger.info("make dir: " + dir) |
| 162 | os.makedirs(dir) |
| 163 | return |
| 164 | logger.info("clear dir: {}, clear hidden files: {}".format(dir, clear_hidden)) |
| 165 | for filename in os.listdir(dir): |
| 166 | if clear_hidden or not filename.startswith("."): |
| 167 | file_path = os.path.join(dir, filename) |
| 168 | if os.path.isfile(file_path) or os.path.islink(file_path): |
| 169 | os.unlink(file_path) |
| 170 | else: |
| 171 | shutil.rmtree(file_path, ignore_errors=True) |
| 172 | |
| 173 | |
| 174 | def which(cmd): |
no test coverage detected