Update Samba build farm to new web site layout.
[build-farm.git] / buildfarm / util.py
1 #!/usr/bin/python
2 # utility functions to support the build farm
3 # Copyright (C) tridge@samba.org, 2001
4 # Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2010
5 #
6 #   This program is free software; you can redistribute it and/or modify
7 #   it under the terms of the GNU General Public License as published by
8 #   the Free Software Foundation; either version 2 of the License, or
9 #   (at your option) any later version.
10 #
11 #   This program is distributed in the hope that it will be useful,
12 #   but WITHOUT ANY WARRANTY; without even the implied warranty of
13 #   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 #   GNU General Public License for more details.
15 #
16 #   You should have received a copy of the GNU General Public License
17 #   along with this program; if not, write to the Free Software
18 #   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19
20 import re
21 import os
22
23 def load_list(fname):
24     """load a list from a file, using : to separate"""
25     ret = []
26     f = open(fname, 'r')
27     try:
28         for l in f.readlines():
29             if l[0] != "#":
30                 l = l.strip('\r\n')
31                 if len(l) > 0:
32                     ret.append(l)
33     finally:
34         f.close()
35     return ret
36
37
38 def FileLoad(filename):
39     """read a file into a string"""
40     f = open(filename, 'r')
41     try:
42         return f.read()
43     finally:
44         f.close()
45
46 def SambaWebFileLoad(webdir, filename):
47     """loads file and changes the links to suit buildfarm"""
48     try:
49         f = open(os.path.join(webdir, filename), 'r')
50         text = f.read()
51     except IOError:
52         return ''
53     else:
54         f.close()
55     def add_virtual_headers(m):
56         try:
57             f = open(os.path.join(webdir, m.group(1)), 'r')
58             text = f.read()
59         except:
60             return ''
61         else:
62             f.close()
63             return text
64     text = re.sub('<!--#include virtual="/samba/(.*)" -->',add_virtual_headers , text)
65     text = re.sub('href="/samba', 'href="http://www.samba.org/samba', text)
66     return text
67
68 def dhm_time(sec):
69     """display a time as days, hours, minutes"""
70     days = int(sec / (60*60*24));
71     hour = int(sec / (60*60)) % 24;
72     min = int(sec / 60) % 60;
73
74     if sec < 0:
75         return "-"
76
77     if days != 0:
78         return "%dd %dh %dm" % (days, hour, min)
79     if hour != 0:
80         return "%dh %dm" % (hour, min)
81     if min != 0:
82         return "%dm" % min
83     return "%ds" % sec