-
Notifications
You must be signed in to change notification settings - Fork 3
/
util.py
102 lines (82 loc) · 2.73 KB
/
util.py
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
import platform
import os
import shutil
import zipfile
import tarfile
import time
import requests
def is_windows():
return platform.system().lower()[0] == 'w'
def is_mac():
# macOS/OSX reports 'Darwin'
return platform.system().lower()[0] == 'd'
def get_os_name():
if is_mac():
return 'mac'
elif is_windows():
return 'win'
else:
raise NotImplementedError
def get_launch_name():
return 'launch' + get_os_name()
def make_dir(path, remove, verbose):
if remove and os.path.exists(path):
if verbose:
print('removing : %s' % path)
shutil.rmtree(path)
if not os.path.exists(path):
if verbose:
print('making folder : %s' % path)
os.mkdir(path)
def extract(source_folder, source_file, destination_folder, verbose):
source = os.path.join(source_folder, source_file)
print('extracting %s to %s' % (source, destination_folder))
extension = source_file[source_file.rfind('.')+1:]
if extension == 'zip':
with zipfile.ZipFile(source) as zf:
# assumes a trusted .zip
zf.extractall(destination_folder)
elif extension == 'tgz':
with tarfile.open(source) as tf:
tf.extractall(destination_folder)
elif extension == 'gz':
with tarfile.open(source) as tf:
tf.extractall(destination_folder)
else:
print('error : unsupported file type %s (extension : %s)' % (source_file, extension))
exit()
def get(url, destination_folder, file_name, verbose):
destination_path = os.path.join(destination_folder, file_name)
if os.path.exists(destination_path):
if verbose:
print('using existing copy of %s from %s' % (file_name, os.path.abspath(destination_path)))
else:
if verbose:
print('get %s to %s' % (url, destination_path))
response = requests.get(url, stream=True)
if response.status_code == 200:
with open(destination_path, 'wb') as out_file:
shutil.copyfileobj(response.raw, out_file)
del response
else:
print('error getting %s from %s' % (file_name, url))
return False
return True
def rm_mk_tree(dir_path):
'''
Clears out a dir. Makes it if it doesn't exist.
:param dir_path: dir to clear out
:return: True on success
'''
# fancy rmtree, since for some reason shutil.rmtree can return before the tree is actually removed
count = 600
while os.path.exists(dir_path) and count > 0:
try:
shutil.rmtree(dir_path)
except FileNotFoundError:
pass
except IOError:
time.sleep(0.1)
count -= 1
os.mkdir(dir_path)
return count > 0