
0c@_C                 @   s   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 m	 Z	 m
 Z
 d d l m Z d d l m Z m Z m Z m Z m Z e j e  Z d j   Z e j d  Z d	 Z d
 d   Z e Z Gd d   d e  Z d S)    )BytesION   )	sysconfigdetect_encodingZipFile)finder)FileOperatorget_export_entryconvert_pathget_executablein_venva  
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
 <assemblyIdentity version="1.0.0.0"
 processorArchitecture="X86"
 name="%s"
 type="win32"/>

 <!-- Identify the application security requirements. -->
 <trustInfo xmlns="urn:schemas-microsoft-com:asm.v3">
 <security>
 <requestedPrivileges>
 <requestedExecutionLevel level="asInvoker" uiAccess="false"/>
 </requestedPrivileges>
 </security>
 </trustInfo>
</assembly>s   ^#!.*pythonw?[0-9.]*([ 	].*)?$z# -*- coding: utf-8 -*-
import re
import sys
from %(module)s import %(import_name)s
if __name__ == '__main__':
    sys.argv[0] = re.sub(r'(-script\.pyw|\.exe)?$', '', sys.argv[0])
    sys.exit(%(func)s())
c             C   s   d |  k r{ |  j  d  rb |  j d d  \ } } d | k r{ | j  d  r{ d | | f }  n |  j  d  s{ d |  }  |  S)N z/usr/bin/env r   "z%s "%s"z"%s")
startswithsplit)
executableenv_executable r   :/tmp/pip-build-jynh7p1z/pip/pip/_vendor/distlib/scripts.pyenquote_executable3   s    
r   c               @   sN  e  Z d  Z d Z e Z d Z d d d d d  Z d d   Z e	 j
 j d	  ri d
 d   Z d d   Z d d   Z d d d d  Z d d   Z e Z d d   Z d d   Z d d d  Z d d   Z e d d    Z e j d d    Z e j d  k s e j d	 k r,e j d  k r,d! d"   Z d d# d$  Z d d% d&  Z d S)'ScriptMakerz_
    A class to copy or create scripts from source scripts or callable
    specifications.
    NTFc             C   s   | |  _  | |  _ | |  _ d |  _ d |  _ t j d k pW t j d k oW t j d k |  _ t	 d  |  _
 | p{ t |  |  _ t j d k p t j d k o t j d k |  _ t j |  _ d  S)NFposixjava X.Ynt)r   r   )
source_dir
target_diradd_launchersforceclobberosname_nameset_modesetvariantsr   _fileop_is_ntsysversion_info)selfr   r   r   dry_runfileopr   r   r   __init__N   s    					!zScriptMaker.__init__c             C   s^   | j  d d  rZ |  j rZ t j j |  \ } } | j d d  } t j j | |  } | S)NguiFpythonpythonw)getr)   r"   pathr   replacejoin)r,   r   optionsdnfnr   r   r   _get_alternate_executable_   s
    z%ScriptMaker._get_alternate_executabler   c             C   s`   y- t  |   } | j d  d k SWd QRXWn, t t f k
 r[ t j d |  d SYn Xd S)zl
            Determine if the specified executable is a script
            (contains a #! line)
               z#!NzFailed to open %sF)openreadOSErrorIOErrorloggerwarning)r,   r   fpr   r   r   	_is_shellg   s    zScriptMaker._is_shellc             C   s^   |  j  |  r= d d  l } | j j j d  d k rV | Sn | j   j d  rV | Sd | S)Nr   zos.nameLinuxz
jython.exez/usr/bin/env %s)rC   r   langSystemgetPropertylowerendswith)r,   r   r   r   r   r   _fix_jython_executables   s    z"ScriptMaker._fix_jython_executablec             C   s   t  j d k r d } nP t |  t |  d } t j d k rJ d } n d } d | k oe | | k } | r d | | d	 } n& d
 } | d | | d 7} | d 7} | S)a  
        Build a shebang line. In the simple case (on Windows, or a shebang line
        which is not too long or contains spaces) use a simple formulation for
        the shebang. Otherwise, use /bin/sh as the executable, with a contrived
        shebang which allows the script to run either under Python or sh, using
        suitable quoting. Thanks to Harald Nordgren for his input.

        See also: http://www.in-ulm.de/~mascheck/various/shebang/#length
                  https://hg.mozilla.org/mozilla-central/file/tip/mach
        r   T   darwini          s   #!s   
s
   #!/bin/sh
s	   '''exec' s    "$0" "$@"
s   ' ''')r"   r#   lenr*   platform)r,   r   post_interpZsimple_shebangZshebang_lengthZmax_shebang_lengthresultr   r   r   _build_shebang   s    		
zScriptMaker._build_shebang    c             C   s  d } |  j  r! |  j  } d } n t j   s9 t   } nq t   rp t j j t j d  d t j	 d   } n: t j j t j	 d  d t j	 d  t j	 d  f  } | r |  j
 | |  } t j j d	  r |  j |  } | r t |  } | j d
  } t j d k r5d | k r5d | k r5| d 7} |  j | |  } y | j d
  Wn" t k
 r|t d |   Yn X| d
 k ry | j |  Wn( t k
 rt d | | f   Yn X| S)NTFscriptszpython%sEXEBINDIRz
python%s%sVERSIONr   zutf-8cliz	-X:Framesz-X:FullFramess
    -X:Framesz,The shebang (%r) is not decodable from utf-8z?The shebang (%r) is not decodable from the script encoding (%r))r   r   is_python_buildr   r   r"   r4   r6   get_pathget_config_varr:   r*   rP   r   rJ   r   encoderS   decodeUnicodeDecodeError
ValueError)r,   encodingrQ   r7   enquoter   shebangr   r   r   _get_shebang   sJ    					
zScriptMaker._get_shebangc             C   s6   |  j  t d | j d | j j d  d d | j  S)Nmoduleimport_name.r   func)script_templatedictprefixsuffixr   )r,   entryr   r   r   _get_script_text   s    zScriptMaker._get_script_textc             C   s   t  j j |  } |  j | S)N)r"   r4   basenamemanifest)r,   exenamebaser   r   r   get_manifest   s    zScriptMaker.get_manifestc             C   s  |  j  o |  j } t j j d  } | j |  s= | | 7} | sP | | } ny | d k rn |  j d  } n |  j d  } t   }	 t |	 d   }
 |
 j	 d |  Wd  QRX|	 j
   } | | | } x| D]} t j j |  j |  } | rt j j |  \ } } | j d  r!| } d | } y |  j j | |  Wqqt k
 rt j d  d	 | } t j j |  rt j |  t j | |  |  j j | |  t j d
  y t j |  Wn t k
 rYn XYqqXn |  j r| j d |  rd | | f } t j j |  rB|  j rBt j d |  q |  j j | |  |  j rq|  j j | g  | j |  q Wd  S)Nzutf-8pytwz__main__.pyz.pyz%s.exez:Failed to write executable - trying to use .deleteme logicz%s.deletemez0Able to replace executable using .deleteme logicrg   z%s.%szSkipping existing file %s)r   r)   r"   linesepr]   rI   _get_launcherr   r   writestrgetvaluer4   r6   r   splitextr   r(   write_binary_file	Exceptionr@   rA   existsremoverenamedebugr!   r%   set_executable_modeappend)r,   namesrc   script_bytes	filenamesextuse_launcherrw   launcherstreamzfzip_datar#   outnamenedfnamer   r   r   _write_script   sX    
	

	zScriptMaker._write_scriptc             C   sL  d } | rF | j  d g   } | rF d d j |  } | j d  } |  j d | d | } |  j |  j d  } | j } t   }	 d |  j k r |	 j |  d |  j k r |	 j d	 | |  j	 d
 f  d |  j k r|	 j d | |  j	 d
 |  j	 d f  | r)| j  d d  r)d }
 n d }
 |  j
 |	 | | | |
  d  S)NrT   interpreter_argsz %sr   zutf-8r7   r   Xz%s%sr   zX.Yz%s-%s.%sr   r0   Fpywrt   )r3   r6   r]   rd   rn   r#   r&   r'   addr+   r   )r,   rm   r   r7   rQ   argsrc   scriptr#   scriptnamesr   r   r   r   _make_script  s*    			zScriptMaker._make_scriptc             C   s/  d } t  j j |  j t |   } t  j j |  j t  j j |   } |  j r| |  j j	 | |  r| t
 j d |  d  Sy t | d  } Wn$ t k
 r |  j s   d  } Ynl X| j   } | s t
 j d |  j   |  d  St j | j d d   } | r!d } | j d  pd	 } | sv| r7| j   |  j j | |  |  j rf|  j j | g  | j |  n t
 j d
 | |  j  |  j j st | j  \ }	 }
 | j d  |  j |	 |  } d | k rd } n d } t  j j |  } |  j | g | | j    | |  | r+| j   d  S)NFznot copying %s (up-to-date)rbz"%s: %s is an empty file (skipping)s   
s   
Tr   rT   zcopying and adjusting %s -> %sr   s   pythonwr   rt   )!r"   r4   r6   r   r
   r   ro   r    r(   newerr@   r   r<   r?   r-   readlinerA   get_command_nameFIRST_LINE_REmatchr5   groupclose	copy_filer%   r   r   infor   seekrd   r   r=   )r,   r   r   adjustr   f
first_liner   rQ   ra   linesrc   r   r   r   r   r   _copy_script5  sR    $ 		
	
	"zScriptMaker._copy_scriptc             C   s
   |  j  j S)N)r(   r-   )r,   r   r   r   r-   i  s    zScriptMaker.dry_runc             C   s   | |  j  _ d  S)N)r(   r-   )r,   valuer   r   r   r-   m  s    r   c             C   s   t  j d  d k r d } n d } d | | f } t j d d  d } t |  j |  } | s d	 | | f } t |   | j S)
NP   Z64Z32z%s%s.exerg   r   r   z(Unable to find resource %s in package %s)structcalcsize__name__rsplitr   findr`   bytes)r,   kindbitsr#   Zdistlib_packageresourcemsgr   r   r   rx   u  s    	
zScriptMaker._get_launcherc             C   sK   g  } t  |  } | d k r1 |  j | |  n |  j | | d | | S)a  
        Make a script.

        :param specification: The specification, which is either a valid export
                              entry specification (to make a script from a
                              callable) or a filename (to make a script by
                              copying from a source location).
        :param options: A dictionary of options controlling script generation.
        :return: A list of all absolute pathnames written to.
        Nr7   )r	   r   r   )r,   specificationr7   r   rm   r   r   r   make  s    zScriptMaker.makec             C   s4   g  } x' | D] } | j  |  j | |   q W| S)z
        Take a list of specifications and make scripts from them,
        :param specifications: A list of specifications.
        :return: A list of all absolute pathnames written to,
        )extendr   )r,   specificationsr7   r   r   r   r   r   make_multiple  s    zScriptMaker.make_multiple) r   
__module____qualname____doc__SCRIPT_TEMPLATEri   r   r/   r:   r*   rP   r   rC   rJ   rS   rd   rn   _DEFAULT_MANIFESTrp   rs   r   r   r   propertyr-   setterr"   r#   r$   rx   r   r   r   r   r   r   r   E   s.   @44-r   )ior   loggingr"   rer   r*   compatr   r   r   	resourcesr   utilr   r	   r
   r   r   	getLoggerr   r@   stripr   compiler   r   r   _enquote_executableobjectr   r   r   r   r   <module>   s   (