o
    Uh'                     @   s   d Z ddlZddlZddlZddlmZ ddlmZmZm	Z	m
Z
mZmZmZmZ ddlmZmZ ddlmZ ddlmZmZmZmZmZ erNddlmZmZ G d	d
 d
ZG dd deZdS )z:Handles all file writing and post-installation processing.    N)Path)TYPE_CHECKINGBinaryIO
CollectionDictIterableOptionalTupleUnion)HashRecordEntry)Script)Schemeconstruct_record_filecopyfileobj_with_hashingfix_shebangmake_file_executable)LauncherKindScriptSectionc                
   @   sz   e Zd ZdZdedededddef
dd	Zd
edeedf de	de
def
ddZd
ededeeeef  ddfddZdS )WheelDestinationzHandles writing the unpacked files, script generation and ``RECORD`` generation.

    Subclasses provide the concrete script generation logic, as well as the RECORD file
    (re)writing.
    namemoduleattrsectionr   returnc                 C      t )a  Write a script in the correct location to invoke given entry point.

        :param name: name of the script
        :param module: module path, to load the entry point from
        :param attr: final attribute access, for the entry point
        :param section: Denotes the "entry point section" where this was specified.
            Valid values are ``"gui"`` and ``"console"``.
        :type section: str

        Example usage/behaviour::

            >>> dest.write_script("pip", "pip._internal.cli", "main", "console")

        NotImplementedError)selfr   r   r   r    r   j/var/www/html/construction_image-detection-poc/venv/lib/python3.10/site-packages/installer/destinations.pywrite_script'   s   zWheelDestination.write_scriptschemepathos.PathLike[str]streamis_executablec                 C   r   )a  Write a file to correct ``path`` within the ``scheme``.

        :param scheme: scheme to write the file in (like "purelib", "platlib" etc).
        :param path: path within that scheme
        :param stream: contents of the file
        :param is_executable: whether the file should be made executable

        The stream would be closed by the caller, after this call.

        Example usage/behaviour::

            >>> with open("__init__.py") as stream:
            ...     dest.write_file("purelib", "pkg/__init__.py", stream)

        r   )r   r"   r#   r%   r&   r   r   r    
write_file:   s   zWheelDestination.write_filerecord_file_pathrecordsNc                 C   r   )a  Finalize installation, after all the files are written.

        Handles (re)writing of the ``RECORD`` file.

        :param scheme: scheme to write the ``RECORD`` file in
        :param record_file_path: path of the ``RECORD`` file with that scheme
        :param records: entries to write to the ``RECORD`` file

        Example usage/behaviour::

            >>> dest.finalize_installation("purelib")

        r   )r   r"   r(   r)   r   r   r    finalize_installationR   s   z&WheelDestination.finalize_installation)__name__
__module____qualname____doc__strr   r!   r   r
   r   boolr'   r   r	   r*   r   r   r   r    r       sB    


r   c                   @   s  e Zd ZdZ			d(deeef deddd	ed
ee dee ddfddZ	de
dedefddZde
dedededef
ddZde
deedf dededef
ddZdedededddef
dd Zde
d!eddfd"d#Zde
d$ed%eee
ef  ddfd&d'ZdS ))SchemeDictionaryDestinationz>Destination, based on a mapping of {scheme: file-system-path}.sha256r   Nscheme_dictinterpreterscript_kindr   hash_algorithmbytecode_optimization_levelsdestdirr   c                 C   s(   || _ || _|| _|| _|| _|| _dS )a  Construct a ``SchemeDictionaryDestination`` object.

        :param scheme_dict: a mapping of {scheme: file-system-path}
        :param interpreter: the interpreter to use for generating scripts
        :param script_kind: the "kind" of launcher script to use
        :param hash_algorithm: the hashing algorithm to use, which is a member
            of :any:`hashlib.algorithms_available` (ideally from
            :any:`hashlib.algorithms_guaranteed`).
        :param bytecode_optimization_levels: Compile cached bytecode for
            installed .py files with these optimization levels. The bytecode
            is specific to the minor version of Python (e.g. 3.10) used to
            generate it.
        :param destdir: A staging directory in which to write all files. This
            is expected to be the filesystem root at runtime, so embedded paths
            will be written as though this was the root.
        N)r3   r4   r5   r6   r7   r8   )r   r3   r4   r5   r6   r7   r8   r   r   r    __init__k   s   
z$SchemeDictionaryDestination.__init__r"   r#   c                 C   sF   t j| j| |}| jd ur!t|}||j}t j| j|S |S )N)osr#   joinr3   r8   r   relative_toanchor)r   r"   r#   file	file_pathrel_pathr   r   r    _path_with_destdir   s   
z.SchemeDictionaryDestination._path_with_destdirr%   r&   c                 C   s   |  ||}tj|rd| }t|tj|}tj|s&t| t|d}t||| j	\}	}
W d   n1 s?w   Y  |rJt
| t|t| j	|	|
S )a  Write contents of ``stream`` to the correct location on the filesystem.

        :param scheme: scheme to write the file in (like "purelib", "platlib" etc).
        :param path: path within that scheme
        :param stream: contents of the file
        :param is_executable: whether the file should be made executable

        - Ensures that an existing file is not being overwritten.
        - Hashes the written content, to determine the entry in the ``RECORD`` file.
        zFile already exists: wbN)rA   r:   r#   existsFileExistsErrordirnamemakedirsopenr   r6   r   r   r   )r   r"   r#   r%   r&   target_pathmessageparent_folderfhash_sizer   r   r    write_to_fs   s   

z'SchemeDictionaryDestination.write_to_fsr$   c                 C   s^   t |}|dkr't|| j}| ||||W  d   S 1 s"w   Y  | ||||S )a  Write a file to correct ``path`` within the ``scheme``.

        :param scheme: scheme to write the file in (like "purelib", "platlib" etc).
        :param path: path within that scheme
        :param stream: contents of the file
        :param is_executable: whether the file should be made executable

        - Changes the shebang for files in the "scripts" scheme.
        - Uses :py:meth:`SchemeDictionaryDestination.write_to_fs` for the
          filesystem interaction.
        scriptsN)r:   fspathr   r4   rN   )r   r"   r#   r%   r&   path_stream_with_different_shebangr   r   r    r'      s   
 z&SchemeDictionaryDestination.write_filer   r   r   r   r   c                 C   s   t ||||}|| j| j\}}t|1}| jtd||dd}	| td|}
t	
|
j}||d@ d? O }t	|
| |	W  d   S 1 sJw   Y  dS )aX  Write a script to invoke an entrypoint.

        :param name: name of the script
        :param module: module path, to load the entry point from
        :param attr: final attribute access, for the entry point
        :param section: Denotes the "entry point section" where this was specified.
            Valid values are ``"gui"`` and ``"console"``.
        :type section: str

        - Generates a launcher using :any:`Script.generate`.
        - Writes to the "scripts" scheme.
        - Uses :py:meth:`SchemeDictionaryDestination.write_to_fs` for the
          filesystem interaction.
        rO   Tr&   i$     N)r   generater4   r5   ioBytesIOrN   r   rA   r:   statst_modechmod)r   r   r   r   r   scriptscript_namedatar%   entryr#   moder   r   r    r!      s   $z(SchemeDictionaryDestination.write_scriptrecordc                 C   sZ   |dvrdS |  ||j}tjtj| j| |j}| jD ]}tj||d|d qdS )z'Compile bytecode for a single .py file.)purelibplatlibN   )optimizequietddir)	rA   r#   r:   rE   r;   r3   r7   
compileallcompile_file)r   r"   r`   rH   dir_path_to_embedlevelr   r   r    _compile_bytecode   s   
z-SchemeDictionaryDestination._compile_bytecoder(   r)   c                    s~   dt dtt  f fdd}t|}t||}j ||dd W d   n1 s+w   Y  |D ]
\ } | q2dS )a!  Finalize installation, by writing the ``RECORD`` file & compiling bytecode.

        :param scheme: scheme to write the ``RECORD`` file in
        :param record_file_path: path of the ``RECORD`` file with that scheme
        :param records: entries to write to the ``RECORD`` file
        file_schemer   c                    s0   |  krd S t jjj|  j  d}|d S )N)start/)r:   r#   relpathr3   )rl   r#   r"   r   r   r    prefix_for_scheme  s   zLSchemeDictionaryDestination.finalize_installation.<locals>.prefix_for_schemeFrS   N)r/   r   listr   rN   rk   )r   r"   r(   r)   rq   record_listrecord_streamr`   r   rp   r    r*      s   	z1SchemeDictionaryDestination.finalize_installation)r2   r   N)r+   r,   r-   r.   r   r/   r   intr   r9   r   rA   r   r0   r   rN   r
   r'   r!   rk   r   r	   r*   r   r   r   r    r1   h   s    

 
"


 r1   )r.   rg   rV   r:   pathlibr   typingr   r   r   r   r   r   r	   r
   installer.recordsr   r   installer.scriptsr   installer.utilsr   r   r   r   r   r   r   r   r1   r   r   r   r    <module>   s    (H