<commit_before>#-*- coding: utf-8 -*-

import os
import errno
import logging
import paramiko

from django.db import models

from .BaseDestination import BaseDestination
from ..mixins     import AccessableMixin

class SFTPDestination(BaseDestination, AccessableMixin):
    
    def backup(sclienelf, contents, subdir, filename, *args, **kwargs):
        print "Hello! This is %s's backup method" % self.__class__.__name__

        fd = os.path.join(self.directory, subdir)
        
        #transport = paramiko.Transport((self.hostname, self.port))
        #transport.connect(username=self.username,
        #                  pkey=paramiko.RSAKey.from_private_key(rsa_private_key))
        #sftp = paramiko.SFTPClient.from_transport(transport)
        #
        client = paramiko.SSHClient()
        client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        client.load_system_host_keys()
        pvtkey = paramiko.RSAKey.from_private_key_file(self.key_filename)
        print (self.hostname,
               int(self.port),
               self.username,
               pvtkey
               )
        client.connect(hostname=self.hostname,
                       port=int(self.port),
                       username=self.username,
                       pkey=pvtkey,
                       timeout=5.0)
        try: 
            sftp = client.open_sftp()
            
            if not self._rexists(sftp,subdir):
                sftp.mkdir(subdir)
                logging.warning('caminho criado')
            
            sftp.chdir(subdir)
            #fn = os.path.join(fd, filename)
            fn = filename
            try:
                with sftp.open(fn, 'wb+') as f:
                    for data in contents.chunks():
                        f.write(data)
            except Exception, e:
                print e
                logging.error(e, errno)
                raise
            return True
        except:
            sftp.close()
            client.close()
            raise
    
    def restore(self, contents, subdir, filename, *args, **kwargs):
        print "Hello! This is %s's restore method" % self.__class__.__name__
        fn = os.path.join(self.directory,
                          subdir,
                          filename)
        try:
            with open(fn, 'rb') as f:
                return ((data for data in iter(lambda: f.read(64 * 1024), '')), True)
        except Exception, e:
            print e
            return None, False
    
    def _rexists(self, sftp, path):
        """
            Verifica se arquivo existe no servidor remoto
            http://docs.python.org/2/library/errno.html#errno.ENOENT
        """
        try:
            sftp.stat(path)
        except IOError, e:
            if e.errno == errno.ENOENT:
                print e, errno
                return False
            raise
        else:
            return True
        
    class Meta:
        verbose_name = u'destino SFTP'<commit_msg>Implemented SFTP recover, for backup recovery<commit_after>#-*- coding: utf-8 -*-

import os
import errno
import logging
import paramiko

from django.db import models

from .BaseDestination import BaseDestination
from ..mixins     import AccessableMixin

class SFTPDestination(BaseDestination, AccessableMixin):
    
    def connect(self):
        client = paramiko.SSHClient()
        client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        client.load_system_host_keys()
        pvtkey = paramiko.RSAKey.from_private_key_file(self.key_filename)
        
        client.connect(hostname=self.hostname,
                       port=int(self.port),
                       username=self.username,
                       pkey=pvtkey,
                       timeout=5.0)
        return client
    
    def backup(self, contents, subdir, filename, *args, **kwargs):
        print "Hello! This is %s's backup method" % self.__class__.__name__

        fd = os.path.join(self.directory, subdir)
        client = self.connect()
        
        try: 
            sftp = client.open_sftp()
            
            if not self._rexists(sftp,subdir):
                sftp.mkdir(subdir)
                logging.warning('caminho criado')
            
            sftp.chdir(subdir)
            try:
                with sftp.open(filename, 'wb+') as f:
                    for data in contents.chunks():
                        f.write(data)
            except Exception, e:
                print e
                logging.error(e, errno)
                raise
            return True
        except:
            sftp.close()
            client.close()
            raise

    
    def restore(self, contents, subdir, filename, *args, **kwargs):
        print "Hello! This is %s's restore method" % self.__class__.__name__
        
        client = self.connect()

        try:
            sftp = client.open_sftp()
            sftp.chdir(subdir)
                
            with sftp.open(filename, 'rb') as f:
                return ([chunks for chunks in iter(lambda: f.read(64 * 1024), '')], True)
        except Exception, e:
            print e
            logging.error(e, errno)
            return None, False
    
    def _rexists(self, sftp, path):
        """
            Verifica se arquivo existe no servidor remoto
            http://docs.python.org/2/library/errno.html#errno.ENOENT
        """
        try:
            sftp.stat(path)
        except IOError, e:
            if e.errno == errno.ENOENT:
                print e, errno
                return False
            raise
        else:
            return True
        
    class Meta:
        verbose_name = u'destino SFTP'
{'fraction_non_alphanumeric': 0.0901884798308966, 'fraction_numerical': 0.0035229874933943982, 'mean_word_length': 1.4783937145351376, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6805853', 'n_tokens_mistral': 1619, 'n_tokens_neox': 1545, 'n_words': 362}