LEFT | RIGHT |
(no file at all) | |
1 import json | 1 import json |
2 import logging | 2 import logging |
3 import os | 3 import os |
4 import tempfile | 4 import tempfile |
5 import urllib | 5 import urllib |
6 import urlparse | 6 import urlparse |
7 import yaml | 7 import yaml |
8 | 8 |
9 from twisted.internet.defer import fail, inlineCallbacks, returnValue, succeed | 9 from twisted.internet.defer import fail, inlineCallbacks, returnValue, succeed |
10 from twisted.web.client import downloadPage, getPage | |
11 from twisted.web.error import Error | 10 from twisted.web.error import Error |
12 from txaws.client.ssl import VerifyingContextFactory | 11 from txaws.client.ssl import VerifyingContextFactory |
13 | 12 |
14 from juju.charm.provider import get_charm_from_path | 13 from juju.charm.provider import get_charm_from_path |
15 from juju.charm.url import CharmURL | 14 from juju.charm.url import CharmURL |
16 from juju.errors import FileNotFound | 15 from juju.errors import FileNotFound |
17 from juju.lib import under | 16 from juju.lib import under |
| 17 from juju.lib.http_client import download_page, get_page |
18 | 18 |
19 from .errors import ( | 19 from .errors import ( |
20 CharmNotFound, CharmError, RepositoryNotFound, ServiceConfigValueError) | 20 CharmNotFound, CharmError, RepositoryNotFound, ServiceConfigValueError) |
21 | 21 |
22 log = logging.getLogger("juju.charm") | 22 log = logging.getLogger("juju.charm") |
23 | 23 |
24 CS_STORE_URL = "https://store.juju.ubuntu.com" | 24 CS_STORE_URL = "https://store.juju.ubuntu.com" |
25 | 25 |
26 | 26 |
27 def _makedirs(path): | 27 def _makedirs(path): |
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
122 def __str__(self): | 122 def __str__(self): |
123 return "charm store" | 123 return "charm store" |
124 | 124 |
125 @inlineCallbacks | 125 @inlineCallbacks |
126 def _get_info(self, charm_url): | 126 def _get_info(self, charm_url): |
127 charm_id = str(charm_url) | 127 charm_id = str(charm_url) |
128 url = "%s/charm-info?charms=%s" % ( | 128 url = "%s/charm-info?charms=%s" % ( |
129 self.url_base, urllib.quote(charm_id)) | 129 self.url_base, urllib.quote(charm_id)) |
130 try: | 130 try: |
131 host = urlparse.urlparse(url).hostname | 131 host = urlparse.urlparse(url).hostname |
132 all_info = json.loads((yield getPage(url, contextFactory=VerifyingCo
ntextFactory(host)))) | 132 all_info = json.loads((yield get_page(url, contextFactory=VerifyingC
ontextFactory(host)))) |
133 charm_info = all_info[charm_id] | 133 charm_info = all_info[charm_id] |
134 for warning in charm_info.get("warnings", []): | 134 for warning in charm_info.get("warnings", []): |
135 log.warning("%s: %s", charm_id, warning) | 135 log.warning("%s: %s", charm_id, warning) |
136 errors = charm_info.get("errors", []) | 136 errors = charm_info.get("errors", []) |
137 if errors: | 137 if errors: |
138 raise CharmError(charm_id, "; ".join(errors)) | 138 raise CharmError(charm_id, "; ".join(errors)) |
139 returnValue(charm_info) | 139 returnValue(charm_info) |
140 except Error: | 140 except Error: |
141 raise CharmNotFound(self.url_base, charm_url) | 141 raise CharmNotFound(self.url_base, charm_url) |
142 | 142 |
143 @inlineCallbacks | 143 @inlineCallbacks |
144 def _download(self, charm_url, cache_path): | 144 def _download(self, charm_url, cache_path): |
145 url = "%s/charm/%s" % (self.url_base, urllib.quote(charm_url.path)) | 145 url = "%s/charm/%s" % (self.url_base, urllib.quote(charm_url.path)) |
146 downloads = os.path.join(self.cache_path, "downloads") | 146 downloads = os.path.join(self.cache_path, "downloads") |
147 _makedirs(downloads) | 147 _makedirs(downloads) |
148 f = tempfile.NamedTemporaryFile( | 148 f = tempfile.NamedTemporaryFile( |
149 prefix=_cache_key(charm_url), suffix=".part", dir=downloads, | 149 prefix=_cache_key(charm_url), suffix=".part", dir=downloads, |
150 delete=False) | 150 delete=False) |
151 f.close() | 151 f.close() |
152 downloading_path = f.name | 152 downloading_path = f.name |
153 host = urlparse.urlparse(url).hostname | 153 host = urlparse.urlparse(url).hostname |
154 try: | 154 try: |
155 yield downloadPage(url, downloading_path, contextFactory=VerifyingCo
ntextFactory(host)) | 155 yield download_page(url, downloading_path, contextFactory=VerifyingC
ontextFactory(host)) |
156 except Error: | 156 except Error: |
157 raise CharmNotFound(self.url_base, charm_url) | 157 raise CharmNotFound(self.url_base, charm_url) |
158 os.rename(downloading_path, cache_path) | 158 os.rename(downloading_path, cache_path) |
159 | 159 |
160 @inlineCallbacks | 160 @inlineCallbacks |
161 def find(self, charm_url): | 161 def find(self, charm_url): |
162 info = yield self._get_info(charm_url) | 162 info = yield self._get_info(charm_url) |
163 revision = info["revision"] | 163 revision = info["revision"] |
164 if charm_url.revision is None: | 164 if charm_url.revision is None: |
165 charm_url = charm_url.with_revision(revision) | 165 charm_url = charm_url.with_revision(revision) |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
205 both the charm's data and all information necessary to specify its | 205 both the charm's data and all information necessary to specify its |
206 source. | 206 source. |
207 """ | 207 """ |
208 url = CharmURL.infer(vague_name, default_series) | 208 url = CharmURL.infer(vague_name, default_series) |
209 if url.collection.schema == "local": | 209 if url.collection.schema == "local": |
210 repo = LocalCharmRepository(repository_path) | 210 repo = LocalCharmRepository(repository_path) |
211 elif url.collection.schema == "cs": | 211 elif url.collection.schema == "cs": |
212 # The eventual charm store url, point to elastic ip for now b2 | 212 # The eventual charm store url, point to elastic ip for now b2 |
213 repo = RemoteCharmRepository(CS_STORE_URL) | 213 repo = RemoteCharmRepository(CS_STORE_URL) |
214 return repo, url | 214 return repo, url |
LEFT | RIGHT |