<s> import hashlib <EOL> import os <EOL> import time <EOL> from django . http import HttpResponse <EOL> from django . http import HttpResponseRedirect <EOL> from django . shortcuts import render_to_response <EOL> from django . views . decorators . csrf import csrf_exempt <EOL> from package_server . forms import UploadFileForm <EOL> from package_server . models import Package <EOL> from tank . settings import STATIC_URL <EOL> ITEM_LIMITS = <NUM_LIT:20> <EOL> @ csrf_exempt <EOL> def upload_package ( request ) : <EOL> if request . method == '<STR_LIT:POST>' : <EOL> form = UploadFileForm ( request . POST , request . FILES ) <EOL> if form . is_valid ( ) : <EOL> artifact = request . POST . get ( '<STR_LIT>' ) <EOL> revision_no = request . POST . get ( '<STR_LIT>' ) <EOL> file_obj = request . FILES . get ( '<STR_LIT:file>' ) <EOL> error_message = str ( ) <EOL> if not artifact : <EOL> error_message = '<STR_LIT>' <EOL> elif not revision_no : <EOL> error_message = '<STR_LIT>' <EOL> if error_message : <EOL> return render_to_response ( '<STR_LIT>' , { <EOL> '<STR_LIT>' : error_message , <EOL> '<STR_LIT>' : STATIC_URL . rstrip ( '<STR_LIT:/>' ) , <EOL> } ) <EOL> else : <EOL> package_name = os . path . basename ( file_obj . name ) <EOL> checksum = generate_checksum ( file_obj ) <EOL> time = generate_timestamp ( ) <EOL> file_obj . seek ( <NUM_LIT:0> ) <EOL> package = Package ( artifact = artifact , name = package_name , <EOL> revision = revision_no , timestamp = time , <EOL> checksum = checksum , file = file_obj ) <EOL> package . save ( ) <EOL> return render_to_response ( '<STR_LIT>' , { <EOL> '<STR_LIT>' : True , <EOL> '<STR_LIT>' : package , <EOL> '<STR_LIT>' : STATIC_URL . rstrip ( '<STR_LIT:/>' ) , <EOL> } ) <EOL> else : <EOL> form = UploadFileForm ( ) <EOL> return render_to_response ( '<STR_LIT>' , { <EOL> '<STR_LIT>' : form , <EOL> '<STR_LIT>' : STATIC_URL . rstrip ( '<STR_LIT:/>' ) , <EOL> } ) <EOL> def list_packages ( request , page_no = <NUM_LIT:1> ) : <EOL> package_list = Package . objects . order_by ( '<STR_LIT:id>' ) . reverse ( ) <EOL> has_package = ( len ( package_list ) > <NUM_LIT:0> ) <EOL> return render_to_response ( '<STR_LIT>' , { <EOL> '<STR_LIT>' : package_list , <EOL> '<STR_LIT>' : has_package , <EOL> '<STR_LIT>' : STATIC_URL . rstrip ( '<STR_LIT:/>' ) , <EOL> } ) <EOL> def check_package ( request ) : <EOL> artifact = request . GET . get ( '<STR_LIT>' ) <EOL> checksum = request . GET . get ( '<STR_LIT>' ) <EOL> package = get_package ( artifact , checksum ) <EOL> if package : <EOL> return HttpResponse ( str ( package ) ) <EOL> else : <EOL> return HttpResponse ( '<STR_LIT>' ) <EOL> def get_latest_package_info ( request ) : <EOL> artifact = request . GET . get ( '<STR_LIT>' ) <EOL> package_name = request . GET . get ( '<STR_LIT>' ) <EOL> package = get_latest_package ( artifact , package_name ) <EOL> if package : <EOL> return HttpResponse ( str ( package ) ) <EOL> else : <EOL> return HttpResponse ( '<STR_LIT>' ) <EOL> def generate_checksum ( fp ) : <EOL> sha1 = hashlib . sha1 ( ) <EOL> while True : <EOL> buffer = fp . read ( <NUM_LIT> ) <EOL> if not buffer : break <EOL> sha1 . update ( buffer ) <EOL> return sha1 . hexdigest ( ) <EOL> def generate_timestamp ( ) : <EOL> return time . strftime ( '<STR_LIT>' ) <EOL> def get_latest_package ( artifact , package_name ) : <EOL> if package_name : <EOL> package_list = Package . objects . filter ( <EOL> artifact = artifact , name = package_name , <EOL> ) . order_by ( '<STR_LIT:id>' ) . reverse ( ) <EOL> else : <EOL> package_list = Package . objects . filter ( <EOL> artifact = artifact , <EOL> ) . order_by ( '<STR_LIT:id>' ) . reverse ( ) <EOL> if len ( package_list ) > <NUM_LIT:0> : <EOL> return package_list [ <NUM_LIT:0> ] <EOL> else : <EOL> return None <EOL> def get_package ( artifact , checksum ) : <EOL> package_list = Package . objects . filter ( <EOL> artifact = artifact , <EOL> checksum = checksum , <EOL> ) <EOL> if len ( package_list ) > <NUM_LIT:0> : <EOL> return package_list [ <NUM_LIT:0> ] <EOL> else : <EOL> return None </s>
