Ubuntu Pastebin

Paste from smoser at Wed, 28 Sep 2016 15:46:13 +0000

Download as text
 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
_SUBP_CACHE = {}
def cached_subp(args, data=None, rcs=None, env=None, capture=True,
                shell=False, logstring=False, decode="replace", target=None,
                update_env=None):
    def tupleize(d):
        if d is None:
            return d
        elif isinstance(d, tuple):
            return d
        elif isinstance(d, dict):
            return tuple(["%s=%s" % (k, env[k]) for k in sorted(d.keys())])
        elif isinstance(d, list):
            return tuple(d)
        else:
            return d

    smashed = tuple([tupleize(d) for d in 
                     (args, data, rcs, env, capture, shell, logstring, decode,
                      target, env)])

    if smashed not in _SUBP_CACHE:
        try:
            print("MISS!")
            _SUBP_CACHE[smashed] = subp(*smashed)
        except Exception as e:
            _SUBP_CACHE[smashed] = e

    r = _SUBP_CACHE[smashed]
    if isinstance(r, Exception):
        raise r
    return r
Download as text