o §§Hal(ã@sŽddlmZddlZddlZddlZddlZddlmZddlm Z ddl m Z e   ¡Gdd„dej jƒZej  de¡ej j dg¡dS) é)Úprint_functionN)Úutil)ÚBackendException)Ústandard_libraryc@s‚eZdZdZdd„Zdd„Zdd„Zdd „Zd d „Zd d „Z dd„Z dd„Z dd„Z dd„Z d dd„Zdd„Zdd„Zdd„ZdS)!Ú 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 Csàtjj ||¡| d¡| d¡| d¡| d¡| d¡| d¡| d¡| d¡| d ¡| d ¡| d ¡|j|_|jdur™t  d ¡d |_ zt |j dƒ}Wnt yl}zt d|j fƒ‚d}~wwi}|D]}| d¡ddd…\}}t| ¡ƒ|| ¡<qq| ¡|d|_|d|_n|j|_|j|_|j dg¡}t|ƒdkoµ|d ¡dv|_| ¡d|jdd…|_| ¡d|jg} z| | ¡WdSt yï}z | |j¡WYd}~dSd}~ww)Nzmega-cmdú mega-execú mega-helpúmega-getú mega-loginú mega-logoutúmega-lsú mega-mkdirúmega-putúmega-rmú mega-whoamiÚHOMEz /.megav3rcÚrz\No password provided in URL and MEGA configuration file for duplicity does not exist as '%s'ú=éÚUsernameÚPasswordÚ no_logoutr)Ú1ÚyesÚtrueú/é) Ú duplicityÚbackendÚBackendÚ__init__Ú_check_binary_existsÚhostnameÚ _hostnameÚusernameÚosÚgetenvÚ_megarcÚopenÚ ExceptionrÚ partitionÚstrÚstripÚcloseÚ _usernameÚ _passwordÚpasswordÚ query_argsÚgetÚlenÚlowerÚ _no_logoutÚensure_mega_cmd_runningÚpathÚ_folderÚ mega_loginÚsubprocess_popenÚ_makedir) ÚselfÚ parsed_urlÚ conf_fileÚeÚmyvarsÚlineÚnameÚvarÚno_logout_optionÚcmd©rFúB/usr/lib/python3/dist-packages/duplicity/backends/megav3backend.pyr .s`            þÿ€ÿ   ÿ €ÿzMegav3Backend.__init__c Cs<z t d|g¡WdSty}ztd|fƒ‚d}~ww)zGChecks that a specified command exists in the running user command pathÚwhichz‚Command '%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!jsþÿ€ÿz"Megav3Backend._check_binary_existscCs6ztjdtjtjd ¡WdStytdƒ‚w)z=Trigger any mega command to ensure mega-cmd server is runningr)ÚstdoutÚstderrzCannot execute mega commandN)rIÚrunÚDEVNULLÚcheck_returncoder)r©r<rFrFrGr6wsý  ÿz%Megav3Backend.ensure_mega_cmd_runningc Csj| ¡dd|g}z| |¡WdSty4}zt|ƒ}d|vr(td|fƒ‚td||fƒ‚d}~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)ÚdeleteÚdecoder)ÚuploadÚ get_canonical)r<Ú source_pathÚremote_filenamerFrFrGÚ_put—s ÿ  þzMegav3Backend._putcCs|j| ¡|j ¡ddS)z-Downloads file from the specified remote path)rSrRN)ÚdownloadrUrB)r<rYÚ local_pathrFrFrGÚ_get¤s þzMegav3Backend._getcCs |jddS)z(Lists files in the specified remote pathT)Ú files_only)Úfolder_contentsrPrFrFrGÚ_list¬s zMegav3Backend._listcCs|j| ¡ddS)z+Deletes file from the specified remote path)rSN)rTrU)r<ÚfilenamerFrFrGÚ_delete±szMegav3Backend._deletecCs,|js dg}| |¡ddg}| |¡dS)z/Function called when backend is done being usedr rÚexitN)r5r:)r<rErFrFrGÚ_close¶s  zMegav3Backend._closec Csøz$tjdddd}| ¡|j ¡ d¡d ¡}||jkr"tdƒ‚Wd Stj y4|  ¡t dƒ‚ty{}z<|j d urE|  ¡|_ d |j|j g}z tj|tjd  ¡Wntyo}z |  ¡t d |fƒ‚d }~wwWYd }~d Sd }~ww) z0Helper function to check existing session existsréT)ÚtimeoutÚcapture_outputú:éÿÿÿÿzUsername is not matchzïs$z1Megav3Backend.folder_contents..)r8r9rIrJrUrj)r<r^rEÚfilesrFrFrGr_äs  zMegav3Backend.folder_contentscCs*d|jd||g}| ¡| |¡dS)z(Downloads a file from a remote MEGA pathr rN©r8r9r:)r<rSrRrErFrFrGr[óózMegav3Backend.downloadc Cstd||jd|g}| ¡z| |¡WdSty9}zt|ƒ}d|vr-td|fƒ‚td||fƒ‚d}~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?rQrFrFrGrVús&þÿÿÿ€÷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  <    $ rÚmegav3)Ú __future__rr%rqrIÚduplicity.backendrrÚduplicity.errorsrÚfuturerÚinstall_aliasesrrrÚregister_backendÚ uses_netlocÚextendrFrFrFrGÚs    u