<s> """<STR_LIT>""" <EOL> import os <EOL> import shutil <EOL> import subprocess <EOL> import tarfile <EOL> import urllib <EOL> import commands <EOL> TMP_PATH = "<STR_LIT>" <EOL> DEST_PATH = "<STR_LIT>" <EOL> DEST_FILE = "<STR_LIT>" <EOL> INIT_SCRIPTS = [ "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" ] <EOL> try : <EOL> import hashlib <EOL> except ImportError : <EOL> import md5 <EOL> class hashlib ( object ) : <EOL> """<STR_LIT>""" <EOL> @ staticmethod <EOL> def md5 ( ) : <EOL> return md5 . new ( ) <EOL> class AgentUpdateError ( Exception ) : <EOL> def __init__ ( self , arg ) : <EOL> self . arg = arg <EOL> def __str__ ( self ) : <EOL> return self . arg <EOL> __repr__ = __str__ <EOL> class UpdateCommand ( commands . CommandBase ) : <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> self . tmp_path = kwargs . get ( "<STR_LIT>" , TMP_PATH ) <EOL> def _get_to_local_file ( self , url , md5sum ) : <EOL> try : <EOL> filename = url [ url . rindex ( '<STR_LIT:/>' ) + <NUM_LIT:1> : ] <EOL> ext_pos = filename . index ( '<STR_LIT:.>' ) <EOL> local_filename = "<STR_LIT>" % ( <EOL> self . tmp_path , <EOL> filename [ : ext_pos ] , <EOL> os . getpid ( ) , <EOL> filename [ ext_pos : ] ) <EOL> except ValueError : <EOL> raise AgentUpdateError ( "<STR_LIT>" ) <EOL> try : <EOL> urllib . urlretrieve ( url , local_filename ) <EOL> except Exception , e : <EOL> raise AgentUpdateError ( str ( e ) ) <EOL> try : <EOL> f = file ( local_filename , '<STR_LIT:rb>' ) <EOL> except : <EOL> os . unlink ( local_filename ) <EOL> raise AgentUpdateError ( "<STR_LIT>" ) <EOL> m = hashlib . md5 ( ) <EOL> while True : <EOL> file_data = f . read ( <NUM_LIT> ) <EOL> if not file_data : <EOL> break <EOL> m . update ( file_data ) <EOL> f . close ( ) <EOL> digest = m . hexdigest ( ) <EOL> if digest != md5sum : <EOL> os . unlink ( local_filename ) <EOL> raise AgentUpdateError ( "<STR_LIT>" % ( <EOL> md5sum , digest ) ) <EOL> return local_filename <EOL> @ commands . command_add ( '<STR_LIT>' ) <EOL> def update_cmd ( self , data ) : <EOL> if isinstance ( data , basestring ) : <EOL> ( url , md5sum ) = data . split ( '<STR_LIT:U+002C>' , <NUM_LIT:1> ) <EOL> elif isinstance ( data , dict ) : <EOL> try : <EOL> url = data [ '<STR_LIT:url>' ] <EOL> md5sum = data [ '<STR_LIT>' ] <EOL> except KeyError : <EOL> return ( <NUM_LIT> , <EOL> "<STR_LIT>" ) <EOL> else : <EOL> return ( <NUM_LIT> , "<STR_LIT>" ) <EOL> try : <EOL> local_filename = self . _get_to_local_file ( url , md5sum ) <EOL> except AgentUpdateError , e : <EOL> return ( <NUM_LIT> , str ( e ) ) <EOL> ext = local_filename [ local_filename . rindex ( '<STR_LIT:.>' ) + <NUM_LIT:1> : ] <EOL> if ext != "<STR_LIT>" : <EOL> dest_filename = "<STR_LIT>" % ( <EOL> DEST_FILE , <EOL> ext ) <EOL> else : <EOL> dest_filename = "<STR_LIT:%s>" <EOL> try : <EOL> t = tarfile . open ( local_filename , '<STR_LIT>' ) <EOL> except tarfile . TarError , e : <EOL> os . unlink ( local_filename ) <EOL> return ( <NUM_LIT> , "<STR_LIT>" % ( local_filename , str ( e ) ) ) <EOL> found_installer = None <EOL> for tarinfo in t . getmembers ( ) : <EOL> name = tarinfo . name <EOL> while name . startswith ( '<STR_LIT>' ) or name . startswith ( '<STR_LIT>' ) or name . startswith ( '<STR_LIT:/>' ) : <EOL> name = name [ <NUM_LIT:1> : ] <EOL> if name == "<STR_LIT>" or ( name . count ( '<STR_LIT:/>' ) == <NUM_LIT:1> and <EOL> name . split ( '<STR_LIT:/>' ) [ <NUM_LIT:1> ] == "<STR_LIT>" ) : <EOL> found_installer = name <EOL> break <EOL> pipe = subprocess . PIPE <EOL> if found_installer : <EOL> dest_path = "<STR_LIT>" % ( DEST_PATH , os . getpid ( ) ) <EOL> try : <EOL> t . extractall ( dest_path ) <EOL> t . close ( ) <EOL> except tarfile . TarError , e : <EOL> os . unlink ( local_filename ) <EOL> return ( <NUM_LIT> , str ( e ) ) <EOL> os . unlink ( local_filename ) <EOL> p = subprocess . Popen ( [ "<STR_LIT>" % ( dest_path , found_installer ) ] , <EOL> stdin = pipe , stdout = pipe , stderr = pipe , env = { } ) <EOL> p . communicate ( None ) <EOL> retcode = p . returncode <EOL> shutil . rmtree ( dest_path , ignore_errors = True ) <EOL> if retcode != <NUM_LIT:0> : <EOL> return ( <NUM_LIT> , "<STR_LIT>" % retcode ) <EOL> else : <EOL> t . close ( ) <EOL> shutil . move ( local_filename , dest_filename ) <EOL> init_script = None <EOL> for script in INIT_SCRIPTS : <EOL> if os . path . exists ( script ) : <EOL> init_script = script <EOL> break <EOL> if not init_script : <EOL> return ( <NUM_LIT> , "<STR_LIT>" ) <EOL> try : <EOL> p = subprocess . Popen ( [ "<STR_LIT>" , init_script , "<STR_LIT>" ] , <EOL> stdin = pipe , stdout = pipe , stderr = pipe , env = { } ) <EOL> p . communicate ( None ) <EOL> retcode = p . returncode <EOL> except OSError , e : <EOL> return ( <NUM_LIT> , "<STR_LIT>" % str ( e ) ) <EOL> if retcode != <NUM_LIT:0> : <EOL> return ( <NUM_LIT> , "<STR_LIT>" % retcode ) <EOL> return ( <NUM_LIT:0> , "<STR_LIT>" ) </s>
