1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 import os
17
18 import hashlib
19 import re
20 import shutil
21 import pwd
22 import sys
23
24 try:
25 import urlparse
26 except ImportError:
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)
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
50
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
65 splitpath.append(os.sep)
66 break
67 splitpath.append(current)
68
69 splitpath.reverse()
70 return splitpath
71
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
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
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
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
145 stoppath = normalize_path(os.sep + stoppath)
146 path = normalize_path(os.sep + path)
147
148
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
156 break
157
158
159 try:
160 os.rmdir(path)
161 except OSError:
162
163 break
164
165 path, current = os.path.split(path)
166 if current == '':
167
168 break
169
170
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
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
198 return urlparse.urlparse(server_url, scheme=scheme)
199
201 return urlparse.urlunparse(url_tuple)
202
204 uid = os.getuid()
205 ent = pwd.getpwuid(uid)
206 return ent[5]
207