import collections import os import re import shutil import sys def file_sync(src_path,dest_path): if src_path==dest_path or not os.path.isdir(src_path) or not os.path.isdir(dest_path): return dir_pa=re.compile(r"target|.mvn|.git") file_pa=re.compile(r"mvnw|.DS_Store|.gitignore|.+iml|.+log") queue_src_path=collections.deque() queue_dest_path=collections.deque() temp_path=r"D:\threadDumps" temp_tiple=os.walk(temp_path) temp_next=next(temp_tiple) temp_dirs=temp_next[1] temp_files=temp_next[2] queue_src_path.append(src_path) queue_dest_path.append(dest_path) while len(queue_src_path)>=0 and len(queue_dest_path)>=0: if len(queue_src_path)==0 and len(queue_dest_path)==0: queue_src_path.append(src_path) queue_dest_path.append(dest_path) src_path=queue_src_path.popleft() dest_path=queue_dest_path.popleft() src_tiple=os.walk(src_path) src_next=next(src_tiple) src_path=src_next[0] src_dirs=src_next[1] src_dirs=[val for val in src_dirs if not dir_pa.match(val)] src_files=src_next[2] src_files=[val for val in src_files if not file_pa.match(val)] if(os.path.isdir(dest_path)): dest_tiple=os.walk(dest_path) dest_next=next(dest_tiple) dest_path=dest_next[0] dest_dirs=dest_next[1] dest_files=dest_next[2] src_files=[val for val in src_files if not val.endswith("iml") or val.endswith("log")] if len(dest_files)>0: temp_files=[val for val in temp_files if val not in dest_files or val not in temp_files] src_files=[val for val in src_files if val not in dest_files] os.chdir(temp_path) i=j=0 while i<len(temp_files) and j<len(src_files): temp_file=temp_files[i] src_file=src_files[j] shutil.copyfile(src_path+os.path.sep + src_file,temp_file) if(temp_file not in temp_files or temp_file not in dest_files): os.rename(temp_file,src_file) if(os.path.isdir(dest_path)): if(os.path.isfile(src_file)): shutil.move(src_file,dest_path) if(os.path.isfile(temp_file)): shutil.move(temp_file,dest_path) i+=1 j+=1 i=j=0 if(len(dest_dirs)>0): temp_dirs=[val for val in temp_dirs if val not in dest_dirs or val not in temp_dirs] src_dirs=[val for val in src_dirs if val not in dest_dirs] while i<len(temp_dirs) and j<len(src_dirs): temp_dir=temp_dirs[i] src_dir=src_dirs[j] if(os.path.isdir(temp_dir) and temp_dir not in dest_dirs): os.rename(temp_dir,src_dir) if(os.path.isdir(src_dir) and src_dir not in dest_dirs): shutil.move(src_dir,dest_path) elif(os.path.isdir(temp_dir) and temp_dir not in dest_dirs): shutil.move(temp_dir,dest_path) i+=1 j+=1 for dest_dir in dest_dirs: queue_src_path.append(src_path+os.path.sep+dest_dir) queue_dest_path.append(dest_path+os.path.sep+dest_dir) src_paths=[] dest_paths=[] def same_files(src_path,dest_path): if src_path==dest_path or not os.path.isdir(src_path) or not os.path.isdir(dest_path): return src_tiple=os.walk(src_path) src_next=next(src_tiple) src_path=src_next[0] src_dirs=src_next[1] src_files=src_next[2] dest_tiple=os.walk(dest_path) dest_next=next(dest_tiple) dest_path=dest_next[0] dest_dirs=dest_next[1] dest_files=dest_next[2] i=len(src_dirs)-1 j=len(dest_dirs)-1 if(len(src_files)==len(dest_files) and i==j): while i>=0 and j>=0: i-=1 j-=1 same_files(os.path.join(src_path,src_dirs[i]),os.path.join(dest_path,src_dirs[j])) else: src_paths.append(src_path) dest_paths.append(dest_path) def move_file_path(file,dir_path): if not os.path.isfile(file) or not os.path.isdir(dir_path):return path = r"D:\threadDumps" os.chdir(path) temp_tiple = os.walk(path) temp_next = next(temp_tiple) temp_dirs=temp_next[1] while(not os.path.isdir(dir_path)): base_name=os.path.basename(dir_path) dir_name=os.path.dirname(dir_path) os.renames(temp_dirs[0],base_name) shutil.move(base_name,dir_name) dir_path=dir_name+os.path.sep+base_name temp_files = temp_next[2] temp_files=[val for val in temp_files] src_len = len(temp_files) if (src_len == 0): return src_file = os.path.basename(file) if(not os.path.isfile(dir_path+os.path.sep + src_file)): shutil.move(temp_files[0],dir_path) os.chdir(dir_path) os.rename(temp_files[0], src_file) shutil.copyfile(file, dir_path + os.path.sep + src_file) for i in range(1,len(sys.argv),2): same_files(sys.argv[i],sys.argv[i+1]) i=len(src_paths)-1 j=len(dest_paths)-1 if len(src_paths)==len(dest_paths): while len(src_paths)==len(dest_paths) and len(src_paths)>0 and len(dest_paths)>0: print(src_paths) print(dest_paths) src_path=src_paths.pop() dest_path=dest_paths.pop() file_sync(src_path,dest_path)
python同步文件
最新推荐文章于 2024-04-20 22:45:38 发布