2017-03-23 01:51:28 +00:00
|
|
|
import logging
|
|
|
|
import hashlib
|
|
|
|
import json
|
|
|
|
|
2018-05-24 21:54:51 +00:00
|
|
|
from data.database import db_transaction
|
2017-03-23 01:51:28 +00:00
|
|
|
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2017-03-23 16:37:32 +00:00
|
|
|
def _ensure_sha256_header(digest):
|
|
|
|
if digest.startswith('sha256:'):
|
|
|
|
return digest
|
|
|
|
return 'sha256:' + digest
|
|
|
|
|
|
|
|
|
2017-03-23 01:51:28 +00:00
|
|
|
def _digest(manifestjson):
|
2017-03-23 16:37:32 +00:00
|
|
|
return _ensure_sha256_header(hashlib.sha256(json.dumps(manifestjson, sort_keys=True)).hexdigest())
|
2017-03-23 01:51:28 +00:00
|
|
|
|
|
|
|
|
2018-05-24 21:54:51 +00:00
|
|
|
def get_manifest_list(digest, models_ref):
|
|
|
|
ManifestList = models_ref.ManifestList
|
2017-03-23 16:37:32 +00:00
|
|
|
return ManifestList.select().where(ManifestList.digest == _ensure_sha256_header(digest)).get()
|
2017-03-23 01:51:28 +00:00
|
|
|
|
|
|
|
|
2018-05-24 21:54:51 +00:00
|
|
|
def get_or_create_manifest_list(manifest_list_json, media_type_name, schema_version, models_ref):
|
|
|
|
ManifestList = models_ref.ManifestList
|
|
|
|
|
2017-03-23 01:51:28 +00:00
|
|
|
digest = _digest(manifest_list_json)
|
|
|
|
media_type_id = ManifestList.media_type.get_id(media_type_name)
|
|
|
|
|
|
|
|
try:
|
2018-05-24 21:54:51 +00:00
|
|
|
return get_manifest_list(digest, models_ref)
|
2017-03-23 01:51:28 +00:00
|
|
|
except ManifestList.DoesNotExist:
|
|
|
|
with db_transaction():
|
|
|
|
manifestlist = ManifestList.create(digest=digest, manifest_list_json=manifest_list_json,
|
|
|
|
schema_version=schema_version, media_type=media_type_id)
|
|
|
|
return manifestlist
|
|
|
|
|
|
|
|
|
2018-05-24 21:54:51 +00:00
|
|
|
def create_manifestlistmanifest(manifestlist, manifest_ids, manifest_list_json, models_ref):
|
2017-03-23 01:51:28 +00:00
|
|
|
""" From a manifestlist, manifests, and the manifest list blob,
|
|
|
|
create if doesn't exist the manfiestlistmanifest for each manifest """
|
|
|
|
for pos in xrange(len(manifest_ids)):
|
|
|
|
manifest_id = manifest_ids[pos]
|
|
|
|
manifest_json = manifest_list_json[pos]
|
|
|
|
get_or_create_manifestlistmanifest(manifest=manifest_id,
|
|
|
|
manifestlist=manifestlist,
|
2018-05-24 21:54:51 +00:00
|
|
|
media_type_name=manifest_json['mediaType'],
|
|
|
|
models_ref=models_ref)
|
|
|
|
|
2017-03-23 01:51:28 +00:00
|
|
|
|
2018-05-24 21:54:51 +00:00
|
|
|
def get_or_create_manifestlistmanifest(manifest, manifestlist, media_type_name, models_ref):
|
|
|
|
ManifestListManifest = models_ref.ManifestListManifest
|
2017-03-23 01:51:28 +00:00
|
|
|
|
|
|
|
media_type_id = ManifestListManifest.media_type.get_id(media_type_name)
|
|
|
|
try:
|
|
|
|
ml = (ManifestListManifest
|
|
|
|
.select()
|
|
|
|
.where(ManifestListManifest.manifest == manifest,
|
|
|
|
ManifestListManifest.media_type == media_type_id,
|
|
|
|
ManifestListManifest.manifest_list == manifestlist)).get()
|
|
|
|
|
|
|
|
except ManifestListManifest.DoesNotExist:
|
|
|
|
ml = ManifestListManifest.create(manifest_list=manifestlist, media_type=media_type_id,
|
|
|
|
manifest=manifest)
|
|
|
|
return ml
|