a
    ˆÏ?e¿  ã                   @   sÒ  d dl Z d dlZd dlmZ d dlmZ d dlmZ d dlmZ d dlmZ d dlm	Z	 d dlm
Z
 d d	lmZ d d
lmZ d dlZddlmZ d dlmZ d dlmZ d dlmZ d dlmZ erÐddlmZ eejdœdd„Zeeeeeeee f f  dœdd„Zee e
ee ee eeeeee f f f dœdd„Zee edœdd„Zee ee dœdd„Zd Zd(ee e	e ee ed! e
eee eeeeee f f f d"œd#d$„Z ee!d%œd&d'„Z"dS ))é    N)ÚPath)ÚDict)ÚIterable)ÚList)ÚOptional)ÚSequence)ÚTuple)ÚTYPE_CHECKING)ÚUnioné   )Ú
UsageError)Úfail©Úabsolutepath)Ú
commonpath©Úsafe_exists)ÚConfig©ÚpathÚreturnc              
   C   sJ   zt  t| ƒ¡W S  t jyD } ztt|ƒƒ|‚W Y d}~n
d}~0 0 dS )zœParse the given generic '.ini' file using legacy IniConfig parser, returning
    the parsed object.

    Raise UsageError if the file cannot be parsed.
    N)Ú	iniconfigÚ	IniConfigÚstrÚ
ParseErrorr   )r   Úexc© r   ú[/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/_pytest/config/findpaths.pyÚ_parse_ini_config   s    r   )Úfilepathr   c              
      sd  | j dkr<t| ƒ}d|v r*t|d  ¡ ƒS | jdkr8i S �n$| j dkrˆt| ƒ}d|jv rht|d  ¡ ƒS d|jv r†ttjdd�dd	� nØ| j d
k�r`t	j
dkr¨ddl}nddl}| jdd�}z| |¡}W n< |j�y } z t| › d|› �ƒ|‚W Y d}~n
d}~0 0 | di ¡ di ¡ dd¡}|du�r`ttttt f dœdd„‰ ‡ fdd„| ¡ D ƒS dS )z�Load pytest configuration from the given file path, if supported.

    Return None if the file does not contain valid pytest configuration.
    z.iniZpytestú
pytest.iniz.cfgztool:pytestú	setup.cfg)ÚfilenameF)Zpytracez.toml)é   é   r   Nzutf-8)Úencodingz: ZtoolZini_options)Úvr   c                 S   s   t | tƒr| S t| ƒS ©N)Ú
isinstanceÚlistr   )r&   r   r   r   Úmake_scalarU   s    z/load_config_dict_from_file.<locals>.make_scalarc                    s   i | ]\}}|ˆ |ƒ“qS r   r   )Ú.0Úkr&   ©r*   r   r   Ú
<dictcomp>X   ó    z.load_config_dict_from_file.<locals>.<dictcomp>)Úsuffixr   ÚdictÚitemsÚnameÚsectionsr   ÚCFG_PYTEST_SECTIONÚformatÚsysÚversion_infoÚtomllibZtomliÚ	read_textÚloadsZTOMLDecodeErrorr   ÚgetÚobjectr
   r   r   )r   r   r9   Z	toml_textÚconfigr   Úresultr   r-   r   Úload_config_dict_from_file%   s4    	






*
r@   )Úargsr   c                 C   s”   g d¢}dd„ | D ƒ} | s$t  ¡ g} | D ]`}t|ƒ}|g|j¢R D ]D}|D ]:}|| }| ¡ rJt|ƒ}|durJ|||f      S qJqBq(ddi fS )zxSearch in the list of arguments for a valid ini-file for pytest,
    and return a tuple of (rootdir, inifile, cfg-dict).)r    z.pytest.inizpyproject.tomlztox.inir!   c                 S   s   g | ]}t |ƒ d ¡s|‘qS )ú-)r   Ú
startswith)r+   Úxr   r   r   Ú
<listcomp>i   r/   z!locate_config.<locals>.<listcomp>N)r   Úcwdr   ÚparentsÚis_filer@   )rA   Zconfig_namesÚargZargpathÚbaseZconfig_nameÚpZ
ini_configr   r   r   Úlocate_config]   s    
rL   )Úpathsr   c                 C   s†   d }| D ]X}|  ¡ sq|d u r$|}q||jv s||kr:qq||jv rJ|}qt||ƒ}|d ur|}q|d u rtt ¡ }n| ¡ r‚|j}|S r'   )ÚexistsrG   r   r   rF   rH   Úparent)rM   Zcommon_ancestorr   Zsharedr   r   r   Úget_common_ancestorx   s$    


rP   c                    sV   t tdœdd„‰t t dœdd„‰ttdœdd„‰ ‡‡fd	d
„| D ƒ}‡ fdd„|D ƒS )N)rD   r   c                 S   s
   |   d¡S )NrB   )rC   ©rD   r   r   r   Ú	is_option�   s    z%get_dirs_from_args.<locals>.is_optionc                 S   s   |   d¡d S )Nz::r   )ÚsplitrQ   r   r   r   Úget_file_part_from_node_id“   s    z6get_dirs_from_args.<locals>.get_file_part_from_node_idr   c                 S   s   |   ¡ r| S | jS r'   )Úis_dirrO   )r   r   r   r   Úget_dir_from_path–   s    z-get_dirs_from_args.<locals>.get_dir_from_pathc                 3   s"   | ]}ˆ|ƒst ˆ |ƒƒV  qd S r'   r   )r+   rI   )rT   rR   r   r   Ú	<genexpr>œ   s   þz%get_dirs_from_args.<locals>.<genexpr>c                    s   g | ]}t |ƒrˆ |ƒ‘qS r   r   )r+   r   )rV   r   r   rE   ¢   r/   z&get_dirs_from_args.<locals>.<listcomp>)r   Úboolr   )rA   Zpossible_pathsr   )rV   rT   rR   r   Úget_dirs_from_args�   s    þrY   z][pytest] section in {filename} files is no longer supported, change to [tool:pytest] instead.r   )ÚinifilerA   Úrootdir_cmd_argr>   r   c                 C   s&  d }t |ƒ}| r8t| ƒ}|}t|ƒp&i }|d u rÚ|j}n¢t|ƒ}	t|	gƒ\}}}|d u rÚ|d u rÚ|	g|	j¢R D ]}
|
d  ¡ rn|
} qÚqn||	gkr t|ƒ\}}}|d u rÚ|d urº|jj	}nt
 ¡ }t||	gƒ}t|ƒrÚ|	}|�rttj |¡ƒ}| ¡ �std |¡ƒ‚|d u�sJ ‚|||�p"i fS )Nzsetup.pyz8Directory '{}' not found. Check your '--rootdir' option.)rY   r   r@   rO   rP   rL   rG   rH   Zinvocation_paramsÚdirr   rF   Ú
is_fs_rootÚosr   Ú
expandvarsrU   r   r6   )rZ   rA   r[   r>   ÚrootdirÚdirsZinipath_ZinipathZinicfgZancestorZpossible_rootdirrF   r   r   r   Údetermine_setup¨   sD    


ÿÿrb   )rK   r   c                 C   s   t j t| ƒ¡d t jkS )z‡
    Return True if the given path is pointing to the root of the
    file system ("/" on Unix and "C:\\" on Windows for example).
    r   )r^   r   Ú
splitdriver   Úsep)rK   r   r   r   r]   Õ   s    r]   )NN)#r^   r7   Úpathlibr   Útypingr   r   r   r   r   r   r	   r
   r   Ú
exceptionsr   Z_pytest.outcomesr   Z_pytest.pathlibr   r   r   Ú r   r   r   r   r@   rL   rP   rY   r5   rb   rX   r]   r   r   r   r   Ú<module>   sL   þ9(þ  ü$û-