second try
[vsorcdistro/.git] / ryu / .eggs / pbr-5.3.1-py2.7.egg / pbr / tests / util.py
1 # Copyright (c) 2013 Hewlett-Packard Development Company, L.P.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 #    http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12 # implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15 #
16 # Copyright (C) 2013 Association of Universities for Research in Astronomy
17 #                    (AURA)
18 #
19 # Redistribution and use in source and binary forms, with or without
20 # modification, are permitted provided that the following conditions are met:
21 #
22 #     1. Redistributions of source code must retain the above copyright
23 #        notice, this list of conditions and the following disclaimer.
24 #
25 #     2. Redistributions in binary form must reproduce the above
26 #        copyright notice, this list of conditions and the following
27 #        disclaimer in the documentation and/or other materials provided
28 #        with the distribution.
29 #
30 #     3. The name of AURA and its representatives may not be used to
31 #        endorse or promote products derived from this software without
32 #        specific prior written permission.
33 #
34 # THIS SOFTWARE IS PROVIDED BY AURA ``AS IS'' AND ANY EXPRESS OR IMPLIED
35 # WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
36 # MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
37 # DISCLAIMED. IN NO EVENT SHALL AURA BE LIABLE FOR ANY DIRECT, INDIRECT,
38 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
39 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
40
41 import contextlib
42 import os
43 import shutil
44 import stat
45 import sys
46
47 try:
48     import ConfigParser as configparser
49 except ImportError:
50     import configparser
51
52
53 @contextlib.contextmanager
54 def open_config(filename):
55     if sys.version_info >= (3, 2):
56             cfg = configparser.ConfigParser()
57     else:
58             cfg = configparser.SafeConfigParser()
59     cfg.read(filename)
60     yield cfg
61     with open(filename, 'w') as fp:
62         cfg.write(fp)
63
64
65 def rmtree(path):
66     """shutil.rmtree() with error handler.
67
68     Handle 'access denied' from trying to delete read-only files.
69     """
70
71     def onerror(func, path, exc_info):
72         if not os.access(path, os.W_OK):
73             os.chmod(path, stat.S_IWUSR)
74             func(path)
75         else:
76             raise
77
78     return shutil.rmtree(path, onerror=onerror)