o Hal(@sddlmZddlZddlZddlZddlZddlmZddlm Z ddl m Z e Gdddej jZej deej jdgdS) )print_functionN)util)BackendException)standard_libraryc@seZdZdZddZddZddZdd Zd d Zd d Z ddZ ddZ ddZ ddZ d ddZddZddZddZdS)! Megav3BackendaxBackend 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 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.mdc Cstjj|||d|d|d|d|d|d|d|d|d |d |d |j|_|jdurt d d |_ zt |j d}Wnt yl}zt d|j fd}~wwi}|D]}|dddd\}}t|||<qq||d|_|d|_n|j|_|j|_|jdg}t|dko|ddv|_|d|jdd|_|d|jg} z|| WdSt y}z ||jWYd}~dSd}~ww)Nzmega-cmd mega-exec mega-helpmega-get mega-login mega-logoutmega-ls mega-mkdirmega-putmega-rm mega-whoamiHOMEz /.megav3rcrz\No password provided in URL and MEGA configuration file for duplicity does not exist as '%s'=UsernamePassword no_logoutr)1yestrue/) duplicitybackendBackend__init___check_binary_existshostname _hostnameusernameosgetenv_megarcopen Exceptionr partitionstrstripclose _username _passwordpassword query_argsgetlenlower _no_logoutensure_mega_cmd_runningpath_folder mega_loginsubprocess_popen_makedir) self parsed_url conf_fileemyvarslinenamevarno_logout_optioncmdrFB/usr/lib/python3/dist-packages/duplicity/backends/megav3backend.pyr .s`                zMegav3Backend.__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)r<rEr?rFrFrGr!jsz"Megav3Backend._check_binary_existscCs6ztjdtjtjdWdStytdw)z=Trigger any mega command to ensure mega-cmd server is runningr)stdoutstderrzCannot execute mega commandN)rIrunDEVNULLcheck_returncoder)rr<rFrFrGr6ws  z%Megav3Backend.ensure_mega_cmd_runningc 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)r9r:r)r+r)r<r7rEr? error_strrFrFrGr;s& zMegav3Backend._makedircCsDz ||Wn tyYnw|j||ddS)zwUploads 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)r< source_pathremote_filenamerFrFrG_puts   zMegav3Backend._putcCs|j||jddS)z-Downloads file from the specified remote path)rSrRN)downloadrUrB)r<rY local_pathrFrFrG_gets zMegav3Backend._getcCs |jddS)z(Lists files in the specified remote pathT) files_only)folder_contentsrPrFrFrG_lists zMegav3Backend._listcCs|j|ddS)z+Deletes file from the specified remote path)rSN)rTrU)r<filenamerFrFrG_deleteszMegav3Backend._deletecCs,|js dg}||ddg}||dS)z/Function called when backend is done being usedr rexitN)r5r:)r<rErFrFrG_closes  zMegav3Backend._closec Csz$tjdddd}||jdd}||jkr"tdWd Stj y4| t dty{}z<|j d urE| |_ d |j|j g}z tj|tjd Wntyo}z | t d |fd }~wwWYd }~d Sd }~ww) z0Helper function to check existing session existsrT)timeoutcapture_output:zUsername is not matchzs$z1Megav3Backend.folder_contents..)r8r9rIrJrUrj)r<r^rEfilesrFrFrGr_s  zMegav3Backend.folder_contentscCs*d|jd||g}|||dS)z(Downloads a file from a remote MEGA pathr rNr8r9r:)r<rSrRrErFrFrGr[zMegav3Backend.downloadc Cstd||jd|g}|z||WdSty9}zt|}d|vr-td|ftd||fd}~ww)z$Uploads a file to a remote MEGA pathrrzReached 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)r8r9r:r)r+r)r<rRrSrEr?rQrFrFrGrVs&zMegav3Backend.uploadcCs*dd|jd|g}|||dS)z&Deletes a file from a remote MEGA pathrz-frNrw)r<rSrErFrFrGrTrxzMegav3Backend.deleteN)F)__name__ __module__ __qualname____doc__r r!r6r;rZr]r`rbrdr9r_r[rVrTrFrFrFrGr#s  <    $ rmegav3) __future__rr%rqrIduplicity.backendrrduplicity.errorsrfuturerinstall_aliasesrrrregister_backend uses_netlocextendrFrFrFrGs    u