3
e                 @   s   d 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
mZ ddlZg Zdd Zdd Zd	d
 Zdd Zdd Zdd Zdd Zdd Zdd ZdS )z
Monkey patching of distutils.
    N)import_module)sixc             C   s"   t j dkr| f| j S tj| S )am  
    Returns the bases classes for cls sorted by the MRO.

    Works around an issue on Jython where inspect.getmro will not return all
    base classes if multiple classes share the same name. Instead, this
    function will return a tuple containing the class itself, and the contents
    of cls.__bases__. See https://github.com/pypa/setuptools/issues/1024.
    Jython)platformpython_implementation	__bases__inspectgetmro)cls r   7/tmp/pip-build-argcdawy/setuptools/setuptools/monkey.py_get_mro   s    	r   c             C   s0   t | tjrtnt | tjr tndd }|| S )Nc             S   s   d S )Nr   )itemr   r   r   <lambda>*   s    zget_unpatched.<locals>.<lambda>)
isinstancer   class_typesget_unpatched_classtypesFunctionTypeget_unpatched_function)r   lookupr   r   r   get_unpatched&   s    r   c             C   s:   dd t | D }t|}|jjds6d|  }t||S )zProtect against re-patching the distutils if reloaded

    Also ensures that no other distutils extension monkeypatched the distutils
    first.
    c             s   s   | ]}|j jd s|V  qdS )
setuptoolsN)
__module__
startswith).0r
   r   r   r   	<genexpr>6   s   z&get_unpatched_class.<locals>.<genexpr>	distutilsz(distutils has already been patched by %r)r   nextr   r   AssertionError)r
   Zexternal_basesbasemsgr   r   r   r   /   s    r   c              C   s   t jtj_tjdk} | r"t jtj_tjdk pxdtj  k o@dk n  pxdtj  k oZdk n  pxdtj  k otdkn  }|rd	}|tjj	_
t  t  x"tjtjtjfD ]}t jj|_qW t jjtj_t jjtj_d
tjk rt jjtjd
 _t  d S )N               r         zhttps://upload.pypi.org/legacy/zdistutils.command.build_ext)r"   r#   r"   )r$   r%   r&   )r"   r   )r"   r"   r%   )r"   r'   )r"   r'   r(   )r"   r#   )r"   r#   r"   )r   Commandr   coresysversion_infofindallfilelistconfigPyPIRCCommandDEFAULT_REPOSITORY+_patch_distribution_metadata_write_pkg_file+_patch_distribution_metadata_write_pkg_infodistcmdDistribution	extension	Extensionmodules#patch_for_msvc_specialized_compiler)Zhas_issue_12885Zneeds_warehouseZ	warehousemoduler   r   r   	patch_allA   s(    




r<   c               C   s   t jjtjj_dS )zDPatch write_pkg_file to also write Requires-Python/Requires-ExternalN)r   r4   write_pkg_filer   DistributionMetadatar   r   r   r   r2   k   s    r2   c              C   s<   dt jdd   kodk n  } | s*dS tjjtjj_dS )z
    Workaround issue #197 - Python 3 prior to 3.2.2 uses an environment-local
    encoding to save the pkg_info. Monkey-patch its write_pkg_info method to
    correct this undesirable behavior.
    r"   Nr$   )r"   )r"   r$   r$   )r+   r,   r   r4   write_pkg_infor   r>   )Zenvironment_localr   r   r   r3   r   s    "r3   c             C   s*   t ||}t| jd| t|||  dS )z
    Patch func_name in target_mod with replacement

    Important - original must be resolved by name to avoid
    patching an already patched function.
    	unpatchedN)getattrvars
setdefaultsetattr)replacementZ
target_mod	func_nameoriginalr   r   r   
patch_func   s    
rH   c             C   s
   t | dS )Nr@   )rA   )	candidater   r   r   r      s    r   c                 s   t d tj dkrdS  fdd} tj| d}tj| d}yt|d  t|d	  W n tk
rl   Y nX yt|d
  W n tk
r   Y nX yt|d  W n tk
r   Y nX dS )z\
    Patch functions in distutils to use standalone Microsoft Visual C++
    compilers.
    zsetuptools.msvcWindowsNc                sL   d| krdnd}||j d }t |}t| }t||sBt||||fS )zT
        Prepare the parameters for patch_func to patch indicated function.
        msvc9Zmsvc9_Zmsvc14__)lstriprA   r   hasattrImportError)mod_namerF   Zrepl_prefixZ	repl_namereplmod)msvcr   r   patch_params   s    

z9patch_for_msvc_specialized_compiler.<locals>.patch_paramszdistutils.msvc9compilerzdistutils._msvccompilerZfind_vcvarsallZquery_vcvarsallZ_get_vc_envZgen_lib_options)r   r   system	functoolspartialrH   rO   )rT   rK   Zmsvc14r   )rS   r   r:      s&    r:   )__doc__r+   distutils.filelistr   r   r   rV   	importlibr   r   Zsetuptools.externr   r   __all__r   r   r   r<   r2   r3   rH   r   r:   r   r   r   r   <module>   s&   	*