|
15 | 15 |
|
16 | 16 | import os |
17 | 17 | import os.path |
| 18 | +import stat |
18 | 19 | import errno |
19 | 20 |
|
20 | 21 | import six |
@@ -262,7 +263,18 @@ def _write_data_files_to_disk(self, pack_ref, data_files): |
262 | 263 | file_path=file_path) |
263 | 264 |
|
264 | 265 | LOG.debug('Writing data file "%s" to "%s"' % (str(data_file), file_path)) |
265 | | - self._write_data_file(pack_ref=pack_ref, file_path=file_path, content=content) |
| 266 | + |
| 267 | + try: |
| 268 | + self._write_data_file(pack_ref=pack_ref, file_path=file_path, content=content) |
| 269 | + except (OSError, IOError) as e: |
| 270 | + # Throw a more user-friendly exception on Permission denied error |
| 271 | + if e.errno == errno.EACCES: |
| 272 | + msg = ('Unable to write data to "%s" (permission denied). Make sure ' |
| 273 | + 'permissions for that pack directory are configured correctly so ' |
| 274 | + 'st2api can write to it.' % (file_path)) |
| 275 | + raise ValueError(msg) |
| 276 | + raise e |
| 277 | + |
266 | 278 | written_file_paths.append(file_path) |
267 | 279 |
|
268 | 280 | return written_file_paths |
@@ -302,18 +314,8 @@ def _write_data_file(self, pack_ref, file_path, content): |
302 | 314 | mode = stat.S_IRWXU | stat.S_IRWXG | stat.S_IROTH | stat.S_IXOTH |
303 | 315 | os.makedirs(directory, mode) |
304 | 316 |
|
305 | | - try: |
306 | | - with open(file_path, 'w') as fp: |
307 | | - fp.write(content) |
308 | | - except IOError as e: |
309 | | - # Throw a more user-friendly exception on Permission denied error |
310 | | - if e.errno == errno.EACCES: |
311 | | - msg = ('Unable to write to pack directory "%s" (permission denied). Make sure ' |
312 | | - 'permissions for that directory are configured correctly so st2api can ' |
313 | | - 'write to it.' % (directory)) |
314 | | - raise ValueError(msg) |
315 | | - |
316 | | - raise e |
| 317 | + with open(file_path, 'w') as fp: |
| 318 | + fp.write(content) |
317 | 319 |
|
318 | 320 | def _dispatch_trigger_for_written_data_files(self, action_db, written_data_files): |
319 | 321 | trigger = ACTION_FILE_WRITTEN_TRIGGER['name'] |
|
0 commit comments