<s> from __future__ import print_function <EOL> import hashlib <EOL> import sys <EOL> import simplejson as json <EOL> from docker_registry . core import exceptions <EOL> import docker_registry . storage as storage <EOL> store = storage . load ( ) <EOL> images_cache = { } <EOL> ancestry_cache = { } <EOL> dry_run = True <EOL> def warning ( msg ) : <EOL> print ( '<STR_LIT>' + msg , file = sys . stderr ) <EOL> def get_image_parent ( image_id ) : <EOL> if image_id in images_cache : <EOL> return images_cache [ image_id ] <EOL> image_json = store . image_json_path ( image_id ) <EOL> parent_id = None <EOL> try : <EOL> info = store . get_json ( image_json ) <EOL> if info [ '<STR_LIT:id>' ] != image_id : <EOL> warning ( '<STR_LIT>' + image_id ) <EOL> parent_id = info . get ( '<STR_LIT>' ) <EOL> except exceptions . FileNotFoundError : <EOL> warning ( '<STR_LIT>' . format ( image_id ) ) <EOL> images_cache [ image_id ] = parent_id <EOL> return parent_id <EOL> def create_image_ancestry ( image_id ) : <EOL> global ancestry_cache <EOL> if image_id in ancestry_cache : <EOL> return <EOL> ancestry = [ image_id ] <EOL> parent_id = image_id <EOL> while True : <EOL> parent_id = get_image_parent ( parent_id ) <EOL> if not parent_id : <EOL> break <EOL> ancestry . append ( parent_id ) <EOL> create_image_ancestry ( parent_id ) <EOL> ancestry_path = store . image_ancestry_path ( image_id ) <EOL> if dry_run is False : <EOL> if not store . exists ( ancestry_path ) : <EOL> store . put_content ( ancestry_path , json . dumps ( ancestry ) ) <EOL> ancestry_cache [ image_id ] = True <EOL> print ( '<STR_LIT>' <EOL> '<STR_LIT>' . format ( len ( ancestry ) , image_id ) ) <EOL> def resolve_all_tags ( ) : <EOL> for namespace in store . list_directory ( store . repositories ) : <EOL> for repos in store . list_directory ( namespace ) : <EOL> try : <EOL> for tag in store . list_directory ( repos ) : <EOL> fname = tag . split ( '<STR_LIT:/>' ) . pop ( ) <EOL> if not fname . startswith ( '<STR_LIT>' ) : <EOL> continue <EOL> yield store . get_content ( tag ) <EOL> except exceptions . FileNotFoundError : <EOL> pass <EOL> def compute_image_checksum ( image_id , json_data ) : <EOL> layer_path = store . image_layer_path ( image_id ) <EOL> if not store . exists ( layer_path ) : <EOL> warning ( '<STR_LIT>' . format ( image_id ) ) <EOL> return <EOL> print ( '<STR_LIT>' . format ( image_id ) ) <EOL> if dry_run : <EOL> return <EOL> h = hashlib . sha256 ( json_data + '<STR_LIT:\n>' ) <EOL> for buf in store . stream_read ( layer_path ) : <EOL> h . update ( buf ) <EOL> checksum = '<STR_LIT>' . format ( h . hexdigest ( ) ) <EOL> checksum_path = store . image_checksum_path ( image_id ) <EOL> store . put_content ( checksum_path , checksum ) <EOL> def load_image_json ( image_id ) : <EOL> try : <EOL> json_path = store . image_json_path ( image_id ) <EOL> json_data = store . get_content ( json_path ) <EOL> info = json . loads ( json_data . decode ( '<STR_LIT:utf8>' ) ) <EOL> if image_id != info [ '<STR_LIT:id>' ] : <EOL> warning ( '<STR_LIT>' . format ( image_id ) ) <EOL> return <EOL> return json_data <EOL> except ( IOError , exceptions . FileNotFoundError , json . JSONDecodeError ) : <EOL> warning ( '<STR_LIT>' . format ( image_id ) ) <EOL> def compute_missing_checksums ( ) : <EOL> for image in store . list_directory ( store . images ) : <EOL> image_id = image . split ( '<STR_LIT:/>' ) . pop ( ) <EOL> if image_id not in ancestry_cache : <EOL> warning ( '<STR_LIT>' . format ( image_id ) ) <EOL> json_data = load_image_json ( image_id ) <EOL> if not json_data : <EOL> continue <EOL> checksum_path = store . image_checksum_path ( image_id ) <EOL> if store . exists ( checksum_path ) : <EOL> continue <EOL> compute_image_checksum ( image_id , json_data ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> if len ( sys . argv ) > <NUM_LIT:1> and sys . argv [ <NUM_LIT:1> ] == '<STR_LIT>' : <EOL> dry_run = False <EOL> for image_id in resolve_all_tags ( ) : <EOL> create_image_ancestry ( image_id ) <EOL> compute_missing_checksums ( ) <EOL> if dry_run : <EOL> print ( '<STR_LIT>' ) <EOL> print ( '<STR_LIT>' ) <EOL> print ( '<STR_LIT>' ) <EOL> print ( '<STR_LIT>' . format ( sys . argv [ <NUM_LIT:0> ] ) ) <EOL> else : <EOL> print ( '<STR_LIT>' ) </s>
