Ejemplo n.º 1
0
    def file_exists(self, path):

        d = ssh.connect(self.hostname, self.username, self.password)

        def on_connected(connection):
            def check(sftp):
                d = paths_exist(sftp, path)

                def close(result):
                    d_close = sftp.close()
                    d_close.addCallback(lambda _: result)
                    return d_close

                d.addCallback(close)
                return d

            def error_while_connection_open(failure):
                d = connection.close()
                d.addBoth(lambda _: failure)
                return d

            d = connection.open_sftp()
            d.addCallback(check)
            d.addErrback(error_while_connection_open)
            return d

        d.addCallback(on_connected)
        return d
    def file_exists(self, path):

        d = ssh.connect(self.hostname, self.username, self.password)

        def on_connected(connection):
            def check(sftp):
                d = paths_exist(sftp, path)

                def close(result):
                    d_close = sftp.close()
                    d_close.addCallback(lambda _: result)
                    return d_close

                d.addCallback(close)
                return d

            def error_while_connection_open(failure):
                d = connection.close()
                d.addBoth(lambda _: failure)
                return d

            d = connection.open_sftp()
            d.addCallback(check)
            d.addErrback(error_while_connection_open)
            return d

        d.addCallback(on_connected)
        return d
Ejemplo n.º 3
0
    def upload_file(self, localpath, remotepath):
        def on_connected(connection):
            def transmit_file(sftp):
                with open(localpath, 'r') as f:
                    content = f.read()

                parentdir = os.path.dirname(remotepath)

                d = self.file_exists(parentdir)

                def create_dir(exists):
                    if not exists:
                        createdir_d = sftp.create_directory(parentdir)

                        def dir_not_created(reason):
                            return Failure(
                                ValueError(
                                    "Target directory %s could not be created. \n Reason:%s."
                                    % (parentdir, reason.getTraceback())))

                        createdir_d.addErrback(dir_not_created)
                        return createdir_d

                    else:
                        return defer.succeed(None)

                d.addCallback(create_dir)

                d.addCallback(lambda _: sftp.write_file(remotepath, content))

                def close(_):
                    d_close = sftp.close()
                    d_close.addCallback(lambda _: remotepath)
                    return d_close

                def file_not_copied(reason):
                    return Failure(
                        ValueError(
                            "Exception while copying file to remote path %s. \n Reason:%s."
                            % (remotepath, reason.getTraceback())))

                d.addCallback(close)
                d.addErrback(file_not_copied)
                return d

            def error_while_connection_open(failure):
                d = connection.close()
                d.addBoth(lambda _: failure)
                return d

            d = connection.open_sftp()
            d.addCallback(lambda sftp: transmit_file(sftp))
            d.addErrback(error_while_connection_open)
            return d

        d = ssh.connect(self.hostname, self.username, self.password)
        d.addCallback(on_connected)

        return d
Ejemplo n.º 4
0
    def fetch_file(self, remotepath, localpath):
        def on_connected(connection):
            def transmit_file(sftp):
                d = sftp.read_file(remotepath)

                def close(data):
                    d_close = sftp.close()
                    d_close.addCallback(lambda _: data)
                    return d_close

                d.addCallback(close)
                return d

            def error_while_connection_open(failure):
                d = connection.close()
                d.addBoth(lambda _: failure)
                return d

            def write_file(data):
                parentdir = os.path.dirname(localpath)
                if not os.path.exists(parentdir):
                    os.makedirs(parentdir)
                with open(localpath, 'w') as f:
                    f.write(data)
                return localpath

            d = connection.open_sftp()
            d.addCallback(lambda sftp: transmit_file(sftp))
            d.addCallback(write_file)
            d.addErrback(error_while_connection_open)
            return d

        d = ssh.connect(self.hostname, self.username, self.password)
        d.addCallback(on_connected)

        return d
    def fetch_file(self, remotepath, localpath):
        def on_connected(connection):
            def transmit_file(sftp):
                d = sftp.read_file(remotepath)

                def close(data):
                    d_close = sftp.close()
                    d_close.addCallback(lambda _: data)
                    return d_close

                d.addCallback(close)
                return d

            def error_while_connection_open(failure):
                d = connection.close()
                d.addBoth(lambda _: failure)
                return d

            def write_file(data):
                parentdir = os.path.dirname(localpath)
                if not os.path.exists(parentdir):
                    os.makedirs(parentdir)
                with open(localpath, "w") as f:
                    f.write(data)
                return localpath

            d = connection.open_sftp()
            d.addCallback(lambda sftp: transmit_file(sftp))
            d.addCallback(write_file)
            d.addErrback(error_while_connection_open)
            return d

        d = ssh.connect(self.hostname, self.username, self.password)
        d.addCallback(on_connected)

        return d
    def upload_file(self, localpath, remotepath):
        def on_connected(connection):
            def transmit_file(sftp):
                with open(localpath, "r") as f:
                    content = f.read()

                parentdir = os.path.dirname(remotepath)

                d = self.file_exists(parentdir)

                def create_dir(exists):
                    if not exists:
                        createdir_d = sftp.create_directory(parentdir)

                        def dir_not_created(reason):
                            return Failure(
                                ValueError(
                                    "Target directory %s could not be created. \n Reason:%s."
                                    % (parentdir, reason.getTraceback())
                                )
                            )

                        createdir_d.addErrback(dir_not_created)
                        return createdir_d

                    else:
                        return defer.succeed(None)

                d.addCallback(create_dir)

                d.addCallback(lambda _: sftp.write_file(remotepath, content))

                def close(_):
                    d_close = sftp.close()
                    d_close.addCallback(lambda _: remotepath)
                    return d_close

                def file_not_copied(reason):
                    return Failure(
                        ValueError(
                            "Exception while copying file to remote path %s. \n Reason:%s."
                            % (remotepath, reason.getTraceback())
                        )
                    )

                d.addCallback(close)
                d.addErrback(file_not_copied)
                return d

            def error_while_connection_open(failure):
                d = connection.close()
                d.addBoth(lambda _: failure)
                return d

            d = connection.open_sftp()
            d.addCallback(lambda sftp: transmit_file(sftp))
            d.addErrback(error_while_connection_open)
            return d

        d = ssh.connect(self.hostname, self.username, self.password)
        d.addCallback(on_connected)

        return d