<s> import datetime <EOL> import logging <EOL> import re <EOL> import time <EOL> import flask <EOL> import gevent <EOL> from docker_registry . core import compat <EOL> from docker_registry . core import exceptions <EOL> json = compat . json <EOL> from . import storage <EOL> from . import toolkit <EOL> from . app import app <EOL> from . lib import mirroring <EOL> from . lib import signals <EOL> store = storage . load ( ) <EOL> logger = logging . getLogger ( __name__ ) <EOL> RE_USER_AGENT = re . compile ( '<STR_LIT>' ) <EOL> RE_VALID_TAG = re . compile ( '<STR_LIT>' ) <EOL> @ app . route ( '<STR_LIT>' , methods = [ '<STR_LIT>' ] ) <EOL> @ toolkit . parse_repository_name <EOL> @ toolkit . requires_auth <EOL> def set_properties ( namespace , repository ) : <EOL> logger . debug ( "<STR_LIT>" . format ( namespace , <EOL> repository ) ) <EOL> data = None <EOL> try : <EOL> data = json . loads ( flask . request . data . decode ( '<STR_LIT:utf8>' ) ) <EOL> except ValueError : <EOL> pass <EOL> if not data or not isinstance ( data , dict ) : <EOL> return toolkit . api_error ( '<STR_LIT>' ) <EOL> private_flag_path = store . private_flag_path ( namespace , repository ) <EOL> if ( data [ '<STR_LIT>' ] == '<STR_LIT>' <EOL> and not store . is_private ( namespace , repository ) ) : <EOL> store . put_content ( private_flag_path , '<STR_LIT>' ) <EOL> elif ( data [ '<STR_LIT>' ] == '<STR_LIT>' <EOL> and store . is_private ( namespace , repository ) ) : <EOL> try : <EOL> store . remove ( private_flag_path ) <EOL> except Exception : <EOL> pass <EOL> return toolkit . response ( ) <EOL> @ app . route ( '<STR_LIT>' , methods = [ '<STR_LIT:GET>' ] ) <EOL> @ toolkit . parse_repository_name <EOL> @ toolkit . requires_auth <EOL> def get_properties ( namespace , repository ) : <EOL> logger . debug ( "<STR_LIT>" . format ( namespace , <EOL> repository ) ) <EOL> is_private = store . is_private ( namespace , repository ) <EOL> return toolkit . response ( { <EOL> '<STR_LIT>' : '<STR_LIT>' if is_private else '<STR_LIT>' <EOL> } ) <EOL> def get_tags ( namespace , repository ) : <EOL> tag_path = store . tag_path ( namespace , repository ) <EOL> greenlets = { } <EOL> for fname in store . list_directory ( tag_path ) : <EOL> full_tag_name = fname . split ( '<STR_LIT:/>' ) . pop ( ) <EOL> if not full_tag_name . startswith ( '<STR_LIT>' ) : <EOL> continue <EOL> tag_name = full_tag_name [ <NUM_LIT:4> : ] <EOL> greenlets [ tag_name ] = gevent . spawn ( <EOL> store . get_content , <EOL> store . tag_path ( namespace , repository , tag_name ) , <EOL> ) <EOL> gevent . joinall ( greenlets . values ( ) ) <EOL> return dict ( ( k , g . value ) for ( k , g ) in greenlets . items ( ) ) <EOL> @ app . route ( '<STR_LIT>' , methods = [ '<STR_LIT:GET>' ] ) <EOL> @ toolkit . parse_repository_name <EOL> @ toolkit . requires_auth <EOL> @ mirroring . source_lookup_tag <EOL> def _get_tags ( namespace , repository ) : <EOL> logger . debug ( "<STR_LIT>" . format ( namespace , <EOL> repository ) ) <EOL> try : <EOL> data = get_tags ( namespace = namespace , repository = repository ) <EOL> except exceptions . FileNotFoundError : <EOL> return toolkit . api_error ( '<STR_LIT>' , <NUM_LIT> ) <EOL> return toolkit . response ( data ) <EOL> @ app . route ( '<STR_LIT>' , methods = [ '<STR_LIT:GET>' ] ) <EOL> @ toolkit . parse_repository_name <EOL> @ toolkit . requires_auth <EOL> @ mirroring . source_lookup_tag <EOL> def get_tag ( namespace , repository , tag ) : <EOL> logger . debug ( "<STR_LIT>" . format ( <EOL> namespace , repository , tag ) ) <EOL> data = None <EOL> tag_path = store . tag_path ( namespace , repository , tag ) <EOL> try : <EOL> data = store . get_content ( tag_path ) <EOL> except exceptions . FileNotFoundError : <EOL> return toolkit . api_error ( '<STR_LIT>' , <NUM_LIT> ) <EOL> return toolkit . response ( data ) <EOL> @ app . route ( '<STR_LIT>' , methods = [ '<STR_LIT:GET>' ] ) <EOL> @ toolkit . parse_repository_name <EOL> @ toolkit . requires_auth <EOL> @ mirroring . source_lookup ( stream = False , cache = True ) <EOL> def get_repository_json ( namespace , repository ) : <EOL> json_path = store . repository_json_path ( namespace , repository ) <EOL> headers = { } <EOL> data = { '<STR_LIT>' : None , <EOL> '<STR_LIT>' : None , <EOL> '<STR_LIT>' : None , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : None } <EOL> try : <EOL> data = store . get_json ( json_path ) <EOL> except exceptions . FileNotFoundError : <EOL> if mirroring . is_mirror ( ) : <EOL> return toolkit . response ( data , code = <NUM_LIT> , headers = headers ) <EOL> return toolkit . response ( data , headers = headers ) <EOL> @ app . route ( <EOL> '<STR_LIT>' , <EOL> methods = [ '<STR_LIT:GET>' ] ) <EOL> @ toolkit . parse_repository_name <EOL> @ toolkit . requires_auth <EOL> def get_repository_tag_json ( namespace , repository , tag ) : <EOL> json_path = store . repository_tag_json_path ( namespace , repository , tag ) <EOL> data = { '<STR_LIT>' : None , <EOL> '<STR_LIT>' : None , <EOL> '<STR_LIT>' : None , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : None } <EOL> try : <EOL> data = store . get_json ( json_path ) <EOL> except exceptions . FileNotFoundError : <EOL> pass <EOL> return toolkit . response ( data ) <EOL> def create_tag_json ( user_agent ) : <EOL> props = { <EOL> '<STR_LIT>' : int ( time . mktime ( datetime . datetime . utcnow ( ) . timetuple ( ) ) ) <EOL> } <EOL> ua = dict ( RE_USER_AGENT . findall ( user_agent ) ) <EOL> if '<STR_LIT>' in ua : <EOL> props [ '<STR_LIT>' ] = ua [ '<STR_LIT>' ] <EOL> if '<STR_LIT>' in ua : <EOL> props [ '<STR_LIT>' ] = ua [ '<STR_LIT>' ] <EOL> for k in [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] : <EOL> if k in ua : <EOL> props [ k ] = ua [ k ] . lower ( ) <EOL> return json . dumps ( props ) <EOL> @ app . route ( '<STR_LIT>' , <EOL> methods = [ '<STR_LIT>' ] ) <EOL> @ toolkit . parse_repository_name <EOL> @ toolkit . requires_auth <EOL> def put_tag ( namespace , repository , tag ) : <EOL> logger . debug ( "<STR_LIT>" . format ( <EOL> namespace , repository , tag ) ) <EOL> if not RE_VALID_TAG . match ( tag ) : <EOL> return toolkit . api_error ( '<STR_LIT>' . format ( <EOL> RE_VALID_TAG . pattern <EOL> ) ) <EOL> data = None <EOL> try : <EOL> data = json . loads ( flask . request . data . decode ( '<STR_LIT:utf8>' ) ) <EOL> except ValueError : <EOL> pass <EOL> if not data or not isinstance ( data , basestring ) : <EOL> return toolkit . api_error ( '<STR_LIT>' ) <EOL> if not store . exists ( store . image_json_path ( data ) ) : <EOL> return toolkit . api_error ( '<STR_LIT>' , <NUM_LIT> ) <EOL> store . put_content ( store . tag_path ( namespace , repository , tag ) , data ) <EOL> sender = flask . current_app . _get_current_object ( ) <EOL> signals . tag_created . send ( sender , namespace = namespace , <EOL> repository = repository , tag = tag , value = data ) <EOL> ua = flask . request . headers . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> data = create_tag_json ( user_agent = ua ) <EOL> json_path = store . repository_tag_json_path ( namespace , repository , tag ) <EOL> store . put_content ( json_path , data ) <EOL> if tag == "<STR_LIT>" : <EOL> json_path = store . repository_json_path ( namespace , repository ) <EOL> store . put_content ( json_path , data ) <EOL> return toolkit . response ( ) <EOL> def delete_tag ( namespace , repository , tag ) : <EOL> logger . debug ( "<STR_LIT>" . format ( <EOL> namespace , repository , tag ) ) <EOL> tag_path = store . tag_path ( namespace , repository , tag ) <EOL> image = store . get_content ( path = tag_path ) <EOL> store . remove ( tag_path ) <EOL> store . remove ( store . repository_tag_json_path ( namespace , repository , <EOL> tag ) ) <EOL> sender = flask . current_app . _get_current_object ( ) <EOL> if tag == "<STR_LIT>" : <EOL> store . remove ( store . repository_json_path ( namespace , repository ) ) <EOL> signals . tag_deleted . send ( <EOL> sender , namespace = namespace , repository = repository , tag = tag , <EOL> image = image ) <EOL> @ app . route ( '<STR_LIT>' , <EOL> methods = [ '<STR_LIT>' ] ) <EOL> @ toolkit . parse_repository_name <EOL> @ toolkit . requires_auth <EOL> def _delete_tag ( namespace , repository , tag ) : <EOL> try : <EOL> delete_tag ( namespace = namespace , repository = repository , tag = tag ) <EOL> except exceptions . FileNotFoundError : <EOL> return toolkit . api_error ( '<STR_LIT>' % tag , <NUM_LIT> ) <EOL> return toolkit . response ( ) <EOL> @ app . route ( '<STR_LIT>' , methods = [ '<STR_LIT>' ] ) <EOL> @ app . route ( '<STR_LIT>' , methods = [ '<STR_LIT>' ] ) <EOL> @ toolkit . parse_repository_name <EOL> @ toolkit . requires_auth <EOL> def delete_repository ( namespace , repository ) : <EOL> """<STR_LIT>""" <EOL> logger . debug ( "<STR_LIT>" . format ( <EOL> namespace , repository ) ) <EOL> try : <EOL> for tag_name , tag_content in get_tags ( <EOL> namespace = namespace , repository = repository ) . items ( ) : <EOL> delete_tag ( <EOL> namespace = namespace , repository = repository , tag = tag_name ) <EOL> store . remove ( store . repository_path ( <EOL> namespace = namespace , repository = repository ) ) <EOL> except exceptions . FileNotFoundError : <EOL> return toolkit . api_error ( '<STR_LIT>' , <NUM_LIT> ) <EOL> else : <EOL> sender = flask . current_app . _get_current_object ( ) <EOL> signals . repository_deleted . send ( <EOL> sender , namespace = namespace , repository = repository ) <EOL> return toolkit . response ( ) </s>
