Package config_common :: Module utils
[hide private]
[frames] | no frames]

Source Code for Module config_common.utils

  1  # 
  2  # Copyright (c) 2008--2018 Red Hat, Inc. 
  3  # 
  4  # This software is licensed to you under the GNU General Public License, 
  5  # version 2 (GPLv2). There is NO WARRANTY for this software, express or 
  6  # implied, including the implied warranties of MERCHANTABILITY or FITNESS 
  7  # FOR A PARTICULAR PURPOSE. You should have received a copy of GPLv2 
  8  # along with this software; if not, see 
  9  # http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt. 
 10  # 
 11  # Red Hat trademarks are not licensed under GPLv2. No permission is 
 12  # granted to use or replicate Red Hat trademarks that are incorporated 
 13  # in this software or its documentation. 
 14  # 
 15   
 16  import os 
 17  # guaranteed to exist even on RHEL 5 because we now require python-hashlib 
 18  import hashlib 
 19  import re 
 20  import shutil 
 21  import pwd 
 22  import sys 
 23   
 24  try: # python2 
 25      import urlparse 
 26  except ImportError: # python3 
 27      import urllib.parse as urlparse 
 28   
 29  import inspect 
 30  from config_common.rhn_log import log_debug 
 31  from rhn.i18n import bstr 
 32   
 33  hashlib_has_usedforsecurity = False 
 34   
 35  if not hasattr(inspect, 'getfullargspec'): 
 36      inspect.getfullargspec = inspect.getargspec 
 37  if 'usedforsecurity' in inspect.getfullargspec(hashlib.new)[0]: 
 38      hashlib_has_usedforsecurity = True 
 39   
 40  _normpath_re = re.compile("^(%s)+" % os.sep) 
41 -def normalize_path(path):
42 """ 43 os.path.normpath does not remove path separator duplicates at the 44 beginning of the path 45 """ 46 return _normpath_re.sub(os.sep, os.path.normpath(path))
47
48 -def join_path(*args):
49 return normalize_path(os.sep.join(args))
50
51 -def path_full_split(path):
52 """ 53 Given a path, it fully splits it into constituent path 54 components (as opposed to os.path.split which splits it into 55 trailing component and preceeding path 56 """ 57 58 path = normalize_path(path) 59 splitpath = [] 60 while 1: 61 path, current = os.path.split(path) 62 if current == '': 63 if path: 64 # Absolute path 65 splitpath.append(os.sep) 66 break 67 splitpath.append(current) 68 69 splitpath.reverse() 70 return splitpath
71
72 -def copyfile_p(src, dst):
73 """ 74 Simple util function, copies src path to dst path, making 75 directories as necessary. File permissions are not preserved. 76 """ 77 78 directory = os.path.split(dst)[0] 79 try: 80 os.makedirs(directory) 81 except OSError: 82 e = sys.exc_info()[1] 83 if e.errno != 17: 84 # not File exists 85 raise 86 87 if os.path.isdir(src): 88 if not os.path.exists(dst): 89 os.mkdir(dst) 90 elif os.path.islink(src): 91 exists = hasattr(os.path, "lexists") and os.path.lexists or os.path.exists 92 if exists(dst): 93 os.remove(dst) 94 os.symlink(os.readlink(src), dst) 95 else: 96 shutil.copyfile(src, dst)
97
98 -def mkdir_p(path, mode=None, symlinks=None, allfiles=None):
99 """ 100 Similar to 'mkdir -p' -- makes all directories necessary to ensure 101 the 'path' is a directory, and return the list of directories that were 102 made as a result 103 """ 104 if mode is None: 105 mode = int('0700', 8) 106 dirs_created = [] 107 108 components = path_full_split(path) 109 for i in range(1,len(components)): 110 d = os.path.join(*components[:i+1]) 111 if symlinks: 112 for symlink in symlinks: 113 if symlink['path'] == d: 114 # create symlink and remove it from symlink list 115 os.symlink(symlink['symlink'], symlink['path']) 116 symlinks.remove(symlink) 117 allfiles.remove(symlink) 118 dirs_created.append(symlink) 119 continue 120 log_debug(8, "testing", d) 121 try: 122 os.mkdir(d, mode) 123 dirs_created.append(d) 124 log_debug(8, "created", d) 125 except OSError: 126 e = sys.exc_info()[1] 127 if e.errno != 17: 128 raise 129 else: 130 log_debug(8, "already exists", d) 131 132 log_debug(6, "dirs_created:", dirs_created) 133 134 return dirs_created
135
136 -def rmdir_p(path, stoppath):
137 """ 138 if rmdir had a -p option, this would be it. remove dir and up 139 until empty dir is hit, or stoppath is reached 140 141 path and stoppath have to be absolute paths 142 """ 143 144 # First normalize both paths 145 stoppath = normalize_path(os.sep + stoppath) 146 path = normalize_path(os.sep + path) 147 148 # stoppath has to be a prefix of path 149 if path[:len(stoppath)] != stoppath: 150 raise OSError("Could not remove %s: %s is not a prefix" % ( 151 path, stoppath)) 152 153 while 1: 154 if stoppath == path: 155 # We're done 156 break 157 158 # Try to remove the directory 159 try: 160 os.rmdir(path) 161 except OSError: 162 # Either the directory is full, or we don't have permissions; stop 163 break 164 165 path, current = os.path.split(path) 166 if current == '': 167 # We're done - reached the root 168 break
169 170 #returns slashstring with any trailing slash removed
171 -def rm_trailing_slash(slashstring):
172 if slashstring[-1] == "/": 173 slashstring = slashstring[0:-1] 174 return slashstring
175
176 -def getContentChecksum(checksum_type, contents):
177 if hashlib_has_usedforsecurity: 178 engine = hashlib.new(checksum_type, usedforsecurity=False) 179 else: 180 engine = hashlib.new(checksum_type) 181 engine.update(contents) 182 return engine.hexdigest()
183
184 -def sha256_file(filename):
185 engine = hashlib.new('sha256') 186 187 fh = open(filename, "rb") 188 while 1: 189 buf = fh.read(4096) 190 if not buf: 191 break 192 193 engine.update(bstr(buf)) 194 195 return engine.hexdigest()
196
197 -def parse_url(server_url, scheme="https"):
198 return urlparse.urlparse(server_url, scheme=scheme)
199
200 -def unparse_url(url_tuple):
201 return urlparse.urlunparse(url_tuple)
202
203 -def get_home_dir():
204 uid = os.getuid() 205 ent = pwd.getpwuid(uid) 206 return ent[5]
207