
    5i                         d dl 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
mZ d dlmZmZ d Zd Zd Zd Zd	 Zd
 Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zy)    )pathmakedirslistdirN)ResourceNotFound)run_nextaur_collectbundle_docker_imagesc                       y)Nzsrc/nextflow.sh r
       ~/home/marpiech/ifpan-abm-pgxpred/analysis/marpiech-gwas-test/venv/lib/python3.12/site-packages/dxpy/nextflow/nextflow_utils.pyget_source_file_namer      s    r   c                 ^    | dk(  ryt        j                  t        j                  |             S )z
    :param resources_dir: Directory with all source files needed to build an applet. Can be an absolute or a relative path.
    :type resources_dir: str or Path
    :returns: The name of the folder
    :rtype: str
    N )r   basenameabspathresources_dirs    r   get_resources_dir_namer      s'     ==m455r   c                 @    t        j                  dt        |             S )Nz/home/dnanexus/)r   joinr   r   s    r   get_resources_subpathr      s    99&(>}(MNNr   c                       y)Nnextflow_pipeline_importerr
   r
   r   r   get_importer_namer   !   s    'r   c                  t    t        j                  t        j                  t        j                        ddd      S )N
templating	templatesnextflow)r   r   dirnamedxpy__file__r
   r   r   get_template_dirr"   %   s$    99T\\$--0,ZXXr   c                    t        j                  t        j                  t        j                        d      }t        j                  |d      }	 t        |d      5 }t        j                  |      |    }t        j                  |i        d d d        |S # 1 sw Y   S xY w# t        $ rU t        j                  |d      }t        |d      5 }t        j                  |      |    }d d d        Y |S # 1 sw Y   Y S xY ww xY w)Nr   z app_asset_projects_ids_prod.jsonrfieldsz#app_asset_projects_ids_staging.json
r   r   r   r    r!   openjsonloaddescriber   )regionnextflow_basepathprojects_path
projects_fprojects        r   get_project_with_assetsr1   )   s    		$,,t}}"=zJII/1STM4-% 	.ii
+F3GMM'"-	. N	. N  4		"35Z[-% 	4ii
+F3G	4 	4 N	4 	4 N4sH   B" 0BB" BB" B" "+D C1&D 1C<	6D <D c                      	 t        dd      5 } t        j                  |       }|j                  d      t	               k(  cd d d        S # 1 sw Y   y xY w# t
        $ r Y yw xY w)Nz /home/dnanexus/dnanexus-job.jsonr$   executableNameF)r(   r)   r*   getr   	Exception)fjob_infos     r   is_importer_jobr8   9   sc    4c: 	Iayy|H<< 015F5HH	I 	I 	I  s-   A 1A		A 	AA A 	A! A!c                 h   dj                  | t                     }	 t        t        j                  t        j
                  |                   t        |d      5 }|j                  |       d d d        y # t        $ r(}|j                  t        j                  k7  r Y d }~Sd }~ww xY w# 1 sw Y   y xY w)Nz{}/{}w)formatr   r   r   r   r   OSErrorerrnoEEXISTr(   write)foldercontent	exec_fileefhs        r   
write_execrE   B   s    v';'=>Idll9567
 
i	 
 	  77ell" s#   1A4 B(4	B%=B  B%(B1c           	      V   t        j                  dt         j                        }t        |       D cg c].  }t	        j
                  t	        j                  | |            s-|0 }}|D cg c]  }|j                  |      s| }}|j                          |r|d   S dS c c}w c c}w )z
    Returns first readme (in alphabetical order) from a root of a given folder
    :param dir: Directory in which we search for readme files
    :type dir: str or Path
    :returns: List[str]
    zHreadme(\.(txt|md|rst|adoc|html|txt|asciidoc|org|text|textile|pod|wiki))?r   N)	recompile
IGNORECASEr   r   isfiler   matchsort)dirreadme_patternr6   	file_listfilereadme_filess         r   find_readmerR   N   s     ZZ kmomzmz{N#CLKqDKK		#q8I,JKIK%.MT.2F2Ft2LDMLM*<?44 LMs   .B!!B!+B&B&c                     t        |       }|rCt        j                  | |      }t        j                  |d      }t        j                  ||       yy)z
    :param destination_dir: Directory where readme is going to be created
    :type destination_dir: str or Path
    :param source_dir: Directory from which readme is going to be copied
    :type source_dir: str or Path
    :returns: None
    z	Readme.mdN)rR   r   r   shutilcopy2)
source_dirdestination_dirreadme_filesource_pathdestination_paths        r   create_readmer[   \   sF     j)Kii
K899_kB["23 r   c                     dj                  |       }t        |d      5 }t        j                  ||       d d d        y # 1 sw Y   y xY w)Nz{}/dxapp.jsonr:   )r;   r(   r)   dump)r@   rA   
dxapp_filedxapps       r   write_dxappr`   l   sA     ''/J	j#	 "%		'5!" " "s	   >Ac           	          t        |       \  }}}t        |       }|rt        |||      }	t        |	      }
ni }
t	        |       }| dd|iidd||diidd||diidd||diig|
di}|S )N*instanceTypeidz$dnanexus_link)rd   r0   )systemRequirementsassetDependsbundledDepends)get_nextflow_assetsget_instance_typer   r   r1   )r,   r   profilecache_dockernextflow_pipeline_paramsnextaur_assetnextflow_assetawscli_assetregional_instance_type
image_refsimage_bundledproject_with_assetsregional_optionss                r   get_regional_optionsru   r   s    2Ef2M/M><.v6(AYZ
,Z81&9"$:# ('2+   ((2+   (&2+   ,)
0 r   c                    t        j                  t        j                  t        j                        dd      }	 t        |d      5 }t        j                  |      }d d d        j                  |       }|s.t        j                  j                  dj                  |             |S # 1 sw Y   LxY w# t        $ r$ t        j                  j                  d| d      w xY w)Nr   z$default_nextflow_instance_types.jsonr$   z!Instance types file not found at .z-Instance type is not specified for region {}.)r   r   r   r    r!   r(   r)   r*   FileNotFoundError
exceptionsr   r4   r;   )r,   json_file_pathr6   instance_type_mappinginstance_types        r   ri   ri      s    YYt||DMM:JHnoNf.#& 	1!$(IIaL!	1
 *--f5Moo../^/e/efl/mnn	1 	1 foo..1RSaRbbc/deefs#   B3 B'B3 'B0,B3 3-C c           	         t        j                  t        j                  t        j                        d      }t        j                  |d      }t        j                  |d      }t        j                  |d      }	 t        |d      5 }t        |d      5 }t        |d      5 }t        j                  |      |    }t        j                  |      |    }	t        j                  |      |    }
d d d        d d d        d d d        t        j                  	i        |	
fS # 1 sw Y   5xY w# 1 sw Y   9xY w# 1 sw Y   =xY w# t        $ r t        j                  |d      }t        j                  |d      }t        j                  |d	      }t        |d      5 }t        |d      5 }t        |d      5 }t        j                  |      |    t        j                  |      |    t        j                  |      |    fcd d d        cd d d        cd d d        cY S # 1 sw Y   nxY wd d d        n# 1 sw Y   nxY wd d d        Y y # 1 sw Y   Y y xY ww xY w)
Nr   znextaur_assets.jsonznextflow_assets.jsonzawscli_assets.jsonr$   r%   znextaur_assets.staging.jsonznextflow_assets.staging.jsonzawscli_assets.staging.jsonr'   )r,   r-   nextaur_assetsnextflow_assetsawscli_assets	nextaur_f
nextflow_fawscli_fnextaurr   awsclis              r   rh   rh      s   		$,,t}}"=zJYY02GHNii 13IJOII/1EFMl.#& 	1)T/35O 	1S]_cdqsv_w 	1  |Dii	*62Gyy,V4HYYx(0F	1 	1 	1 	hr*&((	1 	1 	1 	1 	1 	1  l#46ST))$57UV		"35QR.#& 	l)T/35O 	lS]_cdqsv_w 	l  |D99Y'/:1Fv1NPTPYPYZbPcdjPkk	l 	l 	l 	l 	l 	l 	l 	l 	l 	l 	lls   ;E D6D*!A	D*D*2D6:#E D'#D**D3	/D66D?;E AII'H.4AH	;	H.	I
IH"H.%	I.H73I:II		IIc                 b    |D ])  }t        | t              s y| j                  |      } | ) y | S )a  
    :param args: extra args from command input
    :type args: dict
    :param arg_path: list of a dxapp.json location of an allowed extra_arg (eg. ["runSpec", "timeoutPolicy"])
    :type arg_path: tuple/list
    :returns: nested arg value if it exists in args, otherwise None
    N)
isinstancedictr4   )argsarg_pathkeys      r   
get_nestedr      s<      $%xx}< Kr   c                  <    dgdfdgdfdgdfddgdfddgdfdd	gd
fgS )z
    :returns: tuple (arg_path, target_key)
        arg_path is a list of a dxapp.json location of an allowed extra_arg, target_key is name of an argument for a remote build
    nametitlesummaryrunSpectimeoutPolicytimeout_policyreleasedetailswhatsNew	whats_newr
   r
   r   r    get_allowed_extra_fields_mappingr      sT     6
G
i 
_	%'78
Y	+
Z	 +. r   )osr   r   r   rG   r=   r    r)   rT   loggingdxpy.exceptionsr   dxpy.nextflow.collect_imagesr   r   r   r   r   r   r"   r1   r8   rE   rR   r[   r`   ru   ri   rh   r   r   r
   r   r   <module>r      sw    & & 	      , R	6O(Y 	54 ""Jl,"r   