<s> """<STR_LIT>""" <EOL> import sys <EOL> import os <EOL> from pycclib import cclib <EOL> from cctrl . error import InputErrorException , messages <EOL> from cctrl . settings import VERSION <EOL> from cctrl . app import ParseAppDeploymentName <EOL> from cctrl . auth import update_tokenfile , delete_tokenfile , read_tokenfile , get_email_and_password , create_token <EOL> def check_for_updates ( package_name , latest_version_str , our_version_str = VERSION ) : <EOL> """<STR_LIT>""" <EOL> our = dict ( ) <EOL> latest = dict ( ) <EOL> for version , suffix in ( ( our , our_version_str ) , ( latest , latest_version_str ) ) : <EOL> for part in [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] : <EOL> version [ part ] , _ , suffix = suffix . partition ( '<STR_LIT:.>' ) <EOL> version [ part ] = int ( version [ part ] ) <EOL> version [ '<STR_LIT>' ] = suffix <EOL> for part in [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] : <EOL> if latest [ part ] > our [ part ] : <EOL> if part == '<STR_LIT>' : <EOL> sys . exit ( messages [ '<STR_LIT>' ] . format ( package_name ) ) <EOL> else : <EOL> print >> sys . stderr , messages [ '<STR_LIT>' ] . format ( package_name ) <EOL> return <EOL> def init_api ( settings ) : <EOL> """<STR_LIT>""" <EOL> dirname = os . path . dirname ( __file__ ) <EOL> while len ( dirname ) > <NUM_LIT:1> : <EOL> p = os . path . join ( dirname , '<STR_LIT>' ) <EOL> if os . path . exists ( p ) : <EOL> cclib . CA_CERTS = p <EOL> break <EOL> dirname = os . path . dirname ( dirname ) <EOL> return cclib . API ( token = read_tokenfile ( settings ) , <EOL> url = settings . api_url , <EOL> token_source_url = settings . token_source_url , <EOL> register_addon_url = settings . register_addon_url , <EOL> encode_email = settings . encode_email ) <EOL> def execute_command ( api , command , settings ) : <EOL> while True : <EOL> try : <EOL> command ( ) <EOL> break <EOL> except ( cclib . TokenRequiredError , cclib . UnauthorizedError ) : <EOL> email , password = get_email_and_password ( settings ) <EOL> create_token ( api , settings , email , password ) <EOL> except ParseAppDeploymentName : <EOL> sys . exit ( messages [ '<STR_LIT>' ] ) <EOL> def execute_with_authenticated_user ( api , command , settings ) : <EOL> while True : <EOL> try : <EOL> execute_command ( api , command , settings ) <EOL> break <EOL> except cclib . ForbiddenError , e : <EOL> sys . exit ( messages [ '<STR_LIT>' ] ) <EOL> except cclib . ConnectionException : <EOL> sys . exit ( messages [ '<STR_LIT>' ] ) <EOL> except ( cclib . APIException , InputErrorException ) as e : <EOL> sys . exit ( e ) <EOL> def run ( args , api , settings ) : <EOL> """<STR_LIT>""" <EOL> execute_with_authenticated_user ( api , ( lambda : args . func ( args ) ) , settings ) <EOL> def shutdown ( api , settings ) : <EOL> """<STR_LIT>""" <EOL> if api . check_token ( ) : <EOL> update_tokenfile ( api , settings ) <EOL> else : <EOL> delete_tokenfile ( settings ) </s>
