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