o
    Âif0%  ã                   @   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lZd dlZd dl	m
Z
 d dlZzd dlmZ W n eyE   d dlmZ Y nw ddlmZmZ dddd	œZddd„Zdd„ Zdd„ Zdd„ Zdd„ Z			
		dde
edf de
edf dedede
edf ddfdd„ZdS ) é    N)ÚUnion)Úurlopené   )ÚloggerÚ_check_log_handlerz~\AppData\Local\Pandocz~/binz~/Applications/pandoc)Úwin32ÚlinuxÚdarwinÚlatestc              
      sü   d| dkrdnd |  }zt |ƒ}|j d¡}|d } W n tjjy1 } ztd | ¡ƒ‚d}~ww t d	| › ƒ}| ¡ }t	 
¡ d
 }| d¡sM| d¡rOdnd}t d| d ¡}| | d¡¡}	|	d  d¡d } ddddœ‰ ‡ fdd„|	D ƒ}
|
| fS )aP  Get the urls of pandoc's binaries
    Uses sys.platform keys, but removes the 2 from linux2
    Adding a new platform means implementing unpacking in "DownloadPandocCommand"
    and adding the URL here

    :param str version: pandoc version.
        Valid values are either a valid pandoc version e.g. "1.19.1", or "latest"
        Default: "latest".

    :return: str pandoc_urls: a dictionary with keys as system platform
        and values as the url pointing to respective binaries

    :return: str version: actual pandoc version. (e.g. "latest" will be resolved to the actual one)
    z'https://github.com/jgm/pandoc/releases/r
   ztag/Ú ú/éÿÿÿÿzInvalid pandoc version {}.Nz7https://github.com/jgm/pandoc/releases/expanded_assets/é   ÚarmÚaarchÚamdz#/jgm/pandoc/releases/download/.*(?:z|x86|mac).*\.(?:msi|deb|pkg)zutf-8r   é   r   r   r	   )ÚmsiÚdebÚpkgc                    s$   i | ]}ˆ |d d…  d|› “qS )éýÿÿÿNzhttps://github.com© )Ú.0Úurl_frag©Úext2platformr   úQ/var/www/html/corbot_env/lib/python3.10/site-packages/pypandoc/pandoc_download.pyÚ
<dictcomp>J   s   $ z$_get_pandoc_urls.<locals>.<dictcomp>)r   ÚurlÚsplitÚurllibÚerrorÚ	HTTPErrorÚRuntimeErrorÚformatÚreadÚplatformÚunameÚ
startswithÚreÚcompileÚfindallÚdecode)Úversionr   ÚresponseÚversion_url_fragsÚeÚcontentr'   Úprocessor_architectureÚregexÚpandoc_urls_listÚpandoc_urlsr   r   r   Ú_get_pandoc_urls   s4   ÿÿ€ÿýr6   c                 C   s>   t  | ¡j}||d@ d? O }t d| › d¡ t  | |¡ d S )Ni$  é   zMaking z executable...)ÚosÚstatÚst_moder   ÚinfoÚchmod)ÚpathÚmoder   r   r   Ú_make_executableN   s   r?   c           
   	   C   sœ  t  d| › d¡ t ¡ }t ¡ }tj | ¡} z«t |¡ dd| g}t	 
|¡ t d¡}tdd„ |D ƒƒ}dd	|g}t	 
|¡ d
}tj |dd|¡}tj ||¡}	t  d|› d|› d¡ t ||	¡ t|	ƒ d}tj |dd|¡}tj ||¡}	tj |¡r•t  d|› d|› d¡ t ||	¡ t|	ƒ tj |dddd
d¡}tj |d¡}	t  d|› d¡ t ||	¡ W t |¡ t |¡ d S t |¡ t |¡ w )Nú
Unpacking ú to tempfolder...ÚarÚxÚ.c                 s   s    | ]
}|  d ¡r|V  qdS )zdata.tarN)r(   )r   rC   r   r   r   Ú	<genexpr>a   s   € z _handle_linux.<locals>.<genexpr>ÚtarÚxfÚpandocÚusrÚbinúCopying ú to ú ...úpandoc-citeprocÚshareÚdocÚ	copyrightzcopyright.pandoczCopying copyright to )r   r;   ÚtempfileÚmkdtempr8   Úgetcwdr=   ÚabspathÚchdirÚ
subprocessÚ
check_callÚlistdirÚnextÚjoinÚshutilÚcopyfiler?   ÚexistsÚrmtree)
ÚfilenameÚtargetfolderÚ
tempfolderÚcur_wdÚcmdÚfilesÚarchive_nameÚexeÚsrcÚdstr   r   r   Ú_handle_linuxU   sB   







ÿrj   c                 C   s*  t  d| › d¡ t ¡ }tj |d¡}dd| |g}t |¡ ddtj |dd	¡d
|g}t |¡ d}tj |ddd|¡}tj ||¡}t  d|› d|› d¡ t	 
||¡ t|ƒ d}tj |ddd|¡}tj ||¡}tj |¡r‰t  d|› d|› d¡ t	 
||¡ t|ƒ t	 |¡ t  d¡ d S )Nr@   rA   ÚtmpÚpkgutilz--expandrF   Úxvfz
pandoc.pkgÚPayloadz-CrH   rI   ÚlocalrJ   rK   rL   rM   rN   úDone.)r   r;   rR   rS   r8   r=   r[   rW   rX   r\   r]   r?   r^   r_   )r`   ra   rb   Úpkgutilfolderrd   rg   rh   ri   r   r   r   Ú_handle_darwin{   s0   
ÿ

rr   c                 C   s,  t  d| › d¡ t ¡ }dd| dd| g}t |¡ d}tj |d|¡}tj ||¡}t  d	|› d
|› d¡ t	 
||¡ d}tj |d|¡}tj ||¡}tj |¡rgt  d	|› d
|› d¡ t	 
||¡ d}tj |d|¡}tj ||¡}t  d	|› d
|› d¡ t	 
||¡ t	 |¡ t  d¡ d S )Nr@   rA   Úmsiexecz/az/qbzTARGETDIR=%sz
pandoc.exeÚPandocrK   rL   rM   zpandoc-citeproc.exezCOPYRIGHT.txtrp   )r   r;   rR   rS   rW   rX   r8   r=   r[   r\   r]   r^   r_   )r`   ra   rb   rd   rg   rh   ri   r   r   r   Ú_handle_win32    s,   

ru   Fr   ra   r-   Údelete_installerÚdownload_folderÚreturnc                 C   s˜  t ƒ  tj}| du r5| d¡r#d}t ¡ d }|dkr#td|› dƒ‚t|ƒ\}}||vr1tdƒ‚|| } |  d¡d	 }	|durV| d¡rK|dd	… }t	j
 t	j
 |¡|	¡}	t	j
 |	¡ret d
|	› ¡ n(t d| › d¡ t| ƒ}
t|	dƒ}t |
|¡ W d  ƒ n1 sˆw   Y  |du r•t| }t	j
 |¡}zt	 |¡ W n	 ty«   Y nw tƒ  d| ¡}|dus¼J dƒ‚||	|ƒ |rÊt	 |	¡ dS dS )aS  Download and unpack pandoc

    Downloads prebuild binaries for pandoc from `url` and unpacks it into
    `targetfolder`.

    :param str url: URL for the to be downloaded pandoc binary distribution for
        the platform under which this python runs. If no `url` is give, uses
        the latest available release at the time pypandoc was released.

    :param str targetfolder: directory, where the binaries should be installed
        to. If no `targetfolder` is given, uses a platform specific user
        location: `~/bin` on Linux, `~/Applications/pandoc` on Mac OS X, and
        `~\AppData\Local\Pandoc` on Windows.

    :param str download_folder: Directory, where the installer should download files before unpacking
        to the target folder. If no `download_folder` is given, uses the current directory. example: `/tmp/`, `/tmp`
    Nr   r   Ú64bitz2Linux pandoc is only compiled for 64bit. Got arch=rD   z;Can't handle your platform (only Linux, Mac OS X, Windows).r   r   zUsing already downloaded file zDownloading pandoc from rM   ÚwbÚ_handle_zBCan't handle download, only Linux, Windows and OS X are supported.)r   Úsysr&   r(   Úarchitecturer#   r6   r   Úendswithr8   r=   r[   Ú
expanduserÚisfiler   r;   r   Úopenr\   ÚcopyfileobjÚDEFAULT_TARGET_FOLDERÚmakedirsÚOSErrorÚglobalsÚgetÚremove)r   ra   r-   rv   rw   ÚpfÚarchr5   Ú_r`   r.   Úout_fileÚunpackr   r   r   Údownload_pandocÃ   sL   

ÿÿ
ÿrŽ   )r
   )NNr
   FN)Úloggingr8   Úos.pathr&   r)   r\   rW   r|   rR   Útypingr   r    Úurllib.requestr   ÚImportErrorÚhandlerr   r   rƒ   r6   r?   rj   rr   ru   ÚstrÚboolrŽ   r   r   r   r   Ú<module>   sT   ÿý
1&%#ü
ÿþý
üü