你好
- User
Assistant: User: User: 你好 Assistant: 你好!有什么我可以帮助你的吗? User: import os import paramiko import hashlib import stat import fnmatch
读取忽视列表
def load_ignore_list(ignore_file_path): ignore_patterns = [] if os.path.exists(ignore_file_path): with open(ignore_file_path, 'r', encoding='utf-8') as f: for line in f: line = line.strip() if line and not line.startswith('#'): # 跳过空行和注释行 ignore_patterns.append(line) return ignore_patterns
判断文件或文件夹是否在忽视列表中
def should_ignore(file_path, ignore_patterns, base_folder): relative_path = os.path.relpath(file_path, base_folder).replace("\", "/") for pattern in ignore_patterns: if fnmatch.fnmatch(relative_path, pattern): return True return False
计算文件的哈希值
def calculate_file_hash(file_path, hash_algorithm='md5'): hash_func = hashlib.new(hash_algorithm) with open(file_path, 'rb') as f: while chunk := f.read(4096): hash_func.update(chunk) return hash_func.hexdigest()
获取远程文件的哈希值
def calculate_remote_file_hash(sftp, remote_file_path, hash_algorithm='md5'): hash_func = hashlib.new(hash_algorithm) with sftp.file(remote_file_path, 'rb') as remote_file: while chunk := remote_file.read(4096): hash_func.update(chunk) return hash_func.hexdigest()
创建SSH客户端
def create_ssh_client(server, port, user, password): ssh = paramiko.SSHClient() ssh.load_system_host_keys() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh.connect(server, port, user, password) return ssh
创建远程目录(如果不存在)
def create_remote_directory(sftp, remote_folder): try: sftp.chdir(remote_folder) # 尝试切换到目录,看看是否存在 except IOError: # 如果目录不存在,则递归创建它 parent_dir = os.path.dirname(remote_folder) if parent_dir != remote_folder: create_remote_directory(sftp, parent_dir) sftp.mkdir(remote_folder) sftp.chdir(remote_folder)
上传文件,如果文件不同或不存在
def upload_file_if_needed(sftp, local_file_path, remote_file_path): try: # 计算远程文件哈希值 remote_hash = calculate_remote_file_hash(sftp, remote_file_path) except IOError: # 如果远程文件不存在,则上传 remote_hash = None
# 计算本地文件哈希值 local_hash = calculate_file_hash(local_file_path) if remote_hash != local_hash: sftp.put(local_file_path, remote_file_path) print(f"Uploaded {local_file_path} to {remote_file_path}") else: print(f"{local_file_path} is up to date, no upload needed.")获取远程文件夹中的文件和目录列表
def get_remote_file_list(sftp, remote_folder): file_list = [] try: for entry in sftp.listdir_attr(remote_folder): file_path = os.path.join(remote_folder, entry.filename).replace("\", "/") file_list.append((file_path, stat.S_ISDIR(entry.st_mode))) # 保存文件和目录的状态 if stat.S_ISDIR(entry.st_mode): file_list.extend(get_remote_file_list(sftp, file_path)) # 递归处理子文件夹 except IOError: pass # 远程文件夹不存在时返回空列表 return file_list
递归删除远程文件或目录
def delete_remote_file_or_directory(sftp, remote_path): try: # 尝试删除文件 sftp.remove(remote_path) print(f"Deleted remote file {remote_path}") except IOError: # 如果是目录,则递归删除目录及其内容 try: for entry in sftp.listdir_attr(remote_path): entry_path = os.path.join(remote_path, entry.filename).replace("\", "/") delete_remote_file_or_directory(sftp, entry_path) # 递归删除 sftp.rmdir(remote_path) # 删除空目录 print(f"Deleted remote directory {remote_path}") except IOError as e: print(f"Failed to delete {remote_path}: {str(e)}")
- Assistant
你好!看起来你在编写一个用于同步本地文件夹到远程服务器的Python脚本。这个程序使用了Paramiko库通过SSH进行文件传输,并支持增量同步以及文件和目录的删除操作。代码的主要功能如下:
- 加载忽视列表:从一个文本文件中读取需要忽略的文件和目录的模式。
- 判断是否忽视:检查文件或文件夹是否在忽视列表中。
- 计算文件哈希值:用于判断文件是否需要上传。
- 创建SSH客户端:连接到远程服务器。
- 创建远程目录:确保上传文件的远程目录存在。
- 上传文件:仅在本地文件与远程文件不同时才上传。
- 获取远程文件列表:获取远程目录中的所有文件和目录的列表。
- 删除远程文件或目录:删除远程端不存在于本地的文件或目录。
- 同步文件夹:总体执行增量同步和删除本地不存在的远程文件。
如果你有具体问题,或者需要我解释代码的某一部分,请告诉我!