use pygments for prettyfying diffs.
[amitay/build-farm.git] / web / util.pm
1 ###################################################
2 # utility functions to support the build farm
3 # Copyright (C) tridge@samba.org, 2001
4 #
5 #   This program is free software; you can redistribute it and/or modify
6 #   it under the terms of the GNU General Public License as published by
7 #   the Free Software Foundation; either version 2 of the License, or
8 #   (at your option) any later version.
9 #   
10 #   This program is distributed in the hope that it will be useful,
11 #   but WITHOUT ANY WARRANTY; without even the implied warranty of
12 #   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 #   GNU General Public License for more details.
14 #   
15 #   You should have received a copy of the GNU General Public License
16 #   along with this program; if not, write to the Free Software
17 #   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18
19 package util;
20
21 use Data::Dumper;
22
23 ##############################################
24 # load a list from a file, using : to separate
25 sub load_list($)
26 {
27         my $fname = shift;
28         my @lines;
29         open(FH,"<",$fname);
30         while (<FH>) {
31                 chomp;
32                 push (@lines,$_) unless (/^#/);
33         }
34         close FH;
35         return @lines;
36 }
37
38 ##############################################
39 # load a hash from a file, using : to separate
40 sub load_hash($)
41 {
42         my $fname = shift;
43         my @lines = load_list($fname);
44         my %ret;
45         for my $l (@lines) {
46                 if ($l =~ /^([\w\-]*)\s*:\s*(.*)$/) {
47                         $ret{$1} = $2;
48                 }
49         }
50         return %ret;
51 }
52
53 #####################################################################
54 # check if a string is in an array
55 sub InArray($$)
56 {
57     my ($s, $a) = @_;
58     for my $v (@{$a}) {
59                 return 1 if ($v eq $s);
60     }
61     return 0;
62 }
63
64 #####################################################################
65 # flatten an array of arrays into a single array
66 sub FlattenArray($) 
67
68     my $a = shift;
69     my @b;
70     for my $d (@{$a}) {
71                 push(@b, $_) foreach (@{$d});
72     }
73     return \@b;
74 }
75
76 #####################################################################
77 # flatten an array of hashes into a single hash
78 sub FlattenHash($) 
79
80     my $a = shift;
81     my %b;
82     for my $d (@{$a}) {
83                 for my $k (keys %{$d}) {
84                         $b{$k} = $d->{$k};
85                 }
86     }
87     return \%b;
88 }
89
90 #####################################################################
91 # return the modification time of a file
92 sub FileModtime($)
93 {
94     my($filename) = shift;
95     return (stat($filename))[9];
96 }
97
98 #####################################################################
99 # read a file into a string
100 sub FileLoad($)
101 {
102     my($filename) = shift;
103     local(*INPUTFILE);
104     open(INPUTFILE, $filename) || return "";
105     my($saved_delim) = $/;
106     undef $/;
107     my($data) = <INPUTFILE>;
108     close(INPUTFILE);
109     $/ = $saved_delim;
110     return $data;
111 }
112
113 #####################################################################
114 # write a string into a file
115 sub FileSave($$)
116 {
117     my($filename) = shift;
118     my($v) = shift;
119     local(*FILE);
120     open(FILE, ">$filename") || die "can't open $filename";    
121     print FILE $v;
122     close(FILE);
123 }
124
125 #####################################################################
126 # return a filename with a changed extension
127 sub ChangeExtension($$)
128 {
129     my($fname,$ext) = @_;
130         return "$1.$ext" if ($fname =~ /^(.*)\.(.*?)$/);
131     return "$fname.$ext";
132 }
133
134 #####################################################################
135 # save a data structure into a file
136 sub SaveStructure($$)
137 {
138     my($filename) = shift;
139     my($v) = shift;
140     FileSave($filename, Dumper($v));
141 }
142
143 #####################################################################
144 # load a data structure from a file (as saved with SaveStructure)
145 sub LoadStructure($)
146 {
147     return eval FileLoad(shift);
148 }
149
150 ##########################################
151 # count the number of lines in a buffer
152 sub count_lines($)
153 {
154     my $s = shift;
155     my $count;
156     $count++ while $s =~ /$/gm;
157     return $count;
158 }
159
160 ################
161 # display a time as days, hours, minutes
162 sub dhm_time($)
163 {
164         my $sec = shift;
165         my $days = int($sec / (60*60*24));
166         my $hour = int($sec / (60*60)) % 24;
167         my $min = int($sec / 60) % 60;
168
169         my $ret = "";
170
171         if ($sec < 0) { 
172                 return "-";
173         }
174
175         if ($days != 0) { 
176                 return sprintf("%dd %dh %dm", $days, $hour, $min);
177         }
178         if ($hour != 0) {
179                 return sprintf("%dh %dm", $hour, $min);
180         }
181         if ($min != 0) {
182                 return sprintf("%dm", $min);
183         }
184         return sprintf("%ds", $sec);
185 }
186
187 ##############################################
188 # simple html markup stripper
189 sub strip_html($) {
190         my $string = shift;
191
192         # get rid of comments
193         $string =~ s/<!\-\-(.*?)\-\->/$2/g;
194
195         # and remove tags.
196         while ($string =~ s&<(\w+).*?>(.*?)</\1>&$2&) {
197                 ;
198         }
199
200         return $string;
201 }
202
203 1;