1 # -*- coding: utf-8 -*-
3 # Copyright (C) 2013 LEAP
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18 Implements file helper methods
27 logger = logging.getLogger(__name__)
30 def check_and_fix_urw_only(cert):
32 Test for 600 mode and try to set it if anything different found
36 :param cert: Certificate path
39 mode = stat.S_IMODE(os.stat(cert).st_mode)
41 if mode != int('600', 8):
43 logger.warning('Bad permission on %s attempting to set 600' %
45 os.chmod(cert, stat.S_IRUSR | stat.S_IWUSR)
47 logger.error('Error while trying to chmod 600 %s' %
52 def get_mtime(filename):
54 Returns the modified time or None if the file doesn't exist
56 :param filename: path to check
62 mtime = time.ctime(os.path.getmtime(filename)) + " GMT"
70 Creates the path and all the intermediate directories that don't
75 :param path: path to create
80 except OSError as exc:
81 if exc.errno == errno.EEXIST and os.path.isdir(path):
87 # Twisted implementation of which
88 def which(name, flags=os.X_OK, path_extension="/usr/sbin:/sbin"):
90 Search PATH for executable files with the given name.
92 On newer versions of MS-Windows, the PATHEXT environment variable will be
93 set to the list of file extensions for files considered executable. This
94 will normally include things like ".EXE". This fuction will also find files
95 with the given name ending with any of these extensions.
97 On MS-Windows the only flag that has any meaning is os.F_OK. Any other
98 flags will be ignored.
101 :param name: The name for which to search.
104 :param flags: Arguments to L{os.access}.
107 :param: A list of the full paths to files found, in the
108 order in which they were found.
112 exts = filter(None, os.environ.get('PATHEXT', '').split(os.pathsep))
113 path = os.environ.get('PATH', None)
114 path = path_extension + os.pathsep + path
117 parts = path.split(os.pathsep)
119 p = os.path.join(p, name)
120 if os.access(p, flags):
124 if os.access(pext, flags):