ensure a directory exists at a given path This is a version of os.mkdir, with the following differences: - returns True if it created the directory, False otherwise - ignores EEXIST, protecting against race conditions where the dir may have been created in between
(self, path, mode=None)
| 69 | self.check_dirs() |
| 70 | |
| 71 | def _mkdir(self, path, mode=None): |
| 72 | """ensure a directory exists at a given path |
| 73 | |
| 74 | This is a version of os.mkdir, with the following differences: |
| 75 | |
| 76 | - returns True if it created the directory, False otherwise |
| 77 | - ignores EEXIST, protecting against race conditions where |
| 78 | the dir may have been created in between the check and |
| 79 | the creation |
| 80 | - sets permissions if requested and the dir already exists |
| 81 | """ |
| 82 | if os.path.exists(path): |
| 83 | if mode and os.stat(path).st_mode != mode: |
| 84 | try: |
| 85 | os.chmod(path, mode) |
| 86 | except OSError: |
| 87 | self.log.warning( |
| 88 | "Could not set permissions on %s", |
| 89 | path |
| 90 | ) |
| 91 | return False |
| 92 | try: |
| 93 | if mode: |
| 94 | os.mkdir(path, mode) |
| 95 | else: |
| 96 | os.mkdir(path) |
| 97 | except OSError as e: |
| 98 | if e.errno == errno.EEXIST: |
| 99 | return False |
| 100 | else: |
| 101 | raise |
| 102 | |
| 103 | return True |
| 104 | |
| 105 | @observe('log_dir') |
| 106 | def check_log_dir(self, change=None): |
no outgoing calls
no test coverage detected