-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwatch_for_changes.py
More file actions
72 lines (56 loc) · 2.28 KB
/
Copy pathwatch_for_changes.py
File metadata and controls
72 lines (56 loc) · 2.28 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
import time
import sys
import configparser
import tinys3
import threading
import requests
from watchdog.observers import Observer
from watchdog.events import PatternMatchingEventHandler
class MyHandler(PatternMatchingEventHandler):
def __init__(self,config):
super(MyHandler, self).__init__(ignore_patterns=config.get('Configuration', 'extensions').split(";"))
self.tls = config.get('Configuration', 'tls')
self.endpoint = config.get('Configuration', 'endpoint')
self.access_key = config.get('Configuration', 'access_key')
self.security_key =config.get('Configuration', 'security_key')
self.bucket_name = config.get('Configuration', 'bucket_name')
self.log_file = config.get('Configuration', 'logFile')
self.lock = threading.Lock()
def uploadFunc(self,file_path):
conn = tinys3.Connection(self.access_key, self.security_key, self.tls, self.endpoint)
f = open(file_path,'rb')
file_name = file_path.split("/")
try:
resp = conn.upload(file_name[len(file_name)-1],f,self.bucket_name)
self.logToFile(file_name[len(file_name)-1], resp.status_code, time.time())
except requests.exceptions.HTTPError as e:
self.logToFile(file_name[len(file_name)-1],e.response.status_code, time.time())
except:# cleanup
f.close()
def logToFile(self, file_name, success, epoch_time):
self.lock.acquire()
f = open(self.log_file, 'a+')
print(file_name, ' ', success, ' ', epoch_time , file=f)
f.close()
self.lock.release()
def process(self, event):
if event.event_type == 'created': # TODO handle delete
threading.Thread(target=self.uploadFunc,args=(event.src_path,)).start()
def on_modified(self, event):
self.process(event)
def on_created(self, event):
self.process(event)
if __name__ == '__main__':
args = sys.argv[1:]
config = configparser.ConfigParser()
observer = Observer()
config.read(args[0])
inf = config.get('Configuration', 'input')
observer.schedule(MyHandler(config), path=inf)
observer.start()
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
observer.stop()
observer.join()