3
ûâde8E  ã               @   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mZ ejeƒZdjƒ ZejdƒZd	Zd
d„ ZeZG dd„ deƒZdS )é    )ÚBytesIONé   )Ú	sysconfigÚdetect_encodingÚZipFile)Úfinder)ÚFileOperatorÚget_export_entryÚconvert_pathÚget_executableÚget_platformÚ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   sZ   d| krV| j dƒrD| jddƒ\}}d|krV|j dƒ rVd||f } n| j dƒsVd|  } | S )Nú z/usr/bin/env r   ú"z%s "%s"z"%s")Ú
startswithÚsplit)Ú
executableÚenvÚ_executable© r   ú:/tmp/pip-build-red6nood/pip/pip/_vendor/distlib/scripts.pyÚenquote_executable3   s    

r   c               @   sæ   e Zd ZdZeZdZd*dd„Zdd„ Ze	j
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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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| _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ƒr<| jr<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   sL   y"t |ƒ�}|jdƒdkS Q R X W n$ ttfk
rF   tj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_shellg   s    
zScriptMaker._is_shellc             C   sD   | j |ƒr*dd l}|jjjdƒdkr<|S n|jƒ jdƒr<|S d| S )Nr   zos.nameÚLinuxz
jython.exez/usr/bin/env %s)rD   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}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$   Úlenr+   Ú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jƒ s&tƒ }nxtƒ rLtjjtjdƒdtj	dƒ ƒ}nRtjjtj	dƒdtj	dƒtj	dƒf ƒ}tjj
|ƒsžtjjtj	dƒdtj	dƒ ƒ}|r®| j||ƒ}tjjd	ƒrÄ| j|ƒ}|rÐt|ƒ}|jd
ƒ}tjdkoòd|koòd|k� rþ|d7 }| j||ƒ}y|jd
ƒ W n" tk
�r:   td| ƒ‚Y nX |d
k�r|y|j|ƒ 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#   r5   r7   Úget_pathÚget_config_varÚisfiler;   r+   rR   r   rK   r   ÚencoderX   ÚdecodeÚUnicodeDecodeErrorÚ
ValueError)r-   ÚencodingrS   r8   Úenquoter   Úshebangr   r   r   Ú_get_shebangž   sP    




zScriptMaker._get_shebangc             C   s$   | j t|j|jjdƒd |jd� S )NÚ.r   )ÚmoduleÚimport_nameÚ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#   r5   Ú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 }|s8|| }nZ|dkrL| jdƒ}n
| jdƒ}tƒ }	t|	dƒ�}
|
j	d|ƒ W d Q R X |	j
ƒ }|| | }�xd|D �]Z}tjj| j|ƒ}|�r|tjj|ƒ\}}|jdƒrÔ|}d| }y| jj||ƒ W nŠ tk
�rx   tjdƒ d	| }tjj|ƒ�r*tj|ƒ tj||ƒ | jj||ƒ tjd
ƒ ytj|ƒ W n tk
�rr   Y nX Y nX np| j�r¢|jd| ƒ �r¢d||f }tjj|ƒ�rÈ| j �rÈtjd|ƒ qš| jj||ƒ | j�rì| jj|gƒ |j|ƒ qšW 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 logicrk   z%s.%szSkipping existing file %s)r    r*   r#   Úlineseprc   rJ   Ú_get_launcherr   r   ÚwritestrÚgetvaluer5   r7   r   Úsplitextr   r)   Úwrite_binary_fileÚ	ExceptionrA   rB   ÚexistsÚremoveÚrenameÚdebugr"   r&   Úset_executable_modeÚappend)r-   Únamesri   Úscript_bytesÚ	filenamesÚextÚuse_launcherr}   ÚlauncherÚstreamÚzfÚzip_datar$   ÚoutnameÚnÚeÚdfnamer   r   r   Ú_write_scriptî   sX    






zScriptMaker._write_scriptú-c             C   sn   t ƒ }d| jkr|j|ƒ d| jkr<|jd|| jd f ƒ d| jkrj|jd|| j| jd | jd f ƒ |S )Nr   ÚXz%s%sr   zX.Yz	%s%s%s.%sr   )r'   r(   Úaddr,   Úvariant_separator)r-   r$   rW   r   r   r   Úget_script_filenames$  s    



z ScriptMaker.get_script_filenamesc       
      C   sŒ   d}|r0|j dg ƒ}|r0ddj|ƒ }|jdƒ}| jd||d�}| j|ƒjdƒ}| j|jƒ}|rr|j ddƒrrd	}	nd
}	| j|||||	ƒ d S )NrY   Úinterpreter_argsz %sr   zutf-8)r8   r1   FÚpywrz   )r4   r7   rc   rj   rt   rœ   r$   r—   )
r-   rs   rŒ   r8   rS   Úargsri   ÚscriptÚ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 rX| jj	||ƒ rXt
jd|ƒ d S yt|dƒ}W n  tk
r†   | js~‚ d }Y nFX |jƒ }|s¤t
jd|ƒ d S tj|jddƒƒ}|rÌd}|jdƒpÊd	}|�s|rÞ|jƒ  | jj||ƒ | j�r| 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 is an empty file (skipping)s   
rP   Tr   rY   zcopying and adjusting %s -> %sr   s   pythonwrž   rz   ) r#   r5   r7   r   r
   r   ru   r!   r)   ÚnewerrA   r‡   r=   r@   r.   ÚreadlinerB   ÚFIRST_LINE_REÚmatchr6   ÚgroupÚcloseÚ	copy_filer&   rˆ   r‰   Úinfor   Úseekrj   r—   r>   )r-   r    rŒ   Úadjustr“   ÚfÚ
first_liner§   rS   rg   Úlinesri   r�   r”   r   r   r   Ú_copy_script?  sP    



zScriptMaker._copy_scriptc             C   s   | j jS )N)r)   r.   )r-   r   r   r   r.   r  s    zScriptMaker.dry_runc             C   s   || j _d S )N)r)   r.   )r-   Úvaluer   r   r   r.   v  s    r   c             C   st   t jdƒdkrd}nd}tƒ dkr&dnd}d|||f }tjd	d
ƒd }t|ƒj|ƒ}|snd||f }t|ƒ‚|jS )NÚPé   Z64Z32z	win-arm64z-armr   z
%s%s%s.exerk   r   r   z(Unable to find resource %s in package %s)	ÚstructÚcalcsizer   Ú__name__Úrsplitr   Úfindrf   Úbytes)r-   ÚkindÚbitsZplatform_suffixr$   Zdistlib_packageÚresourceÚmsgr   r   r   r~   ~  s    zScriptMaker._get_launcherc             C   s6   g }t |ƒ}|dkr"| 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.
        N)r8   )r	   r±   r¢   )r-   Úspecificationr8   rŒ   rs   r   r   r   Úmake‘  s    zScriptMaker.makec             C   s(   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-   Úspecificationsr8   rŒ   r¿   r   r   r   Úmake_multiple¤  s    
zScriptMaker.make_multiple)TFN)rY   N)N)N)N)"r·   Ú
__module__Ú__qualname__Ú__doc__ÚSCRIPT_TEMPLATEro   r   r0   r;   r+   rR   r   rD   rK   rX   rj   rt   Ú_DEFAULT_MANIFESTrv   ry   r—   r›   rœ   r¢   r±   Úpropertyr.   Úsetterr#   r$   r%   r~   rÀ   rÃ   r   r   r   r   r   E   s2    

E4
3
r   ) Úior   Úloggingr#   Úrerµ   r+   Úcompatr   r   r   Ú	resourcesr   Úutilr   r	   r
   r   r   r   Ú	getLoggerr·   rA   ÚstriprÈ   Úcompiler¦   rÇ   r   Ú_enquote_executableÚobjectr   r   r   r   r   Ú<module>   s    

