Created
April 11, 2013 14:51
-
-
Save mondalaci/5364020 to your computer and use it in GitHub Desktop.
Automatically overwrite destination file with source file whenever the destination file gets overwritten by an external process.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import os | |
from sys import argv, exit | |
from shutil import copyfile | |
from pyinotify import * | |
def add_watch(): | |
global watch_manager, watch_descriptor, file_to_stick | |
watches = watch_manager.add_watch(file_to_stick, IN_MODIFY | IN_DELETE_SELF) | |
watch_descriptor = watches[file_to_stick] | |
def remove_watch(): | |
global watch_manager, watch_descriptor | |
watch_manager.rm_watch(watch_descriptor) | |
class MyProcessEvent(ProcessEvent): | |
def process_IN_DELETE(self, event): | |
self.stick_file() | |
def process_IN_MODIFY(self, event): | |
self.stick_file() | |
def stick_file(self): | |
global file_to_stick, file_to_clone | |
print 'Sticking %s as %s' % (file_to_clone, file_to_stick) | |
remove_watch() | |
copyfile(file_to_clone, file_to_stick) | |
add_watch() | |
if __name__ == '__main__': | |
if len(argv) != 1+2: | |
program_name = argv[0] | |
print 'Usage %s: FILE-TO-CLONE FILE-TO-STICK' % (program_name) | |
exit(1) | |
file_to_clone = argv[1] | |
file_to_stick = argv[2] | |
watch_manager = WatchManager() | |
notifier = Notifier(watch_manager, MyProcessEvent()) | |
add_watch() | |
while True: | |
try: | |
notifier.process_events() | |
if notifier.check_events(): | |
notifier.read_events() | |
except KeyboardInterrupt: | |
notifier.stop() | |
break |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment