forked from OpenBounds/Processing
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
196 lines (153 loc) · 5.13 KB
/
utils.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
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
import os
import ujson
import logging
import tempfile
import sys
from urllib.parse import urlparse
import shutil
import urllib.request, urllib.error, urllib.parse
from contextlib import closing
import zipfile
import tarfile
import boto3
import hashlib
import click
import requests
CHUNK_SIZE = 1024
def get_files(path):
"""Returns an iterable containing the full path of all files in the
specified path.
:param path: string
:yields: string
"""
if os.path.isdir(path):
for (dirpath, dirnames, filenames) in os.walk(path):
for filename in filenames:
if not filename[0] == '.':
yield os.path.join(dirpath, filename)
else:
yield path
def read_json(path):
"""Returns JSON dict from file.
:param path: string
:returns: dict
"""
with open(path, 'r') as jsonfile:
return ujson.loads(jsonfile.read())
def write_json(path, data):
with open(path, 'w') as jsonfile:
jsonfile.write(ujson.dumps(data, double_precision=5))
def make_sure_path_exists(path):
"""Make directories in path if they do not exist.
Modified from http://stackoverflow.com/a/5032238/1377021
:param path: string
"""
try:
os.makedirs(path)
except:
pass
def get_path_parts(path):
"""Splits a path into parent directories and file.
:param path: string
"""
return path.split(os.sep)
def download(url):
"""Downloads a file and returns a file pointer to a temporary file.
:param url: string
"""
parsed_url = urlparse(url)
urlfile = parsed_url.path.split('/')[-1]
_, extension = os.path.split(urlfile)
fp = tempfile.NamedTemporaryFile('wb', suffix=extension, delete=False)
download_cache = os.getenv("DOWNLOAD_CACHE")
cache_path = None
if download_cache is not None:
cache_path = os.path.join(download_cache,
hashlib.sha224(url.encode()).hexdigest())
if os.path.exists(cache_path):
logging.info("Returning %s from local cache at %s" % (url, cache_path))
fp.close()
shutil.copy(cache_path, fp.name)
return fp
s3_cache_bucket = os.getenv("S3_CACHE_BUCKET")
s3_cache_key = None
if s3_cache_bucket is not None:
s3_cache_key = os.getenv("S3_CACHE_PREFIX", "") + hashlib.sha224(url.encode()).hexdigest()
s3 = boto3.client('s3')
try:
s3.download_fileobj(s3_cache_bucket, s3_cache_key, fp)
logging.info("Found %s in s3 cache at s3://%s/%s" %
(url, s3_cache_bucket, s3_cache_key))
fp.close()
return fp
except:
pass
# logging.exception("error downloading cache file from s3")
if parsed_url.scheme == "http" or parsed_url.scheme == "https":
res = requests.get(url, stream=True, verify=False)
if not res.ok:
raise IOError
for chunk in res.iter_content(CHUNK_SIZE):
fp.write(chunk)
elif parsed_url.scheme == "ftp":
download = urllib.request.urlopen(url)
file_size_dl = 0
block_sz = 8192
while True:
buffer = download.read(block_sz)
if not buffer:
break
file_size_dl += len(buffer)
fp.write(buffer)
fp.close()
if cache_path:
if not os.path.exists(download_cache):
os.makedirs(download_cache)
shutil.copy(fp.name, cache_path)
if s3_cache_key:
logging.info("Putting %s to s3 cache at s3://%s/%s" %
(url, s3_cache_bucket, s3_cache_key))
s3.upload_file(fp.name,
Bucket=s3_cache_bucket,
Key=s3_cache_key
)
return fp
class ZipCompatibleTarFile(tarfile.TarFile):
"""Wrapper around TarFile to make it more compatible with ZipFile"""
def infolist(self):
members = self.getmembers()
for m in members:
m.filename = m.name
return members
def namelist(self):
return self.getnames()
ARCHIVE_FORMAT_ZIP = "zip"
ARCHIVE_FORMAT_TAR_GZ = "tar.gz"
ARCHIVE_FORMAT_TAR_BZ2 = "tar.bz2"
def get_compressed_file_wrapper(path):
archive_format = None
if path.endswith(".zip"):
archive_format = ARCHIVE_FORMAT_ZIP
elif path.endswith(".tar.gz") or path.endswith(".tgz"):
archive_format = ARCHIVE_FORMAT_TAR_GZ
elif path.endswith(".tar.bz2"):
archive_format = ARCHIVE_FORMAT_TAR_BZ2
else:
try:
with zipfile.ZipFile(path, "r") as f:
archive_format = ARCHIVE_FORMAT_ZIP
except:
try:
f = tarfile.TarFile.open(path, "r")
f.close()
archive_format = ARCHIVE_FORMAT_ZIP
except:
pass
if archive_format is None:
raise Exception("Unable to determine archive format")
if archive_format == ARCHIVE_FORMAT_ZIP:
return zipfile.ZipFile(path, 'r')
elif archive_format == ARCHIVE_FORMAT_TAR_GZ:
return ZipCompatibleTarFile.open(path, 'r:gz')
elif archive_format == ARCHIVE_FORMAT_TAR_BZ2:
return ZipCompatibleTarFile.open(path, 'r:bz2')