1
2
3
4
5
6
7
8
9 """
10 Various helper functions
11 """
12
13 try: _
14 except NameError: _ = str
15
16 import os, md5
17
18 from twisted.python import log
19
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
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
39 if b: return "On"
40 else: return "Off"
41
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)
48
49
50 import time, calendar
51 from datetime import datetime, timedelta, tzinfo
52
54 """UTC timezone
55
56 To be throwed away if one day standard timezones
57 get implemented in python...
58 """
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
69 month, day, hour, year = date.split()
70 hour, min, sec = hour.split(':')
71
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
77 """Aware UTC datetime"""
78
79 return datetime.now(utc)
80
84
86
87 return calendar.timegm(date.utctimetuple())
88
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
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
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
117
118
119
120
121