a
    h'                     @   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rd 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   9/var/www/auris/lib/python3.9/site-packages/fsspec/fuse.py__init__   s    zFUSEr.__init__Nc              
   C   sZ  t d| | jr$|dv r$dddS d| j|dgd}z| j|}W n. t	y~ } zt
t|W Y d }~n
d }~0 0 |dd	|d
d	d}|dd}|d dkrtj|B |d< d|d< d|d< n*tj|B |d< |d |d< d|d< d|d< d|v r|d nt |d< d|v r,|d nt |d< d|v rJ|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>:       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   rI   =   s    zFUSEr.mkdirc                 C   s&   d | j|dg}| j| dS rH   )r2   r   r3   r   rmdir)r   r   r   r   r   rK   B   s    zFUSEr.rmdirc                 C   sF   t d|||f | jr$|dv r$dS | j| }|| ||}|S )Nzread %sr   s   ready)r   r1   r   r   seekread)r   r   r(   offsetr:   rB   outr   r   r   rM   G   s    


z
FUSEr.readc                 C   s6   t d||f | j| }|| || t|S )Nzwrite %s)r   r1   r   rL   writelen)r   r   r<   rN   r:   rB   r   r   r   rP   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   rbrR   r)   )	r   r1   r2   r   r3   r   rT   r   r   )r   r   rU   rW   r%   r   r   r   rT   b   s    z
FUSEr.openc                 C   s2   d | j|dg}|dkr"t| j| d S rH   )r2   r   r3   NotImplementedErrorr   rS   )r   r   lengthr:   rW   r   r   r   truncateo   s    zFUSEr.truncatec              
   C   s`   d | j|dg}z| j|d W n2 ttfyZ } ztt|W Y d }~n
d }~0 0 d S )Nr    r   F)	r2   r   r3   r   ZrmOSErrorr4   r   r   )r   r   rW   r;   r   r   r   unlinkv   s
    zFUSEr.unlinkc              
   C   s^   z,|| j v r*| j | }|  | j | W n, tyX } zt| W Y d }~n
d }~0 0 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   rf   r[   rJ   r   r   r   rf      s    zFUSEr.chmod)F)N)N)N)__name__
__module____qualname__r   r>   rG   rI   rK   rM   rP   rX   rT   r]   r_   re   rf   r   r   r   r   r      s   


	

r   TFc           	         s\    fdd} s:t j|d}d|_|  |S z
|  W n tyV   Y n0 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   rk   r   mount_point	ops_classr   r   threadsr   r   <lambda>   s
   zrun.<locals>.<lambda>)targetTN)	threadingThreaddaemonstartKeyboardInterrupt)	r   r   rm   rk   ro   r   rn   functhr   rl   r   run   s    ,
ry   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pg D ]}|	d!\}}}|s|j
d"|d# | }|d$r(t|d%td$  }n"|d&rJ|d%td&  d'v }d(|v r|d(d)\}	}
|	|v r||||	 |
< n|
|i||	< q|||< q| jrtjtj| jd*d+ G d,d- d-tt}|}nt}t| jfi |\}}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   r{      s    

z6main.<locals>.RawDescriptionArgumentParser.format_help)rh   ri   rj   r{   __classcell__r   r   r   r   RawDescriptionArgumentParser   s   r   r
   )progr}   z	--versionversion)actionr   urlzfs url)r   helpsource_pathzsource directory in fsrm   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)rh   ri   rj   r   r   r   r   LoggingFUSEr-  s   r   zMounting %s to %s)rk   ro   r   rn   ) argparseArgumentParsermain__doc__add_argumentr   str
parse_argsoption	partitionerrorlowerendswithintrQ   r|   Zlog_fileloggingbasicConfigDEBUGr   r   r	   r   r   r1   rm   ry   r   rk   ro   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   rr   r9   errnor   r   Zfuser   r   r   r   Zfsspecr   Zfsspec.corer	   	getLoggerr   r   ry   r   rh   sysargvr   r   r   r   <module>   s*   
 
>u