<s> import boto <EOL> from boto . pyami . scriptbase import ScriptBase <EOL> import os , StringIO <EOL> class CopyBot ( ScriptBase ) : <EOL> def __init__ ( self ) : <EOL> super ( CopyBot , self ) . __init__ ( ) <EOL> self . wdir = boto . config . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . log_file = '<STR_LIT>' % self . instance_id <EOL> self . log_path = os . path . join ( self . wdir , self . log_file ) <EOL> boto . set_file_logger ( self . name , self . log_path ) <EOL> self . src_name = boto . config . get ( self . name , '<STR_LIT>' ) <EOL> self . dst_name = boto . config . get ( self . name , '<STR_LIT>' ) <EOL> self . replace = boto . config . getbool ( self . name , '<STR_LIT>' , True ) <EOL> s3 = boto . connect_s3 ( ) <EOL> self . src = s3 . lookup ( self . src_name ) <EOL> if not self . src : <EOL> boto . log . error ( '<STR_LIT>' % self . src_name ) <EOL> dest_access_key = boto . config . get ( self . name , '<STR_LIT>' , None ) <EOL> if dest_access_key : <EOL> dest_secret_key = boto . config . get ( self . name , '<STR_LIT>' , None ) <EOL> s3 = boto . connect ( dest_access_key , dest_secret_key ) <EOL> self . dst = s3 . lookup ( self . dst_name ) <EOL> if not self . dst : <EOL> self . dst = s3 . create_bucket ( self . dst_name ) <EOL> def copy_bucket_acl ( self ) : <EOL> if boto . config . get ( self . name , '<STR_LIT>' , True ) : <EOL> acl = self . src . get_xml_acl ( ) <EOL> self . dst . set_xml_acl ( acl ) <EOL> def copy_key_acl ( self , src , dst ) : <EOL> if boto . config . get ( self . name , '<STR_LIT>' , True ) : <EOL> acl = src . get_xml_acl ( ) <EOL> dst . set_xml_acl ( acl ) <EOL> def copy_keys ( self ) : <EOL> boto . log . info ( '<STR_LIT>' % self . src . name ) <EOL> boto . log . info ( '<STR_LIT>' % self . dst . name ) <EOL> try : <EOL> for key in self . src : <EOL> if not self . replace : <EOL> exists = self . dst . lookup ( key . name ) <EOL> if exists : <EOL> boto . log . info ( '<STR_LIT>' % ( key . name , self . dst . name ) ) <EOL> continue <EOL> boto . log . info ( '<STR_LIT>' % ( key . size , key . name ) ) <EOL> prefix , base = os . path . split ( key . name ) <EOL> path = os . path . join ( self . wdir , base ) <EOL> key . get_contents_to_filename ( path ) <EOL> new_key = self . dst . new_key ( key . name ) <EOL> new_key . set_contents_from_filename ( path ) <EOL> self . copy_key_acl ( key , new_key ) <EOL> os . unlink ( path ) <EOL> except : <EOL> boto . log . exception ( '<STR_LIT>' % key . name ) <EOL> def copy_log ( self ) : <EOL> key = self . dst . new_key ( self . log_file ) <EOL> key . set_contents_from_filename ( self . log_path ) <EOL> def main ( self ) : <EOL> fp = StringIO . StringIO ( ) <EOL> boto . config . dump_safe ( fp ) <EOL> self . notify ( '<STR_LIT>' % ( self . name , self . instance_id ) , fp . getvalue ( ) ) <EOL> if self . src and self . dst : <EOL> self . copy_keys ( ) <EOL> if self . dst : <EOL> self . copy_log ( ) <EOL> self . notify ( '<STR_LIT>' % ( self . name , self . instance_id ) , <EOL> '<STR_LIT>' ) <EOL> if boto . config . getbool ( self . name , '<STR_LIT>' , True ) : <EOL> ec2 = boto . connect_ec2 ( ) <EOL> ec2 . terminate_instances ( [ self . instance_id ] ) </s>
