U
    xÊ¹jÁ'  ã                   @   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mZmZ d dl	m
Z
mZmZmZ d dlmZ d dlmZ e d¡ZG dd„ deƒZd	d
d
efdd„Zdd„ ZedkrÆd dlZeejdd… ƒ dS )é    N)ÚEIOÚENOENT)ÚFUSEÚFuseOSErrorÚLoggingMixInÚ
Operations)Ú__version__)Ú	url_to_fsúfsspec.fusec                   @   s|   e Zd Zddd„Zddd„Zdd„ Zd	d
„ Zdd„ Zdd„ Zdd„ Z	ddd„Z
dd„ Zd dd„Zdd„ Zdd„ Zdd„ ZdS )!ÚFUSErFc                 C   s8   || _ i | _| d¡d | _d| _t d|¡ || _d S )Nú/r   zStarting FUSE at %s)ÚfsÚcacheÚrstripÚrootÚcounterÚloggerÚinfoÚ_ready_file)Úselfr   ÚpathÚ
ready_file© r   úJ/var/www/html/bwave/python/venv/lib/python3.8/site-packages/fsspec/fuse.pyÚ__init__   s    zFUSEr.__init__Nc              
   C   sX  t  d|¡ | jr$|dkr$dddœS d | j| d¡g¡ d¡}z| j |¡}W n, t	k
r| } zt
tƒ|‚W 5 d }~X Y nX | dd	¡| d
d	¡dœ}| dd¡}|d dkrÐtj|B |d< d|d< d|d< n*tj|B |d< |d |d< d|d< d|d< d|k�r|d nt ¡ |d< d|k�r*|d nt ¡ |d< d|k�rH|d nt ¡ |d< |S )Nz
getattr %s©z/.fuse_readyz.fuse_readyÚfileé   )ÚtypeÚst_sizeÚ r   Úuidiè  Úgid)Úst_uidÚst_gidÚmodeiÿ  r   Úst_moder   r   Ú
st_blksizeÚsizei  P é   Úst_nlinkÚatimeÚst_atimeÚctimeÚst_ctimeÚmtimeÚst_mtime)r   Údebugr   Újoinr   Úlstripr   r   r   ÚFileNotFoundErrorr   r   ÚgetÚstatÚS_IFDIRÚS_IFREGÚtime)r   r   Úfhr   ÚexcÚdataÚpermr   r   r   Úgetattr   s,    

zFUSEr.getattrc                 C   sJ   t  d|¡ d | j| d¡g¡}| j |d¡}dd„ |D ƒ}ddg| S )	Nz
readdir %sr    r   Fc                 S   s   g | ]}t j | d ¡¡‘qS )r   )Úosr   Úbasenamer   )Ú.0Úfr   r   r   Ú
<listcomp>:   s     z!FUSEr.readdir.<locals>.<listcomp>Ú.z..)r   r1   r2   r   r3   r   Zls)r   r   r:   Úfilesr   r   r   Úreaddir6   s
    zFUSEr.readdirc                 C   s&   d  | j| d¡g¡}| j |¡ dS ©Nr    r   r   )r2   r   r3   r   Úmkdir©r   r   r%   r   r   r   rH   =   s    zFUSEr.mkdirc                 C   s&   d  | j| d¡g¡}| j |¡ dS rG   )r2   r   r3   r   Úrmdir)r   r   r   r   r   rJ   B   s    zFUSEr.rmdirc                 C   sF   t  d|||f¡ | jr$|dkr$dS | j| }| |¡ | |¡}|S )Nzread %sr   s   ready)r   r1   r   r   ÚseekÚread)r   r   r(   Úoffsetr:   rB   Úoutr   r   r   rL   G   s    


z
FUSEr.readc                 C   s6   t  d||f¡ | j| }| |¡ | |¡ t|ƒS )Nzwrite %s)r   r1   r   rK   ÚwriteÚlen)r   r   r<   rM   r:   rB   r   r   r   rO   R   s
    


zFUSEr.writec                 C   sd   t  d||f¡ d | j| d¡g¡}| j |¡ | j |d¡}|| j| j	< |  j	d7  _	| j	d S )Nz	create %sr    r   Úwbr)   )
r   r1   r2   r   r3   r   ÚtouchÚopenr   r   )r   r   ÚflagsÚfiÚfnrB   r   r   r   ÚcreateY   s    zFUSEr.createc                 C   sj   t  d||f¡ d | j| d¡g¡}|d dkr8d}nd}| j ||¡| j| j< |  jd7  _| jd S )	Nzopen %sr    r   é   r   ÚrbrQ   r)   )	r   r1   r2   r   r3   r   rS   r   r   )r   r   rT   rV   r%   r   r   r   rS   b   s    z
FUSEr.openc                 C   s2   d  | j| d¡g¡}|dkr"t‚| j |¡ d S rG   )r2   r   r3   ÚNotImplementedErrorr   rR   )r   r   Úlengthr:   rV   r   r   r   Útruncateo   s    zFUSEr.truncatec              
   C   s^   d  | j| d¡g¡}z| j |d¡ W n0 ttfk
rX } zttƒ|‚W 5 d }~X Y nX d S )Nr    r   F)	r2   r   r3   r   ZrmÚOSErrorr4   r   r   )r   r   rV   r;   r   r   r   Úunlinkv   s
    zFUSEr.unlinkc              
   C   s\   z,|| j kr*| j | }| ¡  | j  |¡ W n* tk
rV } zt|ƒ W 5 d }~X Y nX dS )Nr   )r   ÚcloseÚpopÚ	ExceptionÚprint)r   r   r:   rB   Úer   r   r   Úrelease}   s    

zFUSEr.releasec                 C   s8   t | jdƒr0d | j| d¡g¡}| j ||¡S t‚d S )NÚchmodr    r   )Úhasattrr   r2   r   r3   re   rZ   rI   r   r   r   re   ‡   s    zFUSEr.chmod)F)N)N)N)Ú__name__Ú
__module__Ú__qualname__r   r>   rF   rH   rJ   rL   rO   rW   rS   r\   r^   rd   re   r   r   r   r   r      s   


	

r   TFc           	         s^   ‡ ‡‡‡‡‡‡fdd„}ˆ s:t j|d�}d|_| ¡  |S z
|ƒ  W n tk
rX   Y nX dS )am  Mount stuff in a local directory

    This uses fusepy to make it appear as if a given path on an fsspec
    instance is in fact resident within the local file-system.

    This requires that fusepy by installed, and that FUSE be available on
    the system (typically requiring a package to be installed with
    apt, yum, brew, etc.).

    Parameters
    ----------
    fs: file-system instance
        From one of the compatible implementations
    path: str
        Location on that file-system to regard as the root directory to
        mount. Note that you typically should include the terminating "/"
        character.
    mount_point: str
        An empty directory on the local file-system where the contents of
        the remote path will appear.
    foreground: bool
        Whether or not calling this function will block. Operation will
        typically be more stable if True.
    threads: bool
        Whether or not to create threads when responding to file operations
        within the mounter directory. Operation will typically be more
        stable if False.
    ready_file: bool
        Whether the FUSE process is ready. The ``.fuse_ready`` file will
        exist in the ``mount_point`` directory if True. Debugging purpose.
    ops_class: FUSEr or Subclass of FUSEr
        To override the default behavior of FUSEr. For Example, logging
        to file.

    c                      s   t ˆˆˆˆd�ˆˆ ˆ d�S )N)r   )Z	nothreadsÚ
foreground)r   r   ©rj   r   Úmount_pointÚ	ops_classr   r   Úthreadsr   r   Ú<lambda>º   s
   üzrun.<locals>.<lambda>)ÚtargetTN)Ú	threadingÚThreadÚdaemonÚstartÚKeyboardInterrupt)	r   r   rl   rj   rn   r   rm   ÚfuncÚthr   rk   r   ÚrunŽ   s    ,
rx   c              	   C   s  G dd„ dt jƒ}|dtjd�}|jddtd� |jdtd	d
� |jdtdd
� |jdtdd
� |jddddd� |jddtdd
� |jddddd� |jddddd� |jdddd d� | | ¡} i }| jpÐg D ]À}| 	d!¡\}}}|sü|j
d"|›�d#� | ¡ }| d$¡�r(t|d%td$ƒ … ƒ}n"| d&¡�rJ|d%td&ƒ … d'k}d(|k�rŠ| d(d)¡\}	}
|	|k�r||||	 |
< n|
|i||	< qÒ|||< qÒ| j�rÈtjtj| jd*d+� G d,d-„ d-ttƒ}|}nt}t| jf|Ž\}}t d.|t| jƒ¡ t|| j| j| j| j| j|d/� d%S )0a¥  Mount filesystem from chained URL to MOUNT_POINT.

    Examples:

    python3 -m fsspec.fuse memory /usr/share /tmp/mem

    python3 -m fsspec.fuse local /tmp/source /tmp/local \
            -l /tmp/fsspecfuse.log

    You can also mount chained-URLs and use special settings:

    python3 -m fsspec.fuse 'filecache::zip::file://data.zip' \
            / /tmp/zip \
            -o 'filecache-cache_storage=/tmp/simplecache'

    You can specify the type of the setting by using `[int]` or `[bool]`,
    (`true`, `yes`, `1` represents the Boolean value `True`):

    python3 -m fsspec.fuse 'simplecache::ftp://ftp1.at.proftpd.org' \
            /historic/packages/RPMS /tmp/ftp \
            -o 'simplecache-cache_storage=/tmp/simplecache' \
            -o 'simplecache-check_files=false[bool]' \
            -o 'ftp-listings_expiry_time=60[int]' \
            -o 'ftp-username=anonymous' \
            -o 'ftp-password=xieyanbo'
    c                       s   e Zd Z‡ fdd„Z‡  ZS )z*main.<locals>.RawDescriptionArgumentParserc                    s,   t ƒ  ¡ }| d¡}| j ¡ |d< d |¡S )Nz

r)   )ÚsuperÚformat_helpÚsplitÚdescriptionr   r2   )r   ÚusageÚparts©Ú	__class__r   r   rz   é   s    

z6main.<locals>.RawDescriptionArgumentParser.format_help)rg   rh   ri   rz   Ú__classcell__r   r   r   r   ÚRawDescriptionArgumentParserè   s   r‚   r
   )Úprogr|   z	--versionÚversion)Úactionr„   Úurlzfs url)r   ÚhelpÚsource_pathzsource directory in fsrl   zlocal directoryz-oz--optionÚappendz3Any options of protocol included in the chained URL)r…   r‡   z-lz
--log-filez%Logging FUSE debug info (Default: '')z-fz--foregroundÚstore_falsez-Running in foreground or not (Default: False)z-tz	--threadsz-Running with threads support (Default: False)z-rz--ready-filezZThe `.fuse_ready` file will exist after FUSE is ready. (Debugging purpose, Default: False)ú=zWrong option: )Úmessagez[int]Nz[bool])Ú1ÚyesÚtrueú-r)   z%(asctime)s %(message)s)ÚlevelÚfilenameÚformatc                   @   s   e Zd ZdS )zmain.<locals>.LoggingFUSErN)rg   rh   ri   r   r   r   r   ÚLoggingFUSEr-  s   r”   zMounting %s to %s)rj   rn   r   rm   ) ÚargparseÚArgumentParserÚmainÚ__doc__Úadd_argumentr   ÚstrÚ
parse_argsÚoptionÚ	partitionÚerrorÚlowerÚendswithÚintrP   r{   Zlog_fileÚloggingÚbasicConfigÚDEBUGr   r   r	   r†   r   r1   rl   rx   rˆ   rj   rn   r   )Úargsr‚   ÚparserÚkwargsÚitemÚkeyÚsepÚvalueÚvalZfs_nameZsetting_namer”   Zfuserr   Zurl_pathr   r   r   r—   Ì   s’    ü   ÿüüü



ýùr—   Ú__main__r)   )r•   r¢   r?   r6   rq   r9   Úerrnor   r   Zfuser   r   r   r   Zfsspecr   Zfsspec.corer	   Ú	getLoggerr   r   rx   r—   rg   ÚsysÚargvr   r   r   r   Ú<module>   s*   
 ù
>u