Xc@sddlZddlZddlZddlZddlZddlZddlmZedZ edZ ddZ edZ edZedZdS( iN(t close_all_fdsc Cs)g}tjjj\}}tjdtd|||fdi|d6} | j|j| j |j |j tjjj\} } tj dt \} } tjdt di|d6d|| | f}|j|j|j |j | j | j y4tjddgd| d tjd td d }WnAtk rtjd dgd| d tjd td d }nX| j |j|j tjddddd|d|gd|jd |d td d }|jj |j|j x!|D]}tjjj|qW| S(Nttargettargstkwargstdebugtduplextpigzs-ctstdintstdoutt close_fdstbufsizeitgziptopenssltencs-es -aes-128-cbcs-Ks-iv(t euca2oolstbundletutiltopen_pipe_fileobjstmultiprocessingtProcesst_create_tarball_from_streamtstarttappendtpidtclosetPipetFalset_calc_sha1_for_pipet subprocesstPopentPIPEtTruetOSErrorRtwaitpid_in_thread(tinfiletoutfiletenc_keytenc_ivttarinfoRtpidst tar_out_rt tar_out_wttar_pt digest_out_rt digest_out_wtdigest_result_rtdigest_result_wtdigest_pR R R((s?/usr/lib/python2.7/site-packages/euca2ools/bundle/pipes/core.pytcreate_bundle_pipeline$sL              c Csg}tjddddd|d|gd|dtjd td d }|j|j|jy:tjd d dgd|jdtjd td d }WnGtk rtjdd dgd|jdtjd td d }nX|j|j|jjt j j j \}} t jdt\} } t jdtdi|d6d|j| | f} | j|j| j|jj| j| jt jdtd||fdi|d6} | j|j|j| jx!|D]}t j j j|qW| S(s Create a pipeline to perform the unbundle operation on infile input. The resulting unbundled image will be written to 'outfile'. :param outfile: file obj to write unbundled image to :param enc_key: the encryption key used to bundle the image :param enc_iv: the encyrption initialization vector used in the bundle :returns multiprocess pipe to read sha1 digest of written image R R s-ds -aes-128-cbcs-Ks-ivRRR R iRs-cR RRRRR(RRRRRRRRR RRRRRRRRRRt_extract_from_tarball_streamR!(R"R#R$R%RR'R R R+R,R-R.R/R*R((s?/usr/lib/python2.7/site-packages/euca2ools/bundle/pipes/core.pytcreate_unbundle_pipelineZsH               cCsd}|r|jnzmxf|js|jtj}|r|j||j|t|7}|r|j|qqPqWWd|r|j n|j X|S(s Synchronously copy data from infile to outfile, updating a progress bar with the total number of bytes copied along the way if one was provided, and return the number of bytes copied. This method must be run on the main thread. :param infile: file obj to read input from :param outfile: file obj to write output to :param progressbar: progressbar object to update with i/o information :param maxbytes: Int maximum number of bytes to write iN( RtclosedtreadRtBUFSIZEtwritetflushtlentupdatetfinishR(R"R#t progressbart bytes_writtentchunk((s?/usr/lib/python2.7/site-packages/euca2ools/bundle/pipes/core.pytcopy_with_progressbars"        cCst|||gtj}zydxJtrq|jtj}|rm|j||j||j q(Pq(W|j |j Wnt k r|sdSnXWd|j |j |j XdS(s Read data from infile and write it to outfile, calculating a running SHA1 digest along the way. When infile hits end-of-file, send the digest in hex form to result_mpconn and exit. :param infile: file obj providing input for digest :param outfile: file obj destination for writing output :param digest_out_pipe_w: fileobj to write digest to :param debug: boolean used in exception handling N(Rthashlibtsha1RR4RR5R9R6R7tsendt hexdigesttIOErrorR(R"R#tdigest_out_pipe_wRtdigestR=((s?/usr/lib/python2.7/site-packages/euca2ools/bundle/pipes/core.pyRs&          cCstd||gtjddd|dtj}z<y|j|d|Wntk rn|shdSnXWd|j|j|jXdS(Nt except_fdstmodesw|tfileobjR (RttarfiletopenRR5taddfileRCR(R"R#R&Rttarball((s?/usr/lib/python2.7/site-packages/euca2ools/bundle/pipes/core.pyRs     cCst||gtjddd|}zNy)t|}tj|j||Wntk rt|sndSnXWd|j|j|jXdS(s Perform tar extract on infile and write to outfile :param infile: file obj providing input for tar :param outfile: file obj destination for tar output :param debug: boolean used in exception handling RGsr|RHN( RRIRJtnexttshutilt copyfileobjt extractfileRCR(R"R#RRLR&((s?/usr/lib/python2.7/site-packages/euca2ools/bundle/pipes/core.pyR1s     (R?RRNRRIteuca2ools.bundle.utilRRRR0R2tNoneR>RRR1(((s?/usr/lib/python2.7/site-packages/euca2ools/bundle/pipes/core.pyts       5 < # !