forked from utahta/pythonbrew
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.py
More file actions
276 lines (243 loc) · 7.93 KB
/
util.py
File metadata and controls
276 lines (243 loc) · 7.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
import os
import errno
import shutil
import subprocess
import re
import posixpath
import tarfile
import platform
import urllib
from subprocess import PIPE, Popen
from pythonbrew.define import PATH_BIN, PATH_PYTHONS, PATH_ETC_CURRENT
from pythonbrew.exceptions import ShellCommandException
from pythonbrew.log import logger
def size_format(b):
kb = 1000
mb = kb*kb
b = float(b)
if b >= mb:
return "%.1fMb" % (b/mb)
if b >= kb:
return "%.1fKb" % (b/kb)
return "%.0fbytes" % (b)
def is_url(name):
if ':' not in name:
return False
scheme = name.split(':', 1)[0].lower()
return scheme in ['http', 'https', 'file', 'ftp']
def is_file(name):
if ':' not in name:
return False
scheme = name.split(':', 1)[0].lower()
return scheme == 'file'
def splitext(name):
base, ext = os.path.splitext(name)
if base.lower().endswith('.tar'):
ext = base[-4:] + ext
base = base[:-4]
return base, ext
def is_archive_file(name):
ext = splitext(name)[1].lower()
archives = ('.zip', '.tar.gz', '.tar.bz2', '.tgz', '.tar')
if ext in archives:
return True
return False
def is_html(content_type):
if content_type and content_type.startswith('text/html'):
return True
return False
def is_gzip(content_type, filename):
if (content_type == 'application/x-gzip'
or tarfile.is_tarfile(filename)
or splitext(filename)[1].lower() in ('.tar', '.tar.gz', '.tar.bz2', '.tgz', '.tbz')):
return True
return False
def is_macosx_snowleopard():
mac_ver = platform.mac_ver()[0]
return mac_ver >= '10.6' and mac_ver < '10.7'
def is_python24(version):
return version >= '2.4' and version < '2.5'
def is_python25(version):
return version >= '2.5' and version < '2.6'
def is_python26(version):
return version >= '2.6' and version < '2.7'
def makedirs(path):
try:
os.makedirs(path)
except OSError, (e, es):
if errno.EEXIST != e:
raise
def symlink(src, dst):
try:
os.symlink(src, dst)
except:
pass
def unlink(path):
try:
os.unlink(path)
except OSError, (e, es):
if errno.ENOENT != e:
raise
def rm_r(path):
"""like rm -r command."""
if os.path.isdir(path):
shutil.rmtree(path)
elif os.path.isfile(path):
unlink(path)
def off():
for root, dirs, files in os.walk(PATH_BIN):
for f in files:
if f == "pythonbrew" or f == "pybrew":
continue
unlink("%s/%s" % (root, f))
unlink("%s/current" % PATH_PYTHONS)
set_current_path(PATH_BIN)
def split_leading_dir(path):
path = str(path)
path = path.lstrip('/').lstrip('\\')
if '/' in path and (('\\' in path and path.find('/') < path.find('\\'))
or '\\' not in path):
return path.split('/', 1)
elif '\\' in path:
return path.split('\\', 1)
else:
return path, ''
def has_leading_dir(paths):
"""Returns true if all the paths have the same leading path name
(i.e., everything is in one subdirectory in an archive)"""
common_prefix = None
for path in paths:
prefix, rest = split_leading_dir(path)
if not prefix:
return False
elif common_prefix is None:
common_prefix = prefix
elif prefix != common_prefix:
return False
return True
def untar_file(filename, location):
if not os.path.exists(location):
makedirs(location)
if filename.lower().endswith('.gz') or filename.lower().endswith('.tgz'):
mode = 'r:gz'
elif filename.lower().endswith('.bz2') or filename.lower().endswith('.tbz'):
mode = 'r:bz2'
elif filename.lower().endswith('.tar'):
mode = 'r'
else:
logger.error('Cannot determine compression type for file %s' % filename)
mode = 'r:*'
tar = tarfile.open(filename, mode)
try:
# note: python<=2.5 doesnt seem to know about pax headers, filter them
leading = has_leading_dir([
member.name for member in tar.getmembers()
if member.name != 'pax_global_header'
])
for member in tar.getmembers():
fn = member.name
if fn == 'pax_global_header':
continue
if leading:
fn = split_leading_dir(fn)[1]
path = os.path.join(location, fn)
if member.isdir():
if not os.path.exists(path):
os.makedirs(path)
else:
try:
fp = tar.extractfile(member)
except (KeyError, AttributeError), e:
# Some corrupt tar files seem to produce this
# (specifically bad symlinks)
logger.error('In the tar file %s the member %s is invalid: %s'
% (filename, member.name, e))
continue
if not os.path.exists(os.path.dirname(path)):
os.makedirs(os.path.dirname(path))
destfp = open(path, 'wb')
try:
shutil.copyfileobj(fp, destfp)
finally:
destfp.close()
os.chmod(path, member.mode)
fp.close()
finally:
tar.close()
def unpack_downloadfile(content_type, download_file, target_dir):
logger.info("Extracting %s into %s" % (os.path.basename(download_file), target_dir))
if is_gzip(content_type, download_file):
untar_file(download_file, target_dir)
else:
logger.error("Cannot determine archive format of %s" % download_file)
return False
return True
def get_current_python_path():
p = Popen('command -v python', stdout=PIPE, shell=True)
p.wait()
if p.returncode == 0:
return p.stdout.read().strip()
else:
return None
def set_current_path(path):
fp = open(PATH_ETC_CURRENT, 'w')
fp.write('PATH_PYTHONBREW="%s"\n' % (path))
fp.close()
def path_to_fileurl(path):
path = os.path.normcase(os.path.abspath(path))
url = urllib.quote(path)
url = url.replace(os.path.sep, '/')
url = url.lstrip('/')
return 'file:///' + url
def fileurl_to_path(url):
assert url.startswith('file:'), ('Illegal scheme:%s' % url)
url = '/' + url[len('file:'):].lstrip('/')
return urllib.unquote(url)
class Subprocess(object):
def __init__(self, log=None, shell=True, cwd=None, print_cmd=False):
self._log = log
self._shell = shell
self._cwd = cwd
self._print_cmd = print_cmd
def chdir(self, cwd):
self._cwd = cwd
def check_call(self, cmd, shell=None, cwd=None):
if shell:
self._shell = shell
if cwd:
self._cwd = cwd
if self._print_cmd:
logger.info(cmd)
if self._log:
cmd = "(%s) >> '%s' 2>&1" % (cmd, self._log)
retcode = subprocess.call(cmd, shell=self._shell, cwd=self._cwd)
if retcode != 0:
raise ShellCommandException('Failed to `%s` command' % cmd)
class Package(object):
def __init__(self, name):
self.name = None
self.version = None
if is_archive_file(name):
name = splitext(name)[0]
m = re.search("^Python-(.*)$", name)
if m:
self.name = name
self.version = m.group(1)
else:
self.name = "Python-%s" % name
self.version = name
class Link(object):
def __init__(self, url):
self._url = url
@property
def filename(self):
url = self._url
url = url.split('#', 1)[0]
url = url.split('?', 1)[0]
url = url.rstrip('/')
name = posixpath.basename(url)
assert name, ('URL %r produced no filename' % url)
return name
@property
def show_msg(self):
return posixpath.basename(self._url.split('#', 1)[0].split('?', 1)[0])