Coverage for appr/models/kv/models_index_base.py: 19%

# Coverage for appr/models/kv/models_index_base.py : 19%

## 264 statements 50 run 214 missing 0 excluded

Hot-keys on this page

r m x p toggle line displays

j k next/prev highlighted chunk

0 (zero) top of page

1 (one) first highlighted chunk

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 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 | from __future__ import absolute_import, division, print_function import datetime import json import appr.semver from appr.exception import (PackageAlreadyExists, PackageNotFound, ResourceNotFound, raise_channel_not_found, raise_package_not_found) DEFAULT_LOCK_TIMEOUT = 3 class ModelsIndexBase(object): packages_key = "packages.json" def __init__(self, package=None): self._packages = None self._releases = None self.package = package self.locks = set() @property def releases_key(self): return self.package + "/" + "releases.json" @property def releases_data(self): path = self.releases_key if self._releases is None: try: self._releases = self._fetch_data(path) except ResourceNotFound: raise_package_not_found(self.package) return self._releases def blob_key(self, digest, mod="sha256"): return "%s/digests/%s/%s" % (self.package, mod, digest) def add_blob(self, b64blob, digest): try: path = self.blob_key(digest) self.get_lock(path) self._write_raw_data(path, b64blob) return True finally: self.release_lock(path) def delete_blob(self, digest): try: path = self.blob_key(digest) self.get_lock(path) self._delete_data(path) return True finally: self.release_lock(path) def get_blob(self, digest): try: path = self.blob_key(digest) return self._fetch_raw_data(path) except ResourceNotFound: raise_package_not_found(self.package, digest) def add_package(self, package_name): try: self.get_lock(self.packages_key) namespace, name = package_name.split("/") if namespace not in self.packages_data['packages']: self.packages_data['packages'][namespace] = {} if name not in self.packages_data['packages'][namespace]: pdata = { "created_at": datetime.datetime.utcnow().isoformat(), 'name': name, 'namespace': namespace} self.packages_data['packages'][namespace][name] = pdata self._write_data(self.packages_key, self.packages_data) finally: self.release_lock(self.packages_key) def delete_package(self, package_name): try: self.get_lock(self.packages_key) namespace, name = package_name.split("/") if (namespace not in self.packages_data['packages'] or name not in self.packages_data['packages'][namespace]): return None pdata = self.packages_data['packages'][namespace].pop(name) if not self.packages_data['packages'][namespace]: self.packages_data['packages'].pop(namespace) self._write_data(self.packages_key, self.packages_data) return pdata finally: self.release_lock(self.packages_key) def add_release(self, package_data, release, media_type, force=False): try: self.get_lock(self.releases_key) try: data = self.releases_data except PackageNotFound: data = {'page': 0, 'channels': {}, 'releases': {}} if release not in data['releases']: data['releases'][release] = {'manifests': {}, 'channels': []} if (release in data['releases'] and media_type in data['releases'][release]['manifests'] and not force): raise PackageAlreadyExists("Package exists already", { "package": self.package, "release": release, "media_type": media_type}) data['releases'][release]['manifests'][media_type] = package_data self._write_data(self.releases_key, data) self.add_package(self.package) return data finally: self.release_lock(self.releases_key) def delete_release(self, release, media_type): try: self.get_lock(self.releases_key) data = self.releases_data if release not in data['releases'] or media_type not in data['releases'][release][ 'manifests']: raise_package_not_found(self.package) data['releases'][release]['manifests'].pop(media_type) if not data['releases'][release]['manifests']: data['releases'].pop(release) if not data['releases']: self.delete_package(self.package) self._write_data(self.releases_key, data) return True finally: self.release_lock(self.releases_key) @property def packages_data(self): if self._packages is None: try: self._packages = self._fetch_data(self.packages_key) except ResourceNotFound: try: self.get_lock(self.packages_key, timeout=None) self._packages = {"page": 0, "packages": {}} self._write_data(self.packages_key, self._packages) finally: self.release_lock(self.packages_key) return self._packages def releases(self, media_type=None): if media_type is not None: result = [] for release_name, release in self.releases_data['releases'].iteritems(): if media_type in release['manifests']: result.append(release_name) else: result = self.releases_data['releases'].keys() return result def release_manifests(self, release): try: manifests = self.releases_data['releases'][release]['manifests'] return manifests except KeyError: raise_package_not_found(self.package, release) def release_formats(self, release=None): if release: return self.release_manifests(release).keys() else: formats = set() for _, release in self.releases_data['releases'].iteritems(): [formats.add(x) for x in release['manifests'].keys()] return list(formats) def release(self, release, media_type): try: return self.release_manifests(release)[media_type] except KeyError: raise_package_not_found(self.package, release, media_type) def ispackage_exists(self): return (len(self.releases()) > 0) def channels(self): data = self.releases_data['channels'] if data: return data.values() else: return [] def channel(self, channel): try: return self.releases_data['channels'][channel] except KeyError: raise_channel_not_found(channel) def _set_channel(self, channel, release): try: self.get_lock(self.releases_key) data = self.releases_data data['channels'][channel] = { 'name': channel, 'current': release, 'package': self.package} if channel not in data['releases'][release]['channels']: data['releases'][release]['channels'].append(channel) self._write_data(self.releases_key, data) return True finally: self.release_lock(self.releases_key) def add_channel(self, channel, current): return self._set_channel(channel, current) def delete_channel(self, channel): """ Delete the channel from all releases """ if not self.ischannel_exists(channel): raise_channel_not_found(channel) try: self.get_lock(self.releases_key) data = self.releases_data for release in self.channel_releases(channel): self._releases = self._delete_channel_release(channel, release) if channel in data['channels']: data['channels'].pop(channel) self._write_data(self.releases_key, data) finally: self.release_lock(self.releases_key) def set_channel_default(self, channel, release): self._check_channel_release(channel, release) return self._set_channel(channel, release) def _check_channel_release(self, channel, release): if not self.ischannel_exists(channel): raise_channel_not_found(channel) if release not in self.releases_data['releases']: raise_package_not_found(self.package, release) def add_channel_release(self, channel, release): self._check_channel_release(channel, release) try: self.get_lock(self.releases_key) data = self.releases_data if channel not in data['releases'][release]['channels']: data['releases'][release]['channels'].append(channel) self._write_data(self.releases_key, data) return True finally: self.release_lock(self.releases_key) def delete_channel_release(self, channel, release): self._check_channel_release(channel, release) try: self.get_lock(self.releases_key) data = self._delete_channel_release(channel, release) releases = self.channel_releases(channel) if not releases: data['channels'].pop(channel) else: self.set_channel_default(channel, releases[0]) self._write_data(self.releases_key, data) return True finally: self.release_lock(self.releases_key) def _delete_channel_release(self, channel, release): data = self.releases_data channels = set(data['releases'][release]['channels']) if channel in channels: channels.discard(channel) data['releases'][release]['channels'] = list(channels) return data def channel_releases(self, channel): if not self.ischannel_exists(channel): raise_channel_not_found(self.package, channel) releases = [ release for release, x in self.releases_data['releases'].iteritems() if channel in x['channels']] ordered_releases = [ str(x) for x in sorted(appr.semver.versions(releases, False), reverse=True)] return ordered_releases def release_channels(self, release): if release not in self.releases_data['releases']: raise_package_not_found(self.package, release) return self.releases_data['releases'][release]['channels'] def package_names(self, namespace=None): result = [] if namespace is not None: if namespace in self.packages_data['packages']: result = [ "%s/%s" % (namespace, name) for name in self.packages_data['packages'][namespace].keys()] else: for namespace, packages in self.packages_data['packages'].iteritems(): for name in packages.keys(): result.append("%s/%s" % (namespace, name)) return result def ischannel_exists(self, channel): return channel in self.releases_data['channels'] def packages(self, namespace=None): result = [] if namespace is not None: if namespace in self.packages_data['packages']: result = self.packages_data['packages'][namespace].values() else: for namespace, packages in self.packages_data['packages'].iteritems(): for _, data in packages.iteritems(): result.append(data) return result def _lock_key(self, key): return "%s.lock" % (key) def get_lock(self, key, ttl=3, timeout=DEFAULT_LOCK_TIMEOUT): lock_key = self._lock_key(key) if lock_key not in self.locks: self._get_lock(lock_key, ttl, timeout) self.locks.add(lock_key) def release_lock(self, key): """ Check if owner of the lock """ lock_key = self._lock_key(key) if lock_key in self.locks: self.locks.discard(lock_key) self._release_lock(lock_key) def _get_lock(self, key, ttl=3, timeout=DEFAULT_LOCK_TIMEOUT): raise NotImplementedError def _release_lock(self, key): """ Remove the lock """ raise NotImplementedError def _fetch_data(self, key): return json.loads(self._fetch_raw_data(key)) def _fetch_raw_data(self, key): raise NotImplementedError def _write_data(self, key, data): return self._write_raw_data(key, json.dumps(data)) def _write_raw_data(self, key, data): raise NotImplementedError def _delete_data(self, key): raise NotImplementedError