Dans la série suppression des liens symboliques : /usr/scripts/lib devient /usr/scripts/cranslib
* Comme ça c'est plus clair que c'est un truc custom crans * Le lien symbolique /usr/scripts/gestion/crans/ est retiré. D'autres suivront.
This commit is contained in:
parent
c57795f23b
commit
10275229a1
35 changed files with 20 additions and 29 deletions
40
cranslib/utils/__init__.py
Normal file
40
cranslib/utils/__init__.py
Normal file
|
@ -0,0 +1,40 @@
|
|||
import re
|
||||
import unicodedata
|
||||
|
||||
def __init__():
|
||||
pass
|
||||
|
||||
def QuoteForPOSIX(string):
|
||||
'''quote a string so it can be used as an argument in a posix shell
|
||||
|
||||
According to: http://www.unix.org/single_unix_specification/
|
||||
2.2.1 Escape Character (Backslash)
|
||||
|
||||
A backslash that is not quoted shall preserve the literal value
|
||||
of the following character, with the exception of a <newline>.
|
||||
|
||||
2.2.2 Single-Quotes
|
||||
|
||||
Enclosing characters in single-quotes ( '' ) shall preserve
|
||||
the literal value of each character within the single-quotes.
|
||||
A single-quote cannot occur within single-quotes.
|
||||
|
||||
source : http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/498202
|
||||
'''
|
||||
|
||||
return "\\'".join(["'" + p + "'" for p in string.split("'")])
|
||||
|
||||
|
||||
def suppression_diacritics(s):
|
||||
"""source : http://wikipython.flibuste.net/moin.py/JouerAvecUnicode#head-1213938516c633958921591439c33d202244e2f4
|
||||
"""
|
||||
def remove(char):
|
||||
deco = unicodedata.decomposition(char)
|
||||
if deco:
|
||||
return unichr(int(deco.split()[0],16))
|
||||
return char
|
||||
return ''.join([remove(a) for a in s])
|
||||
|
||||
supprimer_accents = suppression_diacritics
|
||||
desaccentuer = suppression_diacritics
|
||||
|
50
cranslib/utils/exceptions.py
Normal file
50
cranslib/utils/exceptions.py
Normal file
|
@ -0,0 +1,50 @@
|
|||
#!/usr/bin/env python
|
||||
# -*- coding: iso-8859-15 -*-
|
||||
# #############################################################
|
||||
# ..
|
||||
# .... ............ ........
|
||||
# . ....... . .... ..
|
||||
# . ... .. .. .. .. ..... . ..
|
||||
# .. .. ....@@@. .. . ........ .
|
||||
# .. . .. ..@.@@..@@. .@@@@@@@ @@@@@@. ....
|
||||
# .@@@@. .@@@@. .@@@@..@@.@@..@@@..@@@..@@@@.... ....
|
||||
# @@@@... .@@@.. @@ @@ .@..@@..@@...@@@. .@@@@@. ..
|
||||
# .@@@.. . @@@. @@.@@..@@.@@..@@@ @@ .@@@@@@.. .....
|
||||
# ...@@@.... @@@ .@@.......... ........ ..... ..
|
||||
# . ..@@@@.. . .@@@@. .. ....... . .............
|
||||
# . .. .... .. .. . ... ....
|
||||
# . . .... ............. .. ...
|
||||
# .. .. ... ........ ... ...
|
||||
# ................................
|
||||
#
|
||||
# #############################################################
|
||||
"""
|
||||
exceptions.py
|
||||
|
||||
Fonctions pour gérer les exceptions
|
||||
|
||||
Copyright (c) 2006 by www.crans.org
|
||||
Some code, from cherrypy lib
|
||||
"""
|
||||
|
||||
import sys, traceback
|
||||
|
||||
|
||||
def formatExc(exc=None):
|
||||
"""formatExc(exc=None) -> exc (or sys.exc_info if None), formatted."""
|
||||
if exc is None:
|
||||
exc = sys.exc_info()
|
||||
|
||||
if exc == (None, None, None):
|
||||
return ""
|
||||
|
||||
page_handler_str = ""
|
||||
args = list(getattr(exc[1], "args", []))
|
||||
if args:
|
||||
if len(args) > 1:
|
||||
page_handler = args.pop()
|
||||
page_handler_str = 'Page handler: %s\n' % repr(page_handler)
|
||||
exc[1].args = tuple(args)
|
||||
return page_handler_str + "".join(traceback.format_exception(*exc))
|
||||
|
||||
|
44
cranslib/utils/files.py
Normal file
44
cranslib/utils/files.py
Normal file
|
@ -0,0 +1,44 @@
|
|||
#!/usr/bin/env python
|
||||
# -*- coding: iso-8859-15 -*-
|
||||
# #############################################################
|
||||
# ..
|
||||
# .... ............ ........
|
||||
# . ....... . .... ..
|
||||
# . ... .. .. .. .. ..... . ..
|
||||
# .. .. ....@@@. .. . ........ .
|
||||
# .. . .. ..@.@@..@@. .@@@@@@@ @@@@@@. ....
|
||||
# .@@@@. .@@@@. .@@@@..@@.@@..@@@..@@@..@@@@.... ....
|
||||
# @@@@... .@@@.. @@ @@ .@..@@..@@...@@@. .@@@@@. ..
|
||||
# .@@@.. . @@@. @@.@@..@@.@@..@@@ @@ .@@@@@@.. .....
|
||||
# ...@@@.... @@@ .@@.......... ........ ..... ..
|
||||
# . ..@@@@.. . .@@@@. .. ....... . .............
|
||||
# . .. .... .. .. . ... ....
|
||||
# . . .... ............. .. ...
|
||||
# .. .. ... ........ ... ...
|
||||
# ................................
|
||||
#
|
||||
# #############################################################
|
||||
"""
|
||||
files.py
|
||||
|
||||
Fonction de base sur les fichiers
|
||||
|
||||
Copyright (c) 2006 by www.crans.org
|
||||
"""
|
||||
|
||||
import datetime, time, os
|
||||
|
||||
def ageOfFile(pathToFile):
|
||||
"""renvoie l'age d'un fichier en secondes"""
|
||||
pathToFile = os.path.expanduser(pathToFile)
|
||||
return int(time.time()) - os.path.getmtime(pathToFile)
|
||||
|
||||
def fileIsOlderThan(pathToFile, days=0, hours=0, minutes=0, seconds=0):
|
||||
"""teste si un fichier est plus vieux on non que la valeur donnée"""
|
||||
time= (((days*24) + hours) * 60 + minutes) * 60 + seconds
|
||||
return ageOfFile(pathToFile) > time
|
||||
|
||||
def dirIsEmpty(pathToDir):
|
||||
"""teste : répond True si le dossier est vide."""
|
||||
return os.listdir(pathToDir) == []
|
||||
|
36
cranslib/utils/logs.py
Normal file
36
cranslib/utils/logs.py
Normal file
|
@ -0,0 +1,36 @@
|
|||
# -*- coding: utf8 -*-
|
||||
""" Cr@ns logging : logging utilities for cr@ns scripts
|
||||
"""
|
||||
import logging
|
||||
import os
|
||||
|
||||
LOG_FOLDER = "/var/log/crans/"
|
||||
__version__ = "0.1"
|
||||
|
||||
def getFileLogger(name):
|
||||
LOGGER.warning("getFileLogger is deprecated, use CransFileHandler instead.")
|
||||
logger = logging.getLogger(name)
|
||||
hdlr = CransFileHandler(name)
|
||||
logger.addHandler(hdlr)
|
||||
logger.setLevel(logging.INFO)
|
||||
return logger
|
||||
|
||||
class CransFileHandler(logging.FileHandler):
|
||||
def __init__(self, name):
|
||||
filepath = os.path.join(LOG_FOLDER, name + ".log")
|
||||
logging.FileHandler.__init__(self, filepath)
|
||||
formatter = logging.Formatter('%(asctime)s %(name)s %(levelname)s %(message)s')
|
||||
self.setFormatter(formatter)
|
||||
|
||||
### Un peu de configuration
|
||||
# On log systematiquement les warning, error, exception sous "crans"
|
||||
# sur l'ecran.
|
||||
CRANSLOGGER = logging.getLogger("crans")
|
||||
CRANSLOGGER.setLevel(logging.WARNING)
|
||||
streamhdlr = logging.StreamHandler()
|
||||
streamhdlr.setLevel(logging.ERROR)
|
||||
streamhdlr.setFormatter(logging.Formatter('%(levelname)s: %(message)s'))
|
||||
CRANSLOGGER.addHandler(streamhdlr)
|
||||
CRANSLOGGER.addHandler(CransFileHandler("crans"))
|
||||
|
||||
LOGGER = logging.getLogger("crans.logging")
|
50
cranslib/utils/quota.py
Normal file
50
cranslib/utils/quota.py
Normal file
|
@ -0,0 +1,50 @@
|
|||
|
||||
# -*- coding: utf8 -*-
|
||||
import os
|
||||
|
||||
LABELS = {
|
||||
"/home":u"Dossier personnel",
|
||||
"/var/mail":u"Boite de réception"
|
||||
}
|
||||
|
||||
def getFloat( chose ):
|
||||
chose = chose.replace(',', '.')
|
||||
return float(chose)
|
||||
|
||||
def getUserQuota( userLogin ):
|
||||
pipe = os.popen("sudo quota %s" % userLogin)
|
||||
string_result = pipe.read()
|
||||
pipe.close()
|
||||
string_result = string_result.split("\n")
|
||||
quotas = []
|
||||
for a_line in string_result[2:3]:
|
||||
usage, quota, limite, percentage, fs = a_line.split("\t")
|
||||
line_dict = {
|
||||
"label": "Quota personnel",
|
||||
"usage":getFloat(usage),
|
||||
"quota":getFloat(quota),
|
||||
"limite":getFloat(limite),
|
||||
"%":getFloat(percentage),
|
||||
"filesystem":"rda", # pourquoi pas ?
|
||||
}
|
||||
quotas.append(line_dict)
|
||||
return quotas
|
||||
|
||||
|
||||
|
||||
def fake_getUserQuota( userLogin ):
|
||||
return [
|
||||
{'%': 33.9,
|
||||
'quota': 390.62,
|
||||
'label': u'Dossier personnel (fake)',
|
||||
'limite': 585.94,
|
||||
'filesystem': '/home',
|
||||
'usage': 420.32},
|
||||
{'%': 0.1,
|
||||
'quota': 100.00,
|
||||
'label': u'Boite de r\xe9ception (fake)',
|
||||
'limite': 150.00,
|
||||
'filesystem': '/var/mail',
|
||||
'usage': 0.06}
|
||||
]
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue