ensure a directory exists at a given path This is a version of os.mkdir, with the following differences: - returns whether the directory has been created or not. - ignores EEXIST, protecting against race conditions where the dir may have been created in between th
(self, path: str, mode: Optional[int] = None)
| 72 | self.check_dirs() |
| 73 | |
| 74 | def _mkdir(self, path: str, mode: Optional[int] = None) -> bool: |
| 75 | """ensure a directory exists at a given path |
| 76 | |
| 77 | This is a version of os.mkdir, with the following differences: |
| 78 | |
| 79 | - returns whether the directory has been created or not. |
| 80 | - ignores EEXIST, protecting against race conditions where |
| 81 | the dir may have been created in between the check and |
| 82 | the creation |
| 83 | - sets permissions if requested and the dir already exists |
| 84 | |
| 85 | Parameters |
| 86 | ---------- |
| 87 | path: str |
| 88 | path of the dir to create |
| 89 | mode: int |
| 90 | see `mode` of `os.mkdir` |
| 91 | |
| 92 | Returns |
| 93 | ------- |
| 94 | bool: |
| 95 | returns True if it created the directory, False otherwise |
| 96 | """ |
| 97 | |
| 98 | if os.path.exists(path): |
| 99 | if mode and os.stat(path).st_mode != mode: |
| 100 | try: |
| 101 | os.chmod(path, mode) |
| 102 | except OSError: |
| 103 | self.log.warning( |
| 104 | "Could not set permissions on %s", |
| 105 | path |
| 106 | ) |
| 107 | return False |
| 108 | try: |
| 109 | if mode: |
| 110 | os.mkdir(path, mode) |
| 111 | else: |
| 112 | os.mkdir(path) |
| 113 | except OSError as e: |
| 114 | if e.errno == errno.EEXIST: |
| 115 | return False |
| 116 | else: |
| 117 | raise |
| 118 | |
| 119 | return True |
| 120 | |
| 121 | @observe('log_dir') |
| 122 | def check_log_dir(self, change=None): |
no outgoing calls
no test coverage detected