Example #1
0
    def execute(self, *args):
        # essential parameters check
        valid = EssentialParameters(
            self.__class__.__name__,
            [
                self._host, self._user, self._src_dir, self._src_pattern,
                self._dest_dir
            ],
        )
        valid()

        if isinstance(self._key, str):
            self._logger.warning(
                ("DeprecationWarning: "
                 "In the near future, "
                 "the `key` will be changed to accept only dictionary types."))
            key_filepath = self._key
        else:
            key_filepath = self._source_path_reader(self._key)

        sftp = Sftp(
            self._host,
            self._user,
            self._password,
            key_filepath,
            self._passphrase,
            self._timeout,
            self._retry_count,
            self._port,
        )

        files = super().get_target_files(self._src_dir, self._src_pattern)
        for file in files:
            sftp.put_file(file,
                          os.path.join(self._dest_dir, os.path.basename(file)))
Example #2
0
    def execute(self, *args):
        for k, v in self.__dict__.items():
            self._logger.info("%s : %s" % (k, v))

        # essential parameters check
        valid = EssentialParameters(
            self.__class__.__name__,
            [
                self._host, self._user, self._src_dir, self._src_pattern,
                self._dest_dir
            ],
        )
        valid()

        sftp = Sftp(
            self._host,
            self._user,
            self._password,
            self._key,
            self._timeout,
            self._retry_count,
            self._port,
        )
        files = super().get_target_files(self._src_dir, self._src_pattern)

        if len(files) > 0:
            for file in files:
                sftp.put_file(
                    file, os.path.join(self._dest_dir, os.path.basename(file)))
        else:
            self._logger.info(
                "Files to upload do not exist. File pattern: {}".format(
                    os.path.join(self._src_dir, self._src_pattern)))
            return 0
Example #3
0
    def execute(self, *args):
        # essential parameters check
        valid = EssentialParameters(
            self.__class__.__name__,
            [
                self._host, self._user, self._src_dir, self._src_pattern,
                self._dest_dir
            ],
        )
        valid()

        sftp = Sftp(
            self._host,
            self._user,
            self._password,
            self._key,
            self._timeout,
            self._retry_count,
            self._port,
        )

        files = super().get_target_files(self._src_dir, self._src_pattern)
        for file in files:
            sftp.put_file(file,
                          os.path.join(self._dest_dir, os.path.basename(file)))
Example #4
0
    def execute(self, *args):
        # essential parameters check
        valid = EssentialParameters(
            self.__class__.__name__,
            [
                self._host, self._user, self._src_dir, self._src_pattern,
                self._dest_dir
            ],
        )
        valid()

        sftp = Sftp(
            self._host,
            self._user,
            self._password,
            self._key,
            self._timeout,
            self._retry_count,
            self._port,
        )
        files = super().get_target_files(self._src_dir, self._src_pattern)

        if len(files) > 0:
            for file in files:
                sftp.put_file(
                    file, os.path.join(self._dest_dir, os.path.basename(file)))
                self._logger.info("%s is successfully uploaded." % file)
        else:
            self._logger.info(
                "Files to upload do not exist. File pattern: {}".format(
                    os.path.join(self._src_dir, self._src_pattern)))
            if self._quit is True:
                return StepStatus.SUCCESSFUL_TERMINATION
Example #5
0
    def execute(self, *args):
        # essential parameters check
        valid = EssentialParameters(
            self.__class__.__name__,
            [
                self._host, self._user, self._src_dir, self._src_pattern,
                self._dest_dir
            ],
        )
        valid()

        if isinstance(self._key, str):
            self._logger.warning((
                "DeprecationWarning: "
                "In the near future, "
                "the `key` will be changed to accept only dictionary types. "
                "Please see more information "
                "https://github.com/BrainPad/cliboa/blob/master/docs/modules/sftp_upload.md"
            ))
            key_filepath = self._key
        else:
            key_filepath = self._source_path_reader(self._key)

        sftp = Sftp(
            self._host,
            self._user,
            self._password,
            key_filepath,
            self._passphrase,
            self._timeout,
            self._retry_count,
            self._port,
        )
        files = super().get_target_files(self._src_dir, self._src_pattern)

        if len(files) > 0:
            for file in files:
                if self._ignore_empty_file and os.path.getsize(file) == 0:
                    self._logger.info("0 byte file will no be uploaded %s." %
                                      file)
                    continue
                sftp.put_file(
                    file,
                    os.path.join(self._dest_dir, os.path.basename(file)),
                    self._endfile_suffix,
                )
                self._logger.info("%s is successfully uploaded." % file)
        else:
            self._logger.info(
                "Files to upload do not exist. File pattern: {}".format(
                    os.path.join(self._src_dir, self._src_pattern)))
            if self._quit is True:
                return StepStatus.SUCCESSFUL_TERMINATION
Example #6
0
    def test_put_file(self):
        if sys.version_info.minor < 6:
            # ignore test if python version is less 3.6(assert_called is not supported)
            return

        with ExitStack() as stack:
            mock_func = stack.enter_context(
                patch("cliboa.util.sftp.put_file_func"))
            stack.enter_context(patch("paramiko.SSHClient.connect"))
            stack.enter_context(patch("paramiko.SSHClient.open_sftp"))
            sftp = Sftp(host="test", user="******", password="******")
            sftp.put_file(None, None)
            mock_func.assert_called_once()