9d6c8ca497a70ce1f62a1a4716ab705f5e016401
[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 def load_list(fname):
21     """load a list from a file, using : to separate"""
22     ret = []
23     f = open(fname, 'r')
24     try:
25         for l in f.readlines():
26             if l[0] != "#":
27                 l = l.strip('\r\n')
28                 if len(l) > 0:
29                     ret.append(l)
30     finally:
31         f.close()
32     return ret
33
34
35 def FileLoad(filename):
36     """read a file into a string"""
37     f = open(filename, 'r')
38     try:
39         return f.read()
40     finally:
41         f.close()
42
43
44 def dhm_time(sec):
45     """display a time as days, hours, minutes"""
46     days = int(sec / (60*60*24));
47     hour = int(sec / (60*60)) % 24;
48     min = int(sec / 60) % 60;
49
50     if sec < 0:
51         return "-"
52
53     if days != 0:
54         return "%dd %dh %dm" % (days, hour, min)
55     if hour != 0:
56         return "%dh %dm" % (hour, min)
57     if min != 0:
58         return "%dm" % min
59     return "%ds" % sec