123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778 |
- import contextlib
- import os
- import shutil
- import stat
- import sys
- try:
- import ConfigParser as configparser
- except ImportError:
- import configparser
- @contextlib.contextmanager
- def open_config(filename):
- if sys.version_info >= (3, 2):
- cfg = configparser.ConfigParser()
- else:
- cfg = configparser.SafeConfigParser()
- cfg.read(filename)
- yield cfg
- with open(filename, 'w') as fp:
- cfg.write(fp)
- def rmtree(path):
- """shutil.rmtree() with error handler.
- Handle '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)
|