
    5iF                        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mZmZ ddlmZ 	 ddlZddlmZ dd	lmZ eed
ZddiZ	  ej:                  dej                   xs dv rdnd        ej<                  d       ded<    G d d      Z d Z!d#dZ"d$dZ#d Z$d#dZ%d Z&d#dZ'd#dZ(d Z)d Z*d Z+d  Z,d! Z-	 	 	 d%d"Z.y# e$ rZddlZY dZ[dZ[ww xY w# e$ r  ed       Y cw xY w)&zU
Miscellaneous utility classes and functions for the dx-app-wizard command-line tool
    )print_functionunicode_literalsdivisionabsolute_importN   )BOLDDNANEXUS_LOGOENDCfill)prompt_for_yn   )python)bash)Pythonr   	availableFlibedit zbind ^I rl_completeztab: completeTzANOTE: readline module not available.  Install for tab-completion.c                       e Zd Zd Zd Zy)	Completerc                      d | _         || _        y N)matcheschoices)selfr   s     w/home/marpiech/ifpan-abm-pgxpred/analysis/marpiech-gwas-test/venv/lib/python3.12/site-packages/dxpy/templating/utils.py__init__zCompleter.__init__6   s        c                     |dk(  r't        t        fd| j                              | _        | j                  '|t	        | j                        k  r| j                  |   S y )Nr   c                 &    | j                        S r   )
startswith)choicetexts    r   <lambda>z$Completer.complete.<locals>.<lambda><   s    f6G6G6M r   )listfilterr   r   len)r   r"   states    ` r   completezCompleter.complete:   sU    A:'M"&,,!0 1DL <<#DLL0A(A<<&&r   N)__name__
__module____qualname__r   r(    r   r   r   r   5   s    r   r   c                 P    dj                  d | j                  d      D              S )N
c              3   <   K   | ]  }|j                           y wr   )rstrip).0lines     r   	<genexpr>zclean.<locals>.<genexpr>E   s     =tT[[]=s   )joinsplit)ss    r   cleanr7   D   s    99=qwwt}===r   c                 \    t         d   r#|  t        j                  | j                         y y y )Nr   )completer_statereadlineset_completerr(   )	completers    r   use_completerr=   G   s+    {#	(=y112 )>#r   c                 @   | }||d|z   dz   z  }n|dz  }	 	 t        |      }dk7  r*|&||vr"t        dt        j                  |      z          n
|S ||S |r|S D# t        $ r t        d       t        d       Y Yt        $ r t        d       t        d       Y yw xY w)Nz [z]: : r   r   z.Error: unrecognized response, expected one of )inputKeyboardInterruptprintexitEOFErrorjsondumps)
prompt_strdefaultallow_emptyr   promptvalues         r   prompt_for_varrL   M   s    F$.5(($
	&ME B;"uG';FT[I\\] NL#  ! 	"IG 	"IG	s   A B<BBc                     t        t               dz   | z          t        d       t        t               dz   t               z          t        d       t        t	        d             t        d       y )Nz App Wizard, API vr   zBasic MetadatazPlease enter basic metadata fields that will be used to
describe your app.  Optional fields are denoted by options with square
brackets.  At the end of this wizard, the files necessary for building your
app will be generated from the answers you provide.)rB   r	   r   r
   r   )api_versions    r   print_introrO   f   sY    	-/0
0;
>?	"I	$&#
#df
,-	"I	$ 7 8 9 
"Ir   c                 <   t        t        dt               z   dz   t               z   dz                t	        j
                  d      }	 t        d|       }|j                  |      t        t        d             nt        j                  j                  |      rt        j                  j                  |      rKt        d|z        }|r/t        j                  |       t        t        d|z               	 |S t        d	       t        t        d
|z               	 |S )NThe namea   of your app must be unique on the DNAnexus platform.  After creating your app for the first time, you will be able to publish new versions using the same app name.  App names are restricted to alphanumeric characters (a-z, A-Z, 0-9), and the characters ".", "_", and "-".z^[a-zA-Z0-9._-]+$zApp Namez3The name of your app must match /^[a-zA-Z0-9._-]+$/zwThe directory %s already exists.  Would you like to remove all of its contents and create a new directory in its place?z)Replacing all contents of directory %s...r   zOA file named %s already exists.  Please choose another name or rename your file)rB   r   r   r
   recompilerL   matchospathexistsisdirr   shutilrmtree)rH   name_patternrR   
remove_dirs       r   get_namer^   r   s   	$v'$&0  4F  F  G  H::12L
j'2d#+$LMNww~~d#77==&!.  0i  lp  0p  "qJ!d+d#NQU#UVW K b	 $pswwxyK% r   c                 &   t        d       t        t        dt               z   dz   t               z   dz                t	        dd      }t        d       t        t        dt               z   dz   t               z   dz                t	        dd      }||fS )	Nr   rQ   titlezj, if provided, is what is shown as the name of your app on the website.  It can be any valid UTF-8 string.Titlesummaryzy of your app is a short phrase or one-line description of what your app does.  It can be any UTF-8 human-readable string.SummaryrB   r   r   r
   rL   )rN   r`   rb   s      r   get_metadatare      s    	"I	$v(461  5a  a  b  c7B'E	"I	$v*TV3  7r  r  s  tY+G'>r   c                     | d} t        d       t        t        dt               z   dz   t               z   dz                t	        d|       }|S )Nz0.0.1r   z7You can publish multiple versions of your app, and the versionz of your app is a string with which to tag a particular version.  We encourage the use of Semantic Versioning for labeling your apps (see http://semver.org/ for more details).Versionrd   )rH   rg   s     r   get_versionri      s[    	"I	$H46QT]]`d`ff  j[  [  \  ]Y0GNr   c                    dddd}dddd}t        j                  d      }d	 }| d
} n ||       } t        d       t        t        dt	               z   dz   t               z   dz                	 t        d|       }|j                  |      st        t        d             n-t        |d d       ||d      kD  rt        t        d             nn`t        |d d       ||d      fS )Ni  i     )mhdminuteshoursdaysz^[1-9]\d*[mhd]$c                 h    t        | j                         d         | j                         d   d   z   S )Nr   )strvalueskeys)rn   s    r   timeout_dict_to_strz(get_timeout.<locals>.timeout_dict_to_str   s,     188:a=!AFFHQKN22r   48hr   zSet a ztimeout policyz for your app. Any single entry point of the app that runs longer than the specified timeout will fail with a TimeoutExceeded error. Enter an int greater than 0 with a single-letter suffix (m=minutes, h=hours, d=days) (e.g. "48h").zTimeout policyzLError: enter an int with a single-letter suffix (m=minutes, h=hours, d=days)z%Error: max allowed timeout is 30 days)	rS   rT   rB   r   r   r
   rL   rU   int)rH   max_timeoutunitstime_patternrv   timeouts         r   get_timeoutr~      s    $7<K'7E::/0L3
 %g.	"I	$x$& #33df<  @i  i  j  k
 !17;!!'*$efg"WR[!99$>?@  ws|eGBK000r   c                 r    t        |       d| dz  cxk  rdk  rdz   S  ddddj                  | d	z  d      z   S )
N   d      thstndrd)r   r      
   )rs   get)nums    r   get_ordinal_strr      sI    s8rS3Y4"4tjj4dtPT:U:Y:YZ]`bZbdh:ijjr   c                     ddg} t        t        |              t        d       t        t        dt	               z   dz   t               z   dz   t               z   dz   dj                  |       z                t        d	| 
      }t                |S )Nr   r   r   zYou can write your app in any zprogramming languagez@, but we provide templates for the following supported languagesr?   z, zProgramming languager   )r=   r   rB   r   r   r
   r4   rL   )language_choiceslanguages     r   get_languager      s     &)),-.	"I	$/$&8;QQTXTZZ  ^`  `  cg  ci  i  lp  p  sw  s|  s|  }M  sN  N  O  P4>NOHOOr   c                     g }t        d       t        t        dt               z   dz   t               z   dz                |j	                  d       t        dt               z   dz   t               z          t        t        ddd	             t
        j                  j                  t
        j                  j                  | d
            rL|j	                  d
       t        dt               z   d
z   t               z          t        t        ddd	             t
        j                  j                  t
        j                  j                  | d            rh|j	                  d       t        dt               z   dz   t               z          t        t        dt               z   d
z   t               z   dz   dd	             t        |      dk(  rt        d       yt        t        |             t        dd|      }t                |S )Nr   zThe following common zexecution patternsz7 are currently available for your programming language:basic z<Your app will run on a single machine from beginning to end.z   )initial_indentsubsequent_indentparallelizedzYour app will subdivide a large chunk of work into multiple pieces that can be processed in parallel and independently of each other, followed by a final stage that will merge and process the results as necessary.zscatter-process-gatherzSimilar to z but with the addition of a "scatter" entry point.  This allows you to break out the execution for splitting up the input, or you can call a separate app/applet to perform the splitting.r   z1Automatically using the execution pattern "basic"zExecution patternr   )rB   r   r   r
   appendrV   rW   rY   r4   r&   r=   r   rL   )template_dirpattern_choicespatterns      r   get_patternr      s   O	"I	$&/2FFO  SL  L  M  N7#	#,
 46
)*	$M^cw|
}~	ww}}RWW\\,?@~.cDFl^+df45d  k  |A  UZ  [  	\	ww}}RWW\\,0HIJ78cDFl55>?d=46)N:TVC  GC  C"'%*, 	- ?q AB)O,-0'?SGONr   c                 F    | j                  d|      j                  d|      S )z
    TODO: Rename this?
    DX_APP_WIZARD_NAMEDX_APP_WIZARD_VERSION)replace)template_stringrR   rg   s      r   fill_in_name_and_verr      s'     ""#7>FFG^`ghhr   c                 ~    t        |       } d| vr| d   | d<   d| vrd| d<   nd| d   z   | d<    dj                  di | S )NlabelrR   helpr   r   z+* **{label}** ``{name}``: ``{class}``{help}r,   )dictformat)io_specs    r   format_io_spec_to_markdownr      s[    7mGg"6?W/?8??J'JJr   c                 h
   $%&'( g %|d   &|j                  d&      }|j                  dd      }|d   (|	dz  }	d $$%& (fd}t        j                         D ]   ''d	v s'j                  d
      r |'       " t        j                  j                  t        j                  j                   d            rst        j                  t        j                  j                   d            D ]=  't        'fddD              r |t        j                  j                  d'             ? t        j                  t        j                  j                   d            D ]
  ''j                  d
      r'j                  d      r'j                  d|	z         s=t        t        j                  j                   d'      d      5 }t        |j                         &(      }d|v r|d   D ci c]  }|d   d 
 }}ni }t        |   j                  |||||||      \  }}}}}|j                  d|      }|j                  d|      }|j                  d|      }|j                  d|      }|j                  d|      }|j                  d|
      }|j                  d|      }t        j                  j                  &d'j                  d|	z   &dz               }t        |d      5 }|j                  |       d d d        |j                  d      s|j                  d      r $|       %j!                  |       d d d         |t        j                  j                  d'              d}t        t        j                  j                  &d      d      5 }|j                  |j#                  |||             d d d        %j!                  t        j                  j                  &d             d}d j                  |D cg c]  }d!j#                  |       c}      } t%        |      d"kD  rd#}!nd$}!d%j                  |D cg c]  }d&j#                  |'       c}      }"t        t        j                  j                  &d(      d      5 }#|#j                  |j#                  &| |"|!)             d d d        %j!                  t        j                  j                  &d(             %S c c}w # 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   RxY wc c}w c c}w # 1 sw Y   pxY w)*NrR   r`   rb   r   rg   .c                 |   	 t        j                  | t        j                  t        j                  z  t        j
                  z  t        j                  z  t        j                  z  t        j                  z  t        j                  z         y # t        $ r%}t        dj                  | |             Y d }~y d }~ww xY w)Nz!Unable to change file {} mode: {})rV   chmodstatS_IRUSRS_IWUSRS_IXUSRS_IRGRPS_IXGRPS_IROTHS_IXOTHOSErrorrB   r   )filees     r   	chmod_755z.create_files_from_templates.<locals>.chmod_755  s    	GHHT\\DLL04<<?\\"$(LL137<<@\\"#  	G5<<T1EFF	Gs   B
B 	B;B66B;c                    t        t        j                  j                  |       d      5 }t	        |j                         	      }t        j                  j                  |       }t        |d      5 }|j                  |       ddd       |j                  d      s|j                  d      r |       j                  |       ddd       y# 1 sw Y   MxY w# 1 sw Y   yxY w)z>
        :param path: relative path from template_dir
        rwN.py.sh)	openrV   rW   r4   r   readwriteendswithr   )
rW   template_file	file_textfilled_template_filenamefilled_template_filer   manifestrR   r   rg   s
        r   use_template_filez6create_files_from_templates.<locals>.use_template_file  s     "'',,|T2C8 	6M,]-?-?-A4QI')ww||D$'?$.4 68L$**956'007;S;\;\]b;c23OO45	6 	66 6	6 	6s%   AC4CACC	CC')srctestz
dxapp.json~r   c              3   @   K   | ]  }j                  |        y wr   )r   )r1   exttemplate_filenames     r   r3   z.create_files_from_templates.<locals>.<genexpr>"  s     cs$--c2cs   )r   z.pycz.pyo__pycache__r   coder   
outputSpecDX_APP_WIZARD_INPUT_SIGNATUREDX_APP_WIZARD_INITIALIZE_INPUT DX_APP_WIZARD_DOWNLOAD_ANY_FILESDX_APP_WIZARD_UPLOAD_ANY_FILESDX_APP_WIZARD_OUTPUT DX_APP_WIZARD_PARALLELIZED_INPUT!DX_APP_WIZARD_PARALLELIZED_OUTPUTr   r   r   ak  <!-- dx-header -->
# {app_title} (DNAnexus Platform App)

{summary}

This is the source code for an app that runs on the DNAnexus Platform.
For more information about how to run or modify it, see
https://documentation.dnanexus.com/.
<!-- /dx-header -->

{description}

<!--
TODO: This app directory was automatically generated by dx-app-wizard;
please edit this Readme.md file to include essential documentation about
your app that would be helpful to users. (Also see the
Readme.developer.md.) Once you're done, you can remove these TODO
comments.

For more info, see https://documentation.dnanexus.com/developer.
-->
z	Readme.md)	app_titlerb   descriptiona  # {app_name} Developer Readme

<!--
TODO: Please edit this Readme.developer.md file to include information
for developers or advanced users, for example:

* Information about app internals and implementation details
* How to report bugs or contribute to development
-->

## Running this app with additional computational resources

This app has the following entry points:

{entry_points_list}

{instance_type_override_message}

    {{
      systemRequirements: {{
        {entry_points_hash}
      }},
      [...]
    }}

See <a
href="https://documentation.dnanexus.com/developer/api/running-analyses/io-and-run-specifications#run-specification">Run
Specification</a> in the API documentation for more information about the
available instance types.
r.   z* {0}r   zWhen running this app, you can override the instance type to be used for each
entry point by providing the ``systemRequirements`` field to
```/applet-XXXX/run``` or ```/app-XXXX/run```, as follows:zWhen running this app, you can override the instance type to be used by
providing the ``systemRequirements`` field to ```/applet-XXXX/run``` or
```/app-XXXX/run```, as follows:z
,
        z3"{entry_point}": {{"instanceType": "mem2_hdd2_x2"}})entry_pointzReadme.developer.md)app_nameentry_points_listentry_points_hashinstance_type_override_message)r   rV   listdirr   rW   rX   r4   anyr    r   r   r   language_optionsget_stringsr   r   r   r   r&   ))r   app_jsonr   required_file_input_namesoptional_file_input_namesrequired_file_array_input_namesoptional_file_array_input_namesfile_output_namesr   pattern_suffixparallelized_inputparallelized_outputr   entry_pointsr`   rb   r   code_template_filecode_file_textoutputdummy_output_hashinput_sig_strinit_inputs_strdl_files_strul_files_stroutputs_strfilled_code_filenamefilled_code_filereadme_templatereadme_filedeveloper_readme_templater   r   r   r   developer_readme_filer   r   rR   r   rg   s)   `                                   @@@@@r   create_files_from_templatesr      s    HFDLL$'Ell9b)Gy!GcNG6 6  ZZ5 - ==ARA[A[\_A`+,-
 
ww~~bggll<89!#BGGLLv,N!O 	Gc>bccbggll63DEF	G  ZZ\5(IJ %F%%c*))&1 ++F^,CD"'',,|U<MNPST :Xj%9:L:Q:Q:SUY[b%cN#x/PXYePf,gfVF^T-A,g),g,.) )2>>x?X?X?^?^?P?PR \M?L,P[ &4%;%;<[]j%kN%3%;%;<\^m%nN%3%;%;<^`l%mN%3%;%;<\^j%kN%3%;%;<RT_%`N%3%;%;<^`r%sN%3%;%;<_at%uN+-77<<eEVE^E^_ehv_vx|  @C  yC  FD  ,E(2C8 ?<L(..~>?+44U;?S?\?\]b?c!"67OO$89=: :@ bggll52CDEK%FRO, 
bggll4-s	3 m{/005'_j0klmOOBGGLL{34!> 		R^"_;7>>+#>"_`
<1*>&*$& &**  Vb  ,c  GR,a,h,h  vA,h  -B  ,c  d	bggll4!67	= AV##$=$D$D"3"3/M	 %E % 	 OOBGGLL'<=>Oc -h,? ?5: :tm mJ #` ,c s\   'T2S2?C*T)S7;AT$T	T
T#%T(2T7T<TT	T(T1r   )NFN)r   r   r   r   r,   )/__doc__
__future__r   r   r   r   rV   sysrZ   
subprocessrS   rE   r   utils.printingr   r	   r
   r   clir   gnureadliner:   ImportErrorr   r   r   r   r   r9   parse_and_bindset_completer_delimsrB   r   r7   r=   rL   rO   r^   re   ri   r~   r   r   r   r   r   r   r,   r   r   <module>r     s/  " S R , , ,  > > "     OHY8CSCSCYWY5Z1`op!H!!"%#'OK  >32
.	16k<iK 9;[]-/mg  O&  O	
MNOs)   C =C) C&C!!C&)C98C9