
][\
C                 @   s   d  Z  d d l Z d d l Z d d l Z d d l m 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)glob)Command)*)convert_path	Mixin2to3)logc               @   s+  e  Z d  Z d Z d8 d9 d: d; d< g Z d d g Z i d d 6Z 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   0/usr/lib/python3.4/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 x6 |  j j j   D] \ } } t |  |  j | <qh Wn  |  j   |  _	 t
 |  j t  sy; t |  j  |  _ d |  j k o d k n s t  Wqt t f k
 rt d   YqXn  d  S)	NZbuildr   r   r      zoptimize must be 0, 1, or 2)	build_libr"   )forcer#   )Zset_undefined_optionsdistributionpackagesr   r   r   itemsr   get_data_files
data_files
isinstancer   intAssertionError
ValueErrorZDistutilsOptionError)r   namepathr   r   r   finalize_options*   s"    		)zbuild_py.finalize_optionsc             C   sS   |  j  r |  j   n  |  j r6 |  j   |  j   n  |  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   | rv t |  d   n    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osr.   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    xO | D]G } t t j 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>   r.   isfile)r9   fn)filesr   r   r<      s   	 z,build_py.find_data_files.<locals>.<listcomp>)r   getr   r>   r.   r?   r   extend)r   r   rE   ZglobspatternZfilelistr   )rK   r   rB   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>   r.   r?   mkpathdirname	copy_file)r   Zlastdirr   rE   rF   rG   filenametargetr   r   r   r3      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 |  n  | 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).r7   rH   r   r8   NrU   )r@   r   r>   r.   r?   KeyErrorinsertrL   )r   r   r.   tailZpdirr   r   r   r=      s(    			zbuild_py.get_package_dirc             C   s   | d k rY t  j j |  s1 t d |   n  t  j j |  sY t d |   qY n  | r t  j j | d  } t  j j |  r | St j d d |  n  d  S)NrH   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>   r.   existsZDistutilsFileErrorisdirr?   rI   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>   r.   rI   r   r[   )r   modulemodule_filer   r   r   check_module   s    zbuild_py.check_modulec       	      C   s   |  j  | |  t t j 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  qP |  j d |  qP W| S)Nz*.pyr   zexcluding %s)r]   r   r>   r.   r?   abspathr$   Zscript_namesplitextbasenamerC   Zdebug_print)	r   r   r   Zmodule_filesmodulesZsetup_scriptr   Zabs_fr^   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  q n  t j j | | d  }
 |  j	 | |
  sq n  | 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.
        r7   r   r8   __init__z.pyrU   rU   )
r   r@   r?   rV   r=   r]   rC   r>   r.   r`   )r   r%   rd   r^   r.   r   Zmodule_baser   Zcheckedr\   r_   r   r   r   find_modules   s*    
zbuild_py.find_modulesc             C   sw   g  } |  j  r% | j |  j    n  |  j rs xB |  j D]4 } |  j |  } |  j | |  } | j |  q8 Wn  | 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   rM   rg   r%   r=   re   )r   rd   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)r8   rU   r   )r9   r^   r   r   r   r<   -  s   	 z-build_py.get_source_files.<locals>.<listcomp>)ri   )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>   r.   r?   )r   rF   r   r^   Zoutfile_pathr   r   r   get_module_outfile/  s    zbuild_py.get_module_outfiler8   c             C   s   |  j    } g  } x | D] \ } } } | j d  } |  j |  j | |  } | j |  | r |  j r | j t j j | d d  n  |  j	 d k r | j t j j | d d  q q q W| d d   |  j
 D 7} | S)Nr7   debug_overrideTr   Fc             S   s>   g  |  ]4 \ } } } } | D] } t  j j | |   q q Sr   )r>   r.   r?   )r9   r   rE   rF   rG   rS   r   r   r   r<   C  s   	z(build_py.get_outputs.<locals>.<listcomp>)ri   r@   rl   r   rC   r   	importlibutilcache_from_sourcer   r(   )r   r0   rd   Zoutputsr   r^   r_   rS   r   r   r   r5   3  s"    		zbuild_py.get_outputsc             C   s   t  | t  r! | j d  } n$ t  | t t f  sE t d   n  |  j |  j | |  } t j	 j
 |  } |  j |  |  j | | d d S)Nr7   z:'package' must be a string (dot-separated), list, or tuplerO   r   )r)   strr@   rk   tuple	TypeErrorrl   r   r>   r.   rQ   rP   rR   )r   r^   r_   r   Zoutfiledirr   r   r   build_moduleJ  s    zbuild_py.build_modulec             C   s=   |  j    } x* | D]" \ } } } |  j | | |  q Wd  S)N)rg   ru   )r   rd   r   r^   r_   r   r   r   r1   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=   re   r+   ru   )r   r   r   rd   Zpackage_r^   r_   r   r   r   r2   b  s    
zbuild_py.build_packagesc          
   C   s   t  j r |  j d  d  Sd d l m } |  j } | d	 t j k rV | t j } n  |  j r | | d d d |  j	 d | d |  j
 n  |  j d k r | | d |  j d |  j	 d | d |  j
 n  d  S)
Nz%byte-compiling is disabled, skipping.r   )r4   r8   r   r   prefixdry_runrU   )sysdont_write_bytecoder[   distutils.utilr4   r   r>   sepr   r   rw   r   )r   rK   r4   rv   r   r   r   r4   v  s    			zbuild_py.byte_compile)r	   r
   r   )zcompiler   r   )r   Nr   )r   r   r   )zforcer   r   )__name__
__module____qualname__ZdescriptionZuser_optionsZboolean_optionsZnegative_optr    r/   r6   r'   rB   r3   r=   r]   r`   re   rg   ri   rj   rl   r5   ru   r1   r2   r4   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   sl   g  |  _  |  j r |  j   n  |  j r? |  j   |  j   n  |  j |  j   |  j |  j d d   d  S)Nr0   r   )	updated_filesr   r1   r%   r2   r3   Zrun_2to3r4   r5   )r   r   r   r   r6     s    			
zbuild_py_2to3.runc             C   s=   t  j |  | | |  } | d r9 |  j j | d  n  | S)Nr8   r   )r   ru   r   rC   )r   r^   r_   r   Zresr   r   r   ru     s    
zbuild_py_2to3.build_moduleN)r|   r}   r~   r6   ru   r   r   r   r   r     s   r   )__doc__r>   importlib.utilrn   rx   r   Zdistutils.corer   Zdistutils.errorsrz   r   r   Z	distutilsr   r   r   r   r   r   r   <module>   s   
 |