consolidator
/
copyGuigsToRepo.py
63 строки · 2.5 Кб
1import os2import datetime3import shutil4import paramiko5import getpass6from GuidsSync import GuidsSync,GuidDescr7
8repoHost="vsys01775"9repoUser="guid"10repoPass="cghfdrf"11repoPath="/home/blokhin_sn/repo/guids"12ssh:paramiko.SSHClient = None13sftp:paramiko.SFTPClient = None14gs:GuidsSync = None15
16if __name__=="__main__":17try:18guids_path = os.path.dirname(os.path.realpath(__file__))+"\\guids"19
20print("start copy guids to repo")21ssh = paramiko.SSHClient()22ssh.load_host_keys(os.path.expanduser(os.path.join("~", ".ssh", "known_hosts")))23# passw = getpass.getpass(f"Пароль {repoUser}@{repoHost}:")24ssh.connect(repoHost,username=repoUser,password=repoPass)25print(f"{repoHost} connected")26sftp = ssh.open_sftp()27print("sftp opened")28
29for dirname, subdirs, files in os.walk(guids_path):30for filename in files:31itemName=os.path.join(dirname, filename)32stat =os.stat(itemName)33remote_path=f"{repoPath}/{filename}"34times = (stat.st_atime, stat.st_mtime)35sftp.put(itemName,remote_path)36sftp.utime(remote_path,times)37print(f"{remote_path} done")38
39# gs = GuidsSync(repoHost,repoUser,repoPass)40# changed = gs.compare_files(repoPath,guids_path)41# print("Измененные файлы на сервере:")42# for file_guid in changed[0].keys():43# print(f"{file_guid} {changed[0][file_guid].name} -> {changed[0][file_guid].mtime_dt}")44# print("Измененные локальные файлы:")45# for file_guid in changed[1].keys():46# print(f"{file_guid} {changed[1][file_guid].name} -> {changed[1][file_guid].mtime_dt}")47# flag1 = input("Отправить на сервер? [y/n]:")48# if flag1=="y":49# print("start transfer")50# gs.send_to_remote()51# flag2 = input("Получить файлы с сервера? [y/n]:")52# if flag2=="y":53# print("start transfer")54# gs.get_from_remote()55
56except Exception as exp:57print(f"error: {exp}")58
59finally:60if sftp is not None: sftp.close()61if ssh is not None: ssh.close()62if gs is not None: gs.close_connection()63print("disconnected")64
65