<s> import json <EOL> import logging <EOL> import os <EOL> import urllib . error <EOL> import urllib . request <EOL> from socket import error as socket_error <EOL> log = logging . getLogger ( __name__ ) <EOL> VERSION_ENDPOINT = '<STR_LIT>' <EOL> SHORT_TIMEOUT = <NUM_LIT:3> <EOL> def check_version ( server , version , filename , timeout = SHORT_TIMEOUT ) : <EOL> """<STR_LIT>""" <EOL> address = VERSION_ENDPOINT . format ( server = server ) <EOL> print ( '<STR_LIT>' ) <EOL> log . info ( '<STR_LIT>' , version ) <EOL> log . info ( '<STR_LIT>' , address ) <EOL> try : <EOL> request = urllib . request . Request ( address ) <EOL> response = urllib . request . urlopen ( request , timeout = timeout ) <EOL> except ( urllib . error . HTTPError , urllib . error . URLError , socket_error ) as e : <EOL> print ( '<STR_LIT>' ) <EOL> log . warning ( '<STR_LIT>' , address , <EOL> str ( e ) , stack_info = True ) <EOL> return False <EOL> response_json = json . loads ( response . read ( ) . decode ( '<STR_LIT:utf-8>' ) ) <EOL> if not _validate_api_response ( response_json ) : <EOL> print ( '<STR_LIT>' ) <EOL> log . info ( '<STR_LIT>' , address , <EOL> json . dumps ( response_json ) ) <EOL> return False <EOL> current_version = response_json [ '<STR_LIT:data>' ] [ '<STR_LIT>' ] [ <NUM_LIT:0> ] [ '<STR_LIT>' ] <EOL> if current_version == version : <EOL> print ( '<STR_LIT>' ) <EOL> return True <EOL> download_link = response_json [ '<STR_LIT:data>' ] [ '<STR_LIT>' ] [ <NUM_LIT:0> ] [ '<STR_LIT>' ] <EOL> log . info ( '<STR_LIT>' , current_version , download_link ) <EOL> try : <EOL> request = urllib . request . Request ( download_link ) <EOL> response = urllib . request . urlopen ( request , timeout = timeout ) <EOL> except ( urllib . error . HTTPError , urllib . error . URLError , socket_error ) as e : <EOL> print ( '<STR_LIT>' ) <EOL> log . warning ( '<STR_LIT>' , str ( e ) , <EOL> stack_info = True ) <EOL> return False <EOL> log . info ( '<STR_LIT>' , filename ) <EOL> zip_binary = response . read ( ) <EOL> try : <EOL> _write_zip ( filename , zip_binary ) <EOL> except IOError as e : <EOL> print ( '<STR_LIT>' ) <EOL> log . warning ( '<STR_LIT>' , filename , str ( e ) ) <EOL> return False <EOL> else : <EOL> print ( '<STR_LIT>' . format ( current_version ) ) <EOL> log . info ( '<STR_LIT>' , filename ) <EOL> return True <EOL> def _validate_api_response ( data ) : <EOL> return isinstance ( data , dict ) and '<STR_LIT:data>' in data and isinstance ( data [ '<STR_LIT:data>' ] , dict ) and '<STR_LIT>' in data [ '<STR_LIT:data>' ] and isinstance ( data [ '<STR_LIT:data>' ] [ '<STR_LIT>' ] , list ) and len ( data [ '<STR_LIT:data>' ] [ '<STR_LIT>' ] ) > <NUM_LIT:0> and isinstance ( data [ '<STR_LIT:data>' ] [ '<STR_LIT>' ] [ <NUM_LIT:0> ] , dict ) and '<STR_LIT>' in data [ '<STR_LIT:data>' ] [ '<STR_LIT>' ] [ <NUM_LIT:0> ] and '<STR_LIT>' in data [ '<STR_LIT:data>' ] [ '<STR_LIT>' ] [ <NUM_LIT:0> ] <EOL> def _write_zip ( zip_name , zip_contents ) : <EOL> with open ( zip_name , '<STR_LIT:wb>' ) as f : <EOL> f . write ( zip_contents ) <EOL> os . fsync ( f ) </s>
