U
    Ȃe	G                     @   sx  d Z ddlmZ ddlmZmZ ddlmZ ddlm	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mZmZmZ dd	lmZ dd
lmZ ddlmZ zddlmZmZ dd ZW n, ek
r   ddlm Z mZ dd ZY nX dd Z!dd Z"dd Z#G dd deZ$e%&d' Z(dd Z)dd Z*dd Z+d d!d"Z,d#d$ Z-d%d& Z.d'd( Z/d)d*d+d,gZ0d1d/d0Z1dS )2z6setuptools.command.bdist_egg

Build .egg distributions    )DistutilsSetupError)remove_treemkpath)log)CodeTypeN)six)get_build_platformDistributionensure_directory)
EntryPoint)Library)Command)get_pathget_python_versionc                   C   s   t dS )Npurelib)r    r   r   @/tmp/pip-unpacked-wheel-sm0xk1vo/setuptools/command/bdist_egg.py_get_purelib   s    r   )get_python_libr   c                   C   s   t dS )NF)r   r   r   r   r   r      s    c                 C   s2   d| krt j| d } | dr.| d d } | S )N.r   modulei)ospathsplitextendswith)filenamer   r   r   strip_module#   s
    
r   c                 c   s6   t | D ]&\}}}|  |  |||fV  q
dS )zbDo os.walk in a reproducible way,
    independent of indeterministic filesystem readdir order
    N)r   walksort)dirbasedirsfilesr   r   r   sorted_walk+   s    r#   c              	   C   s6   t d }t|d}|||   W 5 Q R X d S )NaR  
        def __bootstrap__():
            global __bootstrap__, __loader__, __file__
            import sys, pkg_resources, imp
            __file__ = pkg_resources.resource_filename(__name__, %r)
            __loader__ = None; del __bootstrap__, __loader__
            imp.load_dynamic(__name__,__file__)
        __bootstrap__()
        w)textwrapdedentlstripopenwrite)resourcepyfileZ_stub_templatefr   r   r   
write_stub5   s    	r-   c                   @   s   e Zd ZdZdddde  fdddd	gZd
dd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d  Zd!d" Zd#S )$	bdist_eggzcreate an "egg" distribution)z
bdist-dir=bz1temporary directory for creating the distributionz
plat-name=pz;platform name to embed in generated filenames (default: %s))exclude-source-filesNz+remove all .py files from the generated egg)	keep-tempkzPkeep the pseudo-installation tree around after creating the distribution archive)z	dist-dir=dz-directory to put final built distributions in)
skip-buildNz2skip rebuilding everything (for testing/debugging)r2   r5   r1   c                 C   s.   d | _ d | _d| _d | _d| _d | _d | _d S )Nr   )	bdist_dir	plat_name	keep_tempdist_dir
skip_build
egg_outputexclude_source_filesselfr   r   r   initialize_optionsZ   s    zbdist_egg.initialize_optionsc                 C   s   |  d }| _|j| _| jd kr>|  dj}tj|d| _| jd krPt	 | _| 
dd | jd krtd d |j|jt | j o| j }tj| j|d | _d S )Negg_infobdistZegg)r9   r9   z.egg)get_finalized_commandei_cmdr@   r6   
bdist_baser   r   joinr7   r   set_undefined_optionsr;   r	   egg_nameZegg_versionr   distributionhas_ext_modulesr9   )r>   rC   rD   basenamer   r   r   finalize_optionsc   s$    


   zbdist_egg.finalize_optionsc                 C   s   | j | d_tjtjt }| jj	g  }| j_	|D ]}t
|trt|dkrtj|d rtj|d }tj|}||ks||tj r|t|d d  |d f}| jj	| q:z"td| j  | jddd d W 5 || j_	X d S )Ninstall   r      zinstalling package data to %sinstall_data)forceroot)r6   rB   install_libr   r   normcaserealpathr   rH   
data_files
isinstancetuplelenisabs
startswithsepappendr   infocall_command)r>   site_packagesolditemrT   
normalizedr   r   r   do_install_data{   s"    zbdist_egg.do_install_datac                 C   s   | j gS )N)r;   r=   r   r   r   get_outputs   s    zbdist_egg.get_outputsc                 K   sP   t D ]}||| j q|d| j |d| j | j|f|}| | |S )z8Invoke reinitialized command `cmdname` with keyword argsr:   dry_run)INSTALL_DIRECTORY_ATTRS
setdefaultr6   r:   re   reinitialize_commandrun_command)r>   Zcmdnamekwdirnamecmdr   r   r   r^      s    
zbdist_egg.call_commandc                 C   s  |  d td| j | d}|j}d |_| j rH| jsH|  d | j	ddd}||_| 
 \}}g | _g }t|D ]|\}}tj|\}	}
tj| jt|	d }| j| td	| | jsttj|| || |tjd
||< qz|r|| | jjr|   | j}tj|d}| | | jjrltj|d}td| | j	d|dd | | tj|d}|rtd| | js t| t|d}| d| | d |!  n,tj"|r td| | js t#| t$tj|d| %  tj&tj| j'dr<t(d | j)rL| *  t+| j,|| j-| j| . d | j/st0| j| jd t1| jdg dt2 | j,f d S )Nr@   zinstalling library code to %srL   
build_clibrR   r   )warn_dir.pyzcreating stub loader for %s/EGG-INFOscriptszinstalling scripts to %sinstall_scriptsrN   )install_dirZno_epznative_libs.txtz
writing %swt
zremoving %szdepends.txtzxWARNING: 'depends.txt' will not be used by setuptools 0.6!
Use the install_requires/extras_require setup() args instead.)verbosere   modere   
dist_filesr.   )3ri   r   r]   r6   rB   rQ   rH   has_c_librariesr:   r^   get_ext_outputsstubs	enumerater   r   r   rE   r   r\   re   r-   rJ   replacer[   byte_compilerU   rc   r   rr   copy_metadata_tor
   r(   r)   closeisfileunlinkwrite_safety_flagzip_safeexistsr@   warnr<   zap_pyfilesmake_zipfiler;   rw   
gen_headerr8   r   getattrr   )r>   ZinstcmdZold_rootrl   all_outputsext_outputsZ
to_compiler0   Zext_namer   extr+   Zarchive_rootr@   Z
script_dirZnative_libsZ	libs_filer   r   r   run   s    












  zbdist_egg.runc           
   
   C   s   t d t| jD ]\}}}|D ]}tj||}|drTt d| t	| |dr"|}d}t
||}tj|tj|dd }	t d||	f  zt|	 W n tk
r   Y nX t||	 q"qd S )	Nz+Removing .py files from temporary directoryro   zDeleting %s__pycache__z#(?P<name>.+)\.(?P<magic>[^.]+)\.pycname.pyczRenaming file from [%s] to [%s])r   r]   walk_eggr6   r   r   rE   r   debugr   rematchpardirgroupremoveOSErrorrename)
r>   r    r!   r"   r   r   Zpath_oldpatternmZpath_newr   r   r   r      s4    



  zbdist_egg.zap_pyfilesc                 C   s2   t | jdd }|d k	r|S td t| j| jS )Nr   z4zip_safe flag not set; analyzing archive contents...)r   rH   r   r   analyze_eggr6   r}   )r>   safer   r   r   r     s
    
zbdist_egg.zip_safec           
      C   s   t | jjpd}|di d}|d kr0dS |jr<|jrJtd|f djt	j
 }|j}d|j}|jd }tj| j}d	t  }| jsttj| j| jd
 t| jd}	|	| |	  dS )N zsetuptools.installationZeggsecutabler$   zGeggsecutable entry point (%r) cannot have 'extras' or refer to a modulez{}.{}r   r   aH  #!/bin/sh
if [ `basename $0` = "%(basename)s" ]
then exec python%(pyver)s -c "import sys, os; sys.path.insert(0, os.path.abspath('$0')); from %(pkg)s import %(base)s; sys.exit(%(full)s())" "$@"
else
  echo $0 is not the correct name for this egg file.
  echo Please rename it back to %(basename)s and try again.
  exec false
fi
ry   a)r   	parse_maprH   entry_pointsgetattrsextrasr   formatsysversion_infomodule_namerE   r   r   rJ   r;   localsre   r   rk   r(   r)   r   )
r>   Zepmeppyverpkgfullr    rJ   headerr,   r   r   r   r     s0    

zbdist_egg.gen_headerc                 C   sh   t j| j}t j|d}| jjjD ]<}||r&t j||t	|d }t
| | || q&dS )z*Copy metadata (egg info) to the target_dirr   N)r   r   normpathr@   rE   rC   filelistr"   rZ   rX   r
   	copy_file)r>   
target_dirZnorm_egg_infoprefixr   targetr   r   r   r   :  s    
zbdist_egg.copy_metadata_toc                 C   s  g }g }| j di}t| j D ]f\}}}|D ].}tj|d  tkr*||| |  q*|D ]"}|| | d |tj||< q^q| j	
 r| d}|jD ]Z}	t|	trq||	j}
||
}tj|dstjtj| j |r|| q||fS )zAGet a list of relative paths to C extensions in the output distror   rN   rp   	build_extzdl-)r6   r#   r   r   r   lowerNATIVE_EXTENSIONSr\   rE   rH   rI   rB   
extensionsrV   r   Zget_ext_fullnamer   Zget_ext_filenamerJ   rZ   r   )r>   r   r   pathsr    r!   r"   r   Z	build_cmdr   fullnamer   r   r   r|   F  s0    





zbdist_egg.get_ext_outputsN)__name__
__module____qualname__descriptionr   user_optionsboolean_optionsr?   rK   rc   rd   r^   r   r   r   r   r   r|   r   r   r   r   r.   C   s2     	
Q'r.   z.dll .so .dylib .pydc                 c   sH   t | }t|\}}}d|kr(|d |||fV  |D ]
}|V  q8dS )z@Walk an unpacked egg's contents, skipping the metadata directoryrq   N)r#   nextr   )egg_dirwalkerr    r!   r"   Zbdfr   r   r   r   f  s    
r   c           	      C   s   t  D ](\}}tjtj| d|r|  S qt s<dS d}t| D ]R\}}}|D ]B}|dsV|drrqVqV|ds|drVt	| |||o|}qVqH|S )Nrq   FTro   z.pywr   z.pyo)
safety_flagsitemsr   r   r   rE   can_scanr   r   scan_module)	r   r}   flagfnr   r    r!   r"   r   r   r   r   r   q  s    
r   c                 C   s   t  D ]r\}}tj| |}tj|rJ|d ks>t||krzt| q|d k	rt||krt|d}|	d |
  qd S )Nru   rv   )r   r   r   r   rE   r   boolr   r(   r)   r   )r   r   r   r   r,   r   r   r   r     s    

r   zzip-safeznot-zip-safe)TFc                 C   s  t j||}|dd |kr"dS |t| d d t jd}||rJdpLd t j|d  }tjrld}nt	j
d	k r|d
}nd}t|d}|| t|}	|  d}
tt|	}dD ]}||krtd|| d}
qd|kr
dD ]}||krtd|| d}
q|
S )z;Check whether module possibly uses unsafe-for-zipfile stuffNTrN   r   r   r      )            rb)__file____path__z%s: module references %sFinspect)	getsource
getabsfilegetsourcefileZgetfilegetsourcelines
findsourcegetcommentsgetframeinfogetinnerframesgetouterframesstacktracez"%s: module MAY be using inspect.%s)r   r   rE   rX   r   r[   r   r   PY2r   r   r(   readmarshalloadr   dictfromkeysiter_symbolsr   r   )r   r    r   r}   r   r   r   skipr,   coder   symbolsbadr   r   r   r     s4     




r   c                 c   sT   | j D ]
}|V  q| jD ]6}t|tjr0|V  qt|trt|D ]
}|V  qBqdS )zBYield names and strings used by `code` and its nested code objectsN)co_names	co_constsrV   r   string_typesr   r   )r   r   constr   r   r   r     s    


r   c                   C   s2   t jdst jdkrdS td td d S )NjavacliTz1Unable to analyze compiled code on this platform.zfPlease ask the author to include a 'zip_safe' setting (either True or False) in the package's setup.py)r   platformrZ   r   r   r   r   r   r   r     s    
r   rR   rt   rO   install_baseTr$   c                    s   ddl }ttj| d td|    fdd}|rB|jn|j}s|j	| ||d}	t
 D ]\}
}}||	|
| qd|	  n t
 D ]\}
}}|d|
| q| S )aq  Create a zip file from all the files under 'base_dir'.  The output
    zip file will be named 'base_dir' + ".zip".  Uses either the "zipfile"
    Python module (if available) or the InfoZIP "zip" utility (if installed
    and found on the default search path).  If neither tool is available,
    raises DistutilsExecError.  Returns the name of the output zip file.
    r   Nry   z#creating '%s' and adding '%s' to itc                    s`   |D ]V}t jt j||}t j|r|t d d  }sN| || td| qd S )NrN   zadding '%s')	r   r   r   rE   r   rX   r)   r   r   )zrk   namesr   r   r0   base_dirre   r   r   visit  s    zmake_zipfile.<locals>.visit)compression)zipfiler   r   r   rk   r   r]   ZIP_DEFLATED
ZIP_STOREDZipFiler#   r   )zip_filenamer   rw   re   compressrx   r   r   r   r   rk   r!   r"   r   r   r   r     s    	
r   )r   r   Tr$   )2__doc__distutils.errorsr   distutils.dir_utilr   r   	distutilsr   typesr   r   r   r   r%   r   Zsetuptools.externr   pkg_resourcesr   r	   r
   r   Zsetuptools.extensionr   
setuptoolsr   	sysconfigr   r   r   ImportErrordistutils.sysconfigr   r   r#   r-   r.   r   r   splitr   r   r   r   r   r   r   r   rf   r   r   r   r   r   <module>   sX   
  "$     