-
Notifications
You must be signed in to change notification settings - Fork 131
/
filemon
142 lines (124 loc) · 4.86 KB
/
filemon
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
-------------------------------------------------
File Name: fileMonitor
Description :
Author : CoolCat
date: 2019/1/3
-------------------------------------------------
Change Activity:
2019/1/3:
-------------------------------------------------
"""
__author__ = 'CoolCat'
from watchdog.observers import Observer
from watchdog.events import *
import time
import sys
import argparse
class FileEventHandler(FileSystemEventHandler):
def __init__(self):
FileSystemEventHandler.__init__(self)
self.showDir = False
self.unspyDirs = []
pass
def on_created(self, event): # 文件创建
for p in self.unspyDirs:
if p in event.src_path:
return
self.print_str(1, event.is_directory, event.src_path)
def on_deleted(self, event): # 文件删除
for p in self.unspyDirs:
if p in event.src_path:
return
self.print_str(2, event.is_directory, event.src_path)
def on_moved(self, event): # 文件移动
for p in self.unspyDirs:
if p in event.src_path:
return
self.print_str(3, event.is_directory, "from {} to {}".format(event.src_path,event.dest_path))
def on_modified(self, event): # 文件修改
for p in self.unspyDirs:
if p in event.src_path:
return
self.print_str(4, event.is_directory, event.src_path)
def print_str(self, on_type=int, is_directory=bool, src_path=str):
if on_type == 1:
action = u'created'
colour = u'5;32m' # 文件创建显示绿色
if on_type == 2:
action = u'deleted'
colour = u'0;31m' # 文件删除显示红色
if on_type == 3:
action = u'moved'
colour = u'0;36m' # 文件移动显示青色
if on_type == 4:
action = u'modified'
colour = u'0;34m' # 文件修改显示蓝色
f2d = u'file'
if is_directory:
f2d = u'directory'
pstr = time.strftime(u'[%H:%M:%S]:') + u"\033[%s%s\033[0m" % (
colour, u"{0} {1}:{2}".format(f2d, action, src_path))
if is_directory:
if self.showDir:
print(pstr)
else:
print(pstr)
def logo():
print(""" ______ _ __ __ ___ _ __
/ ____/(_)/ /___ / |/ /____ ____ (_)/ /_ ____ _____
/ /_ / // // _ \ / /|_/ // __ \ / __ \ / // __// __ \ / ___/
/ __/ / // // __// / / // /_/ // / / // // /_ / /_/ // /
/_/ /_//_/ \___//_/ /_/ \____//_/ /_//_/ \__/ \____//_/
Github:https://github.com/TheKingOfDuck/FileMonitor
Coding by {} Version:{}""".format("CoolCat","1.0"))
def getOpts():
parser = argparse.ArgumentParser(usage="\n\tfilemon -p ./wwwroot -hp ./wwwroot/runtime -sd no")
parser.add_argument("-p" , metavar=' /path/spydir',type=str, required=False, help="Spy on the file directory")
parser.add_argument("-hp", metavar='/path/notspydir', type=str, required=False,default="TheKingOfDuck", help="Not spy on the file directory")
parser.add_argument("-sd", metavar='no', type=str, required=False, choices=["yes","no"], default="no", help="Show directory or not")
args = parser.parse_args()
return args
if __name__ == "__main__":
# 输出提示
logo()
#获取命令行参数
args = getOpts()
if args.p == None:
# 获取输入,为兼容 python2 不做汉化
if sys.version_info.major == 2:
try:
spyDir = raw_input(time.strftime('[%H:%M:%S]:') + "Please enter a directory:")
unspyDir = raw_input(time.strftime('[%H:%M:%S]:') + "Unnecessary directory:")
showDir = raw_input(time.strftime('[%H:%M:%S]:') + "Display directory changes(yes or no):")
except:
pass
else:
spyDir = input(time.strftime('[%H:%M:%S]:') + "Please enter a directory:")
unspyDir = input(time.strftime('[%H:%M:%S]:') + "Unnecessary directory:")
showDir = input(time.strftime('[%H:%M:%S]:') + "Show directory changes(yes or no):")
else:
spyDir = args.p
if args.hp != "":
unspyDir = args.hp
else:
unspyDir = {}
showDir = args.sd
# 参数格式化
print(time.strftime('[%H:%M:%S]:') + "\033[1;33m%s\033[0m" % "FileMonitor is running...")
event_handler = FileEventHandler()
if showDir == 'yes':
event_handler.showDir = True
if len(unspyDir) > 1:
event_handler.unspyDirs = unspyDir.split(',')
observer = Observer()
observer.schedule(event_handler, spyDir, True)
observer.start()
try:
while True:
time.sleep(0.1)
except KeyboardInterrupt:
observer.stop()
observer.join()