o
    5h-                     @   st  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 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dlmZ ejdd Zejd	d
 Zejddddd Zejdd Zejdd Zdd Zejdddd Zejdddd Zejdd Zejdd Zejdd  Zd!d" Z d#d$ Z!d%d& Z"d'd( Z#d)d*e!ddd+fd,d-Z$ejd.d/ Z%dS )0    N)Path)
safer_name   )contextsenvironment)DALSc              
   c   s    t  H}| d| t  %}| d| t   dV  W d   n1 s)w   Y  W d   n1 s8w   Y  W d   dS W d   dS 1 sPw   Y  dS )za
    Override site.USER_BASE and site.USER_SITE with temporary directories in
    a context.
    zsite.USER_BASEzsite.USER_SITEN)r   ZtempdirsetattrZsave_user_site_setting)monkeypatch	user_base	user_site r   9lib/python3.10/site-packages/setuptools/tests/fixtures.pyuser_override   s   


"r   c                 c   s6    |   }|V  W d    d S 1 sw   Y  d S N)Zas_cwd)ZtmpdirZorigr   r   r   
tmpdir_cwd"   s   
"r   TZsession)Zautousescopec                 C   sN   | j jds	dS tt tjd W d   dS 1 s w   Y  dS )z
    Workaround pytest-dev/pytest-xdist#376

    ``pytest-xdist`` tends to inject '' into ``sys.path``,
    which may break certain isolation expectations.
    Remove the entry so the import
    machinery behaves the same irrespective of xdist.
    ZxdistN )	configZpluginmanagerZ
has_plugin
contextlibsuppress
ValueErrorsyspathremove)requestr   r   r   workaround_xdist_376(   s
   
"r   c                 C   sH   g d}zt j|t| d W | d S  ty#   td Y | d S w )z<
    Clone the 'sampleproject' and return a path to it.
    )gitZclonez%https://github.com/pypa/sampleproject)cwdzUnable to clone sampleprojectZsampleproject)
subprocessZ
check_callstr	Exceptionpytestskip)tmp_pathcmdr   r   r   sample_project9   s   r%   c                 c   s8    t |  d V  W d    d S 1 sw   Y  d S r   )r   r   )r%   r   r   r   sample_project_cwdF   s   "r&   c              
   C   s   t || d]}t|dd }t|dd }|r'|r'||fW  d    S t|jjd r2J t	t
jdddt|t|jjg t|jjd rOJ t|dt|dfW  d    S 1 sgw   Y  d S )NZ
dist_buildz*.tar.gzz*.whlzbuild/lib/build-mbuildz--outdir)r   Zsession_locked_tmp_dirnextglobr   r   Zrootdirexistsr   Zcheck_outputr   
executabler   )tmp_path_factoryr   tmpsdistwheelr   r   r   _build_distributionsT   s*   	

$r1   )r   c                 C   s8   t d}|rt j|rt| S t| |\}}|S )NZPRE_BUILT_SETUPTOOLS_SDISTosgetenvr   r+   r   resolver1   )r-   r   prebuiltr/   _r   r   r   setuptools_sdisto   
   
r8   c                 C   s8   t d}|rt j|rt| S t| |\}}|S )NZPRE_BUILT_SETUPTOOLS_WHEELr2   )r-   r   r6   r7   r0   r   r   r   setuptools_wheely   r9   r:   c                 C   sd   t  }t| d |_ddg|_t||_tj dd |	 W  d   S 1 s+w   Y  dS )z?Virtual env with the version of setuptools under test installedvenv--no-setuptoolsz--wheel=bundleN)Z
PYTHONPATH)
r   
VirtualEnvr   r   rootcreate_optsr   Zreqr   Zcreate)r#   r:   envr   r   r   r;      s   

$r;   c                 C   s.   t  }t| d |_ddg|_|  |S )z7Virtual env without any version of setuptools installedvenv_without_setuptoolsr<   
--no-wheelr   r=   r   r   r>   r?   Z
ensure_envr#   r@   r   r   r   rA      
   
rA   c                 C   s.   t  }t| d |_g d|_|  |S )z1Virtual env without any common packages installed	bare_venv)r<   z--no-piprB   z	--no-seedrC   rD   r   r   r   rF      rE   rF   c                 C   s   t |dksJ t| d3}|D ]'\}}t|d}tj|d}t | |_t	t

 |_|j||d qW d   dS 1 sDw   Y  dS )z
    Create a simple sdist tarball at dist_path, containing the files
    listed in ``files`` as ``(filename, content)`` tuples.
    r   zw:gzutf-8)name)ZfileobjN)lentarfileopenioBytesIOencodeZTarInfogetvaluesizeinttimemtimeZaddfile)	dist_pathfilesZdistfilenameZcontentZ
file_bytes	file_infor   r   r   
make_sdist   s   "rX   c              	   C   s(   t | dtd|d|dfdg dS )z\
    Create a simple sdist tarball at dist_path, containing just a simple
    setup.py.
    setup.pyzT             import setuptools
             setuptools.setup(
                 name=z,
                 version=z
             )
         	setup.cfgr   N)rX   r   )rT   distnameversionr   r   r   make_trivial_sdist   s   r^   c           
   
      s   d|v sJ | d  d } fddtdt d D }td|d|d|d	|d
	}d}d|ftj|d|fg}|dd D ]}tjj| ddg  }	||	df qEt| | dS )z
    Make an sdist tarball with distname and version which also contains one
    package with the same name as distname.  The top-level package is
    designated a namespace package).
    .r   c                    s   g | ]}d   d| qS )r_   N)join).0idxpartsr   r   
<listcomp>   s    z$make_nspkg_sdist.<locals>.<listcomp>r   zE        import setuptools
        setuptools.setup(
            name=z,
            version=z,
            packages=z",
            namespace_packages=[z]
        )
    z7__import__('pkg_resources').declare_namespace(__name__)rY   __init__.pyNr   )	splitrangerI   r   r3   r   r`   appendrX   )
rT   r\   r]   Z	nspackageZpackagesZsetup_pyZinitrU   packagerV   r   rc   r   make_nspkg_sdist   s*   
 rk   c                 C   s&   t | dtdj|||dfdg d S )NrY   z                import setuptools
                setuptools.setup(
                  name={name!r},
                  version={version!r},
                  python_requires={python_requires!r},
                )
                )rH   r]   python_requiresrZ   )rX   r   format)rT   r\   r]   rl   r   r   r   make_python_requires_sdist   s   	rn   Zfoobarz0.1r   c                 C   s  t |}dd| d| gtj| gd}|r|| tj| d}	t|	 |rjg }
g }|D ](}||}|d v rA|}n|
}t	|t
tfrOd|}|| d|  q1tdjd	|
d	|d
}nd}ttj|	dddd}|| W d   n1 sw   Y  |du rtd}ttj|	dddd}|||  W d   n1 sw   Y  tj| | d| d}|||| |	S )a  Creates a source tree under path for a trivial test package that has a
    single requirement in setup_requires--a tarball for that requirement is
    also created and added to the dependency_links argument.

    ``distname`` and ``version`` refer to the name/version of the package that
    the test package requires via ``setup_requires``.  The name of the test
    package itself is just 'test_pkg'.
    test_pkgz0.0z==)rH   r]   Zsetup_requiresZdependency_linkszname version;z: zg
            [metadata]
            {metadata}
            [options]
            {options}
            
)optionsmetadatar   r[   wrG   )encodingNzI            import setuptools
            setuptools.setup(**%r)
        rY   -z.tar.gz)r   r3   r   abspathupdater`   mkdirpoprg   
isinstancetuplelistri   r   rm   rK   write)r   r\   r]   Zmake_packageZsetup_py_templateZsetup_attrsZuse_setup_cfgZnormalized_distnameZtest_setup_attrsro   rr   rs   rH   valuesectionZtest_setup_cfg_contentsfZfoobar_pathr   r   r   create_setup_requires_package  sT   



r   c                 C   sd   t dt dt ddddddid	}|g d
 | d }|  tjj||d |dd |S )Nz
            [build-system]
            requires = ["setuptools"]
            build-backend = "setuptools.build_meta"
            z
            __import__('setuptools').setup(
                pbr=True,
                setup_requires=["pbr"],
            )
            z
            [metadata]
            name = mypkg

            [files]
            packages =
                mypkg
            r   zprint('Hello world!'))rf   zhello.pyztest.txtzAnother file in here.)zpyproject.tomlrY   r[   mypkgother)Zpythonr'   ZpipinstallZpbrr   )prefixZPBR_VERSIONz0.42)r   runry   jaracor   r(   Zsetenv)r#   r	   r;   rU   r   r   r   r   pbr_packagea  s(    r   )&r   rL   r3   r   r   rJ   rR   pathlibr   Zjaraco.pathr   r   r!   Zsetuptools._normalizationr   r   r   r   textwrapr   Zfixturer   r   r   r%   r&   r1   r8   r:   r;   rA   rF   rX   r^   rk   rn   r   r   r   r   r   r   <module>   s`    







	
	

	
	$
M