o ~_@sddlmZddlmZeddlmZddlmZddl Zddl Z ddl Z Gdddej j Zej deej jdgdS) )print_function)standard_library)util)BackendExceptionNc@sreZdZdZddZddZddZdd Zd d Zd d Z ddZ ddZ dddZ ddZ ddZddZdS) MegaBackendz,Connect to remote store using Mega.co.nz APIcCstjj|||d|d|d|d|d|j|_|jdur3t dd|_ n d|_ |j |_ | |_d |_|jd |jd d|_||jd dd dS) Nmegals megamkdirmegagetmegaputmegarmHOMEz/.megarcFz/Root/) duplicitybackendBackend__init___check_binary_existshostname _hostnamepasswordosgetenv_megarcusername _username get_password _password_rootpath_folder_makedir_recursivesplit)self parsed_urlr%@/usr/lib/python3/dist-packages/duplicity/backends/megabackend.pyr%s       zMegaBackend.__init__c Cs<z td|gWdSty}ztd|fd}~ww)z:checks that a specified command exists in the current pathwhichz9command '%s' not found, make sure megatools are installedN) subprocess check_output Exceptionr)r#cmder%r%r&r@s z MegaBackend._check_binary_existscCs8|jr dd|j|g}n dd|jd|j|g}||dS)zcreates a remote directoryr--config-u-pN)rrrsubprocess_popen)r#rr+r%r%r&_makedirIszMegaBackend._makedircCsLtdd|f|j}|D]}|d|}z||WqYqdS)zGcreates a remote directory (recursively the whole path), ingores errorsz mkdir: %sr N)printjoinrr1)r#rpfolderr%r%r&r!Ss zMegaBackend._makedir_recursivecCsJz |t|Wn tyYnw|jt|t|ddS)zDuploads file to Mega (deletes it first, to ensure it does not exist)) local_file remote_fileN)deleterfsdecoder*upload get_canonical)r# source_pathremote_filenamer%r%r&_putas  zMegaBackend._putcCs |jt|t|jddS)zdownloads file from Mega)r7r6N)downloadrr9name)r#r= local_pathr%r%r&_getls   zMegaBackend._getcCs |jddS)zlist files in the backup folderT) files_only)folder_contentsr#r%r%r&_listrs zMegaBackend._listcCs|jt|ddS)zdeletes remote )r7N)r8rr9)r#filenamer%r%r&_deletewszMegaBackend._deleteFcstdjfjrddjjg}n ddjdjjg}t|}t|  d}fdd|D}|r@d d|D}d d|DS) z>lists contents of a folder, optionally ignoring subdirectoriesz megals: %srr-r.r/ cs"g|] }|tjddqS)rN)lenr .0frEr%r& s"z/MegaBackend.folder_contents..cSsg|]}d|vr|qS)r r%rKr%r%r&rNscSsg|]}t|qSr%)rfsencoderKr%r%r&rNs) r2r rrrr(r)rr9stripr")r#rCr+filesr%rEr&rD|s zMegaBackend.folder_contentsc Csftd|f|jrdd|jdd||jd|g}ndd|jd|jdd||jd|g }||dS) Nz megaget: %sr r- --no-progress--pathr r.r/r2rr rrr0)r#r7r6r+r%r%r&r?s zMegaBackend.downloadc Cstd|f|jrdd|jdd|jd||g}ndd|jd|jdd|jd||g }z||WdStyW}zt|}d |vrKtd |ftd ||fd}~ww) Nz megaput: %sr r-rRrSr r.r/ EOVERQUOTAzwMEGA account over quota, could not write file : '%s' . Upgrade your storage at https://mega.nz/pro or remove some data.z0Failed writing file '%s' to MEGA , reason : '%s') r2rr rrr0r*strr)r#r6r7r+r, error_strr%r%r&r:s& zMegaBackend.uploadcCsZtd|f|jrdd|j|jd|g}ndd|jd|j|jd|g}||dS)Nz megarm: %sr r-r r.r/rT)r#r7r+r%r%r&r8s zMegaBackend.deleteN)F)__name__ __module__ __qualname____doc__rrr1r!r>rBrFrHrDr?r:r8r%r%r%r&r"s     rmega) __future__rfuturerinstall_aliasesrrduplicity.errorsrduplicity.backendrr(rrrregister_backend uses_netlocextendr%r%r%r&s