Package zephir :: Package monitor :: Package agentmanager :: Module util
[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 
 17  from hashlib import md5 
 18   
 19  from twisted.python import log 
 20  import time, calendar 
 21  from datetime import datetime, timedelta, tzinfo 
 22   
 23  from zephir.monitor.agentmanager import status 
 24   
 25  log.FileLogObserver.timeFormat = "%Y/%m/%d %H:%M:%S %Z" 
 26   
27 -def percent(quantity, total):
28 """quantity/total rate, as a percentage 29 """ 30 quantity, total = float(quantity), float(total) 31 if total == 0: 32 return 0 33 else: 34 return int((quantity * 100.0) / total)
35
36 -def expand_dirname(dirname):
37 """Expande C{~} et les variables d'environnement dans C{dirname}, 38 et ajoute un séparateur terminal si nécessaire.""" 39 result = os.path.expanduser(os.path.expandvars(dirname)) 40 assert os.path.isdir(result) 41 if not result.endswith(os.path.sep): 42 result += os.path.sep 43 return result
44
45 -def boolean_to_onoff(b):
46 if b: return "On" 47 else: return "Off"
48
49 -def ensure_dirs(*dirs):
50 """Crée les répertoires passés en argument si nécessaire""" 51 for d in dirs: 52 if not os.path.isdir(d): 53 log.msg(_('Creating dir %s') % d) 54 os.makedirs(d) # will die if there is a file, but it's ok
55 56
57 -class UTC(tzinfo):
58 """UTC timezone 59 60 To be throwed away if one day standard timezones 61 get implemented in python... 62 """
63 - def utcoffset(self, dt): return timedelta(0)
64 - def tzname(self, dt): return "UTC"
65 - def dst(self, dt): return timedelta(0)
66 67 utc = UTC() 68 TIME_ORIGIN = datetime(1970,1,1,tzinfo=utc) 69 MONTHS = dict(JAN=1, FEV=2, FEB=2, MAR=3, APR=4, AVR=4, MAY=5, MAI=5, 70 JUN=6, JUI=7, AOU=8, AUG=8, SEP=9, OCT=10, NOV=11, DEC=12) 71
72 -def parse_date(date):
73 month, day, hour, year = date.split() 74 hour, min, sec = hour.split(':') 75 # suppression des accents et mise en majuscule pour les mois 76 month = month.replace('û','u').replace('é','e').upper() 77 month = MONTHS[month] 78 return datetime(int(year), int(month), int(day), int(hour), int(min), int(float(sec)))
79
80 -def utcnow():
81 """Aware UTC datetime""" 82 #return datetime.utcnow().replace(tzinfo=utc) 83 return datetime.now(utc)
84
85 -def utcfromtimestamp(timestamp):
86 #return TIME_ORIGIN + timedelta(seconds=timestamp) 87 return datetime.utcfromtimestamp(timestamp).replace(tzinfo=utc)
88
89 -def timestampfromutc(date):
90 #return (date - TIME_ORIGIN).seconds 91 return calendar.timegm(date.utctimetuple())
92
93 -def status_to_img(s):
94 if isinstance(s, status.Status): 95 if s == status.Unknown(): 96 s = "" 97 elif s == status.OK(): 98 s = "On" 99 else: 100 s = "Off" 101 if s == "": 102 return '<img src="/static/img/gris.gif" alt="??"/>' 103 elif s == "On": 104 return '<img src="/static/img/vert.gif" alt="On"/>' 105 return '<img src="/static/img/rouge.gif" alt="Off"/>'
106 107 # fichiers à vérifier par version de distribution: (nom fichier/repertoire, equivalent sur zephir, extension ou fin de fichier) 108 md5files = { 109 # eole 1.X 110 1:[("variables.eol", "variables.eol",None), 111 ("patch", "patchs",".patch"), 112 ("patch/variante", "patchs/variante",".patch"), 113 ("dicos", "dicos",".eol"), 114 ("dicos/variante", "dicos/variante",".eol"), 115 ], 116 # eole 2.0 117 2:[("zephir.eol", "zephir.eol",None), 118 ("patch", "patchs",".patch"), 119 ("patch/variante", "patchs/variante",".patch"), 120 ("dicos/local", "dicos/local",".xml"), 121 ("dicos/variante", "dicos/variante",".xml"), 122 ], 123 # eole 2.1 124 3:[("variables.eol", "variables.eol",None), 125 ("patch", "patchs",".patch"), 126 ("patch/variante", "patchs/variante",".patch"), 127 ("dicos/local", "dicos/local",".xml"), 128 ("dicos/variante", "dicos/variante",".xml"), 129 ], 130 # eole 2.2 131 4:[("variables.eol", "variables.eol",None), 132 ("patch", "patchs",".patch"), 133 ("patch/variante", "patchs/variante",".patch"), 134 ("dicos/local", "dicos/local",".xml"), 135 ("dicos/variante", "dicos/variante",".xml"), 136 ], 137 # eole 2.3 138 5:[("variables.eol", "variables.eol",None), 139 ("patch", "patchs",".patch"), 140 ("patch/variante", "patchs/variante",".patch"), 141 ("dicos/local", "dicos/local",".xml"), 142 ("dicos/variante", "dicos/variante",".xml"), 143 ], 144 # support eole 2.4 pour Zéphir 2.3 145 6:[("variables.eol", "variables.eol",None), 146 ("patch", "patchs",".patch"), 147 ("patch/variante", "patchs/variante",".patch"), 148 ("dicos/local", "dicos/local",".xml"), 149 ("dicos/variante", "dicos/variante",".xml"), 150 ] 151 } 152
153 -def md5file(filename):
154 """Return the hex digest of a file without loading it all into memory""" 155 fh = open(filename) 156 digest = md5() 157 while 1: 158 buf = fh.read(4096) 159 if buf == "": 160 break 161 digest.update(buf) 162 fh.close() 163 return digest.hexdigest()
164