o ~Ha"@sddlmZddlmZeddlmZddlmZddl Zddl Z ddl Z ddl Z Gdddej jZej deej jdgdS) )print_function)standard_library)util)BackendExceptionNc@szeZdZdZddZddZddZdd Zd d Zd d Z ddZ ddZ ddZ dddZ ddZddZddZdS) Megav2Backenda Backend for MEGA.nz cloud storage, only one that works for accounts created since Nov. 2018 See https://github.com/megous/megatools/issues/411 for more details This MEGA backend resorts to official tools (MEGAcmd) as available at https://mega.nz/cmd MEGAcmd works through a single binary called "mega-cmd", which talks to a backend server "mega-cmd-server", which keeps state (for example, persisting a session). Multiple "mega-*" shell wrappers (ie. "mega-ls") exist as the user interface to "mega-cmd" and MEGA API The full MEGAcmd User Guide can be found in the software's GitHub page below : https://github.com/meganz/MEGAcmd/blob/master/UserGuide.md c Cstjj|||d|d|d|d|d|d|d|d|d |j|_|jdurt d d |_ zt |j d }Wnt yb}zt d |j fd}~wwi}|D]}|dddd\}}t|||<qg||d|_|d|_n |j|_||_d|jdd|_|d|jg}z||WdSt y}z ||jWYd}~dSd}~ww)N mega-login mega-logoutzmega-cmdzmega-cmd-servermega-ls mega-mkdirmega-getmega-putmega-rmHOMEz /.megav2rcrz\No password provided in URL and MEGA configuration file for duplicity does not exist as '%s'=UsernamePassword/) duplicitybackendBackend__init___check_binary_existshostname _hostnamepasswordosgetenv_megarcopen Exceptionr partitionstrstripclose _username _passwordusername get_passwordpath_folder mega_loginsubprocess_popen_makedir) self parsed_url conf_fileemyvarslinenamevarcmdr9B/usr/lib/python3/dist-packages/duplicity/backends/megav2backend.pyr-sN              zMegav2Backend.__init__c Cs<z td|gWdSty}ztd|fd}~ww)zGChecks that a specified command exists in the running user command pathwhichzCommand '%s' not found, make sure 'MEGAcmd' tools (https://mega.nz/cmd) is properly installed and in the running user command pathN) subprocess check_outputr"r)r0r8r3r9r9r:r^sz"Megav2Backend._check_binary_existsc Csj|dd|g}z||WdSty4}zt|}d|vr(td|ftd||fd}~ww)z5Creates a remote directory (recursively if necessary)r z-pzFolder already existszzFolder '%s' could not be created on MEGA because it already exists. Use another path or remove the folder in MEGA manuallyz/Folder '%s' could not be created, reason : '%s'N)r-r.r"r$r)r0r+r8r3 error_strr9r9r:r/hs zMegav2Backend._makedircCsDz ||Wn tyYnw|j||ddS)z{Uploads file to the specified remote folder (tries to delete it first to make sure the new one can be uploaded)) local_file remote_fileN)deletedecoder"upload get_canonical)r0 source_pathremote_filenamer9r9r:_putws  zMegav2Backend._putcCs|j||jddS)z-Downloads file from the specified remote path)r@r?N)downloadrBr6)r0rF local_pathr9r9r:_getszMegav2Backend._getcCs |jddS)z(Lists files in the specified remote pathT) files_only)folder_contents)r0r9r9r:_lists zMegav2Backend._listcCs|j|ddS)z+Deletes file from the specified remote path)r@N)rArB)r0filenamer9r9r:_deleteszMegav2Backend._deletecCsdg}||dS)z/Function called when backend is done being usedrN)r.)r0r8r9r9r:_closeszMegav2Backend._closec Csz tjdddWdStjytdtyM}z,d|j|jg}z tj|tjdWntyA}ztd|fd}~wwWYd}~dSd}~ww) zHelper function to call from each method interacting with MEGA to make sure a session already exists or one is created to start withz mega-session)timeoutzs$z1Megav2Backend.folder_contents..)r,r-r<r=rBrZ)r0rKr8filesr9r9r:rLs  zMegav2Backend.folder_contentscCs*d|jd||g}|||dS)z(Downloads a file from a remote MEGA pathr rNr,r-r.)r0r@r?r8r9r9r:rHzMegav2Backend.downloadc Cstd||jd|g}|z||WdSty9}zt|}d|vr-td|ftd||fd}~ww)z$Uploads a file to a remote MEGA pathr rzReached storage quotazwMEGA account over quota, could not write file : '%s' . Upgrade your storage at https://mega.nz/pro or remove some data.z/Failed writing file '%s' to MEGA, reason : '%s'N)r,r-r.r"r$r)r0r?r@r8r3r>r9r9r:rCszMegav2Backend.uploadcCs*dd|jd|g}|||dS)z&Deletes a file from a remote MEGA pathr z-frNr_)r0r@r8r9r9r:rAr`zMegav2Backend.deleteN)F)__name__ __module__ __qualname____doc__rrr/rGrJrMrOrPr-rLrHrCrAr9r9r9r:r"s 1    rmegav2) __future__rfuturerinstall_aliasesrrduplicity.errorsrduplicity.backendrr<rXrrrregister_backend uses_netlocextendr9r9r9r:s    4