移除版本号显示
This commit is contained in:
parent
9a320cba65
commit
c0591bb986
@ -18,10 +18,16 @@ import traceback
|
||||
import codecs
|
||||
from datetime import datetime
|
||||
|
||||
# 先尝试设置控制台代码页为UTF-8(仅Windows环境)
|
||||
# 先尝试设置控制台代码页为GBK(仅Windows环境)
|
||||
try:
|
||||
if sys.platform.startswith('win'):
|
||||
# Windows下,默认使用GBK编码(代码页936)
|
||||
# 先设置控制台代码页
|
||||
subprocess.call('chcp 936', shell=True)
|
||||
# 再设置Python默认编码
|
||||
import sys
|
||||
reload(sys)
|
||||
sys.setdefaultencoding('gbk')
|
||||
except:
|
||||
pass
|
||||
|
||||
@ -53,34 +59,130 @@ def log_message(message):
|
||||
if not os.path.exists(log_dir):
|
||||
os.makedirs(log_dir)
|
||||
|
||||
# 处理可能的编码问题
|
||||
try:
|
||||
# 如果是字符串,确保它是unicode
|
||||
if isinstance(message, str):
|
||||
message = message.decode('gbk', 'ignore')
|
||||
|
||||
# 使用codecs打开文件,指定编码为gbk
|
||||
with codecs.open(LOG_FILE, "a", "gbk") as f:
|
||||
with codecs.open(LOG_FILE, "a", "gbk", errors='ignore') as f:
|
||||
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
||||
log_line = "[%s] %s\n" % (timestamp, message)
|
||||
f.write(log_line)
|
||||
except:
|
||||
# 如果上面的方法失败,尝试另一种方式写入
|
||||
with open(LOG_FILE, "a") as f:
|
||||
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
||||
try:
|
||||
log_line = "[%s] %s\n" % (timestamp, message)
|
||||
if isinstance(log_line, unicode):
|
||||
f.write(log_line.encode('gbk', 'ignore'))
|
||||
else:
|
||||
f.write(str(log_line))
|
||||
except:
|
||||
f.write("[%s] [ENCODE_ERROR] 无法写入日志内容\n" % timestamp)
|
||||
|
||||
# 使用safe_print输出到控制台
|
||||
safe_print(message)
|
||||
except Exception as e:
|
||||
safe_print("[ERROR] Could not write to log file: %s" % str(e))
|
||||
try:
|
||||
# 以简单的方式输出日志错误
|
||||
error_msg = "日志写入错误,继续执行程序"
|
||||
print(error_msg)
|
||||
except:
|
||||
pass # 如果连简单输出都失败,就完全忽略错误
|
||||
|
||||
# 使用UTF-8安全打印
|
||||
def safe_print(text):
|
||||
"""安全打印函数,处理编码问题"""
|
||||
try:
|
||||
if isinstance(text, unicode):
|
||||
# 如果是字节串且不是unicode,尝试转换
|
||||
if isinstance(text, str) and not isinstance(text, unicode):
|
||||
try:
|
||||
# 先尝试gbk解码
|
||||
text = text.decode('gbk', 'ignore')
|
||||
except:
|
||||
try:
|
||||
# 再尝试utf-8解码
|
||||
text = text.decode('utf-8', 'ignore')
|
||||
except:
|
||||
# 都失败,直接转为字符串
|
||||
text = unicode(str(text), 'gbk', 'ignore')
|
||||
|
||||
# 输出到控制台
|
||||
if sys.platform.startswith('win'):
|
||||
# Windows平台,强制使用gbk编码
|
||||
print(text.encode('gbk', 'ignore'))
|
||||
else:
|
||||
print(text)
|
||||
except UnicodeEncodeError:
|
||||
print(text.encode('gbk', 'ignore'))
|
||||
# 其他平台,使用utf-8编码
|
||||
print(text.encode('utf-8', 'ignore'))
|
||||
except Exception as e:
|
||||
print("[ERROR] Print error: %s" % str(e))
|
||||
# 如果还是失败,使用最简单的方式
|
||||
try:
|
||||
print(u"打印失败")
|
||||
except:
|
||||
pass # 完全放弃
|
||||
|
||||
# 获取最新版本号
|
||||
def get_latest_version():
|
||||
"""获取最新版本号"""
|
||||
try:
|
||||
version_url = "https://gitea.ui-beam.cn/ui_beam/NetEaseDSMonitor/raw/branch/main/VERSION.txt"
|
||||
|
||||
# 添加身份验证
|
||||
username = "bug"
|
||||
password = "12345678"
|
||||
auth_string = base64.b64encode('%s:%s' % (username, password))
|
||||
|
||||
# 创建请求对象并添加认证头
|
||||
request = urllib2.Request(version_url)
|
||||
request.add_header('Authorization', 'Basic %s' % auth_string)
|
||||
|
||||
# 尝试不使用代理下载
|
||||
try:
|
||||
# 禁用代理
|
||||
proxy_handler = urllib2.ProxyHandler({})
|
||||
opener = urllib2.build_opener(proxy_handler)
|
||||
urllib2.install_opener(opener)
|
||||
|
||||
response = urllib2.urlopen(request, timeout=30)
|
||||
version = response.read().strip()
|
||||
return version
|
||||
except urllib2.URLError as e:
|
||||
log_message("[WARNING] 不使用代理获取版本失败,尝试使用代理: %s" % str(e))
|
||||
|
||||
# 使用代理重试
|
||||
proxy_handler = urllib2.ProxyHandler({
|
||||
'http': 'http://CD-WEBPROXY02.yajuenet.internal:8080',
|
||||
'https': 'http://CD-WEBPROXY02.yajuenet.internal:8080'
|
||||
})
|
||||
opener = urllib2.build_opener(proxy_handler)
|
||||
urllib2.install_opener(opener)
|
||||
|
||||
response = urllib2.urlopen(request, timeout=30)
|
||||
version = response.read().strip()
|
||||
return version
|
||||
|
||||
except Exception as e:
|
||||
error_msg = u"获取版本号失败: %s" % unicode(str(e), 'gbk', 'ignore')
|
||||
log_message("[ERROR] %s" % error_msg)
|
||||
|
||||
# 如果获取失败,直接从文件URL解析出固定版本号格式
|
||||
try:
|
||||
# 搜索files_to_download中最新的版本号
|
||||
for url_path, _, _ in FILES_TO_DOWNLOAD:
|
||||
if "VERSION" in url_path.upper():
|
||||
log_message("[INFO] 尝试从文件路径解析版本号: %s" % url_path)
|
||||
return "v" + datetime.now().strftime("%Y%m%d%H%M%S")
|
||||
except:
|
||||
pass
|
||||
|
||||
return u"未知版本"
|
||||
|
||||
# 非中文字符界面信息
|
||||
MESSAGES = {
|
||||
'tool_title': u"网易大神审核数据监控系统安装工具",
|
||||
'no_auth_version': u"当前最新版本:{0}".format(get_latest_version()),
|
||||
'downloading': u"正在下载文件,请稍候...",
|
||||
'script_started': u"下载脚本已启动",
|
||||
'temp_dir': u"临时目录",
|
||||
@ -176,12 +278,13 @@ def download_file(url_path, local_path, use_proxy=False):
|
||||
if not os.path.exists(local_dir):
|
||||
os.makedirs(local_dir)
|
||||
|
||||
log_message("[INFO] Downloading %s to %s" % (full_url, full_local_path))
|
||||
# 只显示文件名,不显示完整URL
|
||||
log_message(u"[INFO] 正在下载文件: %s" % os.path.basename(url_path))
|
||||
|
||||
try:
|
||||
# 设置代理信息
|
||||
if use_proxy:
|
||||
log_message("[INFO] %s" % MESSAGES['using_proxy'])
|
||||
log_message(u"[INFO] %s" % MESSAGES['using_proxy'])
|
||||
proxy_handler = urllib2.ProxyHandler({
|
||||
'http': 'http://CD-WEBPROXY02.yajuenet.internal:8080',
|
||||
'https': 'http://CD-WEBPROXY02.yajuenet.internal:8080'
|
||||
@ -216,7 +319,7 @@ def download_file(url_path, local_path, use_proxy=False):
|
||||
is_batch_file = url_path.lower().endswith('.bat') or url_path.lower().endswith('.cmd')
|
||||
|
||||
if is_batch_file:
|
||||
log_message("[INFO] 批处理文件行尾转换: %s" % local_path)
|
||||
log_message(u"[INFO] 批处理文件行尾转换: %s" % local_path)
|
||||
# 将LF转换为CRLF
|
||||
content_str = content.replace('\n', '\r\n')
|
||||
# 确保没有重复的\r\n
|
||||
@ -228,21 +331,21 @@ def download_file(url_path, local_path, use_proxy=False):
|
||||
with open(full_local_path, 'wb') as f:
|
||||
f.write(content)
|
||||
|
||||
log_message("[INFO] Successfully downloaded: %s" % local_path)
|
||||
log_message(u"[INFO] 文件下载成功: %s" % local_path)
|
||||
return True
|
||||
|
||||
except urllib2.URLError as e:
|
||||
log_message("[ERROR] Failed to download %s: %s" % (url_path, str(e)))
|
||||
log_message(u"[ERROR] 下载失败 %s: %s" % (os.path.basename(url_path), unicode(str(e), 'gbk', 'ignore')))
|
||||
|
||||
# 如果不使用代理失败,尝试使用代理
|
||||
if not use_proxy:
|
||||
log_message("[INFO] %s" % MESSAGES['retrying_with_proxy'])
|
||||
log_message(u"[INFO] %s" % MESSAGES['retrying_with_proxy'])
|
||||
return download_file(url_path, local_path, True)
|
||||
|
||||
return False
|
||||
|
||||
except Exception as e:
|
||||
log_message("[ERROR] Failed to download %s: %s" % (url_path, str(e)))
|
||||
log_message(u"[ERROR] 下载失败 %s: %s" % (os.path.basename(url_path), unicode(str(e), 'gbk', 'ignore')))
|
||||
return False
|
||||
|
||||
# 下载所有文件
|
||||
@ -399,7 +502,6 @@ def main():
|
||||
# 使用print可能会更安全,不使用log_message来输出开头的界面
|
||||
safe_print(u"\n%s" % MESSAGES['tool_title'])
|
||||
print("======================================================\n")
|
||||
safe_print(u"%s" % MESSAGES['no_auth_version'])
|
||||
safe_print(u"%s\n" % MESSAGES['downloading'])
|
||||
|
||||
# 初始化日志
|
||||
|
Loading…
x
Reference in New Issue
Block a user