o
    yAfb                     @  s   d dl mZ d dlZd dlmZ d dlmZmZmZm	Z	 d dl
Z
d dlmZmZmZ d dlmZmZmZ d dlmZ d dlmZ erHd d	lmZ G d
d dZdddddZe	dedZdS )    )annotationsN)contextmanager)TYPE_CHECKINGAny	GeneratorTypeVar)DEFAULT_BUILD_DIRECTORYEXCLUDED_DIRECTORIESBuildEnvVars)normalize_inclusion_mapnormalize_relative_directorynormalize_relative_path)normalize_project_name)locate_file)BuilderInterfacec                   @  s4  e Zd ZdtddZeduddZedvddZedvddZedwddZedwddZ	ddddxddZ
dyd d!Zdyd"d#Zdyd$d%Zdyd&d'Zdyd(d)Zdzd+d,Zed{d.d/Zed{d0d1Zed{d2d3Zedwd4d5Zedvd6d7Zed|d8d9Zed|d:d;Zed|d<d=Zed}d?d@Zed|dAdBZed|dCdDZed}dEdFZed|dGdHZed}dIdJZed}dKdLZed~dNdOZ ed}dPdQZ!ed~dRdSZ"ed~dTdUZ#ddVdWZ$eddYdZZ%d}d[d\Z&dd^d_Z'ddadbZ(ddcddZ)ddedfZ*ddgdhZ+d}didjZ,ddkdlZ-d~dmdnZ.e/ddqdrZ0dsS )BuilderConfigbuilderr   rootstrplugin_namebuild_configdict[str, Any]target_configreturnNonec                 C  s   || _ || _|| _|| _|| _d | _d | _d | _d | _d | _	d | _
d | _d | _d | _d | _d | _d | _d | _d | _d| _d | _i | _t | _d | _d | _d | _d | _d | _d | _d | _d | _d | _ d S NF)!_BuilderConfig__builder_BuilderConfig__root_BuilderConfig__plugin_name_BuilderConfig__build_config_BuilderConfig__target_config_BuilderConfig__hook_config_BuilderConfig__versions_BuilderConfig__dependencies_BuilderConfig__sources_BuilderConfig__packages_BuilderConfig__only_include_BuilderConfig__force_include#_BuilderConfig__vcs_exclusion_files_BuilderConfig__include_spec_BuilderConfig__exclude_spec_BuilderConfig__artifact_spec _BuilderConfig__include_patterns _BuilderConfig__exclude_patterns!_BuilderConfig__artifact_patterns_BuilderConfig__exclude_allbuild_artifact_specbuild_force_includesetbuild_reserved_paths_BuilderConfig__directory"_BuilderConfig__skip_excluded_dirs_BuilderConfig__ignore_vcs_BuilderConfig__only_packages_BuilderConfig__reproducible_BuilderConfig__dev_mode_dirs_BuilderConfig__dev_mode_exact,_BuilderConfig__require_runtime_dependencies(_BuilderConfig__require_runtime_features)selfr   r   r   r   r    r>   >/usr/lib/python3.10/site-packages/hatchling/builders/config.py__init__   s@   
zBuilderConfig.__init__c                 C     | j S N)r   r=   r>   r>   r?   r   G      zBuilderConfig.builderc                 C  rA   rB   )r   rC   r>   r>   r?   r   K   rD   zBuilderConfig.rootc                 C  rA   rB   )r   rC   r>   r>   r?   r   O   rD   zBuilderConfig.plugin_namec                 C  rA   rB   )r   rC   r>   r>   r?   r   S   rD   zBuilderConfig.build_configc                 C  rA   rB   )r    rC   r>   r>   r?   r   W   rD   zBuilderConfig.target_configFT)explicit
is_packagerelative_pathrE   boolrF   c                C  s<   |  |p| |p| jo|  o| | o|p| |S rB   )path_is_build_artifactpath_is_artifactonly_packagespath_is_excludedpath_is_included)r=   rG   rE   rF   r>   r>   r?   include_path[   s   

zBuilderConfig.include_pathc                 C     | j d u rdS | j |S NT)include_spec
match_filer=   rG   r>   r>   r?   rM   f      
zBuilderConfig.path_is_includedc                 C  s$   | j rdS | jd u rdS | j|S )NTF)r/   exclude_specrR   rS   r>   r>   r?   rL   l   s
   
zBuilderConfig.path_is_excludedc                 C  rO   r   )artifact_specrR   rS   r>   r>   r?   rJ   u   rT   zBuilderConfig.path_is_artifactc                 C  rO   r   )r0   rR   rS   r>   r>   r?   rI   {   rT   z$BuilderConfig.path_is_build_artifactc                 C  s
   || j v S rB   )r3   rS   r>   r>   r?   path_is_reserved      
zBuilderConfig.path_is_reservednamec                 C  s:   |t v rdS tj||}| |p| jo| | dS )NT/)r	   ospathjoinrW   skip_excluded_dirsrL   )r=   rY   rG   Zrelative_directoryr>   r>   r?   directory_is_excluded   s   
z#BuilderConfig.directory_is_excludedpathspec.GitIgnoreSpec | Nonec                 C  s   | j d u rzd| jv r| j}d| j d}n| j}d}g }|d|  }t|ts3d| d}t|t	|dD ]*\}}t|t
sNd| d	| d
}t||s]d| d	| d}t||| q8|dd | jD  |rwtj|| _|| _ | jS )Nincludetool.hatch.build.targets.z.includeztool.hatch.build.includeField `` must be an array of strings   	Pattern # in field `` must be a string` cannot be an empty stringc                 s  s&    | ]}d | tjd  d V  qdS )rZ   N)replacer[   sep).0rG   r>   r>   r?   	<genexpr>   s   $ z-BuilderConfig.include_spec.<locals>.<genexpr>)r,   r   r   r   getdefault_include
isinstancelist	TypeError	enumerater   
ValueErrorappendextendpackagespathspecGitIgnoreSpec
from_linesr)   )r=   Zinclude_configZinclude_locationZall_include_patternsZinclude_patternsmessageiZinclude_patternr>   r>   r?   rQ      s0   



zBuilderConfig.include_specc                 C  s   | j d u r{d| jv r| j}d| j d}n| j}d}|  }|d|  }t|ts5d| d}t	|t
|dD ]*\}}t|tsPd| d	| d
}t	||s_d| d	| d}t||| q:| jso||   |rxtj|| _|| _ | jS )Nexcluderb   z.excludeztool.hatch.build.excluderc   rd   re   rf   rg   rh   ri   )r-   r   r   r   default_global_excludern   default_excluderp   rq   rr   rs   r   rt   ru   
ignore_vcsrv   load_vcs_exclusion_patternsrx   ry   rz   r*   )r=   Zexclude_configZexclude_locationZall_exclude_patternsZexclude_patternsr{   r|   Zexclude_patternr>   r>   r?   rU      s2   



zBuilderConfig.exclude_specc                 C  s   | j d u rmd| jv r| j}d| j d}n| j}d}g }|dg }t|ts1d| d}t|t|dD ]*\}}t|t	sLd| d	| d
}t||s[d| d	| d}t
||| q6|rjtj|| _|| _ | jS )N	artifactsrb   z
.artifactsztool.hatch.build.artifactsrc   rd   re   rf   rg   rh   ri   )r.   r   r   r   rn   rp   rq   rr   rs   r   rt   ru   rx   ry   rz   r+   )r=   Zartifact_configZartifact_locationZall_artifact_patternsZartifact_patternsr{   r|   Zartifact_patternr>   r>   r?   rV      s.   



zBuilderConfig.artifact_specc                   s  | j d u ri }| jdi }t|tsd}t|| D ]\}}t|ts0d| d}t|||| q| jdi }t|tsNd| j	 d}t|| D ]\}}t|tsid| j	 d| d}t||||< qRt
tjst
tj  fdd	| D }ni }|| _ | j S )
Nhooksz.Field `tool.hatch.build.hooks` must be a tablezField `tool.hatch.build.hooks.z` must be a table Field `tool.hatch.build.targets.z.hooks` must be a tablez.hooks.c                   s<   i | ]\}} s| d dsttj |  r||qS )zenable-by-defaultT)rn   env_var_enabledr
   ZHOOK_ENABLE_PREFIXupper)rl   	hook_nameconfigZall_hooks_enabledr>   r?   
<dictcomp>  s    
z-BuilderConfig.hook_config.<locals>.<dictcomp>)r!   r   rn   rp   dictrr   items
setdefaultr   r   r   r
   ZNO_HOOKSZHOOKS_ENABLE)r=   hook_configZglobal_hook_configr{   r   r   Ztarget_hook_configZfinal_hook_configr>   r   r?   r      s8   









zBuilderConfig.hook_configc                 C  sv   | j d u r8d| jv r | jd }t|tsd| j d}t|n| jdt}t|ts2d}t|| 	|| _ | j S )N	directoryr   z.directory` must be a stringz3Field `tool.hatch.build.directory` must be a string)
r4   r   rp   r   r   rr   r   rn   r   normalize_build_directory)r=   r   r{   r>   r>   r?   r   .  s   




zBuilderConfig.directoryc                 C  p   | j d u r5d| jv r | jd }t|tsd| j d}t|n| jdd}t|ts2d}t||| _ | j S )Nzskip-excluded-dirsr   z&.skip-excluded-dirs` must be a booleanFz=Field `tool.hatch.build.skip-excluded-dirs` must be a boolean)r5   r   rp   rH   r   rr   r   rn   )r=   r^   r{   r>   r>   r?   r^   @  s   




z BuilderConfig.skip_excluded_dirsc                 C  r   )Nz
ignore-vcsr   z.ignore-vcs` must be a booleanFz5Field `tool.hatch.build.ignore-vcs` must be a boolean)r6   r   rp   rH   r   rr   r   rn   )r=   r   r{   r>   r>   r?   r   T     




zBuilderConfig.ignore_vcsc                 C  r   )Nrequire-runtime-dependenciesr   z0.require-runtime-dependencies` must be a booleanFzGField `tool.hatch.build.require-runtime-dependencies` must be a boolean)r;   r   rp   rH   r   rr   r   rn   )r=   require_runtime_dependenciesr{   r>   r>   r?   r   f  s   




z*BuilderConfig.require_runtime_dependencies	list[str]c           	      C  s  | j d u r~d| jv r| j}d| j d}n| j}d}|dg }t|ts/d| d}t|i }t|dD ]B\}}t|t	sLd| d	| d
}t||s[d| d	| d}t
|t|}|| jjjjvrtd| d| d}t
|d ||< q6t|| _ | j S )Nrequire-runtime-featuresrb   z.require-runtime-featuresz)tool.hatch.build.require-runtime-featuresrc   ` must be an arrayre   	Feature #z of field `rh   ri   	Feature `z` of field `9` is not defined in field `project.optional-dependencies`)r<   r   r   r   rn   rp   rq   rr   rs   r   rt   r   r   metadatacoreoptional_dependencies)	r=   Zfeatures_configZfeatures_locationrequire_runtime_featuresr{   Zall_featuresr|   raw_featurefeaturer>   r>   r?   r   {  s4   





z&BuilderConfig.require_runtime_featuresc                 C  p   | j du r5d| jv r | jd }t|tsd| j d}t|n| jdd}t|ts2d}t||| _ | j S )zx
        Whether or not the target should ignore non-artifact files that do not reside within a Python package.
        Nzonly-packagesr   z!.only-packages` must be a booleanFz8Field `tool.hatch.build.only-packages` must be a boolean)r7   r   rp   rH   r   rr   r   rn   )r=   rK   r{   r>   r>   r?   rK        




zBuilderConfig.only_packagesc                 C  r   )zi
        Whether or not the target should be built in a reproducible manner, defaulting to true.
        Nreproducibler   z .reproducible` must be a booleanTz7Field `tool.hatch.build.reproducible` must be a boolean)r8   r   rp   rH   r   rr   r   rn   )r=   r   r{   r>   r>   r?   r     r   zBuilderConfig.reproduciblec                 C  s   | j du rdd| jv r| j}d| j d}n| j}d}g }|dg }t|ts1d| d}t|t|dD ]*\}}t|t	sLd	| d
| d}t||s[d	| d
| d}t
||| q6|| _ | j S )z
        Directories which must be added to Python's search path in
        [dev mode](../config/environment/overview.md#dev-mode).
        Nzdev-mode-dirsrb   z.dev-mode-dirsztool.hatch.build.dev-mode-dirsrc   rd   re   zDirectory #rg   rh   ri   )r9   r   r   r   rn   rp   rq   rr   rs   r   rt   ru   )r=   Zdev_mode_dirs_configZdev_mode_dirs_locationZall_dev_mode_dirsdev_mode_dirsr{   r|   Zdev_mode_dirr>   r>   r?   r     s*   



zBuilderConfig.dev_mode_dirsc                 C  r   )Nzdev-mode-exactr   z".dev-mode-exact` must be a booleanFz9Field `tool.hatch.build.dev-mode-exact` must be a boolean)r:   r   rp   rH   r   rr   r   rn   )r=   dev_mode_exactr{   r>   r>   r?   r     r   zBuilderConfig.dev_mode_exactc              	   C  s  | j d u ri }| jdg }t|tsd| j d}t|t|dD ]+\}}t|ts:d| d| j d}t||sJd| d| j d}t	|d ||< q#|s`| j
 }|D ]}d ||< qXn"t|t| j
  }|rd	| j d
dttt| }t	|t|| _ | j S )Nversionsr   z&.versions` must be an array of stringsre   z	Version #z$ in field `tool.hatch.build.targets.z.versions` must be a stringz$.versions` cannot be an empty stringz4Unknown versions in field `tool.hatch.build.targets.z.versions`: z, )r"   r   rn   rp   rq   r   rr   rs   r   rt   r   Zget_default_versionsr2   Zget_version_apir]   mapsorted)r=   Zall_versionsr   r{   r|   versionZdefault_versionsZunknown_versionsr>   r>   r?   r     s<   







zBuilderConfig.versionsc                 C  s  | j d u rai }| jdg }t|tsd| j d}t|t|dD ]\}}t|ts;d| d| j d}t|d ||< q$| j	dg }t|tsRd}t|t|dD ]\}}t|tsjd| d	}t|d ||< qW| j
}d
d | jD }| j D ]\}	}
|
dd}t|tsd|	 d}t||rd}|
dg }t|tsd|	 d}t|t|dD ]B\}}t|tsd| d|	 d}t||sd| d|	 d}t|t|}|| jjjjvrd| d|	 d}t|d ||< q|
dg }t|tsd|	 d}t|t|dD ]\}}t|ts+d| d|	 d}t|d ||< qq|rC| jjjjD ]}d ||< q;|r\|D ]}| jjjj| D ]}d ||< qRqHt|| _ | j S )Ndependenciesr   z.dependencies` must be an arrayre   zDependency #z$ of field `tool.hatch.build.targets.z.dependencies` must be a stringz6Field `tool.hatch.build.dependencies` must be an arrayz: of field `tool.hatch.build.dependencies` must be a stringc                 S  s   i | ]}|d qS rB   r>   )rl   r   r>   r>   r?   r   P  s    z.BuilderConfig.dependencies.<locals>.<dictcomp>r   Fz5Option `require-runtime-dependencies` of build hook `z` must be a booleanTr   z1Option `require-runtime-features` of build hook `r   r   z5 of option `require-runtime-features` of build hook `rh   ri   r   z6` of option `require-runtime-features` of build hook `r   z%Option `dependencies` of build hook `z) of option `dependencies` of build hook `)r#   r   rn   rp   rq   r   rr   rs   r   r   r   r   r   r   rH   rt   r   r   r   r   r   r   )r=   r   Ztarget_dependenciesr{   r|   
dependencyZglobal_dependenciesr   r   r   r   Z!hook_require_runtime_dependenciesZhook_require_runtime_featuresr   r   Zhook_dependenciesr>   r>   r?   r   .  s   









	
zBuilderConfig.dependenciesdict[str, str]c                 C  s  | j d u rd| jv r| j}d| j d}n| j}d}i }|dg }t|trYt|dD ]+\}}t|tsBd| d| d}t	||sQd| d| d	}t
|d
|t|< q,nEt|tr| D ]0\}}t|tsxd| d| d}t	|t|}	|	dkrd
}	n|	tj7 }	|	||rt|n|< qbn
d| d}t	|| jD ]}
tj|
\}}|rt|
|vrd
|t|< qtt| | _ | j S )Nsourcesrb   z.sourcesztool.hatch.build.sourcesre   Source #rg   rh   ri    Path for source `` in field `.rc   z'` must be a mapping or array of strings)r$   r   r   r   rn   rp   rq   rs   r   rr   rt   r   r   r   r   r[   rk   rw   r\   splitr   )r=   Zsources_configZsources_locationr   Zraw_sourcesr|   sourcer{   r\   normalized_pathrG   Z_packager>   r>   r?   r     sL   








zBuilderConfig.sourcesc                 C  s   | j d u rfd| jv r| j}d| j d}n| j}d}|d|  }t|ts1d| d}t|t	|dD ]%\}}t|t
sLd| d	| d
}t||s[d| d	| d}t|q6tdd |D | _ | j S )Nrw   rb   z	.packagesztool.hatch.build.packagesrc   rd   re   z	Package #rg   rh   ri   c                 s  s    | ]}t |V  qd S rB   )r   )rl   packager>   r>   r?   rm     s    z)BuilderConfig.packages.<locals>.<genexpr>)r%   r   r   r   rn   default_packagesrp   rq   rr   rs   r   rt   r   )r=   Zpackage_configZpackage_locationrw   r{   r|   r   r>   r>   r?   rw     s(   



zBuilderConfig.packagesc                 C  s   | j d u rtd| jv r| j}d| j d}n| j}d}|di }t|ts/d| d}t|t|	 dD ]6\}\}}|sKd| d	| d
}t
|t|ts]d| d| d}t||sld| d| d
}t
|q6t|| j| _ | j S )Nzforce-includerb   z.force-includeztool.hatch.build.force-includerc   z` must be a mappingre   r   rg   ri   r   r   rh   )r'   r   r   r   rn   rp   r   rr   rs   r   rt   r   r   r   )r=   Zforce_include_configZforce_include_locationforce_includer{   r|   r   rG   r>   r>   r?   r     s0   



zBuilderConfig.force_includec           	      C  s  | j d u rd| jv r| j}d| j d}n| j}d}|d|  p$| j}t|ts4d| d}t	|i }t
|dD ]D\}}t|tsQd| d	| d
}t	|t|}|r\|drkd| d	| d| }t|||v r{d| d| }t||||< q;t|| j| _ | j S )Nzonly-includerb   z.only-includeztool.hatch.build.only-includerc   r   re   zPath #rg   rh   )~z..z` must be relative: zDuplicate path in field `z`: )r&   r   r   r   rn   default_only_includerw   rp   rq   rr   rs   r   r   
startswithrt   r   r   )	r=   Zonly_include_configZonly_include_locationonly_includer{   inclusion_mapr|   rG   r   r>   r>   r?   r     s2   




zBuilderConfig.only_includec                 C  sD   | j  D ]\}}|s||   S ||r|||d  S q|S )Nre   )r   r   r   rj   )r=   rG   r   replacementr>   r>   r?   get_distribution_path(  s   
z#BuilderConfig.get_distribution_pathdict[str, list[str]]c                 C  sd   | j d u r/g g d}t| jd}|d ur|d | t| jd}|d ur,|d | || _ | j S )N)githgz
.gitignorer   z	.hgignorer   )r(   r   r   ru   )r=   Zexclusion_filesZlocal_gitignoreZlocal_hgignorer>   r>   r?   vcs_exclusion_files3  s   

z!BuilderConfig.vcs_exclusion_filesc              	   C  s   g }| j d D ]}t|dd}||  W d    n1 s!w   Y  q| j d D ]9}t|dd)}d}|D ]}| }|dkrFd}q9|drNd}q9|rU|| q9W d    n1 s`w   Y  q,|S )	Nr   zutf-8)encodingr   Fzsyntax: globTzsyntax: )r   openrv   	readlinesstripr   ru   )r=   patternsZexclusion_filefZ	glob_modelineZ
exact_liner>   r>   r?   r   D  s0   

z)BuilderConfig.load_vcs_exclusion_patternsbuild_directoryc                 C  s(   t j|st j| j|}t j|S rB   )r[   r\   isabsr]   r   normpath)r=   r   r>   r>   r?   r   _  s   z'BuilderConfig.normalize_build_directoryrq   c                 C     g S rB   r>   rC   r>   r>   r?   ro   e     zBuilderConfig.default_includec                 C  r   rB   r>   rC   r>   r>   r?   r   h  r   zBuilderConfig.default_excludec                 C  r   rB   r>   rC   r>   r>   r?   r   k  r   zBuilderConfig.default_packagesc                 C  r   rB   r>   rC   r>   r>   r?   r   n  r   z"BuilderConfig.default_only_includec                 C  s   ddt  g}|  |S )Nz	*.py[cdo]rZ   )r   sort)r=   r   r>   r>   r?   r~   q  s   z$BuilderConfig.default_global_excludec                 C  s
   d| _ d S rP   )r/   rC   r>   r>   r?   set_exclude_allv  rX   zBuilderConfig.set_exclude_allc                 C  s   | j  }|| j |S rB   )r   copyupdater1   )r=   r   r>   r>   r?   get_force_includey  s   
zBuilderConfig.get_force_include
build_datar   c              
   c  s    zh|d }|rt j|| _| jt|d | j | j| jfD ]5}|	 D ].\}}|
| j tj rF| j| tj|| j q'tj|rU| j| | q'q!d V  W d | _| j  | j  d S d | _| j  | j  w )Nr   r   )rx   ry   rz   r0   r1   r   r   r   r   r   r   r[   rk   r3   addr   r\   relpathisfileclear)r=   r   Zbuild_artifactsr   r   targetr>   r>   r?   set_build_data~  s*    

zBuilderConfig.set_build_dataN)r   r   r   r   r   r   r   r   r   r   r   r   )r   r   )r   r   )r   r   )rG   r   rE   rH   rF   rH   r   rH   )rG   r   r   rH   )rY   r   rG   r   r   rH   )r   r`   )r   rH   )r   r   )r   r   )rG   r   r   r   )r   r   )r   r   r   r   )r   rq   )r   r   )r   r   r   r   )1__name__
__module____qualname__r@   propertyr   r   r   r   r   rN   rM   rL   rJ   rI   rW   r_   rQ   rU   rV   r   r   r^   r   r   r   rK   r   r   r   r   r   r   rw   r   r   r   r   r   r   ro   r   r   r   r~   r   r   r   r   r>   r>   r>   r?   r      s    
4


	


&%".&#+d1!
$








r   F)defaultenv_varr   r   rH   r   c                C  s   | t jv rt j|  dv S |S )N>   true1)r[   environ)r   r   r>   r>   r?   r     s   
r   BuilderConfigBound)bound)r   r   r   rH   r   rH   )
__future__r   r[   
contextlibr   typingr   r   r   r   rx   Zhatchling.builders.constantsr   r	   r
   Zhatchling.builders.utilsr   r   r   Zhatchling.metadata.utilsr   Zhatchling.utils.fsr   Z#hatchling.builders.plugin.interfacer   r   r   r   r>   r>   r>   r?   <module>   s*           