Created
December 13, 2012 15:55
-
-
Save anonymous/4277373 to your computer and use it in GitHub Desktop.
This file contains 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
import os | |
import sys | |
import time | |
import shutil | |
import win32con | |
import win32file | |
import winnt | |
try: | |
path_to_watch = os.path.abspath (sys.argv[1]) | |
path_to_mirror = os.path.abspath (sys.argv[2]) | |
except: | |
print "Usage: %s path_to_watch path_where_to_mirror" % sys.argv[0] | |
print "Mirroring %s @%s to %s" % (path_to_watch, time.asctime(), path_to_mirror) | |
hDir = win32file.CreateFile( | |
path_to_watch, | |
win32con.GENERIC_READ, | |
win32con.FILE_SHARE_READ|win32con.FILE_SHARE_WRITE, | |
None, | |
win32con.OPEN_EXISTING, | |
win32con.FILE_FLAG_BACKUP_SEMANTICS, | |
None | |
) | |
while 1: | |
events = win32file.ReadDirectoryChangesW( | |
hDir, | |
1024, | |
True, | |
win32con.FILE_NOTIFY_CHANGE_FILE_NAME | | |
win32con.FILE_NOTIFY_CHANGE_DIR_NAME | | |
win32con.FILE_NOTIFY_CHANGE_ATTRIBUTES | | |
win32con.FILE_NOTIFY_CHANGE_SIZE | | |
win32con.FILE_NOTIFY_CHANGE_LAST_WRITE | | |
win32con.FILE_NOTIFY_CHANGE_SECURITY, | |
None, | |
None | |
) | |
oldname = None | |
for id, path in events: | |
cpath = os.path.join(path_to_watch, path) | |
rpath = os.path.join(path_to_mirror, path) | |
if id == winnt.FILE_ACTION_ADDED: | |
print "Adding:", cpath | |
if os.path.isfile(cpath): | |
shutil.copyfile(cpath, rpath) | |
if os.path.isdir(cpath): | |
shutil.copytree(cpath, rpath) | |
if id == winnt.FILE_ACTION_REMOVED: | |
print "Removing:", cpath | |
if os.path.isfile(rpath): | |
os.unlink(rpath) | |
if os.path.isdir(rpath): | |
shutil.rmtree(rpath) | |
if id == winnt.FILE_ACTION_MODIFIED: | |
if os.path.isfile(cpath): | |
print "Updating:", cpath | |
shutil.copyfile(cpath, rpath) | |
if os.path.isdir(cpath): | |
pass | |
if id == winnt.FILE_ACTION_RENAMED_OLD_NAME: | |
oldname = rpath | |
if id == winnt.FILE_ACTION_RENAMED_NEW_NAME: | |
os.rename(oldname, rpath) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment