
    i              
          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	m
Z
mZmZmZ d dlmZ ddlmZ ddlmZmZmZmZ ddlmZmZmZ e
rd d	lmZ  G d
 d      ZdedefdZdededee   dee   def
dZd Z deeef   defdZ!y)    N)Path)TYPE_CHECKINGDictListOptional)msg   )Errors)check_spacy_env_varsdownload_fileensure_pathyget_checksum)get_hashmake_tempdirupload_file)	CloudPathc            
           e Zd ZdZdddedefdZdeded	ed
dfdZddddedee   d	ee   d
ed   fdZ	ddddedee   d	ee   d
ed   fdZ
deded	ed
dfdZded
efdZy)RemoteStoragezPush and pull outputs to and from a remote file storage.

    Remotes can be anything that `smart_open` can support: AWS, GCS, file system,
    ssh, etc.
    gz)compressionproject_rooturlc                @    || _         t        |      | _        || _        y N)rootr   r   r   )selfr   r   r   s       j/var/www/vps2.regionflexible.com/Desarrollo/venv/lib/python3.12/site-packages/weasel/cli/remote_storage.py__init__zRemoteStorage.__init__   s     	$&    pathcommand_hashcontent_hashreturnr   c                    | j                   |z  }|j                         st        d| d      | j                  |||      }|j                         r|S t	               5 }|| j                  t        |            z  }| j                  rd| j                   nd}t        j                  ||      5 }	|	j                  t        |      t        |             ddd       t        ||       ddd       |S # 1 sw Y   xY w# 1 sw Y   |S xY w)a  Compress a file or directory within a project and upload it to a remote
        storage. If an object exists at the full URL, nothing is done.

        Within the remote storage, files are addressed by their project path
        (url encoded) and two user-supplied hashes, representing their creation
        context and their file contents. If the URL already exists, the data is
        not uploaded. Paths are archived and compressed prior to upload.
        zCannot push z: does not exist.zw:wmode)arcnameN)r   existsIOErrormake_urlr   encode_namestrr   tarfileopenaddr   )
r   r    r!   r"   locr   tmptar_locmode_stringtar_files
             r   pushzRemoteStorage.push    s     ii$zz|L->?@@mmD,=::<J^ 	&sD,,SY77G595E5EBt//013KgK8 :HSXs4y9:%	& 
: :	& 
s%   AD 0&C4D 4C=	9D  D
Nr!   r"   c                (   | j                   |z  }|j                         ry| j                  |||      }||S |j                  j                         s|j                  j	                  d       t               5 }||j                  d   z  }t        ||       | j                  rd| j                   nd}t        j                  ||      5 }	d	 fd
}
 |
|	| j                          ddd       ddd       |S # 1 sw Y   xY w# 1 sw Y   |S xY w)a  Retrieve a file from the remote cache. If the file already exists,
        nothing is done.

        If the command_hash and/or content_hash are specified, only matching
        results are returned. If no results are available, an error is raised.
        Nr7   T)parentszr:rr&   c                     t         j                  j                  |       }t         j                  j                  |      }t         j                  j                  ||g      }||k(  S r   )osr    abspathcommonprefix)	directorytargetabs_directory
abs_targetprefixs        r   is_within_directoryz/RemoteStorage.pull.<locals>.is_within_directoryZ   sJ    (*	(B%'WW__V%<
!#!5!5}j6Q!R%66r   c                 8   | j                         D ]N  }t        j                  j                  ||j                        } ||      r7t        t        j                         t        j                  dk\  r| j                  |d       y | j                  |       y )N)      data)filter)
getmembersr=   r    joinname
ValueErrorr
   E201sysversion_info
extractall)tarr    membermember_pathrE   s       r   safe_extractz(RemoteStorage.pull.<locals>.safe_extract`   sv    &)nn&6 >F*,'',,tV[[*IK#6t[#I&0&= => ++w6NN4N?NN40r   )r   r)   findparentmkdirr   partsr   r   r.   r/   )r   r    r!   r"   destr   r2   r3   r4   r5   rV   rE   s              @r   pullzRemoteStorage.pull8   s    yy4;;=ii<liS;J ;;%%'!!$!/ 63		"-c7+9=9I9I4#3#3"45s\\'< 671 !4995-6	66 
/6 6	66 
s%   :ADC;)D;D	 DDc                   | j                  t        |            }g }|-|+| j                  |z  |z  |z  }|j                         r|gng }n|J| j                  |z  |z  j                         rt	        | j                  |z  |z  j                               }n| j                  |z  j                         re| j                  |z  j                         D ]!  }|j                  |j                                # |"|D cg c]  }|j                  d   |k(  s| }}t        |      dk\  r	 |j                  d        |r|d   S dS c c}w # t        $ r t        j                  d       Y .w xY w)a  Find the best matching version of a file within the storage,
        or `None` if no match can be found. If both the creation and content hash
        are specified, only exact matches will be returned. Otherwise, the most
        recent matching file is preferred.
        Nr:   r	   c                 6    | j                         j                  S r   )statst_mtime)xs    r   <lambda>z$RemoteStorage.find.<locals>.<lambda>   s    (9(9 r   )keyzkUnable to sort remote files by last modified. The file(s) pulled from the cache may not be the most recent.)r,   r-   r   r)   listiterdirextendrZ   lensort	Exceptionr   warn)r   r    r!   r"   rM   urlsr   sub_dirs           r   rW   zRemoteStorage.findm   sY    D	*#(@((T/L0<?CJJLC5bD%4,.668TXX_|;DDFG4'') $488: 3GKK 123++/QC399R=L3PCQDQt9>		9	:  tBx)T) R  Hs   <EE)E
 
E+*E+c                 \    | j                   | j                  t        |            z  |z  |z  S )zCConstruct a URL from a subpath, a creation hash and a content hash.)r   r,   r-   )r   r    r!   r"   s       r   r+   zRemoteStorage.make_url   s*    xx$**3t955D|SSr   rM   c                 @    t         j                  j                  |      S )z&Encode a subpath into a URL-safe name.)urllibparse
quote_plus)r   rM   s     r   r,   zRemoteStorage.encode_name   s    ||&&t,,r   )__name__
__module____qualname____doc__r   r-   r   r6   r   r\   rW   r+   r,    r   r   r   r      s     EI 'T ' '
 S   8 '+&*33 sm	3
 sm3 
+	3r '+&*"*"* sm	"*
 sm"* 
+	"*HTT T TC TK T- - -r   r   r1   r#   c                     t        |       S r   )r   )r1   s    r   get_content_hashrx      s    r   	site_hashenv_hashdepscmdc                    t                t        |      D cg c]  }t        |       }}| |g|z   }|j                  |       dj	                  |      j                  d      }t        j                  |      j                         S c c}w )zCreate a hash representing the execution of a command. This includes the
    currently installed packages, whatever environment variables have been marked
    as relevant, and the command.
     utf8)	r   sortedr   rf   rL   encodehashlibmd5	hexdigest)ry   rz   r{   r|   depdep_checksumshashescreation_bytess           r   get_command_hashr      sw     28,?3\#&?M?"]2F
MM#WWV_++F3N;;~&0022	 @s   Bc                  $   t        j                         } t         j                  r#| j                  t        j                                t               }| D ]o  }t        |      }|j                         D ]O  }|j                  d   j                  d      s"|j                  |j                  d   j                  dd             Q q dj                  t        |            j                  d      }t        j                   |      j#                         S )zHash the current Python environment's site-packages contents, including
    the name and version of the libraries. The list we're hashing is what
    `pip freeze` would output.
    r:   z	dist-infoz
.dist-infor~   r   )sitegetsitepackagesENABLE_USER_SITErf   getusersitepackagessetr   re   rZ   endswithr0   replacerL   r   r   r   md5sumr   )	site_dirspackagessite_dirsubpathpackage_bytess        r   get_site_hashr      s    
 $$&I1134uH J>'') 	JG}}R ))+6W]]2.66|RHI	JJ
 GGF8,-44V<M>>-(2244r   envc                     i }| j                         D ]B  \  }}|j                  d      r't        j                  j	                  |dd d      ||<   >|||<   D t        |      S )zConstruct a hash of the environment variables that will be passed into
    the commands.

    Values in the env dict may be references to the current os.environ, using
    the syntax $ENV_VAR to mean os.environ[ENV_VAR]
    $   Nr~   )items
startswithr=   environgetr   )r   env_varsrc   values       r   get_env_hashr      sd     Hiik "
UC JJNN59b9HSM!HSM	"
 Hr   )"r   r=   r   rP   r.   urllib.parsero   pathlibr   typingr   r   r   r   wasabir   errorsr
   utilr   r   r   r   r   r   r   cloudpathlibr   r   r-   rx   r   r   r   rv   r   r   <module>r      s     	  
    6 6   R R 6 6&C- C-L$ 3 33!3)-d3:>s)335$d38n  r   