Compare commits
2 Commits
efb1b7a893
...
4ed2dd484d
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
4ed2dd484d | ||
|
|
554638babb |
@@ -19,14 +19,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 []
|
||||
@@ -224,9 +229,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:
|
||||
@@ -237,22 +240,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)
|
||||
|
||||
Reference in New Issue
Block a user