def sftp_upload_dir(sftp_cli, sftp_dir_path, local_dir_path):
    handle_result = [1, ""]

    try:
        for root, dirs, files in os.walk(local_dir_path):
            if len(files) > 0:
                for filename in files:
                    local_file_path = os.path.join(root, filename)
                    if root != local_dir_path:
                        relative_path = root[len(local_dir_path):]
                        target_sftp_dir = file_util.normal_unix_path(file_util.join_unix_path(sftp_dir_path, relative_path))
                    else:
                        target_sftp_dir = sftp_dir_path

                    rs = sftp_upload_file(sftp_cli, target_sftp_dir, local_file_path)
                    if rs[0] == -1:
                        handle_result[0] = -1
                    if handle_result[1]:
                        handle_result[1] = handle_result[1] + "\n" + rs[1]
                    else:
                        handle_result[1] = rs[1]

            break

    except Exception as e:
        handle_result = [-1, "UPLOAD FAILED, REASON:{}".format(e)]

    return handle_result
def sftp_upload_file(sftp_cli, sftp_dir_path, local_file_path):
    handle_result = [1, ""]
    try:
        sftp_dir_path = file_util.normal_unix_path(sftp_dir_path)
        try:
            sftp_cli.chdir(sftp_dir_path)
        except:
            sftp_dir_path_arr = sftp_dir_path.split(file_util.unix_sep)
            temp_path = ''

            for item in sftp_dir_path_arr:
                if item:
                    if temp_path:
                        temp_path = file_util.join_unix_path(temp_path, item)
                    else:
                        temp_path = file_util.unix_sep + item

                    try:
                        sftp_cli.chdir(temp_path)
                    except:
                        sftp_cli.mkdir(temp_path)

        filename = os.path.basename(local_file_path)
        sftp_file_path = file_util.join_unix_path(sftp_dir_path, filename)
        sftp_cli.put(local_file_path, sftp_file_path)

        handle_result = [1, "UPLOAD " + filename + " SUCCESS"]

    except Exception as e:
        handle_result = [-1, "UPLOAD FAILED, REASON: {}".format(e)]

    return handle_result
Exemple #3
0
def sftp_upload_file(sftp_cli, sftp_dir_path, local_file_path):
    handle_result = [SUCCESS, '']
    try:
        sftp_dir_path = file_util.normal_unix_path(sftp_dir_path)
        try:
            sftp_cli.chdir(sftp_dir_path)
        except:
            sftp_dir_path_arr = sftp_dir_path.split(file_util.unix_sep)
            temp_path = ''

            for item in sftp_dir_path_arr:
                if item:
                    if temp_path:
                        temp_path = file_util.join_unix_path(temp_path, item)
                    else:
                        temp_path = file_util.unix_sep + item

                    try:
                        sftp_cli.chdir(temp_path)
                    except:
                        sftp_cli.mkdir(temp_path)

        filename = os.path.basename(local_file_path)
        sftp_file_path = file_util.join_unix_path(sftp_dir_path, filename)
        sftp_cli.put(local_file_path, sftp_file_path)

        handle_result = [SUCCESS, f'upload {filename} success.']
    except Exception as e:
        handle_result = [FAILED, 'upload failed, reason: {}!'.format(e)]

    return handle_result
Exemple #4
0
    def get_relative_path(self, ori_path):
        mid_path = file_util.normal_unix_path(ori_path)
        if mid_path.startswith(file_util.unix_sep):
            result = mid_path[len(file_util.unix_sep):]
        else:
            result = mid_path

        return result
Exemple #5
0
    def _upload_app(self, sftp_cli, source_file_name, target_file_name, desc_data):
        detail_env = self.config_data[SFTPConfigFile.ENV_FLAG]
        remote_dir = ''
        ver_name = ''
        ver_code = ''
        if re.search('beta_', self.ver_name_code):
            name_code_group = re.split('beta_', self.ver_name_code)
            ver_name = name_code_group[0]
            ver_code = name_code_group[1]
            if re.search('_ent', name_code_group[1]):
                ver_code = re.split('_ent', name_code_group[1])[0]

        elif re.search('_g', self.ver_name_code):
            name_code_group = re.split('_g', self.ver_name_code)
            ver_name = name_code_group[0]
        else:
            if re.search('_ent', self.ver_name_code):
                name_code_group = re.split('_ent', self.ver_name_code)
                ver_name = name_code_group[0]
            else:
                ver_name = self.ver_name_code
        remote_root_dir = self.config_data[_ROOT_SFTP_PATH_FLAG]
        remote_dir = os.path.join(remote_root_dir[self.sftp_root_tag], ver_name, detail_env[self.ver_env])
        remote_dir = file_util.normal_unix_path(remote_dir)

        # 生产版本不一定有ver_code
        if ver_code:
            remote_dir = file_util.join_unix_path(remote_dir, ver_code, self.mobile_os)
        else:
            remote_dir = file_util.join_unix_path(remote_dir, self.mobile_os)

        if len(self.channel):
            remote_dir = file_util.join_unix_path(remote_dir, self.channel)

        # 上传 ipa 包或者 apk 包
        self._upload_setup_file(sftp_cli, remote_dir, source_file_name, target_file_name)

        # 上传打包记录文件(record.txt)
        record_text_data = {'code_version': self.code_version,
                            'target_file_name': target_file_name}
        self._upload_record_file(sftp_cli, remote_dir, record_text_data)

        if desc_data:
            self._upload_desc_file(sftp_cli, remote_dir, desc_data)

        # 上传目录所有文件,进行整体备份
        self._upload_whole_data(sftp_cli, remote_dir)
Exemple #6
0
    def _upload_wechat(self, sftp_cli):
        detail_env = self.config_data[SFTPConfigFile.ENV_FLAG]
        ver_name = ''
        ver_code = ''
        if re.search('(\d\.\d\.\d)(\w*)', self.ver_name_code):
            name_code_group = re.search('(\d\.\d\.\d)(\w*)', self.ver_name_code)
            ver_name = name_code_group.group(1)
            ver_code = name_code_group.group(2)

        # 上传打包记录文件(record.txt)
        self.local_dir_path = self.config_path
        remote_dir = file_util.join_unix_path(self.config_data[_ROOT_SFTP_PATH_FLAG], ver_name, detail_env[self.ver_env])
        remote_dir = file_util.normal_unix_path(remote_dir)
        record_text_data = {'ver_code': ver_code,
                            'code_version': self.code_version,
                            'pack_time': datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')}
        self._upload_record_file(sftp_cli, remote_dir, record_text_data)
Exemple #7
0
def sftp_download_file(sftp_cli,
                       sftp_file_path,
                       local_file_path,
                       force=True,
                       as_file=True,
                       callback=None):
    handle_result = [SUCCESS, '']
    try:
        sftp_file_path = file_util.normal_unix_path(sftp_file_path)
        local_file_path = file_util.normalpath(local_file_path)

        # download file
        if stat.S_ISREG(sftp_cli.stat(sftp_file_path).st_mode):
            sftp_file_name = os.path.basename(sftp_file_path)
            local_dir_path = os.path.dirname(local_file_path)

            if not os.path.isdir(local_dir_path):
                os.makedirs(local_dir_path)

            # 如果本地已有相应文件,强制覆盖时,则直接删除本地现有文件,否则报本地已有相应文件。
            if os.path.isfile(local_file_path):
                if force:
                    os.remove(local_file_path)
                else:
                    handle_result = [
                        FAILED, f'file {local_file_path} already exists!'
                    ]
                    return handle_result

            if as_file:
                with sftp_cli.open(sftp_file_path, 'rb') as fp:
                    shutil.copyfileobj(fp, open(local_file_path, 'wb'))
            else:
                sftp_cli.get(sftp_file_path,
                             local_file_path,
                             callback=callback)

            handle_result = [SUCCESS, f'download {sftp_file_name} success.']
        else:
            handle_result = [
                FAILED, f'sftp_file_path {sftp_file_path} is not a file!'
            ]
    except Exception as e:
        handle_result = [FAILED, 'download failed, reason:{}!'.format(e)]

    return handle_result
Exemple #8
0
    def _download_file(self, sftp_cli, local_dir_path, ver_no, channel,
                       target_file_name, force, as_file):
        env_info = self.config_data[ConfigLabel.ENV_FLAG]
        remote_root_dir = self.config_data[ConfigLabel.SFTP_PATH_FLAG]
        remote_dir = os.path.join(remote_root_dir[self.sftp_root_tag],
                                  self.ver_name, env_info[self.ver_env])
        remote_dir = file_util.normal_unix_path(remote_dir)

        # 生产版本不一定有ver_no
        if ver_no:
            remote_dir = file_util.join_unix_path(remote_dir, ver_no,
                                                  self.mobile_os)
        else:
            remote_dir = file_util.join_unix_path(remote_dir, self.mobile_os)

        if len(channel):
            remote_dir = file_util.join_unix_path(remote_dir, channel)

        sftp_cli.chdir(remote_dir)
        sftp_dir_list = sftp_cli.listdir(remote_dir)
        for filename in sftp_dir_list:
            if filename.endswith(Manager.APK_SUFFIX):
                remote_file_path = file_util.join_unix_path(
                    remote_dir, filename)
                local_file_name = filename
                if len(channel):
                    local_file_name = channel + Manager.APK_SUFFIX
                if target_file_name:
                    local_file_name = target_file_name
                local_file_path = file_util.normalpath(
                    os.path.join(local_dir_path, local_file_name))
                # self._download_single_file(sftp_cli, remote_file_path, local_file_path, force)
                if self.debug:
                    monitor = DownloadMonitor(remote_file_path,
                                              local_file_path)
                    callback = monitor.feedback
                else:
                    callback = None
                self._download_single_file(sftp_cli,
                                           remote_file_path,
                                           local_file_path,
                                           force,
                                           as_file,
                                           callback=callback)
def sftp_upload(sftp_cli, sftp_path, local_path):
    handle_result = [1, ""]
    try:
        sftp_path = file_util.normal_unix_path(sftp_path)
        local_path = file_util.normalpath(local_path)

        if os.path.isdir(local_path):
            rs = sftp_upload_dir(sftp_cli, sftp_path, local_path)
        else:
            rs = sftp_upload_file(sftp_cli, sftp_path, local_path)

        if rs[0] == -1:
            handle_result[0] = -1
        handle_result[1] = handle_result[1] + rs[1]

    except Exception as e:
        result = [-1, "UPLOAD FAILED, REASON:{0}".format(e)]

    return handle_result
def sftp_download(sftp_cli, sftp_file_path, local_dir_path):
    handle_result = [1, ""]
    try:
        sftp_file_path = file_util.normal_unix_path(sftp_file_path)
        local_dir_path = file_util.normalpath(local_dir_path)

        # download file
        if stat.S_ISREG(sftp_cli.stat(sftp_file_path).st_mode):
            sftp_file_name = os.path.basename(sftp_file_path)
            local_dir_path = os.path.join(local_dir_path, sftp_file_name)

            local_dir = os.path.split(local_dir_path)[0]
            local_dir = file_util.normalpath(local_dir)
            if not os.path.exists(local_dir):
                os.makedirs(local_dir)

            sftp_cli.get(sftp_file_path, local_dir_path)

            handle_result = [1, "DOWNLOAD " + sftp_file_name + " SUCCESS"]

        # download dir
        else:
            for filename in sftp_cli.listdir(sftp_file_path):
                sftp_file_name = file_util.join_unix_path(sftp_file_path, filename)
                if is_dir(sftp_cli, sftp_file_name):
                    lad = os.path.join(local_dir_path, filename)
                else:
                    lad = local_dir_path

                rs = sftp_download(sftp_cli, sftp_file_name, lad)

                handle_result[1] = handle_result[1] + rs[1]
                if rs[0] == -1:
                    handle_result[0] = -1
                else:
                    if handle_result[0] != -1:
                        handle_result[0] = 1

    except Exception as e:
        handle_result = [-1, "download fail, reason:{0}".format(e)]

    return handle_result
Exemple #11
0
def sftp_upload(sftp_cli, sftp_path, local_path, only_first_step=True):
    handle_result = [SUCCESS, '']
    try:
        sftp_path = file_util.normal_unix_path(sftp_path)
        local_path = file_util.normalpath(local_path)

        if os.path.isdir(local_path):
            rs = sftp_upload_dir(sftp_cli,
                                 sftp_path,
                                 local_path,
                                 only_first_step=only_first_step)
        else:
            rs = sftp_upload_file(sftp_cli, sftp_path, local_path)

        if rs[0] == FAILED:
            handle_result[0] = FAILED
        handle_result[1] = handle_result[1] + rs[1]
    except Exception as e:
        handle_result = [FAILED, 'upload failed, reason:{}!'.format(e)]

    return handle_result
Exemple #12
0
def sftp_upload_dir(sftp_cli,
                    sftp_dir_path,
                    local_dir_path,
                    only_first_step=True):
    handle_result = [SUCCESS, '']

    try:
        is_completed = False
        is_failed = False
        for root, _, files in os.walk(local_dir_path):
            if root == local_dir_path:
                target_sftp_dir = sftp_dir_path
                if only_first_step:
                    is_completed = True
            else:
                relative_path = root[len(local_dir_path):]
                target_sftp_dir = file_util.normal_unix_path(
                    file_util.join_unix_path(sftp_dir_path, relative_path))

            if len(files) > 0:
                for filename in files:
                    local_file_path = os.path.join(root, filename)
                    rs = sftp_upload_file(sftp_cli, target_sftp_dir,
                                          local_file_path)
                    if handle_result[1]:
                        handle_result[1] = handle_result[1] + '\n' + rs[1]
                    else:
                        handle_result[1] = rs[1]

                    if rs[0] == FAILED:
                        handle_result[0] = FAILED
                        is_failed = True
                        break

            if is_completed or is_failed:
                break
    except Exception as e:
        handle_result = [FAILED, 'upload failed, reason:{}!'.format(e)]

    return handle_result
Exemple #13
0
def sftp_download(sftp_cli, sftp_path, local_dir_path, callback=None):
    handle_result = [SUCCESS, '']
    try:
        sftp_path = file_util.normal_unix_path(sftp_path)
        local_dir_path = file_util.normalpath(local_dir_path)

        # download file
        if stat.S_ISREG(sftp_cli.stat(sftp_path).st_mode):
            sftp_file_name = os.path.basename(sftp_path)
            local_file_path = os.path.join(local_dir_path, sftp_file_name)

            if not os.path.isdir(local_dir_path):
                os.makedirs(local_dir_path)

            sftp_cli.get(sftp_path, local_file_path, callback=callback)

            handle_result = [SUCCESS, f'download {sftp_file_name} success.']
        # download dir
        else:
            for filename in sftp_cli.listdir(sftp_path):
                sftp_file_name = file_util.join_unix_path(sftp_path, filename)
                if is_dir(sftp_cli, sftp_file_name):
                    lad = os.path.join(local_dir_path, filename)
                else:
                    lad = local_dir_path

                rs = sftp_download(sftp_cli, sftp_file_name, lad)

                handle_result[1] = handle_result[1] + rs[1]
                if rs[0] == FAILED:
                    handle_result[0] = FAILED
                else:
                    if handle_result[0] != FAILED:
                        handle_result[0] = SUCCESS
    except Exception as e:
        handle_result = [FAILED, 'download failed, reason:{}!'.format(e)]

    return handle_result