
    3~h9                     `    d Z ddlZddlZddlZddlmZ ddlmZ  G d d      Z G d d	      Z	y)
z$
File transfer via SFTP and/or SCP.
    N)Path   )debugc                   <    e Zd ZdZd Zed        Zd ZddZddZ	y)	Transferzp
    `.Connection`-wrapping class responsible for managing file upload/download.

    .. versionadded:: 2.0
    c                     || _         y N)
connection)selfr
   s     X/var/www/peopleoo.sandbox-dev.co.uk/venv/lib/python3.12/site-packages/fabric/transfer.py__init__zTransfer.__init__   s	    $    c                 6    | j                   j                         S r	   )r
   sftp)r   s    r   r   zTransfer.sftp!   s    ##%%r   c                     	 t        j                  | j                  j                  |      j                        S # t        $ r Y yw xY w)NF)statS_ISDIRr   st_modeIOError)r   paths     r   is_remote_dirzTransfer.is_remote_dir%   s;    	<<		t 4 < <== 		s   7: 	AANc                    |st        d      |}t        j                  | j                  j	                         xs | j                  j                  d      |      }|}t        |d      xr t        |j                        }t        j                  |      }|s|}|s|j                  | j                  j                  | j                  j                  | j                  j                  t        j                  |      |      }|j!                  t"        j$                        r#|}t"        j&                  j                  ||      }n"t"        j&                  j)                  |      \  }}	t"        j&                  j+                  |      }t-        |      j/                  dd       |r| j                  j1                  ||       no| j                  j3                  ||       |rP| j                  j5                  |      j6                  }
t5        j8                  |
      }t#        j:                  ||       t=        ||||| j                  	      S )
a  
        Copy a file from wrapped connection's host to the local filesystem.

        :param str remote:
            Remote file to download.

            May be absolute, or relative to the remote working directory.

            .. note::
                Most SFTP servers set the remote working directory to the
                connecting user's home directory, and (unlike most shells) do
                *not* expand tildes (``~``).

                For example, instead of saying ``get("~/tmp/archive.tgz")``,
                say ``get("tmp/archive.tgz")``.

        :param local:
            Local path to store downloaded file in, or a file-like object.

            **If None or another 'falsey'/empty value is given** (the default),
            the remote file is downloaded to the current working directory (as
            seen by `os.getcwd`) using its remote filename. (This is equivalent
            to giving ``"{basename}"``; see the below subsection on
            interpolation.)

            **If a string is given**, it should be a path to a local directory
            or file and is subject to similar behavior as that seen by common
            Unix utilities or OpenSSH's ``sftp`` or ``scp`` tools.

            For example, if the local path is a directory, the remote path's
            base filename will be added onto it (so ``get('foo/bar/file.txt',
            '/tmp/')`` would result in creation or overwriting of
            ``/tmp/file.txt``).

            This path will be **interpolated** with some useful parameters,
            using `str.format`:

            - The `.Connection` object's ``host``, ``user`` and ``port``
              attributes.
            - The ``basename`` and ``dirname`` of the ``remote`` path, as
              derived by `os.path` (specifically, its ``posixpath`` flavor, so
              that the resulting values are useful on remote POSIX-compatible
              SFTP servers even if the local client is Windows).
            - Thus, for example, ``"/some/path/{user}@{host}/{basename}"`` will
              yield different local paths depending on the properties of both
              the connection and the remote path.

            .. note::
                If nonexistent directories are present in this path (including
                the final path component, if it ends in `os.sep`) they will be
                created automatically using `os.makedirs`.

            **If a file-like object is given**, the contents of the remote file
            are simply written into it.

        :param bool preserve_mode:
            Whether to `os.chmod` the local file so it matches the remote
            file's mode (default: ``True``).

        :returns: A `.Result` object.

        .. versionadded:: 2.0
        .. versionchanged:: 2.6
            Added ``local`` path interpolation of connection & remote file
            attributes.
        .. versionchanged:: 2.6
            Create missing ``local`` directories automatically.
        zRemote path must not be empty!.write)hostuserportdirnamebasenameT)parentsexist_ok)
remotepathfl)r"   	localpathorig_remoteremote
orig_locallocalr
   )
ValueError	posixpathjoinr   getcwd	normalizehasattrcallabler   r   formatr
   r   r   r   r   endswithossepr   splitabspathr   mkdirgetfogetr   r   S_IMODEchmodResult)r   r'   r)   preserve_moder&   r(   is_file_likeremote_filenamedir_path_remote_modemodes               r   r9   zTransfer.get+   s   \ =>>II:$))"5"5c":F

 
ug.H8EKK3H#,,V4#E LL__))__))__))!))&1( ! E ~~bff% UO< ggmmE2!GGOOE*EN   = IIOOv%O8IIMMVuM= "iinnV4<<||K0%#!
 	
r   c                 x   |st        d      t        |d      xr t        |j                        }|}|rt	        |dd      }nt
        j                  j                  |      }|s*|rt        d      |}t        dj                  |             nR| j                  |      rA|rt        j                  ||      }n(|rt        d      t        dj                  ||            |}t        j                  | j                  j                         xs | j                  j                  d	      |      }||k7  rd
}t        |j                  ||             |}	|s?t
        j                  j!                  |      }||	k7  rt        dj                  |	|             |rnd}t        |j                  ||             |j#                         }
	 |j%                  d       | j                  j'                  ||       |j%                  |
       nt        dj                  ||             | j                  j)                  ||       |rPt        j*                  |      j,                  }t+        j.                  |      }| j                  j1                  ||       t3        |||	|| j4                        S # |j%                  |
       w xY w)a  
        Upload a file from the local filesystem to the current connection.

        :param local:
            Local path of file to upload, or a file-like object.

            **If a string is given**, it should be a path to a local (regular)
            file (not a directory).

            .. note::
                When dealing with nonexistent file paths, normal Python file
                handling concerns come into play - for example, trying to
                upload a nonexistent ``local`` path will typically result in an
                `OSError`.

            **If a file-like object is given**, its contents are written to the
            remote file path.

        :param str remote:
            Remote path to which the local file will be written.

            .. note::
                Most SFTP servers set the remote working directory to the
                connecting user's home directory, and (unlike most shells) do
                *not* expand tildes (``~``).

                For example, instead of saying ``put("archive.tgz",
                "~/tmp/")``, say ``put("archive.tgz", "tmp/")``.

                In addition, this means that 'falsey'/empty values (such as the
                default value, ``None``) are allowed and result in uploading to
                the remote home directory.

            .. note::
                When ``local`` is a file-like object, ``remote`` is required
                and must refer to a valid file path (not a directory).

        :param bool preserve_mode:
            Whether to ``chmod`` the remote file so it matches the local file's
            mode (default: ``True``).

        :returns: A `.Result` object.

        .. versionadded:: 2.0
        zLocal path must not be empty!r   nameNzAMust give non-empty remote path when local is a file-like object!z$Massaged empty remote path into {!r}zXCan't put a file-like-object into a directory unless it has a non-empty .name attribute!zUSomehow got an empty local file basename ({!r}) when uploading to a directory ({!r})!r   z,Massaged relative remote path {!r} into {!r}z+Massaged relative local path {!r} into {!r}z'Uploading file-like object {!r} to {!r}r   )r#   r"   zUploading {!r} to {!r})r$   r"   r%   )r*   r/   r0   r   getattrr3   r   r   r   r1   r   r+   r,   r   r-   r.   r6   tellseekputfoputr   r   r:   r;   r<   r
   )r   r)   r'   r=   r>   r&   
local_baseprejoined_remotemsgr(   pointer
local_moderC   s                r   rJ   zTransfer.put   sl   \ <==ug.H8EKK3H  5J))%0J W  $<CCFKL' "
;$r  %ovv&  "II:$))"5"5c":F
 %%@C#**-v67 
GGOOE*E
"AHH"E ;C#**UF+,jjlG$

1		5V<

7#*11%@AIIMMEfM= WWU^33
||J/		-#!
 	
 

7#s   .J& &J9)NT)
__name__
__module____qualname____doc__r   propertyr   r   r9   rJ    r   r   r   r      s2    % & &N
`H
r   r   c                       e Zd ZdZd Zy)r<   a  
    A container for information about the result of a file transfer.

    See individual attribute/method documentation below for details.

    .. note::
        Unlike similar classes such as `invoke.runners.Result` or
        `fabric.runners.Result` (which have a concept of "warn and return
        anyways on failure") this class has no useful truthiness behavior. If a
        file transfer fails, some exception will be raised, either an `OSError`
        or an error from within Paramiko.

    .. versionadded:: 2.0
    c                 J    || _         || _        || _        || _        || _        y r	   )r)   r(   r'   r&   r
   )r   r)   r(   r'   r&   r
   s         r   r   zResult.__init__X  s.     
 %  '$r   N)rP   rQ   rR   rS   r   rU   r   r   r<   r<   F  s    "%r   r<   )
rS   r3   r+   r   pathlibr   utilr   r   r<   rU   r   r   <module>rZ      s2    
    n
 n
b	#% #%r   