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
20 log.FileLogObserver.timeFormat = "%Y/%m/%d %H:%M:%S %Z"
21
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
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
41 if b: return "On"
42 else: return "Off"
43
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)
50
51
52 import time, calendar
53 from datetime import datetime, timedelta, tzinfo
54
56 """UTC timezone
57
58 To be throwed away if one day standard timezones
59 get implemented in python...
60 """
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
71 month, day, hour, year = date.split()
72 hour, min, sec = hour.split(':')
73
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
79 """Aware UTC datetime"""
80
81 return datetime.now(utc)
82
86
88
89 return calendar.timegm(date.utctimetuple())
90
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
99 md5files = {
100
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
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
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
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
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
143
144
145
146
147