
    5i,                         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	m
Z
mZ ddlmZ ddlZ ej                  d      Z ej                  d      Zd	 Zd
 Zd Zd Zd Zd Zd Zd Zd Zy)z
Executable Builder
+++++++++++++++++++

Contains utility methods useful for deploying executables (apps, applets, workflows)
onto the platform.

    )print_functionunicode_literalsdivisionabsolute_importN   )resolve_pathis_container_id)try_callz^[a-zA-Z0-9._\-]+$zz^([1-9][0-9]*|0)\.([1-9][0-9]*|0)\.([1-9][0-9]*|0)(-[-0-9A-Za-z]+(\.[-0-9A-Za-z]+)*)?(\+[-0-9A-Za-z]+(\.[-0-9A-Za-z]+)*)?$c                 B    t        |       r| ddfS t        t        |       S )z
    Parses dest_str, which is (roughly) of the form
    PROJECT:/FOLDER/NAME, and returns a tuple (project, folder, name)
    N)r	   r
   r   )dest_strs    y/home/marpiech/ifpan-abm-pgxpred/analysis/marpiech-gwas-test/venv/lib/python3.12/site-packages/dxpy/executable_builder.pyget_parsed_destinationr   )   s(     x $%% L(++    c                    d| vrd}dD ]C  }t         j                  j                  t         j                  j                  ||            sA|} n |Et	        t         j                  j                  ||            5 }|j                         | d<   ddd       d| vrdD ]  }t         j                  j                  t         j                  j                  ||            sAt	        t         j                  j                  ||            5 }|j                         | d<   ddd        y yy# 1 sw Y   xY w# 1 sw Y    yxY w)z
    Modifies the provided json_spec dict (which may be an app, applet,
    workflow spec) to inline the contents of the readme file into
    "description" and the developer readme into "developerNotes".
    descriptionN)z	README.mdz	Readme.mdz	readme.mddeveloperNotes)zREADME.developer.mdzReadme.developer.mdzreadme.developer.md)ospathexistsjoinopenread)	json_specsrc_dirreadme_filenamefilenamefhs        r   inline_documentation_filesr   A   s
    I%= 	Hww~~bggll7H=>"*	 &bggll7O<= 5+-779	-(5 y([ 	Hww~~bggll7H=>"'',,w9: <b24'')I./<		 )	5 5<s   :D'D3'D03D=	c                 p    | D ]"  }	 t         j                  j                  |       $ y# t        $ r Y 1w xY w)z.
    Destroys all projects from the list.
    N)dxpyapiproject_destroy	Exception)projectsprojects     r   delete_temporary_projectsr&   [   s>      	HH$$W-  		s   )	55c                    t        j                         }| s"t         j                  j                  |      d   S d}| j	                  d      r| |k7  rd}nJ| j	                  d      r7	 t         j                  j                  |       }|d   sdj                  |       }nd	}|r ||      | S #  dj                  |       }Y !xY w)
aB  
    Check if the requesting user can perform billable activities on behalf of the billTo
    If not specified, default to the billTo of the requesting user
    otherwise it must be either the ID of the requesting user, 
    or an org of which the requesting user is a member with 'allowBillableActivities' permission
    billToNzuser-z.Cannot request another user to be the "billTo"zorg-allowBillableActivitieszoYou are not a member in {} with allowBillableActivities permission. Please check the billing policy of the org.zmCannot retrieve billing information for {}. Please check your access level and the billing policy of the org.z4The field "billTo" must be a valid ID of a user/org.)r    whoamir!   user_describe
startswithorg_describeformat)bill_toexecutable_builder_exceptionuser_idexception_msgmember_accesss        r   get_valid_bill_tor4   e   s     kkmGxx%%g.x88M'"w''9H			F	#	Z HH11':M !:; P  W  W  X_  ` M*=99N	Z J  Q  Q  RY  ZMs   &5B* *B?c                    | j                  d      s| j                  d      sJ | j                  d      d   dk(  r7t        j                  j                  }t        j
                  j                  }d}n6t        j                  j                  }t        j
                  j                  }d}d}d	}d
}d
}t        j                  dg d      }	  || dddddi      }	|	d   }|	d   }|	d   }|s |d
d
d
      S | j                  d      d   }|s |dj                  |             ||||      S # t        j                  j                  $ r5}
|
j                  dk(  rd	}n|
j                  dk(  r ||      |
Y d
}
~
d
}
~
ww xY w)a  
    Checks if the current user is a developer of the app or global workflow
    with the given name. If the app/global workflow exists and the user has
    developer rights to it, the function returns a named tuple representing
    the executable that was queried.
    zapp-zglobalworkflow--r   appzQAn app with the given name already exists and you are not a developer of that appzaA global workflow with the given name already exists and you are not a developer of that workflowTFNFoundExecutable)nameversionidfields)isDeveloperForr:   r;   )input_paramsr=   r:   r;   ResourceNotFoundPermissionDenied   zYou are not a developer for {n})n)r,   	partitionr    app_builderAppBuilderExceptionr!   app_describeworkflow_builderWorkflowBuilderExceptionglobal_workflow_describecollections
namedtuple
exceptions
DXAPIErrorr9   r.   )prefixed_nameexception_typedescribe_methodr2   name_already_existsis_developerr:   executable_idr8   describe_outputename_without_prefixs               r   verify_developer_rightsrW      s    ##F+}/G/GHY/Z[Zs#A&%/))==((//_ 	 ..GG((;;o 	 LGM!,,->@YZO)-8@UYNRIMCO 8PQ ''78!),'-  D$4@@'11#6q9>EEH[E\]] 3WWW# ??%% 66''"'VV)) //G	  s   
D/ /F+E<<Fc                 L    | |yt        |       t        |      k7  r |d      y)z
    Verifies the regions passed with --region CLI argument and the ones
    specified in regionalOptions are the same (if both CLI and spec were used)
    NzT--region and the 'regionalOptions' key in the JSON file or --extra-args do not agree)set)	from_specfrom_command_liner0   s      r   assert_consistent_regionsr\      s?    
 -5
9~.//*  ,B  C  	C 0r   c           
         |j                  d      }| dk(  rdnd}t        |t              s |d      |s |d|z   dz         t        |j	                               }|D ]  \  }}t        |t              s |d|z   d	z   |z   d
z         t        |j                               t        |d   d   j                               k7  r t        |j                               t        |d   d   j                               z
  rV||d   d   }	}t        t        t        |j                               t        |d   d   j                               z
              }
nU|d   d   |}	}t        t        t        |d   d   j                               t        |j                               z
              }
 |ddj                  |
||	      z         | dk(  sl|D ]$  }||j                  di       v s ||dz   |z           y)z
    Validates the "regionalOptions" field and verifies all the regions used
    in "regionalOptions" have the same options.
    regionalOptionsr7   zdxapp.json or --extra-argszdxworkflow.json or --extra-argsz1The field 'regionalOptions' in  must be a mappingzThe field 'regionalOptions' in z must be a non-empty mappingzThe field 'regionalOptions['z']' in z must be a mappingr   r   z>All regions in regionalOptions must specify the same options; z"{} was given for {} but not for {}runSpecz= cannot be given in both runSpec and in regional options for N)
get
isinstancedictlistitemsrY   keysnextiterr.   )	exec_typer   r0   reg_options_specjson_fnregional_options_listregionopts_for_regionwith_keywithout_keykey_namekeys               r   assert_consistent_reg_optionsrr      s4   
 !}}%67.75.@*FgG&-*+^__*-7:XXZ 	Z !1!7!7!9:#8 d/40./MPV/V%0&(/002F0G H H##%&#.CA.Fq.I.N.N.P*QQ?'')*S1Fq1I!1L1Q1Q1S-TT(.0Ea0H0K+S)=)=)?%@3G\]^G_`aGbGgGgGiCj%j kl(=a(@(CV+S)>q)A!)D)I)I)K%LsSbSgSgSiOj%j kl.P4<<hR]^_ 
 & d)--	2666YY\bbd dd#dr   c                     t               }	 t        t        j                  d| z   dz   i       j                  d            }|S #   |dj	                  |             xY w)zS
    Validates requested bill_to and returns the set of its permitted regions.
    /z	/describepermittedRegionsz%Failed to get permitted regions of {})rY   r    DXHTTPRequestr`   r.   )r/   r0   billable_regionss      r   get_permitted_regionsrx      sg     udd00w1LbQUUVhij d*+R+Y+YZa+bccs   4A Ac                     |j                  d      }|t        | ||       t        |||       d}||j                         }|S ||}|S )a  
    Return a list of regions in which the global executable (app or global workflow)
    will be enabled, based on the "regionalOption" in their JSON specification
    and/or --region CLI argument used with "dx build".

    :param exec_type: 'app' or 'globalworkflow'
    :type json_spec: str.
    :param json_spec: The contents of dxapp.json or dxworkflow.json
    :type json_spec: dict or None.
    :param from_command_line: The regional options specified on the command-line via --region.
    :type from_command_line: list or None.
    :param executable_builder_exception: Exception that will be thrown.
    :type executable_builder_exception: AppBuilderException or WorkflowBuilderException.
    r^   N)r`   rr   r\   re   )rh   r   r[   r0   rZ   enabled_regionss         r   get_enabled_regionsr{      sf      /0I%i<XYi):<XYO#..*  
	&+r   )__doc__
__future__r   r   r   r   r   rerJ   utils.resolverr   r	   clir
   r    compileGLOBAL_EXEC_NAME_REGLOBAL_EXEC_VERSION_REr   r   r&   r4   rW   r\   rr   rx   r{    r   r   <module>r      s   ( S R 	 	  9   bjj!56 #  %a  b ,04:2XjC"dH	r   