Ajout d'un système de log
This commit is contained in:
parent
6a9d14b514
commit
8e8f390db2
1 changed files with 17 additions and 7 deletions
|
@ -11,6 +11,7 @@ import pwd
|
||||||
import sys
|
import sys
|
||||||
import json
|
import json
|
||||||
import smtplib
|
import smtplib
|
||||||
|
import datetime
|
||||||
from email.mime.text import MIMEText
|
from email.mime.text import MIMEText
|
||||||
from email.mime.multipart import MIMEMultipart
|
from email.mime.multipart import MIMEMultipart
|
||||||
|
|
||||||
|
@ -83,9 +84,9 @@ def validate(roles,mode='r'):
|
||||||
return True
|
return True
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def getpath(filename):
|
def getpath(filename,backup=False):
|
||||||
"""Récupère le chemin du fichier `filename'"""
|
"""Récupère le chemin du fichier `filename'"""
|
||||||
return os.path.join(STORE, '%s.json' % filename)
|
return os.path.join(STORE, '%s.%s' % (filename,'bak' if backup else 'json'))
|
||||||
|
|
||||||
def writefile(filename, contents):
|
def writefile(filename, contents):
|
||||||
"""Écrit le fichier de manière sécure"""
|
"""Écrit le fichier de manière sécure"""
|
||||||
|
@ -144,7 +145,7 @@ def putfile(filename):
|
||||||
old = getfile(filename)
|
old = getfile(filename)
|
||||||
oldroles = old['roles']
|
oldroles = old['roles']
|
||||||
except TypeError:
|
except TypeError:
|
||||||
old = "//Nouveau fichier"
|
old = "[Création du fichier]"
|
||||||
pass
|
pass
|
||||||
else:
|
else:
|
||||||
if not validate(oldroles,'w'):
|
if not validate(oldroles,'w'):
|
||||||
|
@ -152,7 +153,7 @@ def putfile(filename):
|
||||||
|
|
||||||
notification("Modification de %s" % filename,\
|
notification("Modification de %s" % filename,\
|
||||||
"Le fichier %s a été modifié par %s." %\
|
"Le fichier %s a été modifié par %s." %\
|
||||||
(filename,MYUID),filename,str(old))
|
(filename,MYUID),filename,old)
|
||||||
|
|
||||||
|
|
||||||
writefile(filepath, json.dumps({'roles': roles, 'contents': contents}))
|
writefile(filepath, json.dumps({'roles': roles, 'contents': contents}))
|
||||||
|
@ -169,13 +170,20 @@ def rmfile(filename):
|
||||||
if validate(roles,'w'):
|
if validate(roles,'w'):
|
||||||
notification("Suppression de %s" % filename,\
|
notification("Suppression de %s" % filename,\
|
||||||
"Le fichier %s a été supprimé par %s." %\
|
"Le fichier %s a été supprimé par %s." %\
|
||||||
(filename,MYUID),filename,str(old))
|
(filename,MYUID),filename,old)
|
||||||
os.remove(getpath(filename))
|
os.remove(getpath(filename))
|
||||||
else:
|
else:
|
||||||
return False
|
return False
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def notification(subject,corps,fname,old):
|
def notification(subject,corps,fname,old):
|
||||||
|
back = open(getpath(fname,True),'a')
|
||||||
|
back.write(json.dumps(old))
|
||||||
|
back.write('\n')
|
||||||
|
back.write('* %s: %s\n' % (str(datetime.datetime.now()),corps))
|
||||||
|
back.close()
|
||||||
|
|
||||||
|
# Puis envoi du message
|
||||||
conn = smtplib.SMTP('localhost')
|
conn = smtplib.SMTP('localhost')
|
||||||
frommail = CRANSP_MAIL
|
frommail = CRANSP_MAIL
|
||||||
tomail = DEST_MAIL
|
tomail = DEST_MAIL
|
||||||
|
@ -186,12 +194,14 @@ def notification(subject,corps,fname,old):
|
||||||
msg['From'] = "cranspasswords <%s>" % CRANSP_MAIL
|
msg['From'] = "cranspasswords <%s>" % CRANSP_MAIL
|
||||||
msg['To'] = DEST_MAIL
|
msg['To'] = DEST_MAIL
|
||||||
msg.preamble = "cranspasswords report"
|
msg.preamble = "cranspasswords report"
|
||||||
info = MIMEText(corps + #"\nCi-joint l'ancien fichier." +
|
info = MIMEText(corps +
|
||||||
|
"\nLa précédente version a été sauvegardée" +
|
||||||
|
#"\nCi-joint l'ancien fichier." +
|
||||||
"\n\n-- \nCranspasswords.py",_charset="utf-8")
|
"\n\n-- \nCranspasswords.py",_charset="utf-8")
|
||||||
msg.attach(info)
|
msg.attach(info)
|
||||||
#old = MIMEText(old)
|
#old = MIMEText(old)
|
||||||
#old.add_header('Content-Disposition', 'attachment', filename=fname)
|
#old.add_header('Content-Disposition', 'attachment', filename=fname)
|
||||||
#msg.attach(old)
|
#msg.attach(str(old))
|
||||||
conn.sendmail(frommail,tomail,msg.as_string())
|
conn.sendmail(frommail,tomail,msg.as_string())
|
||||||
conn.quit()
|
conn.quit()
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue