-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwatch.py
More file actions
executable file
·89 lines (79 loc) · 2.41 KB
/
watch.py
File metadata and controls
executable file
·89 lines (79 loc) · 2.41 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
#!/usr/bin/env python
# encoding: utf-8
"""
filesync.watch
Created by Brennan Chapman on 2012-08-07.
Copyright (c) 2012 Moonbot Studios. All rights reserved.
Watch folder components
"""
import os
import sys
import time
import threading
from sync import Sync
class WatchFolder(threading.Thread):
def __init__(self, src, dst, **kwargs):
threading.Thread.__init__(self)
self.src = src
self.dst = dst
self.freq = 5
if kwargs.has_key('watchFreq'):
self.freq = kwargs['watchFreq']
del kwargs['watchFreq']
self.kwargs = kwargs
def pathWalk(self, path):
"""
Walk through the supplied directory.
Return a list of all the file and folder paths.
"""
result = []
for root, dirs, files in os.walk(path):
for name in files:
result.append(os.path.join(root, name))
for name in dirs:
result.append(os.path.join(root, name))
return result
def loadInitContents(self, s):
self.initContents = s.origdiff.create
init = []
for folder in s.origdiff.create:
for item in s.origdiff.create[folder]:
# Store the mtime in case anything get's updated
path = os.path.join(folder, item)
mtime = os.stat(path).st_mtime
init.append([path, mtime])
self.initContents = init
def getInitContents(self):
"""
Check the initital contents for any updates
based on modification time.
Return a list of paths that haven't been updated.
"""
result = []
for item in self.initContents:
try:
mtime = os.stat(item[0]).st_mtime
if mtime <= item[1]:
result.append(item[0])
except:
pass
return result
def progress(self, msg, perc):
"""
Display the progress messages from the sync
"""
sys.stdout.write("\n" + msg.replace(self.dst, ''))
sys.stdout.flush()
def run(self):
"""
Thread: Run
"""
s = Sync(self.src, self.dst, **self.kwargs)
s.diff()
self.loadInitContents(s)
s.progressfnc = self.progress
while True:
s.diff()
s.difftrim(create=self.getInitContents())
s.run()
time.sleep(self.freq)