]> mj.ucw.cz Git - checkmail.git/blob - maint/release.pm
release.pm: Added release notifiers
[checkmail.git] / maint / release.pm
1 #!/usr/bin/perl
2 # A simple system for making software releases
3 # (c) 2003--2010 Martin Mares <mj@ucw.cz>
4
5 package UCW::Release;
6 use strict;
7 use warnings;
8 use Getopt::Long;
9
10 our $verbose = 0;
11
12 sub new($$) {
13         my ($class,$basename) = @_;
14         my $s = {
15                 "PACKAGE" => $basename,
16                 "rules" => [
17                         # p=preprocess, s=subst, -=discard
18                         '(^|/)(CVS|\.arch-ids|{arch}|\.git|tmp)/' => '-',
19                         '\.(lsm|spec)$' => 'ps',
20                         '(^|/)README$' => 's'
21                         ],
22                 "directories" => [
23                         ],
24                 "conditions" => {
25                         },
26                 "DATE" => `date '+%Y-%m-%d' | tr -d '\n'`,
27                 "LSMDATE" => `date '+%y%m%d' | tr -d '\n'`,
28                 "distfiles" => [
29                         ],
30                 "archivedir" => "/home/mj/tmp/archives/$basename",
31                 "uploads" => [
32                         ],
33                 "notifiers" => [
34                         ],
35                 # Options
36                 "do_test" => 1,
37                 "do_patch" => 1,
38                 "diff_against" => "",
39                 "do_upload" => 1,
40                 "do_git_tag" => 0,
41                 "force_git_tag" => 0,
42                 "do_notify" => 0,
43         };
44         bless $s;
45         return $s;
46 }
47
48 sub GetVersionFromFile($) {
49         my ($s,$file,$rx) = @_;
50         open F, $file or die "Unable to open $file for version autodetection";
51         while (<F>) {
52                 chomp;
53                 if (/$rx/) {
54                         $s->{"VERSION"} = $1;
55                         print "Detected version $1 from $file\n" if $verbose;
56                         last;
57                 }
58         }
59         close F;
60         if (!defined $s->{"VERSION"}) { die "Failed to auto-detect version"; }
61         return $s->{"VERSION"};
62 }
63
64 sub GetVersionsFromChangelog($) {
65         my ($s,$file,$rx) = @_;
66         open F, $file or die "Unable to open $file for version autodetection";
67         while (<F>) {
68                 chomp;
69                 if (/$rx/) {
70                         if (!defined $s->{"VERSION"}) {
71                                 $s->{"VERSION"} = $1;
72                                 print "Detected version $1 from $file\n" if $verbose;
73                         } elsif ($s->{"VERSION"} eq $1) {
74                                 # do nothing
75                         } else {
76                                 $s->{"OLDVERSION"} = $1;
77                                 print "Detected previous version $1 from $file\n" if $verbose;
78                                 last;
79                         }
80                 }
81         }
82         close F;
83         if (!defined $s->{"VERSION"}) { die "Failed to auto-detect version"; }
84         return $s->{"VERSION"};
85 }
86
87 sub InitDist($) {
88         my ($s,$dd) = @_;
89         $s->{"DISTDIR"} = $dd;
90         print "Initializing dist directory $dd\n" if $verbose;
91         `rm -rf $dd`; die if $?;
92         `mkdir -p $dd`; die if $?;
93 }
94
95 sub ExpandVar($$) {
96         my ($s,$v) = @_;
97         if (defined $s->{$v}) {
98                 return $s->{$v};
99         } else {
100                 die "Reference to unknown variable $v";
101         }
102 }
103
104 sub CopyFile($$$$) {
105         my ($s,$f,$dir,$action) = @_;
106
107         (my $d = $f) =~ s@(^|/)[^/]*$@@;
108         $d = "$dir/$d";
109         -d $d || `mkdir -p $d`; die if $?;
110
111         my $preprocess = ($action =~ /p/);
112         my $subst = ($action =~ /s/);
113         if ($preprocess || $subst) {
114                 open I, "$f" or die "open($f): $?";
115                 open O, ">$dir/$f" or die "open($dir/$f): $!";
116                 my @ifs = ();   # stack of conditions, 1=satisfied
117                 my $empty = 0;  # last line was empty
118                 my $is_makefile = ($f =~ /(Makefile|.mk)$/);
119                 while (<I>) {
120                         if ($subst) {
121                                 s/@([0-9A-Za-z_]+)@/$s->ExpandVar($1)/ge;
122                         }
123                         if ($preprocess) {
124                                 if (/^#/ || $is_makefile) {
125                                         if (/^#?ifdef\s+(\w+)/) {
126                                                 if (defined ${$s->{"conditions"}}{$1}) {
127                                                         push @ifs, ${$s->{"conditions"}}{$1};
128                                                         next;
129                                                 }
130                                                 push @ifs, 0;
131                                         } elsif (/^#ifndef\s+(\w+)/) {
132                                                 if (defined ${$s->{"conditions"}}{$1}) {
133                                                         push @ifs, -${$s->{"conditions"}}{$1};
134                                                         next;
135                                                 }
136                                                 push @ifs, 0;
137                                         } elsif (/^#if\s+/) {
138                                                 push @ifs, 0;
139                                         } elsif (/^#?endif/) {
140                                                 my $x = pop @ifs;
141                                                 defined $x or die "Improper nesting of conditionals";
142                                                 $x && next;
143                                         } elsif (/^#?else/) {
144                                                 my $x = pop @ifs;
145                                                 defined $x or die "Improper nesting of conditionals";
146                                                 push @ifs, -$x;
147                                                 $x && next;
148                                         }
149                                 }
150                                 @ifs && $ifs[$#ifs] < 0 && next;
151                                 if (/^$/) {
152                                         $empty && next;
153                                         $empty = 1;
154                                 } else { $empty = 0; }
155                         }
156                         print O;
157                 }
158                 close O;
159                 close I;
160                 ! -x $f or chmod(0755, "$dir/$f") or die "chmod($dir/$f): $!";
161         } else {
162                 `cp -a "$f" "$dir/$f"`; die if $?;
163         }
164 }
165
166 sub GenPackage($) {
167         my ($s) = @_;
168         $s->{"PKG"} = $s->{"PACKAGE"} . "-" . $s->{"VERSION"};
169         my $dd = $s->{"DISTDIR"};
170         my $pkg = $s->{"PKG"};
171         my $dir = "$dd/$pkg";
172         print "Generating $dir\n";
173
174         FILES: foreach my $f (`find . -type f`) {
175                 chomp $f;
176                 $f =~ s/^\.\///;
177                 my $action = "";
178                 my @rules = @{$s->{"rules"}};
179                 while (@rules) {
180                         my $rule = shift @rules;
181                         my $act = shift @rules;
182                         if ($f =~ $rule) {
183                                 $action = $act;
184                                 last;
185                         }
186                 }
187                 ($action =~ /-/) && next FILES;
188                 print "$f ($action)\n" if $verbose;
189                 $s->CopyFile($f, $dir, $action);
190         }
191
192         foreach my $d (@{$s->{"directories"}}) {
193                 `mkdir -p $dir/$d`; die if $?;
194         }
195
196         if (-f "$dir/Makefile") {
197                 print "Cleaning up\n";
198                 `cd $dir && make distclean >&2`; die if $?;
199         }
200
201         print "Creating $dd/$pkg.tar.gz\n";
202         my $tarvv = $verbose ? "vv" : "";
203         `cd $dd && tar cz${tarvv}f $pkg.tar.gz $pkg >&2`; die if $?;
204         push @{$s->{"distfiles"}}, "$dd/$pkg.tar.gz";
205
206         my $adir = $s->{"archivedir"};
207         my $afile = "$adir/$pkg.tar.gz";
208         print "Archiving to $afile\n";
209         -d $adir or `mkdir -p $adir`;
210         `cp $dd/$pkg.tar.gz $afile`; die if $?;
211
212         return $dir;
213 }
214
215 sub GenFile($$) {
216         my ($s,$f) = @_;
217         my $sf = $s->{"DISTDIR"} . "/" . $s->{"PKG"} . "/$f";
218         my $df = $s->{"DISTDIR"} . "/$f";
219         print "Generating $df\n";
220         `cp $sf $df`; die if $?;
221         push @{$s->{"distfiles"}}, $df;
222 }
223
224 sub Usage($) {
225         my ($s) = @_;
226         my $usage = <<FOE ;
227 Usage: $0 <options>
228
229 Options:
230 --[no]verbose           Be chatty about the inner workings of the release system {verbose}
231 --[no]test              Test the package before uploading {do_test}
232 --[no]patch             Make a patch against the previous version {do_patch}
233 --diff-against=<ver>    Set which version we create the patch against
234 --[no]upload            Upload released files {do_upload}
235 --[no]git-tag           Tag the Git repository with "v<version>" {do_git_tag}
236 --force-git-tag         Rewrite the Git tag if it already exists {force_git_tag}
237 --[no]notify            Call scripts to notify the world about the release {do_notify}
238 FOE
239         sub state($) {
240                 return "(default: " . ($_[0] ? "on" : "off") . ")";
241         }
242         $usage =~ s[{(\w+)}][state($s->{$1})]ge;
243         die $usage;
244 }
245
246 sub ParseOptions($) {
247         my ($s) = @_;
248         $s->{"do_git_tag"} = 1 if (-d ".git");
249         $s->{"do_notify"} = 1 if @{$s->{"notifiers"}};
250         GetOptions(
251                 "verbose!" => \$verbose,
252                 "test!" => \$s->{"do_test"},
253                 "patch!" => \$s->{"do_patch"},
254                 "diff-against=s" => \$s->{"diff_against"},
255                 "upload!" => \$s->{"do_upload"},
256                 'git-tag!' => \$s->{"do_git_tag"},
257                 'force-git-tag!' => \$s->{"force_git_tag"},
258                 'notify!' => \$s->{"do_notify"},
259         ) || $s->Usage;
260 }
261
262 sub Test($) {
263         my ($s) = @_;
264         my $dd = $s->{"DISTDIR"};
265         my $pkg = $s->{"PKG"};
266         my $log = "$dd/$pkg.log";
267         print "Doing a test compilation\n";
268         `( cd $dd/$pkg && make ) >$log 2>&1`;
269         die "There were errors. Please inspect $log" if $?;
270         `grep -q [Ww]arning $log`;
271         $? or print "There were warnings! Please inspect $log.\n";
272         print "Cleaning up\n";
273         `cd $dd/$pkg && make distclean`; die if $?;
274 }
275
276 sub MakePatch($) {
277         my ($s) = @_;
278         my $dd = $s->{"DISTDIR"};
279         my $pkg1 = $s->{"PKG"};
280         my $oldver;
281         if ($s->{"diff_against"} ne "") {
282                 $oldver = $s->{"diff_against"};
283         } elsif (defined $s->{"OLDVERSION"}) {
284                 $oldver = $s->{"OLDVERSION"};
285         } else {
286                 print "WARNING: No previous version known. No patch generated.\n";
287                 return;
288         }
289         my $pkg0 = $s->{"PACKAGE"} . "-" . $oldver;
290
291         my $oldarch = $s->{"archivedir"} . "/" . $pkg0 . ".tar.gz";
292         -f $oldarch or die "MakePatch: $oldarch not found";
293         print "Unpacking $pkg0 from $oldarch\n";
294         `cd $dd && tar xzf $oldarch`; die if $?;
295
296         my $diff = $s->{"PACKAGE"} . "-" . $oldver . "-" . $s->{"VERSION"} . ".diff.gz";
297         print "Creating a patch from $pkg0 to $pkg1: $diff\n";
298         `cd $dd && diff -ruN $pkg0 $pkg1 | gzip >$diff`; die if $?;
299         push @{$s->{"distfiles"}}, "$dd/$diff";
300 }
301
302 sub Upload($) {
303         my ($s) = @_;
304         foreach my $u (@{$s->{"uploads"}}) {
305                 my $url = $u->{"url"};
306                 print "Upload to $url :\n";
307                 my @files = ();
308                 my $filter = $u->{"filter"} || ".*";
309                 foreach my $f (@{$s->{"distfiles"}}) {
310                         if ($f =~ $filter) {
311                                 print "\t$f\n";
312                                 push @files, $f;
313                         }
314                 }
315                 print "<confirm> "; <STDIN>;
316                 if ($url =~ m@^scp://([^/]+)(.*)@) {
317                         $, = " ";
318                         my $host = $1;
319                         my $dir = $2;
320                         $dir =~ s@^/~@~@;
321                         $dir =~ s@^/\./@@;
322                         my $cmd = "scp @files $host:$dir\n";
323                         `$cmd`; die if $?;
324                 } elsif ($url =~ m@ftp://([^/]+)(.*)@) {
325                         my $host = $1;
326                         my $dir = $2;
327                         open FTP, "|ftp -v $host" or die;
328                         print FTP "cd $dir\n";
329                         foreach my $f (@files) {
330                                 (my $ff = $f) =~ s@.*\/([^/].*)@$1@;
331                                 print FTP "put $f $ff\n";
332                         }
333                         print FTP "bye\n";
334                         close FTP;
335                         die if $?;
336                 } else {
337                         die "Don't know how to handle this URL scheme";
338                 }
339         }
340 }
341
342 sub GitTag($) {
343         my ($s) = @_;
344         my $tag = 'v' . $s->{'VERSION'};
345         my $force = ($s->{'force_git_tag'} ? '--force' : '');
346         print "Tagging Git repository with $tag\n";
347         `git tag $tag $force`; die if $?;
348         print "Pushing the tags upstream\n";
349         `git push --tags`; die if $?;
350 }
351
352 sub AddUcwNotifier($) {
353         my ($r) = @_;
354         push @{$r->{"notifiers"}}, sub {
355                 my ($s) = @_;
356                 print "Updating web pages\n";
357                 my $pkg = $s->{'PACKAGE'};
358                 my $ver = $s->{'VERSION'};
359                 `ssh jw 'cd www && bin/release-prog $pkg $ver'`; die if $?;
360         };
361 };
362
363 sub Dispatch($) {
364         my ($s) = @_;
365         $s->Test if $s->{"do_test"};
366         $s->MakePatch if $s->{"do_patch"};
367         $s->GitTag if $s->{"do_git_tag"};
368         $s->Upload if $s->{"do_upload"};
369         if ($s->{"do_notify"}) {
370                 for my $f (@{$s->{"notifiers"}}) {
371                         &$f($s);
372                 }
373         }
374 }
375
376 1;