
    =h                     b    d dl Z d dlZd dlZd dlZd dlmZmZ d dlmZ d dl	m
Z
  G d de      Zy)    N)ABCabstractmethod)logcolor)Pathc                   t    e Zd Z	 	 	 	 	 	 	 	 	 	 	 ddZd Zd Zd Zd Zd Zd Z	d	 Z
ed
        Zed        Zy)WorkflowRunnerBaseNc                    || _         || _        || _        |	xs g | _        || _        || _        t        j                  j                  |      | _	        t        j                  j                  |      | _
        || _        |
| _        || _        || _        || _        || _        || _        || _        d | _        d | _        g | _        g | _        | j/                          | j1                          y N)loggermanifest_listsoftware_nameunneeded_argsthreadsstepnameospathabspath
input_pathoutput_pathadditional_argsoutput_typeleafoutput_for_next_stepshellcarry_previousrescan_outputmode
start_timeend_timeinput_file_listcommand_queuedynamic_flags_from_argsdynamic_mode)selfr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   s                    G/mounts/lovelace/software/shotgun-workflow/dev/modules/software_base.py__init__zWorkflowRunnerBase.__init__   s    & ***0b ''//*577??;7.&	$8!
,*	!$$&    c                      y r
    r$   s    r%   r#   zWorkflowRunnerBase.dynamic_mode3   s    r'   c                      y r
   r)   r*   s    r%   r"   z*WorkflowRunnerBase.dynamic_flags_from_args6       r'   c                 f   t        j                  | j                        D cg c]L  }t         j                  j	                  t         j                  j                  | j                  |            sK|N }}g }|D ]  }|j                  t        t         j                  j                  |            j                  d d t         j                  j                  | j                  t        t         j                  j                  |            j                         d| j                   d d        |S c c}w )N.)idinput1input2output1output2)r   listdirr   r   isfilejoinappendr   basenamestemr   )r$   fdiscover_list
new_outputfiles        r%   rediscover_outputz$WorkflowRunnerBase.rediscover_output:   s    $&JJt/?/?$@v$@qBGGNNSUSZSZS_S_`d`p`prsStDu$@v
!DBGG,,T2499 ggllD,<,<dBGGDTDTUYDZ>\>a>acddefjfvfvewx  "  ws   AD./D.c                 F    t        j                  | j                  d       y )NTexist_ok)r   makedirsr   r*   s    r%   create_output_dirz$WorkflowRunnerBase.create_output_dirG   s    
D$$t4r'   c           
         dd| j                    d| j                    d| j                   d| j                   ddg| _        | j                  j	                  dt
        j                   t
        j                   t
        j                   | j                   t
        j                          | j                  j	                  d	t
        j                   d
| j                   t
        j                          | j                          | j                  d   j                  dd      }| j                  j	                  d	t
        j                   | j                   dt
        j                   d| d       y )NSampleIDHorizon1z/input_file1z/input_file2z/output_file1z/output_file2)r/   horizonr0   r1   r2   r3   -> z--> zCreate directory for output r   z -z
 -z command to run for each input:
)r   r   r    r   infor   INFO	UNDERLINEBOLDr   ENDCbuild_command_queuer!   replacer   )r$   sample_commands     r%   dry_runzWorkflowRunnerBase.dry_runK   sV   !)6)6**+=9**+=9!
   	3x}}oh.@.@-A(--QUQ^Q^P_`h`m`m_nop4.J4K[K[J\]e]j]j\klm  "++A.66tVD4t/A/A.BBabjboboappr  tB  sC  CE  F  	Gr'   c                    | j                   j                  t        j                   dt        j                          t        j                  | j                        D ]  \  }}}|D ]  }t
        j                  j                  ||      }t
        j                  j                  || j                        }t
        j                  j                  | j                  |      }t        j                  t
        j                  j                  |      d       t        j                  ||         | j                   j                  t        j                   dt        j                          y )Nz#--> Copying previous step output...Tr@   z	--> Done.)r   rJ   r   rK   rN   r   walkr   r   r6   relpathr   rB   dirnameshutilcopy2)r$   root_filesr=   src_pathrel_pathdst_paths           r%   carryzWorkflowRunnerBase.carry[   s    HMM?*Mhmm_]^ ggdoo6ND!U77<<d377??8T__E77<<(8(8(CBGGOOH5EXx0  7 	HMM?)HMM?CDr'   c                    t        j                          | _        | j                  j                  dt        j
                   t        j                   t        j                   | j                   t        j                          | j                  r| j                          t        | j                  d      D ]  \  }}| j                  j                  t        j
                   d| dt        | j                         dt        j                          | j                  j                  t        j
                   dt        j                   | t        j                          t!        j"                  | j$                  r|n|j'                         t         j(                  t         j(                  | j$                        }|j+                         \  }}|j,                  }| j                  j                  |j/                                | j                  j1                  |j/                                |dk(  ri| j                  j                  t        j
                   d	| dt        | j                         d
t        j                   | t        j                   	       | j                  j                  t        j2                   d	| dt        | j                         dt        j                   | t        j                   	       | j                  j                  t        j2                   dt        j                          t5        d        t        j                          | _        | j8                  r| j;                         S y )NrH      z--> Starting Subprocess /z...z--> Command: )stdoutstderrr   r   z--> Subprocess z complete, return code z error, return code z<--> Halting workflow due to non-zero subprocess return code.)timer   r   rJ   r   rK   rL   rM   r   rN   r   r_   	enumerater!   len	REFERENCE
subprocessPopenr   splitPIPEcommunicate
returncodedecodeerrorERRORexitr   r   r>   )r$   icmdprocessrc   rd   return_codes          r%   runzWorkflowRunnerBase.runh   s   ))+3x}}oh.@.@-A(--QUQ^Q^P_`h`m`m_nopJJL   2 2A6FAsKK.Fqc3tOaOaKbJccfgogtgtfuvwKKmH<N<N;OPSuU]UbUbTcde !&&zzsyy{!!jj	G %002NFF!,,KKKV]]_-KKfmmo. a  HMM?/!Ac$J\J\F]E^^uv~  wI  wI  vJ  KV  JW  X`  Xe  Xe  Wf  "g  h  HNN#3?1#Qs4K]K]G^F__st|  uG  uG  tH  IT  HU  V^  Vc  Vc  Ud  "e  f  HNN#33opxp}p}o~!  AQ1 72 		 ))++r'   c                      y r
   r)   r*   s    r%   find_input_filesz#WorkflowRunnerBase.find_input_files   r,   r'   c                      y r
   r)   r*   s    r%   rO   z&WorkflowRunnerBase.build_command_queue   r,   r'   )   stepname_DEFAULTsoftware_name_DEFAULTNoutput_DEFAULTFTFFFN)__name__
__module____qualname__r&   r#   r"   r>   rC   rR   r_   rw   r   ry   rO   r)   r'   r%   r   r      sy     #-$!#*X5G 	E'R    r'   r   )r   ri   rW   re   abcr   r   workflow.logr   pathlibr   r   r)   r'   r%   <module>r      s"    # # # ! Q Qr'   