-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsync.py
More file actions
129 lines (87 loc) · 4.29 KB
/
Copy pathsync.py
File metadata and controls
129 lines (87 loc) · 4.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import os
from os import walk
import shutil
from distutils.dir_util import copy_tree
import datetime
import time
import filecmp
# Input of synchronization parameters
s=str(input('Path to the source folder: ')) # path to the source folder
r=str(input('Path to the replica folder: ')) # path to the replica folder
sessions=int(input('Number of sessions= ')) # number of sessions
interval=float(input('Interval of sync in seconds= '))
# Function, which returnes content of the folder
def folder_content(folder):
folder=os.walk(folder)
for path, dirs, file in folder:
content=[path, dirs, file]
break
return content
# Function, which copies files and folders from source directory to target one
def folder_transfer(source_path, dest_path):
source=folder_content(source_path)
dest=folder_content(dest_path)
# Files copying
for file in source[2]:
if file not in dest[2]: # copies files if it are not present in a replica folder
item_to_copy = r'{0}/{1}'.format(source[0], file)
dest_path = r'{0}'.format(dest[0])
shutil.copy(item_to_copy, dest_path)
log.write(f'\n file "{file}" is copied to {dest[0]}')
print(f'\n {file} is copied to {dest[0]}')
else: # if files are already presented in a replica folder, it compares the content between source and replica files
# and replace replica one by source one in case of non-identity
source_item = ('{0}\{1}'.format(source[0], file))
replica_item = ('{0}\{1}'.format(dest[0], file))
if filecmp.cmp(source_item, replica_item, shallow=False)==False:
item_to_copy = r'{0}\{1}'.format(source[0], file)
dest_path = r'{0}'.format(dest[0])
shutil.copy(item_to_copy, dest_path)
log.write(f'\n file "{file}" in {dest[0]} is replaced')
print(f'\n {file} in {dest[0]} is replaced')
# Files deleting
for file in dest[2]:
if file not in source[2]:
item_to_delete = ('{0}/{1}'.format(dest[0],file))
os.remove(item_to_delete)
print('\n {0}/{1} deleted'.format(dest[0],file))
log.write('\n {0}/{1} deleted'.format(dest[0],file))
# Folders deleting
for fold in dest[1]:
if fold not in source[1]:
item_to_delete = ('{0}/{1}'.format(dest[0], fold))
shutil.rmtree(item_to_delete)
print('\n {0}/{1} deleted'.format(dest[0],fold))
log.write('\n {0}/{1} deleted'.format(dest[0],fold))
# Folders copying
if source[1]!=[]:
for folder in source[1]:
if folder not in dest[1]:
os.mkdir('{0}/{1}'.format(dest[0], folder))
item_to_copy = r'{0}/{1}'.format(source[0], folder)
dest_path = r'{0}/{1}'.format(dest[0], folder)
copy_tree(item_to_copy, dest_path)
print(f'\n {folder} is copied to {dest[0]}')
log.write(f'\n folder "{folder}" is copied to {dest[0]}')
# recursive part which goes deeper along directory tree and repeats itself (copies)
else:
lvl_down_source = str('{0}/{1}'.format(source[0],folder))
lvl_down_dest = str('{0}/{1}'.format(dest[0],folder))
folder_transfer(lvl_down_source, lvl_down_dest)
# Function which supplies repetition with specified interval
def sync(source_path,dest_path, sessions, interval):
i=1
while i<=sessions:
print('\n session ', i)
log.write(f'\n session {i}')
folder_transfer(source_path,dest_path)
i+=1
time.sleep(interval)
# Creation of log file in the same directory, where script exist
log=open(f'{os.path.realpath(os.path.dirname(__file__))}/log.txt', 'w+')
log.write(str(datetime.datetime.now()))
log.close()
# Writing of operations into log-file
log=open(f'{os.path.realpath(os.path.dirname(__file__))}/log.txt', 'a+')
sync(s,r,sessions,interval)
log.close()