<s> """<STR_LIT>""" <EOL> import posixpath <EOL> import logging <EOL> import six <EOL> from contextlib import closing <EOL> from paste . httpheaders import CONTENT_DISPOSITION <EOL> from pyramid . httpexceptions import HTTPNotFound , HTTPForbidden , HTTPBadRequest <EOL> from pyramid . security import NO_PERMISSION_REQUIRED , remember <EOL> from pyramid . view import view_config <EOL> from pyramid_duh import argify , addslash <EOL> from six . moves . urllib . request import urlopen <EOL> from pypicloud . route import ( APIResource , APIPackageResource , <EOL> APIPackagingResource , APIPackageFileResource ) <EOL> from pypicloud . util import normalize_name <EOL> LOG = logging . getLogger ( __name__ ) <EOL> @ view_config ( context = APIPackagingResource , request_method = '<STR_LIT:GET>' , <EOL> subpath = ( ) , renderer = '<STR_LIT>' ) <EOL> @ addslash <EOL> @ argify <EOL> def all_packages ( request , verbose = False ) : <EOL> """<STR_LIT>""" <EOL> if verbose : <EOL> packages = request . db . summary ( ) <EOL> else : <EOL> packages = request . db . distinct ( ) <EOL> i = <NUM_LIT:0> <EOL> while i < len ( packages ) : <EOL> package = packages [ i ] <EOL> name = package if isinstance ( package , basestring ) else package [ '<STR_LIT:name>' ] <EOL> if not request . access . has_permission ( name , '<STR_LIT>' ) : <EOL> del packages [ i ] <EOL> continue <EOL> i += <NUM_LIT:1> <EOL> return { '<STR_LIT>' : packages } <EOL> @ view_config ( context = APIPackageResource , request_method = '<STR_LIT:GET>' , <EOL> subpath = ( ) , renderer = '<STR_LIT>' , permission = '<STR_LIT>' ) <EOL> @ addslash <EOL> def package_versions ( context , request ) : <EOL> """<STR_LIT>""" <EOL> normalized_name = normalize_name ( context . name ) <EOL> versions = request . db . all ( normalized_name ) <EOL> return { <EOL> '<STR_LIT>' : versions , <EOL> '<STR_LIT>' : request . access . has_permission ( normalized_name , '<STR_LIT>' ) , <EOL> } <EOL> def fetch_dist ( request , package_name , package_url ) : <EOL> """<STR_LIT>""" <EOL> filename = posixpath . basename ( package_url ) <EOL> with closing ( urlopen ( package_url ) ) as url : <EOL> data = url . read ( ) <EOL> return request . db . upload ( filename , six . BytesIO ( data ) , package_name ) , data <EOL> @ view_config ( context = APIPackageFileResource , request_method = '<STR_LIT:GET>' , <EOL> permission = '<STR_LIT>' ) <EOL> def download_package ( context , request ) : <EOL> """<STR_LIT>""" <EOL> package = request . db . fetch ( context . filename ) <EOL> if not package : <EOL> if request . registry . fallback != '<STR_LIT>' : <EOL> return HTTPNotFound ( ) <EOL> if not request . access . can_update_cache ( ) : <EOL> return request . forbid ( ) <EOL> dists = request . locator . get_project ( context . name ) <EOL> dist = None <EOL> source_url = None <EOL> for version , url_set in six . iteritems ( dists . get ( '<STR_LIT>' , { } ) ) : <EOL> if dist is not None : <EOL> break <EOL> for url in url_set : <EOL> if posixpath . basename ( url ) == context . filename : <EOL> source_url = url <EOL> dist = dists [ version ] <EOL> break <EOL> if dist is None : <EOL> return HTTPNotFound ( ) <EOL> LOG . info ( "<STR_LIT>" , context . filename , <EOL> request . registry . fallback_url ) <EOL> package , data = fetch_dist ( request , dist . name , source_url ) <EOL> disp = CONTENT_DISPOSITION . tuples ( filename = package . filename ) <EOL> request . response . headers . update ( disp ) <EOL> request . response . body = data <EOL> request . response . content_type = '<STR_LIT>' <EOL> return request . response <EOL> response = request . db . download_response ( package ) <EOL> return response <EOL> @ view_config ( context = APIPackageFileResource , request_method = '<STR_LIT:POST>' , <EOL> subpath = ( ) , renderer = '<STR_LIT>' , permission = '<STR_LIT>' ) <EOL> @ argify <EOL> def upload_package ( context , request , content ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> return request . db . upload ( content . filename , content . file , <EOL> name = context . name ) <EOL> except ValueError as e : <EOL> return HTTPBadRequest ( * e . args ) <EOL> @ view_config ( context = APIPackageFileResource , request_method = '<STR_LIT>' , <EOL> subpath = ( ) , permission = '<STR_LIT>' ) <EOL> def delete_package ( context , request ) : <EOL> """<STR_LIT>""" <EOL> package = request . db . fetch ( context . filename ) <EOL> if package is None : <EOL> return HTTPBadRequest ( "<STR_LIT>" % context . filename ) <EOL> request . db . delete ( package ) <EOL> return request . response <EOL> @ view_config ( context = APIResource , name = '<STR_LIT:user>' , request_method = '<STR_LIT>' , <EOL> subpath = ( '<STR_LIT>' ) , renderer = '<STR_LIT>' , <EOL> permission = NO_PERMISSION_REQUIRED ) <EOL> @ argify <EOL> def register ( request , password ) : <EOL> """<STR_LIT>""" <EOL> if not request . access . allow_register and not request . access . need_admin ( ) : <EOL> return HTTPNotFound ( ) <EOL> username = request . named_subpaths [ '<STR_LIT:username>' ] <EOL> request . access . register ( username , password ) <EOL> if request . access . need_admin ( ) : <EOL> request . access . approve_user ( username ) <EOL> request . access . set_user_admin ( username , True ) <EOL> request . response . headers . extend ( remember ( request , username ) ) <EOL> return request . response <EOL> @ view_config ( context = APIResource , name = '<STR_LIT:user>' , subpath = ( '<STR_LIT:password>' ) , <EOL> request_method = '<STR_LIT:POST>' , permission = '<STR_LIT>' ) <EOL> @ argify <EOL> def change_password ( request , old_password , new_password ) : <EOL> """<STR_LIT>""" <EOL> if not request . access . verify_user ( request . userid , old_password ) : <EOL> return HTTPForbidden ( ) <EOL> request . access . edit_user_password ( request . userid , new_password ) <EOL> return request . response <EOL> @ view_config ( context = APIResource , name = '<STR_LIT>' , renderer = '<STR_LIT>' , <EOL> permission = NO_PERMISSION_REQUIRED ) <EOL> @ argify ( wheel = bool , prerelease = bool ) <EOL> def fetch_requirements ( request , requirements , wheel = True , prerelease = False ) : <EOL> """<STR_LIT>""" <EOL> if not request . access . can_update_cache ( ) : <EOL> return HTTPForbidden ( ) <EOL> packages = [ ] <EOL> for line in requirements . splitlines ( ) : <EOL> dist = request . locator . locate ( line , prerelease , wheel ) <EOL> if dist is not None : <EOL> try : <EOL> packages . append ( fetch_dist ( request , dist . name , dist . source_url ) [ <NUM_LIT:0> ] ) <EOL> except ValueError : <EOL> pass <EOL> return { <EOL> '<STR_LIT>' : packages , <EOL> } </s>
