Package zephir :: Package monitor :: Package agentmanager :: Module util
[hide private]
[frames] | no frames]

Source Code for Module zephir.monitor.agentmanager.util

  1  # -*- coding: UTF-8 -*- 
  2  ########################################################################### 
  3  # Eole NG - 2007 
  4  # Copyright Pole de Competence Eole  (Ministere Education - Academie Dijon) 
  5  # Licence CeCill  cf /root/LicenceEole.txt 
  6  # eole@ac-dijon.fr 
  7  ########################################################################### 
  8   
  9  """ 
 10  Various helper functions 
 11  """ 
 12   
 13  try: _ # localized string fetch function 
 14  except NameError: _ = str 
 15   
 16  import os, md5 
 17   
 18  from twisted.python import log 
 19   
20 -def percent(quantity, total):
21 """quantity/total rate, as a percentage 22 """ 23 quantity, total = float(quantity), float(total) 24 if total == 0: 25 return 0 26 else: 27 return int((quantity * 100.0) / total)
28
29 -def expand_dirname(dirname):
30 """Expande C{~} et les variables d'environnement dans C{dirname}, 31 et ajoute un séparateur terminal si nécessaire.""" 32 result = os.path.expanduser(os.path.expandvars(dirname)) 33 assert os.path.isdir(result) 34 if not result.endswith(os.path.sep): 35 result += os.path.sep 36 return result
37
38 -def boolean_to_onoff(b):
39 if b: return "On" 40 else: return "Off"
41
42 -def ensure_dirs(*dirs):
43 """Crée les répertoires passés en argument si nécessaire""" 44 for d in dirs: 45 if not os.path.isdir(d): 46 log.msg(_('Creating dir %s') % d) 47 os.makedirs(d) # will die if there is a file, but it's ok
48 49 50 import time, calendar 51 from datetime import datetime, timedelta, tzinfo 52
53 -class UTC(tzinfo):
54 """UTC timezone 55 56 To be throwed away if one day standard timezones 57 get implemented in python... 58 """
59 - def utcoffset(self, dt): return timedelta(0)
60 - def tzname(self, dt): return "UTC"
61 - def dst(self, dt): return timedelta(0)
62 63 utc = UTC() 64 TIME_ORIGIN = datetime(1970,1,1,tzinfo=utc) 65 MONTHS = dict(JAN=1, FEV=2, FEB=2, MAR=3, APR=4, AVR=4, MAY=5, MAI=5, 66 JUN=6, JUI=7, AOU=8, AUG=8, SEP=9, OCT=10, NOV=11, DEC=12) 67
68 -def parse_date(date):
69 month, day, hour, year = date.split() 70 hour, min, sec = hour.split(':') 71 # suppression des accents et mise en majuscule pour les mois 72 month = month.replace('û','u').replace('é','e').upper() 73 month = MONTHS[month] 74 return datetime(int(year), int(month), int(day), int(hour), int(min), int(float(sec)))
75
76 -def utcnow():
77 """Aware UTC datetime""" 78 #return datetime.utcnow().replace(tzinfo=utc) 79 return datetime.now(utc)
80
81 -def utcfromtimestamp(timestamp):
82 #return TIME_ORIGIN + timedelta(seconds=timestamp) 83 return datetime.utcfromtimestamp(timestamp).replace(tzinfo=utc)
84
85 -def timestampfromutc(date):
86 #return (date - TIME_ORIGIN).seconds 87 return calendar.timegm(date.utctimetuple())
88
89 -def status_to_img(s):
90 if s == "": 91 return '<img src="/static/img/gris.gif" alt="??"/>' 92 if s == "On": 93 return '<img src="/static/img/vert.gif" alt="On"/>' 94 return '<img src="/static/img/rouge.gif" alt="Off"/>'
95 96 # fichiers à vérifier : (nom fichier/repertoire, equivalent sur zephir, extension ou fin de fichier) 97 md5files = [("config.eol", "zephir.eol",None), 98 ("patch", "patchs",".patch"), 99 ("patch/variante", "patchs/variante",".patch"), 100 ("dicos/local", "dicos/local",".xml"), 101 ("dicos/variante", "dicos/variante",".xml"), 102 ] 103
104 -def md5file(filename):
105 """Return the hex digest of a file without loading it all into memory""" 106 fh = open(filename) 107 digest = md5.new() 108 while 1: 109 buf = fh.read(4096) 110 if buf == "": 111 break 112 digest.update(buf) 113 fh.close() 114 return digest.hexdigest()
115 116 # def test_main(): 117 # test_support.run_unittest(UserStringTest) 118 119 # if __name__ == "__main__": 120 # test_main() 121