HEX
Server: Apache
System: Linux pdx1-shared-a1-38 6.6.104-grsec-jammy+ #3 SMP Tue Sep 16 00:28:11 UTC 2025 x86_64
User: mmickelson (3396398)
PHP: 8.1.31
Disabled: NONE
Upload Files
File: //usr/lib/python3/dist-packages/duplicity/backends/__pycache__/adbackend.cpython-310.pyc
o

�~�_�C�@s�ddlmZddlmZddlmZddlZddlZddlZddl	Z	ddl
Z
ddlmZddl
ZddlmZddlmZddlmZGd	d
�d
ejj�Zej�de�dS)�)�print_function)�division)�inputN)�DEFAULT_BUFFER_SIZE)�BackendException)�config)�logc@s�eZdZdZej�d�ZdZdZ	dZ
ddgZdZd	Z
d
Zdd�Zd
d�Zdd�Zdd�Zdd�Zdd�Zdd�Zdd�Zdd�Zdd�Zdd �Zd!d"�Zd#d$�Zd%S)&�	ADBackendz�
    Backend for Amazon Drive. It communicates directly with Amazon Drive using
    their RESTful API and does not rely on externally setup software (like
    acd_cli).
    z~/.duplicity_ad_oauthtoken.jsonzhttps://www.amazon.com/ap/oaz$https://api.amazon.com/auth/o2/tokenz'https://breunig.xyz/duplicity/copy.htmlzclouddrive:read_otherzclouddrive:writez=amzn1.application-oa2-client.791c9c2d78444e85a32eb66f92eb6bcc�@5b322c6a37b25f16d848a6a556eddcc30314fc46ae65c87068ff1bc4588d715b�CDuplicityFormBoundaryd66364f7f8924f7e9d478e19cf4b871d114a1e00262542cCs�tjj�||�d|_d|_i|_d|_|j�	d�|_
tjdkr%t
�d�zddladdlmaWnty<td��w|��|��dS)	Nz%https://drive.amazonaws.com/drive/v1/z/https://content-na.drive.amazonaws.com/cdproxy/�/l
zxYour --volsize is bigger than 10 GiB, which is the maximum file size on Amazon Drive that does not require work arounds.r)�
OAuth2Sessiona
Amazon Drive backend requires python-requests and python-requests-oauthlib to be installed.

For Debian and derivates use:
  apt-get install python-requests python-requests-oauthlib
For Fedora and derivates use:
  yum install python-requests python-requests-oauthlib)�	duplicity�backend�Backend�__init__�metadata_url�content_url�names_to_ids�backup_target_id�path�lstrip�
backup_targetr�volsizer�
FatalError�requests�requests_oauthlibr
�ImportErrorr�initialize_oauth2_session�resolve_backup_target)�self�
parsed_url�r"�>/usr/lib/python3/dist-packages/duplicity/backends/adbackend.pyr<s(
���	zADBackend.__init__c

s��fdd�}d}zt�j��
}t�|�}Wd�n1swYWnty=}z
t�d|�WYd}~nd}~wwt�j�j	�j
|�j�jd��j|d��_
|dur^�j
��j��j
��jd�}|jtjjkrpd}|dur�tj��r~tj��s�t�d�j��j
��j�\}}td	�td
�t|�td	�td��dd
d���}�j
j �j�j|d�}�j
��jd�}|�!�||�|��}	d|	vs�d|	vr�t�d�|	d�_|	d�_"dS)z1Setup or refresh oauth2 session with Amazon Drivec
s~z!t�jd��}t�||�Wd�WdS1swYWdSty>}zt�d�j|f�WYd}~dSd}~ww)zStores oauth2 token on disk�wNz�Could not save the OAuth2 token to %s. This means you may need to do the OAuth2 authorization process again soon. Original error: %s)�open�OAUTH_TOKEN_PATH�json�dump�	Exceptionr�Error)�token�f�err�r r"r#�
token_updateras&�����z:ADBackend.initialize_oauth2_session.<locals>.token_updaterNzMCould not load OAuth2 token. Trying to create a new one. (original error: %s))�	client_id�
client_secret)�scope�redirect_urir+�auto_refresh_kwargs�auto_refresh_urlr/zaccount/endpointz�The OAuth2 token could not be loaded from %s and you are not running duplicity interactively, so duplicity cannot possibly access Amazon Drive.�z�In order to allow duplicity to access Amazon Drive, please open the following URL in a browser and copy the URL of the page you see after authorization here:zURL of the resulting page: zhttp://zhttps://�)r1�authorization_response�metadataUrl�
contentUrlz1Could not retrieve endpoint URLs for this account)#r%r&r'�load�IOErrorr�Noticer
�	CLIENT_ID�OAUTH_SCOPE�OAUTH_REDIRECT_URL�
CLIENT_SECRET�OAUTH_TOKEN_URL�http_client�
refresh_token�getr�status_coder�codes�ok�sys�stdout�isatty�stdinr�authorization_url�OAUTH_AUTHORIZE_URL�printr�replace�strip�fetch_token�raise_for_statusr)
r r/r+r,r-�endpoints_responserM�_�
redirected_to�urlsr"r.r#r^sz�������
�����
�

z#ADBackend.initialize_oauth2_sessioncs�|j�|jd�}|��ddd}dd�|j�d�D�D]R�t�d���d�}�|kr1|d	}|�	|jd
||f�}�fdd�|D�}t
|�dkrVt�d
|�f�qt
|�dkrc|dd}qt�
d��|�|��}qt�
d|�||_dS)z/Resolve node id for remote backup target folderz)nodes?filters=kind:FOLDER AND isRoot:true�datar�idcSsg|]}|r|�qSr"r")�.0�xr"r"r#�
<listcomp>�sz3ADBackend.resolve_backup_target.<locals>.<listcomp>rz^[A-Za-z0-9_-]*�*z4nodes?filters=kind:FOLDER AND name:%s AND parents:%scsg|]}|�d��kr|�qS)�name)rE�rZr,��	componentr"r#r\�s�z[There are multiple folders with the same name below one parent.
ParentID: %s
FolderName: %sr7z'Folder %s does not exist yet. Creating.zBackup target folder has id: %sN)rCrErr'r�split�re�search�group�read_all_pages�lenrr�Debug�mkdirr)r �response�parent_node_id�query�matches�
candidatesr"r`r#r�s0����
zADBackend.resolve_backup_targetcCs||jvr	|��|j�|�S)z.Find id of remote file in backup target folder)r�_listrE�r �remote_filenamer"r"r#�get_file_id�s
zADBackend.get_file_idcCs>||gdd�}|jj|jdt�|�d�}|��|��dS)z/Create a new folder as a child of a parent node�FOLDER)r^�parents�kind�nodes)rXrY)rC�postrr'�dumpsrS)r rl�folder_namerXrkr"r"r#rj�s�zADBackend.mkdirccs��|j}t�d|dt�|�d|�VdVdV|���}	|�t�}|r.|Vnnq#Wd�n1s:wYt�d|d	|�VdS)
z>Generator for multipart/form-data file upload from source filez9--%s
Content-Disposition: form-data; name="metadata"

z%s
z--%s
sKContent-Disposition: form-data; name="content"; filename="i_love_backups"
s*Content-Type: application/octet-stream

TNz

--%s--
� multipart/form-data; boundary=%s)�MULTIPART_BOUNDARY�str�encoder'ryr%�readr)r �metadata�source_path�boundary�streamr,r"r"r#�multipart_stream�s.����

��
�zADBackend.multipart_streamcCs�g}d}d|vr
dnd}	|||}|j�|�}|jdkr'td|j|f��|��}d|vr?t|d�d	kr?|�|d�n	|St|�|d
krM	|Sd|vrT	|S|d}q
)z5Iterates over nodes API URL until all pages were readr6�?z&startToken=z?startToken=T��z*Pagination failed with status=%s on URL=%srXr�count�	nextToken)rCrErFrr'rh�extend)r �url�result�
next_token�token_param�
paginated_urlrk�parsedr"r"r#rg�s.
�
����zADBackend.read_all_pagescCs|�|�td|��)z@Report error when file already existed in location and delete itz�Upload failed, because there was a file with the same name as %s already present. The file was deleted, and duplicity will retry the upload unless the retry limit has been reached.)�_deleterrqr"r"r#�raise_for_existing_files
�z!ADBackend.raise_for_existing_filec
Cs�|j�|jd�}|��|��d}tj�|j�}||kr(t	d|j||f��||j
vr9t�d|�|�
|�|d|jgd�}dd|ji}|�||�}|jj|jd	||d
�}	|	jdkre|�|�nr|	jdkrrt�d
|�ne|	jdks||	jdkr�t�d||	jtjf�tjd}
|
dkr�|
d8}
t�d�|�|�d}||kr�t�d�dS|dkr�t�d|
d�q�|�|�|
dks�t	d|��t�d||	jf�|	��|	��}d|vr�t	d||f��|d|j
|d<dS)z#Upload a local file to Amazon Drivez
account/quota�	availablez[Out of space: trying to store "%s" (%d bytes), but only %d bytes available on Amazon Drive.z^File %s seems to already exist on Amazon Drive. Deleting before attempting to upload it again.�FILE)r^rvruzContent-Typer{znodes?suppress=deduplication)rX�headersi���z%s uploaded successfullyi�i�z�%s upload failed with timeout status code=%d. Speculatively waiting for %d seconds to see if Amazon Drive finished the upload anyway�rr7�sizez-Upload turned out to be successful after all.N���z.Uploaded file is not yet there, %d tries left.z<%s upload failed and file did not show up within time limit.z0%s upload returned an undesirable status code %srYzW%s was uploaded, but returned JSON does not contain ID of new file. Retrying.
JSON:

%sr^)rCrErrSr'�osr�getsizer^rrrrir�rr|r�rxrrFr��Infor�timeout�time�sleep�_query)
r r�rr�quotar��source_sizer�r�rXrk�tries�remote_sizer�r"r"r#�_puts����
�
��

��


�
����zADBackend._putcCs�|�d��?}|�|�}|durtd|��|jj|jd|ddd�}|��|jtd�D]	}|r7|�	|�q.|�
�Wd�dS1sGwYdS)	zDownload file from Amazon Drive�wbNz1File "%s" cannot be downloaded: it does not existz/nodes/z/contentT)r�)�
chunk_size)r%rsrrCrErrS�iter_contentr�write�flush)r rr�
local_path�
local_file�file_idrk�chunkr"r"r#�_getes$
���
�
"�zADBackend._getcCsL|�|�}|dur
ddiS|j�|jd|�}|��d|��ddiS)z)Retrieve file size info from Amazon DriveNr�r��nodes/�contentProperties)rsrCrErrSr'�r rrr�rkr"r"r#r�ws
zADBackend._querycCs8|�|jd|jd�}dd�|D�|_t|j���S)z(List files in Amazon Drive backup folderr�z/children?filters=kind:FILEcSsi|]	}|d|d�qS)r^rYr"r_r"r"r#�
<dictcomp>�sz#ADBackend._list.<locals>.<dictcomp>)rgrrr�list�keys)r �filesr"r"r#rp�s��zADBackend._listcCsH|�|�}|durtd|��|j�|jd|�}|��|j|=dS)zDelete file from Amazon DriveNz.File "%s" cannot be deleted: it does not existztrash/)rsrrC�putrrSrr�r"r"r#r��s
��zADBackend._deleteN)�__name__�
__module__�__qualname__�__doc__r�r�
expanduserr&rNrBr@r?r>rAr|rrrrsrjr�rgr�r�r�r�rpr�r"r"r"r#r	&s."M 
	Jr	�ad)�
__future__rr�builtinsr�os.pathr�r'rIr�rd�ior�duplicity.backendr�duplicity.errorsrrrrrr	�register_backendr"r"r"r#�<module>s"v