Add 'apps/arv-web/' from commit 'f9732ad8460d013c2f28363655d0d1b91894dca5'
[arvados.git] / sdk / python / arvados / commands / _util.py
1 #!/usr/bin/env python
2
3 import argparse
4 import errno
5 import os
6
7 def _pos_int(s):
8     num = int(s)
9     if num < 0:
10         raise ValueError("can't accept negative value: %s" % (num,))
11     return num
12
13 retry_opt = argparse.ArgumentParser(add_help=False)
14 retry_opt.add_argument('--retries', type=_pos_int, default=3, help="""
15 Maximum number of times to retry server requests that encounter temporary
16 failures (e.g., server down).  Default 3.""")
17
18 def _ignore_error(error):
19     return None
20
21 def _raise_error(error):
22     raise error
23
24 def make_home_conf_dir(path, mode=None, errors='ignore'):
25     # Make the directory path under the user's home directory, making parent
26     # directories as needed.
27     # If the directory is newly created, and a mode is specified, chmod it
28     # with those permissions.
29     # If there's an error, return None if errors is 'ignore', else raise an
30     # exception.
31     error_handler = _ignore_error if (errors == 'ignore') else _raise_error
32     tilde_path = os.path.join('~', path)
33     abs_path = os.path.expanduser(tilde_path)
34     if abs_path == tilde_path:
35         return error_handler(ValueError("no home directory available"))
36     try:
37         os.makedirs(abs_path)
38     except OSError as error:
39         if error.errno != errno.EEXIST:
40             return error_handler(error)
41     else:
42         if mode is not None:
43             os.chmod(abs_path, mode)
44     return abs_path