-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtesttask2.py
More file actions
61 lines (57 loc) · 2.45 KB
/
testtask2.py
File metadata and controls
61 lines (57 loc) · 2.45 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
import os, filecmp, time, shutil
while True:
source = input('Enter path to source directory: ')
if os.path.isdir(source):
break
print('Input correct path, please')
while True:
target = input('Enter path to target directory: ')
if os.path.isdir(target) and target != source:
break
print('Input correct path, please')
while True:
interval = input('input interval for check (sec): ')
if interval.isdecimal():
interval = int(interval)
break
print('Input correct interval, please')
def sync_dirs(source, target):
dirsSrc = [o for o in os.listdir(source) if os.path.isdir(os.path.join(source, o))]
filesSrc = [o for o in os.listdir(source) if os.path.isfile(os.path.join(source, o))]
dirsTar = [o for o in os.listdir(target) if os.path.isdir(os.path.join(target, o))]
filesTar = [o for o in os.listdir(target) if os.path.isfile(os.path.join(target, o))]
for dir in dirsTar:
if dir not in dirsSrc:
dirPath = os.path.join(target, dir)
print('remove target dir \'{}\''.format(dirPath))
shutil.rmtree(dirPath)
for file in filesTar:
if file not in filesSrc:
filePath = os.path.join(target, file)
print('remove target file \'{}\''.format(filePath))
os.remove(filePath)
for dir in dirsSrc:
if dir not in dirsTar:
dirPathSource = os.path.join(source, dir)
dirPathTarget = os.path.join(target, dir)
print('copy dir \'{}\' to \'{}\''.format(dirPathSource, dirPathTarget))
shutil.copytree(dirPathSource, dirPathTarget)
else:
newSource = os.path.join(source, dir)
newTarget = os.path.join(target, dir)
sync_dirs(newSource, newTarget)
for file in filesSrc:
if file not in filesTar:
filePath = os.path.join(source, file)
print('copy file \'{}\' to \'{}\''.format(filePath, target))
shutil.copy2(filePath, target)
else:
newSource = os.path.join(source, file)
newTarget = os.path.join(target, file)
if not filecmp.cmp(newSource, newTarget):
print('replace file \'{}\' with \'{}\''.format(newTarget, newSource))
shutil.copy2(newSource, newTarget)
while True:
sync_dirs(source, target)
print('sleeping {} sec'.format(interval))
time.sleep(interval)