Accept Merge Request #2033: (feature/6694 -> develop)
Merge Request: 修改ftp传输代码 Created By: @胡尧 Accepted By: @胡尧 URL: https://jikimo-hn.coding.net/p/jikimo_sfs/d/jikimo_sf/git/merge/2033?initial=true
This commit is contained in:
@@ -37,12 +37,17 @@ class MainController(http.Controller):
|
||||
'password': maintenance_equipment.ftp_password
|
||||
}
|
||||
# 传输nc文件
|
||||
if transfer_nc_files(
|
||||
source_ftp_info,
|
||||
target_ftp_info,
|
||||
'/' + str(model_id),
|
||||
'/home/jikimo/testdir',
|
||||
end_with=tool_groups_id.name + '-all.nc'):
|
||||
return {'code': 200, 'message': 'success'}
|
||||
else:
|
||||
return {'code': 500, 'message': '传输失败'}
|
||||
try:
|
||||
result = transfer_nc_files(
|
||||
source_ftp_info,
|
||||
target_ftp_info,
|
||||
'/' + str(model_id),
|
||||
'/',
|
||||
match_str=r'^\d*_\d*-' + tool_groups_id.name + r'-\w{2}-all\.nc$'
|
||||
)
|
||||
if result:
|
||||
return {'code': 200, 'message': 'success'}
|
||||
else:
|
||||
return {'code': 404, 'message': '未找到编程文件'}
|
||||
except Exception as e:
|
||||
return {'code': 500, 'message': str(e)}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
import logging
|
||||
import os
|
||||
import re
|
||||
from ftplib import FTP, error_perm
|
||||
|
||||
_logger = logging.getLogger(__name__)
|
||||
@@ -132,7 +133,14 @@ class FtpController:
|
||||
|
||||
|
||||
|
||||
def transfer_nc_files(source_ftp_info, target_ftp_info, source_dir, target_dir, end_with='.nc', keep_dir=False):
|
||||
def transfer_nc_files(
|
||||
source_ftp_info,
|
||||
target_ftp_info,
|
||||
source_dir,
|
||||
target_dir,
|
||||
end_with=None,
|
||||
match_str=None,
|
||||
keep_dir=False):
|
||||
"""
|
||||
从源FTP服务器下载所有{end_with}结尾的文件并上传到目标FTP服务器,保持目录结构
|
||||
|
||||
@@ -143,11 +151,12 @@ def transfer_nc_files(source_ftp_info, target_ftp_info, source_dir, target_dir,
|
||||
target_dir: str, 目标FTP上的目标目录
|
||||
keep_dir: bool, 是否保持目录结构,默认False
|
||||
"""
|
||||
trans_status = [False]
|
||||
try:
|
||||
# 连接源FTP
|
||||
source_ftp = FtpController(
|
||||
source_ftp_info['host'],
|
||||
source_ftp_info['port'],
|
||||
source_ftp_info['port'],
|
||||
source_ftp_info['username'],
|
||||
source_ftp_info['password']
|
||||
)
|
||||
@@ -157,7 +166,7 @@ def transfer_nc_files(source_ftp_info, target_ftp_info, source_dir, target_dir,
|
||||
target_ftp = FtpController(
|
||||
target_ftp_info['host'],
|
||||
target_ftp_info['port'],
|
||||
target_ftp_info['username'],
|
||||
target_ftp_info['username'],
|
||||
target_ftp_info['password']
|
||||
)
|
||||
source_ftp.ftp.set_pasv(1)
|
||||
@@ -183,8 +192,14 @@ def transfer_nc_files(source_ftp_info, target_ftp_info, source_dir, target_dir,
|
||||
traverse_dir(f"{current_dir}/{item}", new_relative_path)
|
||||
source_ftp.ftp.cwd('..')
|
||||
except:
|
||||
# 如果是.nc文件则传输
|
||||
if item.lower().endswith(end_with):
|
||||
matched = False
|
||||
# 文件名匹配字符串BT30-(两个字符)-all.nc, 例6667_20250422-BT30-ZM-all.nc
|
||||
if match_str and re.match(match_str, item):
|
||||
matched = True
|
||||
elif end_with and item.lower().endswith(end_with):
|
||||
matched = True
|
||||
|
||||
if matched:
|
||||
# 下载到临时文件
|
||||
temp_path = f"/tmp/{item}"
|
||||
with open(temp_path, 'wb') as f:
|
||||
@@ -198,6 +213,7 @@ def transfer_nc_files(source_ftp_info, target_ftp_info, source_dir, target_dir,
|
||||
with open(temp_path, 'rb') as f:
|
||||
target_ftp.ftp.storbinary(f'STOR {target_path}', f)
|
||||
|
||||
trans_status[0] = True
|
||||
# 删除临时文件
|
||||
os.remove(temp_path)
|
||||
logging.info(f"已传输文件: {item}")
|
||||
@@ -233,17 +249,17 @@ def transfer_nc_files(source_ftp_info, target_ftp_info, source_dir, target_dir,
|
||||
logging.info(f"已清空目标目录 {target_dir}")
|
||||
except Exception as e:
|
||||
logging.error(f"清空目标目录失败: {str(e)}")
|
||||
return False
|
||||
raise Exception(f"清空目标目录失败: {str(e)}")
|
||||
|
||||
# 开始遍历
|
||||
traverse_dir(source_dir)
|
||||
|
||||
logging.info("所有.nc文件传输完成")
|
||||
return True
|
||||
logging.info("所有文件传输完成")
|
||||
return trans_status[0]
|
||||
|
||||
except Exception as e:
|
||||
logging.error(f"传输过程出错: {str(e)}")
|
||||
return False
|
||||
raise e
|
||||
|
||||
finally:
|
||||
# 关闭FTP连接
|
||||
|
||||
Reference in New Issue
Block a user