This repository has been archived by the owner on Feb 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
watch
executable file
·57 lines (46 loc) · 1.86 KB
/
watch
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
#!/usr/bin/env python
# Script to watch the input directory of the BagIt Indexer
# (https://github.com/mjordan/bagit_indexer) for new, changed,
# renamed, and deleted Bags.
#
# To use this script, you must first install http://pythonhosted.org/watchdog/.
#
# Usage: ./watch /path/to/input/dir
#
# Hit ctrl-c to exit.
import os
import sys
import time
from subprocess import call
from watchdog.observers import Observer
from watchdog.events import PatternMatchingEventHandler
# You will need to change this to the full path to the indexer and
# tombstone scripts, unless you are runing it in the Vagrant VM.
bagit_indexer_path = "/home/vagrant/bagit_indexer/index"
bagit_tombstoner_path = "/home/vagrant/bagit_indexer/tombstone"
class WatchHandler(PatternMatchingEventHandler):
patterns = ["*.zip", "*.tgz", "*.tar.gz", "*.7z"]
ignore_directories = True
# @todo: Log actions.
def on_created(self, event):
call(["php", bagit_indexer_path, "-i", os.path.abspath(event.src_path)])
# def on_modified(self, event):
# call(["php", bagit_indexer_path, "-i", os.path.abspath(event.src_path)])
# Covers moving and renaming. @todo: confirm that we are dealing with the
# same serialized Bag file by comparing its checksum on disk with the one
# stored in ElasticSearch. See https://github.com/mjordan/bagit_indexer/issues/9.
# def on_moved(self, event):
# call(["php", bagit_indexer_path, "-i", os.path.abspath(event.src_path)])
def on_deleted(self, event):
call(["php", bagit_tombstoner_path, "-p", os.path.abspath(event.src_path)])
if __name__ == '__main__':
args = sys.argv[1:]
observer = Observer()
observer.schedule(WatchHandler(), path=args[0] if args else '.')
observer.start()
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
observer.stop()
observer.join()