pull/167/head
liuzheng712 2016-03-23 22:38:02 +08:00
parent 17a7470a2a
commit b3f83c3362
2 changed files with 96 additions and 37 deletions

View File

@ -162,17 +162,25 @@ class TermLogRecorder(object):
self.log is the all output with delta time log. self.log is the all output with delta time log.
self.vim_pattern is the regexp for check vi/vim/fg model. self.vim_pattern is the regexp for check vi/vim/fg model.
Usage: Usage:
recorder = TermLogRecorder(User) recorder = TermLogRecorder(user=UserObject) # or recorder = TermLogRecorder(uid=UserID)
recoder.write(messages) recoder.write(messages)
recoder.save() # save all log into database recoder.save() # save all log into database
# The following methods all have `user`,`uid`,args. Same as __init__
list = recoder.list() # will give a object about this user's all log info list = recoder.list() # will give a object about this user's all log info
recoder.load_full_log(filemane) # will get full log recoder.load_full_log(filemane) # will get full log
recoder.load_history(filename) # will only get the command history list recoder.load_history(filename) # will only get the command history list
recoder.share_to(filename,user=UserObject) # or recoder.share_to(filename,uid=UserID). will share this commands to someone
recoder.unshare_to(filename,user=UserObject) # or recoder.unshare_to(filename,uid=UserID). will unshare this commands to someone
""" """
def __init__(self, user): def __init__(self, user=None, uid=None):
self.log = {} self.log = {}
if isinstance(user, User):
self.user = user self.user = user
elif uid:
self.user = User.objects.get(id=uid)
else:
self.user = None
self.recoderStartTime = time.time() self.recoderStartTime = time.time()
self.__init_screen_stream() self.__init_screen_stream()
self.recoder = True self.recoder = True
@ -227,6 +235,8 @@ class TermLogRecorder(object):
date = datetime.datetime.now().strftime('%Y%m%d') date = datetime.datetime.now().strftime('%Y%m%d')
filename = str(uuid.uuid4()) filename = str(uuid.uuid4())
filepath = os.path.join(path, 'tty', date, filename + '.zip') filepath = os.path.join(path, 'tty', date, filename + '.zip')
if not os.path.isdir(os.path.join(path, 'tty', date)):
os.makedirs(os.path.join(path, 'tty', date), mode=0777)
while os.path.isfile(filepath): while os.path.isfile(filepath):
filename = str(uuid.uuid4()) filename = str(uuid.uuid4())
filepath = os.path.join(path, 'tty', date, filename + '.zip') filepath = os.path.join(path, 'tty', date, filename + '.zip')
@ -238,27 +248,43 @@ class TermLogRecorder(object):
zf.close() zf.close()
record = TermLog.objects.create(logPath=filepath, logPWD=password, filename=filename, record = TermLog.objects.create(logPath=filepath, logPWD=password, filename=filename,
history=json.dumps(self.CMD), timestamp=int(self.recoderStartTime)) history=json.dumps(self.CMD), timestamp=int(self.recoderStartTime))
if self.user:
record.user.add(self.user) record.user.add(self.user)
except: except:
record = TermLog.objects.create(logPath='locale', logPWD=password, log=json.dumps(self.log), record = TermLog.objects.create(logPath='locale', logPWD=password, log=json.dumps(self.log),
filename=filename, history=json.dumps(self.CMD), filename=filename, history=json.dumps(self.CMD),
timestamp=int(self.recoderStartTime)) timestamp=int(self.recoderStartTime))
if self.user:
record.user.add(self.user) record.user.add(self.user)
def list(self): def list(self, user=None, uid=None):
tmp = [] tmp = []
self._lists = TermLog.objects.filter(user=self.user.id) if isinstance(user, User):
user = user
elif uid:
user = User.objects.get(id=uid)
else:
user = self.user
if user:
self._lists = TermLog.objects.filter(user=user.id)
for i in self._lists.all(): for i in self._lists.all():
tmp.append( tmp.append(
{'filename': i.filename, 'locale': i.logPath == 'locale', 'nick': i.nick, 'timestamp': i.timestamp, {'filename': i.filename, 'locale': i.logPath == 'locale', 'nick': i.nick, 'timestamp': i.timestamp,
'date': i.datetimestamp}) 'date': i.datetimestamp})
return tmp return tmp
def load_full_log(self, filename): def load_full_log(self, filename, user=None, uid=None):
if isinstance(user, User):
user = user
elif uid:
user = User.objects.get(id=uid)
else:
user = self.user
if user:
if self._lists: if self._lists:
self.file = self._lists.get(filename=filename) self.file = self._lists.get(filename=filename)
else: else:
self.file = TermLog.objects.get(user=self.user.id, filename=filename) self.file = TermLog.objects.get(user=user.id, filename=filename)
if self.file.logPath == 'locale': if self.file.logPath == 'locale':
return self.file.log return self.file.log
else: else:
@ -269,10 +295,43 @@ class TermLogRecorder(object):
return self._data return self._data
except KeyError: except KeyError:
return 'ERROR: Did not find %s file' % filename return 'ERROR: Did not find %s file' % filename
return 'ERROR User(None)'
def load_history(self, filename): def load_history(self, filename, user=None, uid=None):
if isinstance(user, User):
user = user
elif uid:
user = User.objects.get(id=uid)
else:
user = self.user
if user:
if self._lists: if self._lists:
self.file = self._lists.get(filename=filename) self.file = self._lists.get(filename=filename)
else: else:
self.file = TermLog.objects.get(user=self.user.id, filename=filename) self.file = TermLog.objects.get(user=user.id, filename=filename)
return self.file.history return self.file.history
return 'ERROR User(None)'
def share_to(self, filename, user=None, uid=None):
if isinstance(user, User):
user = user
elif uid:
user = User.objects.get(id=uid)
else:
pass
if user:
TermLog.objects.get(filename=filename).user.add(user)
return True
return False
def unshare_to(self, filename, user=None, uid=None):
if isinstance(user, User):
user = user
elif uid:
user = User.objects.get(id=uid)
else:
pass
if user:
TermLog.objects.get(filename=filename).user.remove(user)
return True
return False

View File

@ -22,7 +22,7 @@ import tornado.httpclient
from tornado.websocket import WebSocketClosedError from tornado.websocket import WebSocketClosedError
from tornado.options import define, options from tornado.options import define, options
from pyinotify import WatchManager, ProcessEvent, IN_DELETE, IN_CREATE, IN_MODIFY, AsyncNotifier # from pyinotify import WatchManager, ProcessEvent, IN_DELETE, IN_CREATE, IN_MODIFY, AsyncNotifier
import select import select
from connect import Tty, User, Asset, PermRole, logger, get_object, gen_resource from connect import Tty, User, Asset, PermRole, logger, get_object, gen_resource
@ -97,12 +97,12 @@ class MyThread(threading.Thread):
pass pass
class EventHandler(ProcessEvent): # class EventHandler(ProcessEvent):
def __init__(self, client=None): # def __init__(self, client=None):
self.client = client # self.client = client
#
def process_IN_MODIFY(self, event): # def process_IN_MODIFY(self, event):
self.client.write_message(f.read()) # self.client.write_message(f.read())
def file_monitor(path='.', client=None): def file_monitor(path='.', client=None):