~ubuntu-branches/ubuntu/precise/kompozer/precise

« back to all changes in this revision

Viewing changes to mozilla/config/fast-update.pl

  • Committer: Bazaar Package Importer
  • Author(s): Anthony Yarusso
  • Date: 2007-08-27 01:11:03 UTC
  • Revision ID: james.westby@ubuntu.com-20070827011103-2jgf4s6532gqu2ka
Tags: upstream-0.7.10
ImportĀ upstreamĀ versionĀ 0.7.10

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env perl
 
2
#
 
3
# fast-update.pl [-h hours] [-m module] [-r branch]
 
4
#
 
5
# This command, fast-update.pl, does a (fast) cvs update of the current 
 
6
# directory. It is fast because the cvs up command is only run on those 
 
7
# directories / sub-directories where changes have occured since the 
 
8
# last fast-update.
 
9
#
 
10
# The last update time is stored in a ".fast-update" file in the current
 
11
# directory. Thus one can choose to only fast-update a branch of the tree 
 
12
# and then fast-update the whole tree later.
 
13
#
 
14
# The first time this command is run in a directory the last cvs update
 
15
# time is assumed to be the timestamp of the CVS/Entries file.
 
16
#
 
17
use Getopt::Long;
 
18
 
 
19
my $filename = ".fast-update";
 
20
my $start_time = time();
 
21
 
 
22
my $branch;
 
23
my $module="SeaMonkeyAll";
 
24
my $maxdirs=5;
 
25
my $rootdir = "";
 
26
my $hours = 0;
 
27
my $dir = '';
 
28
 
 
29
&GetOptions('d=s' => \$dir, 'h=s' => \$hours, 'm=s' => \$module, 'r=s' => \$branch);
 
30
 
 
31
#print "dir = ($dir), hours = ($hours), module = ($module), branch = ($branch)\n";
 
32
if ($dir) {
 
33
  chdir '..';
 
34
  chdir $dir;
 
35
}
 
36
 
 
37
if (!$hours) {
 
38
  $hours = get_hours_since_last_update();
 
39
}
 
40
if (!$hours) {
 
41
  $hours = 24;
 
42
}
 
43
 
 
44
 
 
45
# pull out the current directory
 
46
# if there is no such file, this will all just fail, which is ok
 
47
open REPOSITORY, "<CVS/Repository";
 
48
$rootdir = <REPOSITORY>;
 
49
chop $rootdir;
 
50
close REPOSITORY;
 
51
 
 
52
# try to guess the current branch by looking at all the
 
53
# files in CVS/Entries
 
54
if (!$branch) {
 
55
  my $foundbranch =0;
 
56
  
 
57
  open ENTRIES, "<CVS/Entries";
 
58
  while (<ENTRIES>) {
 
59
    chop;
 
60
    @entry = split(/\//);
 
61
    my ($type, $file, $ver, $date, $unknown, $tag) = @entry;
 
62
    
 
63
    # the tag usually starts with "T"
 
64
    $thisbranch = substr($tag, 1);
 
65
    
 
66
    # look for more than one branch
 
67
    if ($type eq "") {
 
68
      
 
69
      if ($foundbranch and ($lastbranch ne $thisbranch)) {
 
70
        die "Multiple branches in this directory, cannot determine branch\n";
 
71
      }
 
72
      $foundbranch = 1;
 
73
      $lastbranch = $thisbranch;
 
74
    }
 
75
    
 
76
  }
 
77
  
 
78
  $branch = $lastbranch if ($foundbranch);
 
79
 
 
80
  close ENTRIES;
 
81
}
 
82
 
 
83
# check for a static Tag
 
84
# (at least that is what I think this does)
 
85
# (bonsai does not report changes when the Tag starts with 'N')
 
86
# (I do not really understand all this)
 
87
if ($branch) {
 
88
  open TAG, "<CVS/Tag";
 
89
  my $line = <TAG>;
 
90
  if ($line =~ /^N/) { 
 
91
    print "static tag, ignore branch\n";
 
92
    $branch = '';
 
93
  }
 
94
  close TAG;
 
95
}
 
96
 
 
97
 
 
98
my $url = "http://bonsai.mozilla.org/cvsquery.cgi?module=${module}&branch=${branch}&branchtype=match&sortby=File&date=hours&hours=${hours}&cvsroot=%2Fcvsroot";
 
99
 
 
100
my $esc_dir = escape($dir);
 
101
if ($dir) {
 
102
  $url .= "&dir=$esc_dir";
 
103
}
 
104
 
 
105
print "Contacting bonsai for updates to ${module} ";
 
106
print "on the ${branch} branch " if ($branch);
 
107
print "in the last ${hours} hours ";
 
108
print "within the $rootdir directory..\n" if ($rootdir);
 
109
#print "url = $url\n";
 
110
 
 
111
# first try wget, then try lynx, then try curl
 
112
 
 
113
# this is my lame way of checking if a command succeeded AND getting
 
114
# output from it. I'd love a better way. -alecf@netscape.com
 
115
my $have_checkins = 0;
 
116
open CHECKINS,"wget --quiet --output-document=- \"$url\"|" or
 
117
  die "Error opening wget: $!\n";
 
118
 
 
119
$header = <CHECKINS> and $have_checkins=1;
 
120
 
 
121
if (!$have_checkins) {
 
122
 
 
123
  open CHECKINS, "lynx -source '$url'|" or die "Error opening lynx: $!\n";
 
124
 
 
125
  $header = <CHECKINS> and $have_checkins = 1;
 
126
}
 
127
 
 
128
if (!$have_checkins) {
 
129
 
 
130
  open CHECKINS, "curl -s '$url'|" or die "Error opening curl $!\n";
 
131
 
 
132
  $header = <CHECKINS> and $have_checkins = 1;
 
133
}
 
134
 
 
135
$have_checkins || die "Couldn't get checkins\n";
 
136
 
 
137
open REALOUT, ">.fast-update.bonsai.html" || die "argh $!\n";
 
138
print "Processing checkins...";
 
139
while (<CHECKINS>) {
 
140
  print REALOUT $_;
 
141
  
 
142
  if (/js_file_menu\((.*),\s*\'(.*)\'\s*,\s*(.*),\s*(.*),\s*(.*),\s*(.*)\)/) {
 
143
    my ($repos, $dir, $file, $rev, $branch, $event) =
 
144
      ($1, $2, $3, $4, $5, $6);
 
145
    push @dirlist, $dir;
 
146
  }
 
147
}
 
148
 
 
149
print "done.\n";
 
150
close REALOUT;
 
151
unlink '.fast-update.bonsai.html';
 
152
 
 
153
my $lastdir = "";
 
154
my @uniquedirs;
 
155
 
 
156
foreach $dir (sort @dirlist) {
 
157
  next if ($lastdir eq $dir);
 
158
 
 
159
  my $strippeddir = "";
 
160
  $lastdir = $dir;
 
161
 
 
162
  # now strip out $rootdir
 
163
  if ($rootdir) {
 
164
 
 
165
    # only deal with directories that start with $rootdir
 
166
    if (substr($dir, 0, (length $rootdir)) eq $rootdir) {
 
167
 
 
168
      if ($dir eq $rootdir) {
 
169
        $strippeddir = ".";
 
170
      } else {
 
171
        $strippeddir = substr($dir,(length $rootdir) + 1 );
 
172
      }
 
173
 
 
174
    }
 
175
  } else {
 
176
    $strippeddir = $dir;
 
177
  }
 
178
 
 
179
  if ($strippeddir) {
 
180
    push @uniquedirs, $strippeddir;
 
181
  }
 
182
}
 
183
 
 
184
my $status = 0;
 
185
if (scalar(@uniquedirs)) {
 
186
  print "Updating tree..($#uniquedirs directories)\n";
 
187
  my $i=0;
 
188
  my $dirlist = "";
 
189
  foreach $dir (sort @uniquedirs) {
 
190
    if (!-d $dir) {
 
191
      cvs_up_parent($dir);
 
192
    }
 
193
    $dirlist .= "\"$dir\" ";
 
194
    $i++;
 
195
    if ($i == 5) {
 
196
      $status |= spawn("cvs -z3 -q -f up -l -d $dirlist\n");
 
197
      $dirlist = "";
 
198
      $i=0;
 
199
    }
 
200
  }
 
201
  if ($i) {
 
202
    $status |= spawn("cvs -z3 -q -f up -l -d $dirlist\n");
 
203
  }
 
204
}
 
205
else {
 
206
  print "No directories to update.\n";
 
207
}
 
208
 
 
209
close CHECKINS;
 
210
if ($status == 0) {
 
211
  set_last_update_time($filename, $start_time);
 
212
  print "successfully updated $module/$dir\n";
 
213
}
 
214
else {
 
215
  print "error while updating $module/$dir\n";
 
216
}
 
217
 
 
218
exit $status;
 
219
 
 
220
sub cvs_up_parent {
 
221
  my ($dir) = @_;
 
222
  my $pdir = $dir;
 
223
  $pdir =~ s|/*[^/]*/*$||;
 
224
  #$pdir =~ s|/$||;
 
225
  #$pdir =~ s|[^/]*$||;
 
226
  #$pdir =~ s|/$||;
 
227
  if (!$pdir) {
 
228
    $pdir = '.';
 
229
  }
 
230
  if (!-d $pdir) {
 
231
    cvs_up_parent($pdir);
 
232
  }
 
233
  $status |= system "cvs -z3 -q -f up -d -l $pdir\n";
 
234
}
 
235
 
 
236
sub get_hours_since_last_update {
 
237
  # get the last time this command was run
 
238
  my $last_time = get_last_update_time($filename);
 
239
  if (!defined($last_time)) {
 
240
    #
 
241
    # This must be the first use of fast-update.pl so use the timestamp 
 
242
    # of a file that: 
 
243
    #  1) is managed by cvs
 
244
    #  2) the user should not be tampering with
 
245
    #  3) that gets updated fairly frequently.
 
246
    #
 
247
    $last_time = (stat "CVS/Entries")[9];
 
248
    if (defined($last_time)) {
 
249
      $last_time -= 3600*24; # for safety go back a bit
 
250
      print "use fallback time of ".localtime($last_time)."\n";
 
251
    }
 
252
  }
 
253
  if(!defined($last_time)) {
 
254
    print "last_time not defined\n";
 
255
  }
 
256
 
 
257
  # figure the hours (rounded up) since the last fast-update
 
258
  my $hours = int(($start_time - $last_time + 3600)/3600);
 
259
  print "last updated $hours hour(s) ago at ".localtime($last_time)."\n";
 
260
  return $hours;
 
261
}
 
262
 
 
263
# returns time of last update if known
 
264
sub get_last_update_time {
 
265
  my ($filename) = @_;
 
266
  if (!-r $filename) {
 
267
    return undef;
 
268
  }
 
269
  open FILE, "<$filename";
 
270
  my $line = <FILE>;
 
271
  if (!defined(line)) {
 
272
    return undef;
 
273
  }
 
274
#  print "line = $line";
 
275
  $line =~ /^(\d+):/;
 
276
  return $1;
 
277
}
 
278
 
 
279
sub set_last_update_time {
 
280
  my ($filename, $time) = @_;
 
281
  my $time_str = localtime($time);
 
282
  open FILE, ">$filename";
 
283
  print FILE "$time: last fast-update.pl at ".localtime($time)."\n";
 
284
}
 
285
 
 
286
# URL-encode data
 
287
sub escape {
 
288
  my ($toencode) = @_;
 
289
  $toencode=~s/([^a-zA-Z0-9_.-])/uc sprintf("%%%02x",ord($1))/eg;
 
290
  return $toencode;
 
291
}
 
292
 
 
293
sub spawn {
 
294
  my ($procname) = @_;
 
295
  return system "$procname";
 
296
}