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
def _upload_setup_file(self, sftp_cli, remote_dir, source_file_name, target_file_name): # 判断 sftp 文件夹有没有之前的老包,有的话则需要先删除 try: sftp_cli.chdir(remote_dir) sftp_dir_list = sftp_cli.listdir(remote_dir) for filename in sftp_dir_list: if os.path.splitext(filename)[1] == '.ipa': if (target_file_name.find('ent') > 0 and filename.find('ent') > 0) or \ (target_file_name.find('ent') == -1 and filename.find('ent') == -1): print('开始删除') print(filename) sftp_cli.remove(filename) break elif os.path.splitext(filename)[1] == '.apk': sftp_cli.remove(file_util.join_unix_path(remote_dir, filename)) break except Exception as e: print(e) local_file_path = os.path.join(self.local_dir_path, source_file_name) result = sftp_util.sftp_upload_file(sftp_cli, remote_dir, local_file_path) if self.mobile_os.lower() == 'android': sftp_cli.rename(file_util.join_unix_path(remote_dir, source_file_name), file_util.join_unix_path(remote_dir, target_file_name)) print(result[1])
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
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 _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)
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 _upload_record_file(self, sftp_cli, remote_dir, data, file_name='record.txt'): sftp_cli.chdir(remote_dir) sftp_dir_list = sftp_cli.listdir(remote_dir) remote_file_path = file_util.join_unix_path(remote_dir, file_name) local_file_path = file_util.normalpath(os.path.join(self.local_dir_path, file_name)) for filename in sftp_dir_list: if file_name in filename: sftp_util.sftp_download(sftp_cli, remote_file_path, self.local_dir_path) with open(local_file_path, 'a') as json_file: json.dump(data, json_file, sort_keys=True) json_file.write(os.linesep) result = sftp_util.sftp_upload_file(sftp_cli, remote_dir, local_file_path) print(result[1]) if result[0] == 1: os.remove(local_file_path)
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)
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
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
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
def _upload_whole_data(self, sftp_cli, remote_dir): remote_path = file_util.join_unix_path(remote_dir, 'data', self.code_version) result = sftp_util.sftp_upload(sftp_cli, remote_path, self.local_dir_path) print(result[1])