o
    ȷe                     @   s   d dl mZ d dlmZ d dlmZ d dlmZ d dlZd dl	Z	d dl
Z
d dlmZ G dd deZed	kr?e Ze  dS dS )
    )OptionParser)
ServiceDef)	Submitter)ResultProcessorN)StringIOc                   @   sj   e Zd ZdZdddddddZd	d
 Zdd Zdd Zdd Zdd Z	dd Z
dd Zdd Zdd ZdS )BSz*usage: %prog [options] config_file commandz#Clear input queue and output bucketz!Submit local files to the servicezStart the servicez6Report on the status of the service buckets and queuesz$Retrieve output generated by a batchz0List all batches stored in current output_domain)resetsubmitstartstatusretrievebatchesc                 C   s  d | _ t| jd| _| jjddddd | jjddd	d
dd | jjddd	d
dd | jjddd	d
ddd | jjddd	d
ddd | jjdddddd | jjdd | jjdd d	d
d!d"d | jjd#d$ | jjd%d&d'd
d(d)d | jjd*d+d	d
d,d-d d S ).N)usagez--help-commands
store_truehelp_commandsz'provides help on the available commands)actiondesthelpz-az--access-keystorestringzyour AWS Access Key)r   typer   z-sz--secret-keyzyour AWS Secret Access Keyz-pz--pathpathz3the path to local directory for submit and retrieve)r   r   r   r   z-kz	--keypairkeypairz.the SSH keypair used with launched instance(s)z-lz--leaveleavez>leave the files (don't retrieve) files during retrieve commandF)r   z-nz--num-instancesnum_instancesz"the number of launched instance(s)   )r   z-iz--ignore-dirsappendignorez4directories that should be ignored by submit commandz-bz
--batch-idbatchz1batch identifier required by the retrieve command)service_namer   Usageparser
add_optionset_defaults)self r%   G/var/www/ideatree/venv/lib/python3.10/site-packages/boto/services/bs.py__init__)   s>   
zBS.__init__c                 C   s2   t d | j D ]}t d|| j| f  q	d S )Nz

Commands:z  %s		%s)printCommandskeys)r$   keyr%   r%   r&   print_command_helpA   s   zBS.print_command_helpc                 C   s   | j d}|r)td d}| }|r#|d7 }|| | }|std|  | j d}| j d}|r\|rA|j|jkrAd S td d}|D ]
}|d7 }|  qItd	|  d S d S )
Ninput_queuezclearing out input queuer   r   zdeleted %d messagesoutput_bucketinput_bucketz'delete generated files in output bucketzdeleted %d keys)sdget_objr(   readdelete_messagenamedelete)r$   iqimobibkr%   r%   r&   do_resetF   s.   

zBS.do_resetc              	   C   s   | j js
| jd tj| j js| jd| j j  t| j}|| j jd | j j	d d d| j j}t
d|d   t
d|d   d S )NNo path providedInvalid path (%s)Tz"A total of %d files were submittedr   zBatch Identifier: %sr   )optionsr   r!   errorosexistsr   r0   submit_pathr   r(   )r$   str%   r%   r&   	do_submit]   s   

zBS.do_submitc           
      C   s   | j d}| j dd}| j dd}|s| jd t }| j ds>| j d | j dd|j	 | j dd	|j
 t }| j | ||g}|d
 }|j| | jj| jj||gd}td|  td|j  |jD ]	}	td|	j  qrd S )Nami_idinstance_typezm1.smallsecurity_groupdefaultz3ami_id option is required when starting the serviceCredentialsaws_access_key_idaws_secret_access_keyr   )	user_datakey_name	max_countrH   security_groupszStarting AMI: %sz0Reservation %s contains the following instances:z	%s)r0   getr!   r@   botoconnect_ec2has_sectionadd_sectionsetrL   rM   r   writeget_all_imagesrungetvaluer?   r   r   r(   id	instances)
r$   rG   rH   rI   ec2rD   rsimgrr7   r%   r%   r&   do_starth   s0   
zBS.do_startc                 C   s   | j d}|rtd|j| f  | j d}| j d}|rA|r+|j|jkr+d S d}|D ]}|d7 }q/td|j|f  d S d S )Nr-   z7The input_queue (%s) contains approximately %s messagesr.   r/   r   r   z'The output_bucket (%s) contains %d keys)r0   r1   r(   r\   countr4   )r$   r6   r9   r:   totalr;   r%   r%   r&   	do_status   s   
zBS.do_statusc                 C   sx   | j js
| jd tj| j js| jd| j j  | j js&| jd t| j j| j}|j	| j j| j j
 d d S )Nr=   r>   z1batch identifier is required for retrieve command)get_file)r?   r   r!   r@   rA   rB   r   r   r0   get_resultsr   )r$   rD   r%   r%   r&   do_retrieve   s   zBS.do_retrievec                 C   sN   | j d}|rtd |d}|D ]	}td|j  qd S | jd d S )Noutput_domainzAvailable Batches:z['type'='Batch']z  %sz&No output_domain specified for service)r0   r1   r(   queryr4   r!   r@   )r$   dr_   itemr%   r%   r&   
do_batches   s   
zBS.do_batchesc                 C   s   | j  \| _| _| jjr|   td t| jdkr#| j 	d | jd | _
t| j
| _| jd | _t| d| j rJt| d| j }|  d S | j 	d| j  d S )Nr      z$config_file and command are requiredr   zdo_%szcommand (%s) not recognized)r!   
parse_argsr?   argsr   r,   sysexitlenr@   config_filer   r0   commandhasattrgetattr)r$   methodr%   r%   r&   main   s   

zBS.mainN)__name__
__module____qualname__r    r)   r'   r,   r<   rF   rb   re   rh   rm   ry   r%   r%   r%   r&   r      s$    

r   __main__)optparser   boto.services.servicedefr   boto.services.submitr   boto.services.resultr   rS   rq   rA   boto.compatr   objectr   rz   bsry   r%   r%   r%   r&   <module>   s    