@@ -6,8 +6,15 @@ import requests
import tempfile
import json
# 存储上次上传的信息
LAST_UPLOAD_FILE = ' .last_upload.json '
# 缓存目录和文件配置
CACHE_DIR = ' .working_cache '
UPLOAD_CACHE_FILE = os . path . join ( CACHE_DIR , ' upload.json ' )
DOWNLOAD_CACHE_FILE = os . path . join ( CACHE_DIR , ' download.json ' )
# 确保缓存目录存在
def ensure_cache_dir ( ) :
""" 确保缓存目录存在 """
os . makedirs ( CACHE_DIR , exist_ok = True )
def get_file_hash ( file_path ) :
@@ -19,14 +26,19 @@ def get_file_hash(file_path):
return hash_md5 . hexdigest ( )
def get_working_files ( ) :
""" 从环境变量WORKING_FILES获取要打包的文件列表 """
working_files_env = os . environ . get ( ' WORKING_FILES ' , ' ' )
def get_working_dir ( ) :
""" 获取工作目录,处理默认值和路径转换 """
working_dir = os . environ . get ( ' WORKING_DIR ' , ' . ' )
# 确保WORKING_DIR是绝对路径
if not os . path . isabs ( working_dir ) :
working_dir = os . path . abspath ( working_dir )
return working_dir
def get_working_files ( ) :
""" 从环境变量WORKING_FILES获取要打包的文件列表 """
working_files_env = os . environ . get ( ' WORKING_FILES ' , ' ' )
working_dir = get_working_dir ( )
if not working_files_env :
return [ ]
@@ -64,8 +76,8 @@ def check_files_modified(working_files):
current_hashes [ file_path ] = get_file_hash ( file_path )
# 读取上次的哈希值
if os . path . exists ( LAST_ UPLOAD_FILE) :
with open ( LAST_ UPLOAD_FILE, ' r ' ) as f :
if os . path . exists ( UPLOAD_CACHE_ FILE ) :
with open ( UPLOAD_CACHE_ FILE , ' r ' ) as f :
last_data = json . load ( f )
# 比较哈希值
@@ -123,6 +135,16 @@ def download_and_extract(download_url, extract_dir='.', password=None):
# 确保解压目录存在
os . makedirs ( extract_dir , exist_ok = True )
# 检查是否与最后一次下载的URL相同
ensure_cache_dir ( )
if os . path . exists ( DOWNLOAD_CACHE_FILE ) :
with open ( DOWNLOAD_CACHE_FILE , ' r ' ) as f :
last_data = json . load ( f )
if last_data . get ( ' download_url ' ) == download_url :
print ( f " URL { download_url } is the same as last download. Skipping download. " )
return True
# 下载ZIP文件到临时目录
with tempfile . NamedTemporaryFile ( suffix = ' .zip ' , delete = False ) as tmp :
zip_path = tmp . name
@@ -167,6 +189,26 @@ def download_and_extract(download_url, extract_dir='.', password=None):
raise e
print ( f " Download and extraction completed successfully. " )
# 将下载信息保存到解压目录
download_info = {
' download_url ' : download_url ,
' extract_dir ' : extract_dir ,
' timestamp ' : os . path . getmtime ( zip_path ) ,
' encrypted ' : bool ( password ) ,
' extracted_files ' : len ( os . listdir ( extract_dir ) ) if os . path . exists ( extract_dir ) else 0
}
download_info_file = os . path . join ( extract_dir , ' .download_info.json ' )
with open ( download_info_file , ' w ' ) as f :
json . dump ( download_info , f , indent = 2 )
# 保存下载信息到全局缓存
ensure_cache_dir ( )
with open ( DOWNLOAD_CACHE_FILE , ' w ' ) as f :
json . dump ( download_info , f , indent = 2 )
print ( f " Download information saved to { download_info_file } " )
return True
except ( requests . exceptions . RequestException , zipfile . BadZipFile , IOError , Exception ) as e :
raise Exception ( f " Failed to download and extract: { str ( e ) } " )
@@ -199,9 +241,7 @@ def upload_working_files(password=None):
try :
# 获取WORKING_DIR
working_dir = os . environ . get ( ' WORKING_DIR ' , ' . ' )
if not os . path . isabs ( working_dir ) :
working_dir = os . path . abspath ( working_dir )
working_dir = get_working_dir ( )
# 打包文件
with zipfile . ZipFile ( zip_path , ' w ' , zipfile . ZIP_DEFLATED ) as zipf :
@@ -212,22 +252,31 @@ def upload_working_files(password=None):
# 如果提供了密码, 创建加密ZIP文件
if password :
# 直接创建加密ZIP文件, 替换 原有的非加密文件
# 删除 原有的非加密文件
os . unlink ( zip_path )
# 获取WORKING_DIR
working_dir = os . environ . get ( ' WORKING_DIR ' , ' . ' )
if not os . path . isabs ( working_dir ) :
working_dir = os . path . abspath ( working_dir )
working_dir = get_working_dir ( )
# 使用内置的zipfile模块 创建加密ZIP
with zipfile . ZipFile ( zip_path , ' w ' , zipfile . ZIP_DEFLATED , compresslevel = 9 ) as zipf :
# 尝试使用7zip 创建加密ZIP文件
try :
import subprocess
# 构建7zip命令 - 注意: 7zip参数之间没有空格
cmd = [ ' 7z ' , ' a ' , ' -tzip ' , ' -p ' + password , ' -mx9 ' , zip_path ]
# 添加所有文件到命令中
for file_path in working_files :
# 计算相对路径,保持目录结构
arcname = os . path . relpath ( file_path , working_dir )
zipf . write ( file_path , arcname )
# 设置密码用于解密
zipf . setpassword ( password . encode ( ) )
# 7zip需要相对路径, 从working_dir开始
cmd . append ( os . path . relpath ( file_path , working_dir ) )
# 切换到working_dir执行命令, 确保相对路径正确
subprocess . run ( cmd , check = True , capture_output = True , cwd = working_dir )
except ( subprocess . CalledProcessError , FileNotFoundError ) :
# 如果7zip不可用, 抛出错误
raise Exception ( " Failed to create encrypted ZIP file: 7zip is not available or failed to execute " )
# 上传到tmpfile
download_url = upload_to_tmpfile ( zip_path )
@@ -239,7 +288,8 @@ def upload_working_files(password=None):
current_hashes [ file_path ] = get_file_hash ( file_path )
# 保存本次上传信息
with open ( LAST_UPLOAD_FILE , ' w ' ) as f :
ensure_cache_dir ( )
with open ( UPLOAD_CACHE_FILE , ' w ' ) as f :
json . dump ( {
' hashes ' : current_hashes ,
' download_url ' : download_url ,