-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp.py
79 lines (61 loc) · 2.03 KB
/
http.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
import os
import requests
from release.storage import AbstractStorageProvider
class HttpStorageProvider(AbstractStorageProvider):
name = 'http'
def __init__(self, url):
self.__url = url.rstrip('/') + '/'
def _get_absolute(self, path):
assert not path.startswith('/')
return self.__url + path
def copy(self,
source_path,
destination_path):
raise NotImplementedError()
def upload(self,
destination_path,
blob=None,
local_path=None,
no_cache=None,
content_type=None):
raise NotImplementedError()
def download_inner(self, path, local_path):
local_path_tmp = '{}.tmp'.format(local_path)
url = self._get_absolute(path)
try:
with open(local_path_tmp, 'w+b') as f:
r = requests.get(url, stream=True)
r.raise_for_status()
for chunk in r.iter_content(chunk_size=4096):
f.write(chunk)
os.rename(local_path_tmp, local_path)
except:
# Delete the temp file, re-raise.
try:
os.remove(local_path_tmp)
except Exception:
pass
raise
def exists(self, path):
url = self._get_absolute(path)
# TODO(cmaloney): 200 is overly restrictive here... After hitting more
# webservers expand to include other common / valid status codes that
# indicate resource is found / exists.
return requests.head(url=url).status_code == 200
def fetch(self, path):
r = requests.get(url=self._get_absolute(path))
r.raise_for_status()
return r.content
def remove_recursive(self, path):
raise NotImplementedError()
def list_recursive(self, path):
raise NotImplementedError()
@property
def url(self):
return self.__url
@property
def read_only(self):
return True
factories = {
'read': HttpStorageProvider
}