
    5i=D                         d Z ddlmZmZmZ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mZmZ ddlmZmZ ddlmZ d	 Zd
 Zd Zi fdZdi fdZdi fdZy)z
App, Applet, and Workflow Unbuilder
+++++++++++++

Contains utility methods for "decompiling" an app, applet, or workflow
from the platform and generating an equivalent source directory.

    )print_functionunicode_literalsdivisionabsolute_importN   )get_handlerdownload_dxfile)err_exitDXError   )flatten_json_arrayc                     t        | t              rPt        | j                               D ]3  \  }}t        |t              rt	        |       |dk(  s|g k(  s|i k(  s1| |= 5 yy)z8
    Aggressively cleans up things that look empty.
     N)
isinstancedictlistitems_recursive_cleanup)fookeyvals      /home/marpiech/ifpan-abm-pgxpred/analysis/marpiech-gwas-test/venv/lib/python3.12/site-packages/dxpy/utils/executable_unbuilder.pyr   r   )   s]     #tsyy{+ 	JS##t$"3'byC2IH		     c           
          t        | d      5 }|j                  t        t        j                  |dd      d             |j                  d       d d d        y # 1 sw Y   y xY w)Nwr   ),z: )indent
separatorspatterns
)openwriter   jsondumps)filenamejson_contentfs      r   _write_json_filer(   5   sQ    	h	 	"4::l1Q\#]_ijk	  s   AAA"c                 h    t        | d      5 }|j                  |       d d d        y # 1 sw Y   y xY w)Nr   )r!   r"   )r%   contentr'   s      r   _write_simple_filer+   ;   s/    	h	 	  s   (1c                    g d}g d}t        j                         }|D ]  }||v s||   s||   ||<    dD ]  }||v s||   ||   ||<    |j                  dd      }g }|D ]A  }t        j                         }	|D ]  }||v s||   s||   |	|<    |j                  |	       C ||d<   t	        d|       |j                  dd      }
|
rt        d	|
       y y )
N)nametitlesummarydxapiversionoutputFoldertreeTurnaroundTimeThreshold)idr-   
executablefolderinputexecutionPolicysystemRequirements)inputsoutputsstages zdxworkflow.jsondescriptionr   	Readme.md)collectionsOrderedDictgetappendr(   r+   )workflow_objdescribe_outputdxworkflow_json_keysdxworkflow_json_stage_keysdxworkflow_jsonr   r<   
new_stagesstage	new_stagereadmes              r   _dump_workflowrM   @   s/   K"K "--/O# 8/!oc&:#23#7OC 8 % 8/!oc&:&F#23#7OC 8   2.FJ %++-	- 	,Ce|c
!&s	#	, 	)$% !+OH &8  3F;/ r   Fc           	          | j                         }|d   d   dk(  rd}n_|d   d   j                  d      rd}nEt        dj                  |d   d         t        j
                         t	        j                  d	       d
j                        }t        j                  d       t        |d      5 }|j                  |d   d          d d d        d }	 t        j                  j                  t        j                               d   }t        t        j                   d|z   dz   i       j                  d            }	t        |d   d   j%                               }
|
j'                  	      sIt        dj                  dj)                  |
j+                  |	            |      t        j
                         |
j-                  |	       t               }|sd}|
st#        dj                  |d               	 t        j                  j/                  t        j0                  dddii      d   }||
v r-|}t        dj                  |      t        j
                         n8t3        |
      d   }t        d j                  |      t        j
                         d! }d}|d   d   |   D ]
  }	 t5        |d"         }|j7                         }t9        |t        j:                        s>|j=                         j                  d#      r^|st        j                  d$       d}d%j                  |      }t?        ||       t        d&j                  |j                  d            t        j
                          ||d$       t        j@                  |       |jC                  |j                  d              d}|s*t        d(j                  |      t        j
                         tK        jL                         }| jO                         | jQ                         z   }|D ]-  }|| jS                         v r||v r||   ||<   ||v s&||   ||<   / |j                  d)d      rd|d)<   |d   d= ||d   d*<   d+}d,D ]  }||j%                         vrtU        ||         D ]e  \  }}tK        jL                         }|D ]  }||j%                         v s||   ||<    |j%                         D ]  }||vs||   ||<    |||   |<   g  d-d.d/}||d   j                  d0d1      v r|d   d0   jW                  |       i |d2<   |
D ]  }i |d2   |<   d3|d   v r?|d   d3   |   }|D ]$  } 	 ||    d4   d5   }! ||| |!|      }"|"||    d4   d5<   & ||d2   |   d6<   |d   d   |   }#|#D $cg c]  }$|$d   |vs|$ }%}$|%sv|%|d2   |   d7<    |d   j[                  dd        |d   j[                  d7d        |d   j[                  d3d        |d   j[                  d6d        d|v rt]        |d          d8|v rt]        |d8          | j_                         D ]  }||v s||   r||=  |j                  d9d:      }&|j                  d;d:      }'ta        d<|       |&rtc        d=|&       |'rtc        d>|'       y y # 1 sw Y   xY w# t"        $ rC t        dj                        t        j
                         t	        j                  d	       Y w xY w#  d }Y xY w# t"        $ rU t        d'j                  |      t        j
                         |jE                          tG        jH                  d$       Y  w xY w# tX        $ r Y  w xY wc c}$w )?NrunSpecinterpreterbashshpythonpyz@Sorry, I don't know how to get executables with interpreter {}.
)filer   zsrc/code.{}srcr   codec                     dj                  | ||      }t        |d      5 }|j                  |       ddd       |S # 1 sw Y   |S xY w)z
        Writes the string `code` into a file at the relative path
        "src/<region>_<entry_point>_clusterBootstrap.<suffix>"
        zsrc/{}_{}_clusterBootstrap.{}r   N)formatr!   r"   )regionentry_pointrW   suffixscript_namer'   s         r   "make_cluster_bootstrap_script_filez?_dump_app_or_applet.<locals>.make_cluster_bootstrap_script_fileq   sL    
 6<<V[RXY+s# 	qGGDM		s	   <AbillTo/z	/describepermittedRegionszJFailed to get permitted regions where {} can perform billable activities.
bundledDependsByRegionzkRegion(s) {} are not among the permitted regions of {}. Resources from these regions will not be available.z, FzCannot download resources of the requested executable {} since it is not available in any of the billable regions. You can use the --omit-resources flag to skip downloading the resources. r-   fieldsrZ   Tinput_paramsz:Trying to download resources from the current region {}...r   zATrying to download resources from one of the enabled region {}...c                 &   t        j                  |       5 }|j                         D ]C  }|j                  j	                  d      r|j                  dd  |_        |j                  ||       E 	 d d d        j                          y # 1 sw Y   xY w)Nr`   r   )tarfiler!   
getmembersr-   
startswithextractclose)tarfnamepathtms       r   untar_strip_leading_slashz6_dump_app_or_applet.<locals>.untar_strip_leading_slash   sv    h' '1 'Avv((-!"IIa&''
 GGI' 's   ABBr4   AssetBundle	resourceszresources/{}.tar.gzzUnpacking resource {}z%Failed to download {} from region {}.zDownloading resources from region {} failed. Please try downloading with their IDs in dxapp.json, or skip downloading resources entirely by using the --omit-resources flag.hiddenrU   )r-   labelhelpclasstyper   optionaldefaultchoicessuggestionsgroup)	inputSpec
outputSpecz
dx-toolkitapt)r-   package_managerexecDependsr=   regionalOptionssystemRequirementsByRegionclusterSpecbootstrapScriptr9   bundledDependsaccessr>   r   developerNotesz
dxapp.jsonr?   zReadme.developer.md)2rB   ri   printrY   sysstderrexitosmkdirr!   r"   dxpyapiuser_describewhoamisetDXHTTPRequestr   keysissubsetjoin
differenceintersection_updateproject_describeWORKSPACE_IDr   r   get_idr   DXFileget_propertiesr	   unlinkaddclearshutilrmtreer@   rA   _get_required_keys_get_optional_keys_get_describe_output_keys	enumerateremoveKeyErrorpopr   _get_cleanup_keysr(   r+   )(r5   omit_resourcesrE   infor\   scriptr'   r^   bill_topermitted_regionsenabled_regionsdeps_downloadeddownload_completedcurrent_regionsource_regionrp   created_resources_directorydepfile_handle
handler_idfname
dxapp_jsonall_keysr   ordered_spec_keysspec_keyispecordered_spec
dx_toolkitrZ   region_sys_reqsr[   bootstrap_scriptr%   region_dependsdregion_bundle_dependsrL   devnotess(                                           r   _dump_app_or_appletr   _   s   >>DI}%/	i	'	2	28	<RYYOM*,25**	> !!&)FHHUO	fc	 )a	Y'()((((7A 2 23=;3NPR S W WXj kl $y/*BCHHJKO##$56{  C  CIIo001BCDgOUXU_U_	b ''(9: eO" \\b\bcghnco\pr r
	"!XX66t7H7HX`ckmqbrWs6tu}~N _,+MNUUVcdknkukuv 1!4MU\\]jkrur|r|}	 ',#	?#;<]K $	&C )#d)4(//1
!+t{{; --/33MB 3HH[)26/-55jA
E2-44SWWV_ECJJW)%=		% ##CGGFO43$	&H "&
 " ]]c]cdq]ry|  zD  zDE ((*J,,.1N1N1PPH (*6688SO=S-c2JsO$;"3iJsO	(
 xx% #
8 	9f%$*Jy&!1- 3:??,, H!56 
	3GAt&224L( 2$))+%(,S	L%2 yy{ 2//(,S	L%2 '3Jx #
	33  '5AJZ	*..}bAA9m,33J? %'J !! Z02
$%f-':i+@@(34PQRXYO  / 
	'6{'CM'RSd'e$A&BMBRBH JH V^OK0?@QR
 IXJ()&12FG#I./GHP,: _qai>^ _ _ DYJ()&12BC/Z6 y6=y.5 y:4@y248 J:i01::h/0++-  *Z_3  XXmR(Fxx("-H \:.;/0(; }) )"  [bbcjkrur|r|}6	"!Np  =DDZQ^_ ZZ)  %%'k*d    !`sj   0Z$A(Z1 56\  )8\
"\
B-\
!]+];%];$Z.1A[=<[= \
A]('](+	]87]8c                 <   	 t        j                         }t        j                  |       t        | t        j
                        rt        | |       nt        | t        j                        rdt        j                  }|st        d      t        j                  j                  |dddii      d   }| j                  ||      }t        | |       nt        | ||       t        j                         y#  t                Y $xY w# t        j                         w xY w)a  
    Reconstitutes an app, applet, or a workflow into a directory that would
    create a functionally identical executable if "dx build" were run on it.
    destination_directory will be the root source directory for the
    executable.

    :param executable: executable, i.e. app, applet, or workflow, to be dumped
    :type executable: DXExecutable (either of: DXApp, DXApplet, DXWorkflow, DXGlobalWorkflow)
    :param destination_directory: an existing, empty, and writable directory
    :type destination_directory: str
    :param omit_resources: if True, executable's resources will not be downloaded
    :type omit_resources: boolean
    :param describe_output: output of a describe API call on the executable
    :type describe_output: dictionary
    ziA project needs to be selected to "dx get" a global workflow. You can use "dx select" to select a projectrc   rZ   Trd   N)r   getcwdchdirr   r   
DXWorkflowrM   DXGlobalWorkflowr   r   r   r   append_underlying_workflow_descr   r
   )r5   destination_directoryr   rE   old_cwdcurrent_projectrZ   s          r   dump_executabler   O  s     ))+
&'j$//2:7
D$9$9: #//O"A AXX..=ERVGW<X / ZZbdF(HHZ`aO:7
NOL 	

s   CC3 3D?D D)__doc__
__future__r   r   r   r   r@   r#   r   r   rg   r   r   r   r   r	   
exceptionsr
   r   pretty_printr   r   r(   r+   rM   r   r   r=   r   r   <module>r      sg   " S R   	 
    + * ,	
 24 0> 49" n<` GL]_ ,r   