
    ~	]j1              
      Z   d dl mZ d dlZd dlZd dlZd dlZd dlZd dlZd dlZd dl	Z	d dl
Z
d dlZd dlmZ  ee                                          j        d         Zedz  Zdddd	d
dgdddddg ddddddddgddZdDdZdEd ZdFd#ZdGdHd(ZdIdJd+ZdKd,ZdLd0ZdMd5ZdNd8ZdOd;ZdPd<Ze d=k    rw	  ej!         e                       dS # e"$ rTZ# e$ ej%        d>d? e&e#          j          d@e# dAd?B          ej'        C            ej!        d           Y dZ#[#dS dZ#[#ww xY wdS )Q    )annotationsN)Path   zscript/project-manifest.jsonzhttp://192.168.1.104:8022z/v1/footage-render-huyenan/jobsfootage_receiptfootagetts_receiptlayout_receipt)basecreatereceipt_key
output_keyupstream_keyszhttp://192.168.1.104:8023z/v1/huyenan-render-final/jobsfinal_receiptfinal_master)intro_receiptintro_qa_receiptr   footage_qa_receiptzhttp://192.168.1.104:8024z!/v1/huyenan-transcode-upload/jobstranscode_receiptfinal_uploadfinal_qa_receipt)r   final	transcodepathr   returnstrc                   t          j                    }|                     d          5 t          fdd          D ]}|                    |           	 d d d            n# 1 swxY w Y   |                                S )Nrbc                 .                          d          S )Ni   )read)handles   script/run-render-stage.py<lambda>zsha256.<locals>.<lambda>   s    &++o">">     r#   )hashlibsha256openiterupdate	hexdigest)r   digestchunkr    s      @r!   r%   r%      s    ^F	4 !F>>>>DD 	! 	!EMM%    	!! ! ! ! ! ! ! ! ! ! ! ! ! ! ! s   ,A##A'*A'manifestdictkeyc                   |                      d          pi                      |          }t          |t                    r|st          d| d          t	          |          }|                                s
t          |z  }	 |                                                    t                                                     n%# t          $ r}t          d|           |d }~ww xY w|S )Nactive_pathszactive_paths.z is missingzactive path escapes project: )
get
isinstancer   RuntimeErrorr   is_absoluteROOTresolverelative_to
ValueError)r,   r.   valuer   excs        r!   resolve_activer;   !   s    \\.))/R44S99EeS!! = =;3;;;<<<;;D d{K""4<<>>2222 K K K@3@@AAsJKKs   >C 
C$CC$r9   Nonec                   | j                             dd           t          j        |dd          dz                       d          }t          j        d| j         d| j         	          \  }}	 t          j	        |d
          5 }|
                    |           |                                 t          j        |                                           d d d            n# 1 swxY w Y   t          j        ||            t          j                            |          rt          j        |           d S d S # t          j                            |          rt          j        |           w w xY w)NT)parentsexist_okF   )ensure_asciiindent
utf-8.)prefixdirwb)parentmkdirjsondumpsencodetempfilemkstempnameosfdopenwriteflushfsyncfilenoreplacer   existsunlink)r   r9   datafdrP   r    s         r!   atomic_jsonr\   /   s   KdT222Ju5;;;dBJJ7SSD'749'7'7'7T[IIIHBYr4   	&FLLLLNNNHV]]__%%%	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	
47>>$ 	IdOOOOO	 	27>>$ 	IdOOOO	s1   2D: AC#D: #C''D: *C'+D: :6E0   urltimeoutintc                    t           j                            | |          5 }t          j        |                                                    d                    cd d d            S # 1 swxY w Y   d S )Nr_   rD   )urllibrequesturlopenrK   loadsr   decode)r^   r_   responses      r!   get_jsonri   >   s    			W		5	5 ;z(--//0099::; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ;s   9A((A,/A,<   payloadc                   t           j                            | t          j        |d                              d          ddid          }t           j                            ||          5 }t          j        |                                	                    d                    cd d d            S # 1 swxY w Y   d S )	NFrA   rD   zContent-Typezapplication/jsonPOST)rZ   headersmethodrb   )
rc   rd   RequestrK   rL   rM   re   rf   r   rg   )r^   rk   r_   rd   rh   s        r!   	post_jsonrr   C   s    n$$Stz'PU/V/V/V/]/]^e/f/fq  BT  qU  ^d$  e  eG					9	9 ;Xz(--//0099::; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ;s   .9B44B8;B8c                |    t          j        t          j        dddddddt	          |           gd	                    S )
Nffprobez-verrorz-show_entrieszeformat=duration,size,format_name:stream=index,codec_name,codec_type,width,height,sample_rate,channelsz-ofrK   T)text)rK   rf   
subprocesscheck_outputr   )r   s    r!   probery   I   sM    :j-4/ovs4yy/ 	     r#   stageprojectoutputc           
        | dk    r|i dt          t          |d                    ddddddd	t          t          |d
                    dt          |          dddddddddddddddddddddd d!d"iS | d#k    rOt          t          |d$                    t          t          |d                    t          |          ddddd d"d%	S t          t          |d&                    t          |          d'd(dddd)dd*	S )+Nr   layoutfootage_dirz(/data/video-pipeline/GacMaiAudio/footagepatternz*.mp4	recursiveTaudio	tts_audior|   seedl   	Do 	overwriteFwidth  height8  fpsr]   center_xi  center_yr   center_widthi`  center_heightvideo_encoder
h264_nvencpresetp4cq   r   intro)	intro_video_pathfootage_video_pathoutput_video_pathr   r   r   r   r   r   r   i / ʚ;`   )	input_video_pathr   target_output_bytesmax_output_bytesr   r   r   audio_bitrate_kbpsr   )r   r;   )rz   r{   r|   s      r!   request_payloadr   Q   s   	
c.(;;<<
>KMw
w
 +T
3:CwXc@d@d<e<e
 c&kk
 $*;
 9DU
 T	
 $T	
 ,1"	

 

 (

 -;C

 BQRV
 \
 ,4T
 <@
 
 	
  #N7G$D$D E E"%nWi&H&H"I"I!$VR$RV)T	
 
 	
  w G GHH_bci_j_j(jbY^  r#   keys	list[str]
canon_hash
list[dict]c                   g }|D ]}t          | |          }|                                st          d| d|           t          j        |                    d                    }|                    d          dk    s|                    d          durt          d	|           |                    d
          |k    rt          d|           |                    |t          |          t          |          d           |S )Nzupstream receipt missing: z -> rD   encodingstatus	completedverifiedTz)upstream receipt not completed/verified: source_canon_sha256zupstream canon hash mismatch: )r.   r   r%   )
r;   rX   r3   rK   rf   	read_textr1   appendr   r%   )r{   r   r   valuesr.   r   r9   s          r!   verify_upstreamr   i   s   F 	O 	Ogs++{{}} 	MKCKKTKKLLL
4>>7>;;<<99X+--:1F1Fd1R1RP3PPQQQ99*++z99EEEFFFc3t99tMMNNNNMr#   receipt
str | Nonec                n   |                                  sd S t          j        |                     d                    }|                    d          |k    rt          d          |                    d          dv r*|                    d          rt          |d                   S t          d          )	NrD   r   r   z*existing render receipt is stale for canonr   >   queuedrunning	submittedjob_idzFterminal render receipt already exists and requires audit/invalidation)rX   rK   rf   r   r1   r3   r   )r   r   rZ   s      r!   current_jobr   x   s    >> t:g'''99::Dxx%&&*44GHHHxx???DHHXDVDV?4>"""
_
`
``r#   jobupstreamc                <   |                                 r|                                j        dk    rt          d          t	          |          }|                    dg           }d |D             }d |D             }	t          |d         d                   }
|                    d          d	k    |                    d
          pi                     d          du t          |                    d          p)|                    d          p|                    d                    t          |          k    t          |          dk    t          |	          dk    t          |          dk    oUt          |d                             dd                    t          |d                             dd                    fdk    t          |          dk    o|d                             d          dk    t          |	          dk    o|	d                             d          dk    |
dk    d	}d|
i}| dk    r|                    d
          pi }t          t	          t          |d                    d         d                   }||d<   |                    |                    d          du |                    d          du |                    d          dv t          |
|z
            dk    d            nX| d!k    rt          t	          t          |d"                    d         d                   }t          t	          t          |d                    d         d                   }|                    ||||z   d#           t          |
|z
  |z
            dk    |d$<   nt          t	          t          |d%                    d         d                   }||d&<   |                    |                                j        d'k     |                    d
          pi                     d(          du t          |
|z
            dk    d)           t          |                                          }i d|rd	nd*d|d+|d,| d-t          |                    d-          p|                    d.                    d/|dt          |          d0t          |          d1|                                j        d2|
d3|d4|                    d
          d5|d6d7 |                                D             d8|d9t"          j                            t"          j        j                                                  d:d;d<d= |                                D             iS )>Nr   zrender output missing or emptystreamsc                D    g | ]}|                     d           dk    |S )
codec_typevideor1   .0items     r!   
<listcomp>z&independent_verify.<locals>.<listcomp>   .    KKKd,)?)?7)J)JT)J)J)Jr#   c                D    g | ]}|                     d           dk    |S )r   r   r   r   s     r!   r   z&independent_verify.<locals>.<listcomp>   r   r#   formatdurationr   r   verificationr   Tr|   output_pathr   r   r   r   )r   r   
codec_nameh264aac)	endpoint_status_completedendpoint_verifiedoutput_path_exact	one_video	one_audio
resolution
video_h264	audio_aacpositive_durationr   r   
story_fullmirror_appliedsource_audio_discardedaudio_status>   okpresentnarration_mixedg{Gz?)r   r   r   duration_matches_story_audior   r   )r   r   expected_sum#duration_matches_intro_plus_footager   source_finalr   under_one_gb)r   endpoint_under_one_gbduration_matches_masterfailedr   rz   r   idinput_receiptsartifact_sha256artifact_bytesduration_secondsduration_basisendpoint_verificationindependent_probechecksc                     i | ]\  }}|||d S ))r   r9    r   kvs      r!   
<dictcomp>z&independent_verify.<locals>.<dictcomp>   sd      LG  LG  LG  pt  pq  st  MN  ]^  ij  Pk  Pk  LG  LG  LGr#   latest_responsecompleted_athash_methodzsha256 streaming 8 MiBblockersc                    g | ]	\  }}||
S r   r   r   s      r!   r   z&independent_verify.<locals>.<listcomp>   sV      `
G  `
G  `
G  g
k
  g
h
  j
k
  EF  `
G  a
b
  `
G  `
G  `
Gr#   )rX   statst_sizer3   ry   r1   floatr   lenr`   r;   r(   absallr   r%   itemsdatetimenowtimezoneutc	isoformat)rz   r{   r|   r   r   r   infor   r   r   r   r   basisr   source_durationintro_durationfootage_durationr   s                     r!   independent_verifyr	     s   ==?? =fkkmm3q88;<<<==Dhhy"%%GKKgKKKEKKgKKKET(^J/00H%(WWX%6%6+%E!ggn55;@@LLPTT !2!2!lcggm6L6L!lPSPWPWXkPlPlmmqtu{q|q||ZZ1_3u::?%jjAoy3uQx||GQ/G/G+H+H#eTUhll[cefNgNgJhJh*imy*y%jjAoN%(,,|*D*D*NZZ1_Lql)C)Cu)Lcknoco F x E	ww~..4"nWk&J&J K KH UV` abb-l)9)9:J)K)Kt)So{oo  AY  pZ  pZ  ^b  pb  t@  tD  tD  ES  tT  tT  X|  t|  ^a  bj  m|  b|  ^}  ^}  AE  ^E  F  F  	G  	G  	G  	G	'		u^GW%E%EFFxPQ[\]] ~gy'I'I!J!J8!TU_!`aa~:J\jm}\}~~8;H~<UXh<h8i8imq8q455nWn&M&M N Nx XYc dee /nv{{}}'<}'Lhkhohop~hh  iF  DF  hK  hK  LZ  h[  h[  _c  hc  @C  DL  O^  D^  @_  @_  cg  @g  h  h  	i  	i  	i6==??##H HHX;kk8  HZ  HShjt  Hv}  @E  H  GO  QT  UX  U\  U\  ]e  Uf  Uf  Uw  jm  jq  jq  rv  jw  jw  Qx  Qx  H  zJ  LT  H  Vc  eh  io  ep  ep  H  rC  EK  LR  ES  ES  H  Ue  gm  gr  gr  gt  gt  g|  H  ~P  RZ  H  \l  ns  H  uL  NQ  NU  NU  Vd  Ne  Ne  H  gz  |@  H  BJ  LG  LG  x~  xD  xD  xF  xF  LG  LG  LG  H  IZ  \_  H  ao  qy  qB	  qF	  qF	  G	O	  G	X	  G	\	  q]	  q]	  qg	  qg	  qi	  qi	  H  k	x	  z	R
  H  T
^
  `
G  `
G  o
u
  o
{
  o
{
  o
}
  o
}
  `
G  `
G  `
G  H  H  Hr#   c                 
   t          j                    } |                     dt                     |                     dt          d           |                                 }|j        }t          |         }t          j        t          
                    d                    }t          |d          }t          j        |
                    d                    }|                    d	          d
k    s|                    d          durt          d          |d         }t          ||d                   }t          ||d                   }	t          ||d         |          }
t          |d         dz             }|                    d	          dk    s|                    d          durt          d|           t!          |||	          }t#          ||          }||	                                rt          d|	           t'          |d         |d         z   |          }t)          |                    d          p|                    d          pd          }|st          d|           t+          |dd||||d         |d         z   dz   |z   |||
|t,          j                            t,          j        j                                                  d            |d         |d         z   dz   |z   }t7          j                    |j        z   }	 t          |          }t)          |                    d	d!                    }|d"v rnt+          ||d|||||||
|t,          j                            t,          j        j                                                  d#           t7          j                    |k    r8t=          t          j        |d|dt)          |          d$d%                     d&S t7          j         d'           |d
k    rjt+          ||d||||t,          j                            t,          j        j                                                  d(           t          | d)|           tC          |||	|||
          }t+          ||           t=          t          j        |d	         |d         ||t)          |	          t)          |          |d*         d+d%                     |d         rd,nd-S ).Nrz   )choicesz
--max-waiti@8  )typedefaultrD   r   promotion_receiptr   r   r   Tz#promotion is absent or not verifiedcanon_sha256r   r   r   r
   z/healthr   r   z!worker health/NVENC gate failed: z*output exists without a live audited job: r   r   r    zcreate response has no job id: r   F/)r   r   rz   r   r   
status_urlrd   healthr   created_responsesubmitted_atunknown>   r   	cancelledr   )r   r   rz   r   r   r  rd   r  r   r   
updated_at)r   r   r   resume_requiredr   rm   r@      )r   r   rz   r   r   r   	failed_atz job terminal failure: r   )r   r   rz   r   r|   r   r   r   r   )"argparseArgumentParseradd_argumentSTAGESr`   
parse_argsrz   rK   rf   PROJECT_MANIFESTr   r;   r1   r3   r   ri   r   r   rX   rr   r   r\   r   r   r  r  r  time	monotonicmax_waitprintrL   sleepr	  )parserargsrz   cfgr{   promotion_path	promotionr   r   r|   r   r  rk   r   createdr  deadliner   r   results                       r!   mainr/     s   $&&F
000
3>>>DJE
-Cj)33W3EEFFG#G-@AAN
>33W3EEFFI}}X+--z1J1JRV1V1V@AAA>*JWc-&899GGS%677FwO(<jIIHc&kI-..Fzz(t##vzz,'?'?t'K'KGvGGHHHeWf55G*--F~==?? 	VTFTTUUUCK#h-7AAW[[**Egkk$.?.?E2FF 	LJJJKKKGQVoy  FL  \_  `f  \g  jm  nv  jw  \w  z}  \}  @F  \F  SZ  fl  @H  ^e  w  wH  wL  wL  MU  M^  Mb  wc  wc  wm  wm  wo  wo  p  p  	q  	q  	qVs8},s2V;J~$-/H	z""SWWXy1122999GEEjt  AG  Wa  nu  AG  [c  x{  KS  K\  K`  K`  ai  ar  av  Kw  Kw  KA  KA  KC  KC  D  D  	E  	E  	E>x''$*EVhly|  ~E  zF  zF  G  G  V[  \  \  \  ]  ]  ]1
1	 GEEjt  AG  \_  nv  n  nC  nC  DL  DU  DY  nZ  nZ  nd  nd  nf  nf  g  g  	h  	h  	heDDFDDEEEwZRRF   	$*x 0fZ>P[`lr  B  CI  J  J  WZ  [b  Wc  Wc  qw  xB  qC  D  D  SX  Y  Y  Y  Z  Z  Zz")11)r#   __main__r   Fz: )r   r   ru   rm   )file)r   r   r   r   )r,   r-   r.   r   r   r   )r   r   r9   r-   r   r<   )r]   )r^   r   r_   r`   r   r-   )rj   )r^   r   rk   r-   r_   r`   r   r-   )r   r   r   r-   )rz   r   r{   r-   r|   r   r   r-   )r{   r-   r   r   r   r   r   r   )r   r   r   r   r   r   )rz   r   r{   r-   r|   r   r   r-   r   r   r   r   r   r-   )r   r`   )(
__future__r   r  r   r$   rK   rQ   rw   sysrN   r"  urllib.requestrc   pathlibr   __file__r6   r>   r5   r!  r  r%   r;   r\   ri   rr   ry   r   r   r   r	  r/  __name__exit	Exceptionr:   r%  rL   r  stderrr   r#   r!   <module>r;     s#   " " " " " "     				     



            tH~~'*88 3?`  rC  S\  p}  O  oP  Q  Q1=\m|  M[  n|  n|  n|  }  }5Ad  vI  Yg  {J  L^  z_  `  `
 
         ; ; ; ; ;
; ; ; ; ;      0   a a a a!H !H !H !HH/* /* /* /*d z   jdjH%TTRUYYM_JhJhcfJhJhiix}~~~  FI  FP  	Q  	Q  	Q  	Q s   5C D(A	D##D(