summaryrefslogtreecommitdiff
path: root/pbr/d2to1/tests/util.py
blob: 0337b7f0ae4625d29778a18ccd12ee1035641c58 (plain)
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
from __future__ import with_statement

import contextlib
import os
import shutil
import stat


from ConfigParser import ConfigParser


@contextlib.contextmanager
def open_config(filename):
    cfg = ConfigParser()
    cfg.read(filename)
    yield cfg
    with open(filename, 'w') as fp:
        cfg.write(fp)


def rmtree(path):
    """
    shutil.rmtree() with error handler for 'access denied' from trying to
    delete read-only files.
    """

    def onerror(func, path, exc_info):
        if not os.access(path, os.W_OK):
            os.chmod(path, stat.S_IWUSR)
            func(path)
        else:
            raise

    return shutil.rmtree(path, onerror=onerror)