Merge branch 'master' into 3140-project-content-tabs
[arvados.git] / sdk / python / arvados / commands / _util.py
1 #!/usr/bin/env python
2
3 import errno
4 import os
5
6 def _ignore_error(error):
7     return None
8
9 def _raise_error(error):
10     raise error
11
12 def make_home_conf_dir(path, mode=None, errors='ignore'):
13     # Make the directory path under the user's home directory, making parent
14     # directories as needed.
15     # If the directory is newly created, and a mode is specified, chmod it
16     # with those permissions.
17     # If there's an error, return None if errors is 'ignore', else raise an
18     # exception.
19     error_handler = _ignore_error if (errors == 'ignore') else _raise_error
20     tilde_path = os.path.join('~', path)
21     abs_path = os.path.expanduser(tilde_path)
22     if abs_path == tilde_path:
23         return error_handler(ValueError("no home directory available"))
24     try:
25         os.makedirs(abs_path)
26     except OSError as error:
27         if error.errno != errno.EEXIST:
28             return error_handler(error)
29     else:
30         if mode is not None:
31             os.chmod(abs_path, mode)
32     return abs_path