B
    Kkd@                 @   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eZd ZedZd	Zd
d ZG d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.]*([ 	].*)?$a|  # -*- coding: utf-8 -*-
if __name__ == '__main__':
    import sys, re

    def _resolve(module, func):
        __import__(module)
        mod = sys.modules[module]
        parts = func.split('.')
        result = getattr(mod, parts.pop(0))
        for p in parts:
            result = getattr(result, p)
        return result

    try:
        sys.argv[0] = re.sub(r'(-script\.pyw?|\.exe)?$', '', sys.argv[0])

        func = _resolve('%(module)s', '%(func)s')
        rc = func() # None interpreted as 0
    except Exception as e:  # only supporting Python >= 2.6
        sys.stderr.write('%%s\n' %% e)
        rc = 1
    sys.exit(rc)
c             C   sX   d| krT|  drB| dd\}}d|krT| dsTd||f } n|  dsTd|  } | S )N z/usr/bin/env r   "z%s "%s"z"%s")
startswithsplit)
executableenv_executable r   </tmp/pip-install-gxxfd9b7/pip/pip/_vendor/distlib/scripts.py_enquote_executableB   s    

r   c               @   s   e Zd ZdZeZdZd'ddZdd Ze	j
d	rBd
d Zdd Zdd Z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sejd	krejd krd!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   sz   || _ || _|| _d| _d| _tjdkp:tjdko:tjdk| _t	d| _
|pRt|| _tjdkprtjdkortjdk| _d S )NFposixjava) zX.Ynt)
source_dir
target_diradd_launchersforceclobberosname_nameset_modesetvariantsr   _fileop_is_nt)selfr   r   r   dry_runfileopr   r   r   __init__[   s    

zScriptMaker.__init__c             C   s@   | ddr<| jr<tj|\}}|dd}tj||}|S )NguiFpythonpythonw)getr(   r!   pathr   replacejoin)r)   r   optionsdnfnr   r   r   _get_alternate_executablek   s
    z%ScriptMaker._get_alternate_executabler   c          	   C   sL   y"t |}|ddkS Q R X W n$ ttfk
rF   td| dS X d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_shells   s    
zScriptMaker._is_shellc             C   sD   |  |r*dd l}|jjddkr<|S n| dr<|S d| S )Nr   zos.nameLinuxz
jython.exez/usr/bin/env %s)r@   r   langSystemgetPropertylowerendswith)r)   r   r   r   r   r   _fix_jython_executable   s    
z"ScriptMaker._fix_jython_executablec             C   s   t jdkrd}n8t|t| d }tjdkr4d}nd}d|koF||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
   #!/bin/sh
s	   '''exec' s    "$0" "$@"
s   ' ''')r!   r"   lensysplatform)r)   r   post_interpsimple_shebangshebang_lengthmax_shebang_lengthresultr   r   r   _build_shebang   s    

zScriptMaker._build_shebang    c             C   s`  d}| j r| j }d}n^t s&t }nNt rLtjtddt	d }n(tjt	ddt	dt	df }|r| 
||}tjd	r| |}tj|}|rt|}|d
}tjdkrd|krd|kr|d7 }| ||}y|d
 W n" tk
r   td| Y nX |d
kr\y|| W n& tk
rZ   td||f Y n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!   r1   r3   get_pathget_config_varr7   rN   rO   r   rG   normcaser   encoderU   decodeUnicodeDecodeError
ValueError)r)   encodingrP   r4   enquoter   shebangr   r   r   _get_shebang   sL    



zScriptMaker._get_shebangc             C   s   | j t|j|jd S )N)modulefunc)script_templatedictprefixsuffix)r)   entryr   r   r   _get_script_text   s    
zScriptMaker._get_script_textc             C   s   t j|}| j| S )N)r!   r1   basenamemanifest)r)   exenamebaser   r   r   get_manifest   s    zScriptMaker.get_manifestc             C   s  | j o
| j}tjd}||s*||7 }|s8|| }nZ|dkrL| d}n
| d}t }	t|	d}
|
	d| W d Q R X |	
 }|| | }x`|D ]V}tj| j|}|r|tj|\}}|dr|}d| }y| j|| W n tk
rx   td d	| }tj|r*t| t|| | j|| td
 yt| W n tk
rr   Y nX Y nX nl| jr|d| sd||f }tj|r| jstd| q| j|| | jr| j|g || qW d 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 logic.z%s.%szSkipping existing file %s)r   r(   r!   linesepr`   rF   _get_launcherr   r   writestrgetvaluer1   r3   r   splitextr   r'   write_binary_file	Exceptionr=   r>   existsremoverenamedebugr    r$   set_executable_modeappend)r)   namesrf   script_bytes	filenamesextuse_launcherry   launcherstreamzfzip_datar"   outnamenedfnamer   r   r   _write_script   sX    






zScriptMaker._write_scriptc             C   s   d}|r0| dg }|r0dd| }|d}| jd||d}| |d}|j}t }	d| jkrp|	| d| jkr|	d	|t	j
d
 f  d| jkr|	d|t	j
d d f  |r| ddrd}
nd}
| |	||||
 d S )NrV   interpreter_argsz %sr   zutf-8)r4   r   Xz%s%sr   zX.Yz%s-%srH   r-   Fpywru   )r0   r3   r`   rg   ro   r"   r%   r&   addrN   versionr   )r)   rn   r   r4   rP   argsrf   scriptr"   scriptnamesr   r   r   r   _make_script   s(    




zScriptMaker._make_scriptc             C   s  d}t j| jt|}t j| jt j|}| jsT| j	||sTt
d| d S yt|d}W n  tk
r   | jsz d }Y nLX | }|st
d|  | d S t|dd}|rd}|dpd	}|s|r|  | j|| | jr| j|g || nt
d
|| j | jjst|j\}	}
|d | |	|}d|kr^d}nd}t j|}| |g||  || |r|  d S )NFznot copying %s (up-to-date)rbz"%s: %s is an empty file (skipping)s   
rL   Tr   rV   zcopying and adjusting %s -> %sr   s   pythonwr   ru   )!r!   r1   r3   r   r
   r   rp   r   r'   newerr=   r   r9   r<   r*   readliner>   get_command_nameFIRST_LINE_REmatchr2   groupclose	copy_filer$   r   r   infor   seekrg   r   r:   )r)   r   r   adjustr   f
first_liner   rP   rd   linesrf   r   r   r   r   r   _copy_script7  sR    



zScriptMaker._copy_scriptc             C   s   | j jS )N)r'   r*   )r)   r   r   r   r*   k  s    zScriptMaker.dry_runc             C   s   || j _d S )N)r'   r*   )r)   valuer   r   r   r*   o  s    r   c             C   sH   t ddkrd}nd}d||f }tddd }t||j}|S )	NP   Z64Z32z%s%s.exerx   r   r   )structcalcsize__name__rsplitr   findbytes)r)   kindbitsr"   Zdistlib_packagerT   r   r   r   rz   w  s    zScriptMaker._get_launcherc             C   s6   g }t |}|dkr"| || 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.
        N)r4   )r	   r   r   )r)   specificationr4   r   rn   r   r   r   make  s    zScriptMaker.makec             C   s(   g }x|D ]}| | || 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)   specificationsr4   r   r   r   r   r   make_multiple  s    
zScriptMaker.make_multiple)TFN)rV   N)N)N)N) r   
__module____qualname____doc__SCRIPT_TEMPLATErj   r   r,   r7   rN   rO   r   r@   rG   rU   rg   ro   _DEFAULT_MANIFESTrq   rt   r   r   r   propertyr*   setterr!   r"   r#   rz   r   r   r   r   r   r   r   R   s.    

84
4
r   )ior   loggingr!   rer   rN   compatr   r   r   	resourcesr   utilr   r	   r
   r   r   	getLoggerr   r=   stripr   compiler   r   r   objectr   r   r   r   r   <module>   s   

