
][\W                 @   s   d  Z  d d l Z d d l Z d d l m Z d d l m Z d d l m Z d d l	 m
 Z
 d d l m Z m Z d d l m Z d d l Z e j d	  Z Gd
 d   d e  Z Gd d   d e e  Z d S)zRdistutils.command.build_scripts

Implements the Distutils 'build_scripts' command.    N)ST_MODE)	sysconfig)Command)newer)convert_path	Mixin2to3)logs   ^#!.*python[0-9.]*([ 	].*)?$c               @   sj   e  Z d  Z d Z d d d g Z d g Z d d   Z d d   Z d d   Z d d   Z	 d d   Z
 d S)build_scriptsz("build" scripts (copy and fixup #! line)
build-dir=ddirectory to "build" (copy) toforcef1forcibly build everything (ignore file timestampsexecutable=e*specify final destination interpreter pathc             C   s1   d  |  _  d  |  _ d  |  _ d  |  _ d  |  _ d  S)N)	build_dirscriptsr   
executableoutfiles)self r   5/usr/lib/python3.4/distutils/command/build_scripts.pyinitialize_options   s
    				z build_scripts.initialize_optionsc             C   s)   |  j  d d d d  |  j j |  _ d  S)	NZbuildr	   r   r   r   )zbuild_scriptsz	build_dir)forcer   )
executabler   )Zset_undefined_optionsZdistributionr   )r   r   r   r   finalize_options%   s
    	zbuild_scripts.finalize_optionsc             C   s   |  j  S)N)r   )r   r   r   r   get_source_files,   s    zbuild_scripts.get_source_filesc             C   s   |  j  s d  S|  j   d  S)N)r   copy_scripts)r   r   r   r   run/   s    	zbuild_scripts.runc          "   C   s  |  j  |  j  g  } g  } x|  j D]} d } t |  } t j j |  j t j j |   } | j |  |  j	 r t
 | |  r t j d |  q& n  y t | d  } Wn' t k
 r |  j s   n  d } Yn Xt j | j  \ } } | j d  | j   }	 |	 s*|  j d |  q& n  t j |	  }
 |
 r]d } |
 j d  pWd	 } n  | rt j d
 | |  j  | j |  |  j st j s|  j } n: t j j t j d  d t j d  t j d  f  } t j |  } d | | d } y | j d  Wn' t k
 r9t  d j! |    Yn Xy | j |  Wn* t k
 rwt  d j! | |    Yn Xt | d  ' } | j" |  | j# | j$    Wd QXn  | r| j%   qq& | r| j%   n  | j |  |  j& | |  q& Wt j' d k rx | D]| } |  j r5t j d |  qt j( |  t) d @} | d Bd @} | | k rt j d | | |  t j* | |  qqWn  | | f S)a"  Copy each script listed in 'self.scripts'; if it's marked as a
        Python script in the Unix way (first line matches 'first_line_re',
        ie. starts with "\#!" and contains "python"), then adjust the first
        line to refer to the current Python interpreter as we copy.
        Fznot copying %s (up-to-date)rbNr   z%s is an empty file (skipping)T       zcopying and adjusting %s -> %sZBINDIRz
python%s%sZVERSIONZEXEs   #!s   
zutf-8z.The shebang ({!r}) is not decodable from utf-8zAThe shebang ({!r}) is not decodable from the script encoding ({})wbposixzchanging mode of %si  im  z!changing mode of %s from %o to %o)+Zmkpathr   r   r   ospathjoinbasenameappendr   r   r   debugopenOSErrordry_runtokenizedetect_encodingreadlineseekwarnfirst_line_rematchgroupinfor   Zpython_buildr   Zget_config_varfsencodedecodeUnicodeDecodeError
ValueErrorformatwrite
writelines	readlinescloseZ	copy_filenamestatr   chmod)r   r   updated_filesZscriptZadjustZoutfiler   encodinglinesZ
first_liner5   Zpost_interpr   ZshebangZoutffileZoldmodeZnewmoder   r   r   r   5   s    $	
					zbuild_scripts.copy_scriptsN)r
   r   r   )zforcer   r   )r   r   r   )__name__
__module____qualname__ZdescriptionZuser_optionsZboolean_optionsr   r   r   r    r   r   r   r   r   r	      s   		r	   c               @   s   e  Z d  Z d d   Z d S)build_scripts_2to3c             C   s8   t  j |   \ } } |  j s. |  j |  n  | | f S)N)r	   r   r.   Zrun_2to3)r   r   rD   r   r   r   r      s    	zbuild_scripts_2to3.copy_scriptsN)rH   rI   rJ   r   r   r   r   r   rK      s   rK   )__doc__r&   rerB   r   Z	distutilsr   Zdistutils.corer   Zdistutils.dep_utilr   Zdistutils.utilr   r   r   r/   compiler4   r	   rK   r   r   r   r   <module>   s   