File: util.py

package info (click to toggle)
freespace2 24.2.0%2Brepack-3
  • links: PTS, VCS
  • area: non-free
  • in suites: forky, sid
  • size: 43,740 kB
  • sloc: cpp: 595,005; ansic: 21,741; python: 1,174; sh: 457; makefile: 243; xml: 181
file content (46 lines) | stat: -rw-r--r-- 1,236 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# miscellaneous utilities

import os
import time
from functools import wraps

from requests import RequestException

GLOBAL_TIMEOUT = 30

def retry_multi(max_retries):
    """!
    @brief Decorator - Retry a function `max_retries` times.
    
    @param [in] `max_retries` Maximum number of times to retry the decorated function before giving up

    @details  Intended for use with functions from the `request` module, only catches `RequestException`'s
    @note This has been copied from https://stackoverflow.com/a/23892489
    """

    def retry(func):
        @wraps(func)
        def wrapper(*args, **kwargs):
            num_retries = 0
            while num_retries <= max_retries:
                try:
                    ret = func(*args, **kwargs)
                    break
                except RequestException:
                    if num_retries == max_retries:
                        raise
                    num_retries += 1
                    time.sleep(5)
            return ret

        return wrapper

    return retry


def expand_config_vars(config):
    """
    @brief Expands the shell variable for the repo string from `config`
    """

    config["git"]["repo"] = os.path.expandvars(config["git"]["repo"])