U
    pdo                     @   s   d dl Z d dlZd dlZd dlZd dlZd dlZd dlZdddgZdZej	
drXdZne ZG dd deZe jd	d
 Zdd ZdddZdddZdd ZdddZedkre  dS )    NZipAppErrorcreate_archiveget_interpreterz8# -*- coding: utf-8 -*-
import {module}
{module}.{fn}()
winutf-8c                   @   s   e Zd ZdS )r   N)__name__
__module____qualname__ r
   r
   /usr/lib/python3.8/zipapp.pyr   !   s   c              	   c   s8   t | ttjfr.t| |}|V  W 5 Q R X n| V  d S N)
isinstancestrosPathLikeopen)archivemodefr
   r
   r   _maybe_open%   s    r   c                 C   s$   |r d| t d }| | dS )zWrite a shebang line.   #!   
N)encodeshebang_encodingwrite)r   interpreterZshebangr
   r
   r   _write_file_prefix.   s    r   c              
   C   s   t | dZ}|d}|dkr*d}|  t |d&}t|| || t|| W 5 Q R X W 5 Q R X |rt|trt	
|t	|jtjB  dS )z8Copy an application archive, modifying the shebang line.rb   r       wbN)r   readreadliner   r   shutilZcopyfileobjr   r   r   chmodstatst_modeS_IEXEC)r   Znew_archiver   srcZfirst_2Zdstr
   r
   r   _copy_archive5   s    


 r)   Fc              
   C   s  d}t | drt | drd}nt| } |  r4d}|rHt| || dS |  sXtd| d  }|rt|rttd|s|std	d}|r|d
\}	}
}tdd |		dD }tdd |	dD }|
d
kr|r|std| t
j|	|d}|dkr| d}nt |ds"t|}t|d}t|| |rDtjntj}tj|d|d^}| dD ]4}|| }|dks||rf|||  qf|r|d|d W 5 Q R X W 5 Q R X |rt |ds|| jtjB  dS )ab  Create an application archive from SOURCE.

    The SOURCE can be the name of a directory, or a filename or a file-like
    object referring to an existing archive.

    The content of SOURCE is packed into an application archive in TARGET,
    which can be a filename or a file-like object.  If SOURCE is a directory,
    TARGET can be omitted and will default to the name of SOURCE with .pyz
    appended.

    The created application archive will have a shebang line specifying
    that it should run with INTERPRETER (there will be no shebang line if
    INTERPRETER is None), and a __main__.py which runs MAIN (if MAIN is
    not specified, an existing __main__.py will be used).  It is an error
    to specify MAIN for anything other than a directory source with no
    __main__.py, and it is an error to omit MAIN if the directory has no
    __main__.py.
    Fr!   r"   TNzSource does not existz__main__.pyz8Cannot specify entry point if the source has __main__.pyzArchive has no entry point:c                 s   s   | ]}|  V  qd S r   isidentifier.0partr
   r
   r   	<genexpr>{   s     z!create_archive.<locals>.<genexpr>.c                 s   s   | ]}|  V  qd S r   r+   r-   r
   r
   r   r0   |   s     zInvalid entry point: )modulefnz.pyzr   r    w)compression*r   )hasattrpathlibPathis_filer)   existsr   	partitionallsplitMAIN_TEMPLATEformatZwith_suffixr   r   zipfileZZIP_DEFLATEDZ
ZIP_STOREDZZipFileZrglobrelative_tor   Zas_posixZwritestrr   r$   r%   r&   r'   )sourcetargetr   mainfilter
compressedZsource_is_fileZhas_mainZmain_pymodsepr3   Zmod_okZfn_okfdr5   zZchildZarcnamer
   r
   r   r   L   sX    




&c              
   C   sF   t | d2}|ddkr8|  tW  5 Q R  S W 5 Q R X d S )Nr   r   r   )r   r!   r"   stripdecoder   )r   r   r
   r
   r   r      s    c                 C   s<  ddl }| }|jddddd |jdddd	d |jd
dddd |jddddd |jddddd |jddd || } | jrtj| jst	dt
| j}td|pd td tj| jr| jdkstj| jrtj| j| jrt	d| jrt	dt| j| j| j| j| jd dS )zRun the zipapp command line interface.

    The ARGS parameter lets you specify the argument list directly.
    Omitting ARGS (or setting it to None) works as for argparse, using
    sys.argv[1:] as the argument list.
    r   Nz--outputz-ozAThe name of the output archive. Required if SOURCE is an archive.)defaulthelpz--pythonz-pzEThe name of the Python interpreter to use (default: no shebang line).z--mainz-mzLThe main function of the application (default: use an existing __main__.py).z
--compressz-c
store_truezQCompress files with the deflate method. Files are stored uncompressed by default.)actionrO   z--infoFz)Display the interpreter from the archive.)rN   rQ   rO   rC   z'Source directory (or existing archive).)rO   z%Can only get info for an archive filezInterpreter: {}z<none>z-In-place editing of archives is not supportedz,Cannot change the main function when copying)r   rE   rG   )argparseArgumentParseradd_argument
parse_argsinfor   pathisfilerC   
SystemExitr   printr@   sysexitoutputr;   samefilerE   r   Zpythoncompress)argsrR   parserr   r
   r
   r   rE      sN    








 rE   __main__)N)NNNNF)N)
contextlibr   r8   r#   r%   r[   rA   __all__r?   platform
startswithr   getfilesystemencoding
ValueErrorr   contextmanagerr   r   r)   r   r   rE   r   r
   r
   r
   r   <module>   s0   



    
J
1