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