Files

84 lines
3.0 KiB
Python
Raw Permalink Normal View History

"""Append remaining bytes to partially uploaded file."""
import base64
import hashlib
import subprocess
import sys
PYTHON = r"C:\Users\simon\.workbuddy\binaries\python\versions\3.13.12\python.exe"
JMS = r"C:\Users\simon\.workbuddy\skills\jumpserver-ops\scripts\jms_ops.py"
LOCAL_FILE = r"D:\资料\03-项目开发\wecom_it_smart_desk\deploy_agent_v8.tar.gz"
REMOTE_FILE = "/tmp/deploy_agent_v8.tar.gz"
CHUNK_SIZE = 12 * 1024 # 12KB
def run_jms(*args, timeout=30):
cmd = [PYTHON, JMS] + list(args)
result = subprocess.run(cmd, capture_output=True, timeout=timeout)
out = result.stdout.decode('utf-8', errors='replace') if result.stdout else ''
err = result.stderr.decode('utf-8', errors='replace') if result.stderr else ''
return out + err
def main():
with open(LOCAL_FILE, "rb") as f:
data = f.read()
local_md5 = hashlib.md5(data).hexdigest()
local_size = len(data)
# Check current remote file size
result = run_jms("exec", "-c", f"stat -c %s {REMOTE_FILE}", "--cmd-timeout", "15")
# Parse the number from output
remote_size = 0
for line in result.split('\n'):
line = line.strip()
if line.isdigit():
remote_size = int(line)
break
print(f"Local size: {local_size}")
print(f"Remote size: {remote_size}")
if remote_size >= local_size:
# File already complete, just verify MD5
print("File already complete, verifying MD5...")
result = run_jms("exec", "-c", f"md5sum {REMOTE_FILE}", "--cmd-timeout", "15")
print(f"Remote MD5: {result.strip()}")
print(f"Local MD5: {local_md5}")
if local_md5 in result:
print("\n✅ MD5 verified!")
else:
print("\n❌ MD5 mismatch, need to re-upload!")
return
# Upload remaining bytes
remaining = data[remote_size:]
total_chunks = (len(remaining) + CHUNK_SIZE - 1) // CHUNK_SIZE
print(f"Remaining: {len(remaining)} bytes ({total_chunks} chunks)")
for i in range(total_chunks):
chunk = remaining[i * CHUNK_SIZE : (i + 1) * CHUNK_SIZE]
b64 = base64.b64encode(chunk).decode("ascii")
cmd = f'echo -n "{b64}" | base64 -d >> {REMOTE_FILE}'
run_jms("exec", "-c", cmd, "--cmd-timeout", "15")
print(f" Appended chunk {i + 1}/{total_chunks}")
# Verify
print("\nVerifying MD5...")
result = run_jms("exec", "-c", f"md5sum {REMOTE_FILE}", "--cmd-timeout", "15")
print(f"Remote MD5: {result.strip()}")
print(f"Local MD5: {local_md5}")
if local_md5 in result:
print("\n✅ MD5 verified - upload complete!")
else:
print("\n❌ MD5 mismatch!")
# Check final size
result = run_jms("exec", "-c", f"stat -c %s {REMOTE_FILE}", "--cmd-timeout", "15")
for line in result.split('\n'):
if line.strip().isdigit():
print(f"Final remote size: {line.strip()}")
break
sys.exit(1)
if __name__ == "__main__":
main()