Main.py:
import win32file, win32con, time, sys, os, re, threading, FileSystemWatcher, shutil, hashlib
global WatchedPath
WatchedPath = "Z:\\"
global changes
changes = []
global processLocked
processLocked = False
global datapath
datapath = "D:\\DATA_test\\"
global blacklist
blacklist = []
class Startup():
def __init__(self):
Main()
class Main():
def __init__(self):
Main.watcherloop(self, WatchedPath)
def watcherloop(self, path, index=0):
global processLocked
changes.append([])
while True:
changes[index] = FileSystemWatcher.FileSystemWatcher.windows_watch_path(self, path)
if len(changes[index]) > 0 and not processLocked:
processLocked = True
#print("Changes" + str(index) + ": " + str(changes[index][0]))
Main.FileHandler(self, changes[index][0])
del changes[index][0]
processLocked = False
def FileHandler(self, file):
# File:
# 0 = Path, 1 = File, 3 = Action
if str(file[1]).endswith(".bak"):
return
#print("Handling: " + str(file))
if file[2] == 1:
#file created
Main.getFile(self, file[0],file[1])
elif file[2] == 2:
#file deleted
Main.deleteFile(self, file[1])
print("Deleted " + str(file[1]))
elif file[2] == 3:
#file updated
Main.getFile(self, file[0],file[1])
elif file[2] == 4:
#renamed
pass
elif file[2] == 5:
#renamed
pass
else:
print("Unexpected FileEvent")
return
def getFile(self, remotename, filename):
if os.path.isfile(os.path.join(datapath, filename)):
if Main.createmd5(self, os.path.join(remotename, filename)) == Main.createmd5(self, os.path.join(datapath, filename)):
return
try:
shutil.copyfile(os.path.join(remotename, filename), os.path.join(datapath, filename))
print("Downloaded " + str(filename))
except PermissionError:
print("Permission denied while copying " + str(remotename))
return
def deleteFile(self, filename):
if os.path.isfile(os.path.join(datapath, str(filename))):
os.remove(os.path.join(datapath, str(filename)))
return
def createmd5(self, filePath):
fh = open(filePath, "rb")
m = hashlib.md5()
while True:
data = fh.read(8192)
if not data:
break
m.update(data)
return m.hexdigest()
if __name__ == "__main__":
Startup()
FileSystemWatcher.py:
import win32file, win32con
class FileSystemWatcher(object):
def windows_watch_path(self, watched_path):
ACTIONS = {
1 : 1,#"Created",
2 : 2,#"Deleted",
3 : 3,#"Updated",
4 : 4,#"RenamedFrom",
5 : 5#"RenamedTo"
}
# Thanks to Claudio Grondi for the correct set of numbers
FILE_LIST_DIRECTORY = 0x0001
try:
hDir = win32file.CreateFile (
watched_path
, FILE_LIST_DIRECTORY
, win32con.FILE_SHARE_READ |
win32con.FILE_SHARE_WRITE |
win32con.FILE_SHARE_DELETE
, None
, win32con.OPEN_EXISTING
, win32con.FILE_FLAG_BACKUP_SEMANTICS
, None
)
except:
# either it does not exist by this time, or some other issue... blah.
# we'll just say "it 'changed' from 'some other expected state'"
return [[watched_path, '', ACTIONS[2]]]
results = 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
)
files_changed = []
for action, fn in results:
files_changed.append(
[
watched_path
, fn
, ACTIONS[action]
]
)
# print fullfn, ACTIONS.get(action, "Unknown")
return files_changed