
(h&C                 @   s   d  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 Td d l	 m
 Z
 m Z d d l m Z Gd d   d e  Z Gd	 d
   d
 e e  Z d S)zHdistutils.command.build_py

Implements the Distutils 'build_py' command.    N)Command)*)convert_path	Mixin2to3)logc               @   s*  e  Z d  Z d Z d8 d9 d: d; d< g Z d d g Z d d i 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' d(   Z d) d*   Z d+ d,   Z d- d. d/  Z d0 d1   Z d2 d3   Z d4 d5   Z d6 d7   Z d	 S)=build_pyz5"build" pure Python modules (copy to build directory)
build-lib=ddirectory to "build" (copy) tocompileccompile .py to .pyc
no-compileN!don't compile .py files [default]	optimize=Olalso compile with optimization: -O1 for "python -O", -O2 for "python -OO", and -O0 to disable [default: -O0]forcef2forcibly build everything (ignore file timestamps)c             C   sL   d  |  _  d  |  _ d  |  _ d  |  _ d  |  _ d |  _ d |  _ d  |  _ d  S)Nr   )	build_lib
py_modulespackagepackage_datapackage_dirr   optimizer   )self r   L/tmp/pip-build-kmztzv_d/setuptools/setuptools/_distutils/command/build_py.pyinitialize_options    s    							zbuild_py.initialize_optionsc             C   s  |  j  d d d  |  j j |  _ |  j j |  _ |  j j |  _ i  |  _ |  j j r x3 |  j j j   D] \ } } t |  |  j | <qh W|  j   |  _	 t
 |  j t  sy; t |  j  |  _ d |  j k o d k n s t  Wn$ t t f k
 rt d   Yn Xd  S)	Nbuildr   r   r      zoptimize must be 0, 1, or 2)	build_libr"   )forcer#   )set_undefined_optionsdistributionpackagesr   r   r   itemsr   get_data_files
data_files
isinstancer   intAssertionError
ValueErrorDistutilsOptionError)r   namepathr   r   r   finalize_options*   s"    		)zbuild_py.finalize_optionsc             C   sM   |  j  r |  j   |  j r0 |  j   |  j   |  j |  j d d   d  S)Ninclude_bytecoder   )r   build_modulesr&   build_packagesbuild_package_databyte_compileget_outputs)r   r   r   r   runC   s    	
	

zbuild_py.runc                s   g  } |  j  s | Sx |  j  D] } |  j |  } t j j |  j g | j d    } d   | rs t |  d     f d d   |  j | |  D } | j	 | | | | f  q W| S)z?Generate list of '(package,src_dir,build_dir,filenames)' tuples.r      c                s    g  |  ] } |   d    q S)Nr   ).0file)plenr   r   
<listcomp>t   s   	 z+build_py.get_data_files.<locals>.<listcomp>)
r&   get_package_dirosr0   joinr   splitlenfind_data_filesappend)r   datar   src_dir	build_dir	filenamesr   )r=   r   r(   a   s    	%%zbuild_py.get_data_filesc                s   |  j  j d g   |  j  j | g   } g    x[ | D]S } t j t j j t j |  t |    }   j   f d d   | D  q5 W  S)z6Return filenames for package's data files in 'src_dir' c                s4   g  |  ]* } |   k r t  j j |  r |  q Sr   )r@   r0   isfile)r;   fn)filesr   r   r>      s   	 z,build_py.find_data_files.<locals>.<listcomp>)	r   getglobr@   r0   rA   escaper   extend)r   r   rG   Zglobspatternfilelistr   )rM   r   rD   y   s    -$zbuild_py.find_data_filesc             C   s   d } x |  j  D]v \ } } } } xa | D]Y } t j j | |  } |  j t j j |   |  j t j j | |  | d d q) Wq Wd S)z$Copy data files into build directoryNpreserve_modeF)r)   r@   r0   rA   mkpathdirname	copy_file)r   Zlastdirr   rG   rH   rI   filenametargetr   r   r   r5      s    zbuild_py.build_package_datac             C   s   | j  d  } |  j s5 | r. t j j |   Sd Sn g  } x | r y |  j d j |  } Wn- t k
 r | j d | d  | d =Yq> X| j d |  t j j |   Sq> W|  j j d  } | d k	 r | j d |  | r t j j |   Sd Sd S)zReturn the directory, relative to the top of the source
           distribution, where package 'package' should be found
           (at least according to the 'package_dir' option, if any).r9   rJ   r   r:   NrZ   )rB   r   r@   r0   rA   KeyErrorinsertrN   )r   r   r0   tailZpdirr   r   r   r?      s(    			zbuild_py.get_package_dirc             C   s   | d k rP t  j j |  s. t d |   t  j j |  sP t d |   | r t  j j | d  } t  j j |  r | St j d d |  d  S)NrJ   z%package directory '%s' does not existz>supposed package directory '%s' exists, but is not a directoryz__init__.pyz!package init file '%s' not found z(or not a regular file))	r@   r0   existsDistutilsFileErrorisdirrA   rK   r   warn)r   r   r   init_pyr   r   r   check_package   s    
	zbuild_py.check_packagec             C   s1   t  j j |  s) t j d | |  d Sd Sd  S)Nz!file %s (for module %s) not foundFT)r@   r0   rK   r   ra   )r   modulemodule_filer   r   r   check_module   s    zbuild_py.check_modulec       	      C   s   |  j  | |  t j t j j t j |  d   } g  } t j j |  j j  } xx | D]p } t j j |  } | | k r t j j	 t j j
 |   d } | j | | | f  q\ |  j d |  q\ W| S)Nz*.pyr   zexcluding %s)rc   rO   r@   r0   rA   rP   abspathr%   script_namesplitextbasenamerE   debug_print)	r   r   r   Zmodule_filesmodulesZsetup_scriptr   Zabs_frd   r   r   r   find_package_modules   s    '"zbuild_py.find_package_modulesc             C   s  i  } g  } x	|  j  D] } | j d  } d j | d d   } | d } y | | \ } } Wn' t k
 r |  j |  } d } Yn X| s |  j | |  }	 | d f | | <|	 r | j | d |	 f  t j j | | d  }
 |  j	 | |
  s q | j | | |
 f  q W| S)a  Finds individually-specified Python modules, ie. those listed by
        module name in 'self.py_modules'.  Returns a list of tuples (package,
        module_base, filename): 'package' is a tuple of the path through
        package-space to the module; 'module_base' is the bare (no
        packages, no dots) module name, and 'filename' is the path to the
        ".py" file (relative to the distribution root) that implements the
        module.
        r9   r   r:   __init__z.pyrZ   rZ   )
r   rB   rA   r[   r?   rc   rE   r@   r0   rf   )r   r&   rl   rd   r0   r   Zmodule_baser   checkedrb   re   r   r   r   find_modules   s*    
zbuild_py.find_modulesc             C   sq   g  } |  j  r" | j |  j    |  j rm x? |  j D]4 } |  j |  } |  j | |  } | j |  q5 W| S)a4  Compute the list of all modules that will be built, whether
        they are specified one-module-at-a-time ('self.py_modules') or
        by whole packages ('self.packages').  Return a list of tuples
        (package, module, module_file), just like 'find_modules()' and
        'find_package_modules()' do.)r   rQ   rp   r&   r?   rm   )r   rl   r   r   mr   r   r   find_all_modules  s    		zbuild_py.find_all_modulesc             C   s   d d   |  j    D S)Nc             S   s   g  |  ] } | d  q S)r:   rZ   r   )r;   rd   r   r   r   r>   -  s   	 z-build_py.get_source_files.<locals>.<listcomp>)rr   )r   r   r   r   get_source_files,  s    zbuild_py.get_source_filesc             C   s.   | g t  |  | d g } t j j |   S)Nz.py)listr@   r0   rA   )r   rH   r   rd   Zoutfile_pathr   r   r   get_module_outfile/  s    zbuild_py.get_module_outfiler:   c             C   s   |  j    } g  } x | D] \ } } } | j d  } |  j |  j | |  } | j |  | r |  j r | j t j j | d d  |  j	 d k r | j t j j | d |  j	  q W| d d   |  j
 D 7} | S)Nr9   optimizationrJ   r   c             S   s>   g  |  ]4 \ } } } } | D] } t  j j | |   q q Sr   )r@   r0   rA   )r;   r   rG   rH   rI   rX   r   r   r   r>   C  s   	z(build_py.get_outputs.<locals>.<listcomp>)rr   rB   ru   r   rE   r   	importlibutilcache_from_sourcer   r)   )r   r2   rl   outputsr   rd   re   rX   r   r   r   r7   3  s"    		zbuild_py.get_outputsc             C   s   t  | t  r! | j d  } n! t  | t t f  sB t d   |  j |  j | |  } t j	 j
 |  } |  j |  |  j | | d d S)Nr9   z:'package' must be a string (dot-separated), list, or tuplerT   r   )r*   strrB   rt   tuple	TypeErrorru   r   r@   r0   rV   rU   rW   )r   rd   re   r   outfiledirr   r   r   build_moduleJ  s    	zbuild_py.build_modulec             C   s=   |  j    } x* | D]" \ } } } |  j | | |  q Wd  S)N)rp   r   )r   rl   r   rd   re   r   r   r   r3   Y  s    zbuild_py.build_modulesc             C   sx   xq |  j  D]f } |  j |  } |  j | |  } x< | D]4 \ } } } | | k sY t  |  j | | |  q8 Wq
 Wd  S)N)r&   r?   rm   r,   r   )r   r   r   rl   Zpackage_rd   re   r   r   r   r4   b  s    
zbuild_py.build_packagesc          
   C   s   t  j r |  j d  d  Sd d l m } |  j } | d	 t j k rS | t j } |  j r | | d d d |  j	 d | d |  j
 |  j d k r | | d |  j d |  j	 d | d |  j
 d  S)
Nz%byte-compiling is disabled, skipping.r   )r6   r:   r   r   prefixdry_runrZ   )sysdont_write_bytecodera   distutils.utilr6   r   r@   sepr   r   r   r   )r   rM   r6   r   r   r   r   r6   v  s    			zbuild_py.byte_compile)r   r	   r
   )zcompiler   r   )r   Nr   )r   r   r   )zforcer   r   )__name__
__module____qualname__descriptionuser_optionsboolean_optionsnegative_optr   r1   r8   r(   rD   r5   r?   rc   rf   rm   rp   rr   rs   ru   r7   r   r3   r4   r6   r   r   r   r   r      s8    	

'4	r   c               @   s(   e  Z d  Z d d   Z d d   Z d S)build_py_2to3c             C   sf   g  |  _  |  j r |  j   |  j r9 |  j   |  j   |  j |  j   |  j |  j d d   d  S)Nr2   r   )	updated_filesr   r3   r&   r4   r5   run_2to3r6   r7   )r   r   r   r   r8     s    		
	

zbuild_py_2to3.runc             C   s:   t  j |  | | |  } | d r6 |  j j | d  | S)Nr:   r   )r   r   r   rE   )r   rd   re   r   resr   r   r   r     s    
zbuild_py_2to3.build_moduleN)r   r   r   r8   r   r   r   r   r   r     s   r   )__doc__r@   importlib.utilrw   r   rO   distutils.corer   Zdistutils.errorsr   r   r   	distutilsr   r   r   r   r   r   r   <module>   s   
 |