mussa | fd5b805 | 2014-05-06 10:04:54 -0700 | [diff] [blame] | 1 | # Copyright (c) 2014 The Chromium OS Authors. All rights reserved. |
| 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
| 4 | |
| 5 | import errno |
| 6 | import os |
| 7 | import shutil |
| 8 | |
| 9 | |
| 10 | def rm_dir_if_exists(dir_to_remove): |
| 11 | """ |
| 12 | Removes a directory. Does not fail if the directory does NOT exist. |
| 13 | |
| 14 | @param dir_to_remove: path, directory to be removed. |
| 15 | |
| 16 | """ |
| 17 | try: |
| 18 | shutil.rmtree(dir_to_remove) |
| 19 | except OSError as e: |
mussa | 5fec82c | 2014-05-27 14:35:41 -0700 | [diff] [blame] | 20 | if e.errno != errno.ENOENT: |
mussa | fd5b805 | 2014-05-06 10:04:54 -0700 | [diff] [blame] | 21 | raise |
| 22 | |
| 23 | |
| 24 | def rm_dirs_if_exist(dirs_to_remove): |
| 25 | """ |
| 26 | Removes multiple directories. Does not fail if directories do NOT exist. |
| 27 | |
| 28 | @param dirs_to_remove: list of directory paths to be removed. |
| 29 | |
| 30 | """ |
| 31 | for dr in dirs_to_remove: |
| 32 | rm_dir_if_exists(dr) |
| 33 | |
| 34 | |
| 35 | def ensure_file_exists(filepath): |
| 36 | """ |
| 37 | Verifies path given points to an existing file. |
| 38 | |
| 39 | @param filepath: path, path to check. |
| 40 | |
| 41 | @raises IOError if the path given does not point to a valid file. |
| 42 | |
| 43 | """ |
| 44 | error_msg = 'File %s does not exist.' % filepath |
| 45 | if not os.path.isfile(filepath): |
| 46 | raise IOError(error_msg) |
| 47 | |
| 48 | |
| 49 | def ensure_all_files_exist(filepaths): |
| 50 | """ |
| 51 | Verifies all paths given point to existing files. |
| 52 | |
| 53 | @param filepaths: List of paths to check. |
| 54 | |
| 55 | @raises IOError if given paths do not point to existing files. |
| 56 | |
| 57 | """ |
| 58 | for filepath in filepaths: |
| 59 | ensure_file_exists(filepath) |
| 60 | |
| 61 | |
| 62 | def ensure_dir_exists(dirpath): |
| 63 | """ |
| 64 | Verifies path given points to an existing directory. |
| 65 | |
| 66 | @param dirpath: path, dir to check. |
| 67 | |
| 68 | @raises IOError if path does not point to an existing directory. |
| 69 | |
| 70 | """ |
| 71 | error_msg = 'Directory %s does not exist.' % dirpath |
| 72 | if not os.path.isdir(dirpath): |
| 73 | raise IOError(error_msg) |
| 74 | |
| 75 | |
| 76 | def ensure_all_dirs_exist(dirpaths): |
| 77 | """ |
| 78 | Verifies all paths given point to existing directories. |
| 79 | |
| 80 | @param dirpaths: list of directory paths to check. |
| 81 | |
| 82 | @raises IOError if given paths do not point to existing directories. |
| 83 | |
| 84 | """ |
| 85 | for dirpath in dirpaths: |
| 86 | ensure_dir_exists(dirpath) |
| 87 | |
| 88 | |
| 89 | def make_leaf_dir(dirpath): |
| 90 | """ |
| 91 | Creates a directory, also creating parent directories if they do not exist. |
| 92 | |
| 93 | @param dirpath: path, directory to create. |
| 94 | |
| 95 | @raises whatever exception raised other than "path already exist". |
| 96 | |
| 97 | """ |
| 98 | try: |
| 99 | os.makedirs(dirpath) |
| 100 | except OSError as e: |
| 101 | if e.errno != errno.EEXIST: |
| 102 | raise |
| 103 | |
| 104 | |
| 105 | def make_leaf_dirs(dirpaths): |
| 106 | """ |
| 107 | Creates multiple directories building all respective parent directories if |
| 108 | they do not exist. |
| 109 | |
| 110 | @param dirpaths: list of directory paths to create. |
| 111 | |
| 112 | @raises whatever exception raised other than "path already exists". |
| 113 | """ |
| 114 | for dirpath in dirpaths: |
| 115 | make_leaf_dir(dirpath) |