summaryrefslogtreecommitdiff
path: root/dist/patme
blob: 0fd50ee4aa360633f3d8c6bfba8661cece289b47 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
#!/usr/bin/perl

use Getopt::GUI::Long;
use QWizard;
use QWizard::API;
use Data::Dumper;
use Cwd;

use Getopt::Std;
Getopt::GUI::Long::Configure(qw(display_help no_ignore_case));

use strict;

our %opts =
(
 'd' => $ENV{'HOME'} . "/src/snmp/patme/",
 'b' => 'main,5.6,5.5,5.4,5.3',
 'p' => '-p0',
);

# sets the order shown
our @codetrees = ('main',
		  '5.6',
		  '5.5',
		  '5.4',
		  '5.3',
		  '5.2',
		  '5.1',
		  '5.0',
		  'UCD');

our %codetrees = ('5.0' => 'V5-0-patches',
		  '5.1' => 'V5-1-patches',
		  '5.2' => 'V5-2-patches',
		  '5.3' => 'V5-3-patches',
		  '5.4' => 'V5-4-patches',
		  '5.5' => 'V5-5-patches',
		  '5.6' => 'V5-6-patches',
		  'main' => 'net-snmp',
		  'UCD' => 'V4-2-patches');

our (@captures, $capfilt, $result, %captures, $capturenum);

GetOptions(\%opts,
	   ['f|file=s',           'Patch file'],
	   ['d|base-directory=s', 'Base directory of checkouts'],
	   ['p|patch-args=s',     'Default patch arguments (-p1)'],

	   ['GUI:separator',   'Patch application specifics;'],
	   ['b|braches=s',     'Branches to apply to (eg 5.1,5.2,...)'],
	   ['m|commit-msg=s',  'Default commit message to use'],
	   ['D|subdir=s',      'Apply patches to a subdirectory'],
	   ['u|no-update',     'Do not run svn status/update in the directory first.  Only use this if it\'s known clean.'],
	  );

my %bs;
if ($opts{'b'}) {
    map { $bs{$_} = 1; } split(/,\s*/,$opts{'b'}); 
}
$opts{'d'} .= "/" if ($opts{'d'} !~ /\/$/);

my $qw = new QWizard();
my $pris = load_primaries();
$qw->{'primaries'} = $pris;

$qw->qwparam('svncommit',$opts{'m'}) if ($opts{'m'});

$qw->magic('top');

sub make_tops {
    my @tops;
    foreach my $k (@codetrees) {
	push @tops, 
	  qw_checkbox($k, "Apply to $k", 1, 0, 
		      default => $qw->qwparam($k) || $bs{$k},
		      override => 1);
    }
    return @tops;
}

sub load_primaries {
    my @tops = make_tops();
    return
      {
       top =>
       qw_primary('top','Select packages to apply the patch to:', '', 
		  [@tops,
		  qw_text('basedir', 'Base code directory:', 
			  default => $opts{'d'}),
		  qw_hidden('no_confirm',1),
		  qw_text('patchfile','Patch file:', default => $opts{f},
			 check_value => sub { 
			     return "patch file doesn't exist" if (! -f qwparam('patchfile'))
			 }),
		  qw_checkbox('noupdate','Don\'t run svn update/revert first:', 
			      1, 0, default => $opts{'u'} || 0)],
		  [],[],sub_modules => ['commit', 'commitmsg', 'maketest',
					'edit', 'applying', 'check',
					'patch_info']),

       patch_info =>
       qw_primary('check','Checking code directory status:', '',
		  [qw_paragraph('patch pieces:',
				sub { capture("egrep '^(---|\\+\\+\\+)' " . 
					      qwparam('patchfile'))},
				width => 80,
				height => 30),
		   qw_text('patchargs','Patch arguments',
			   default => $opts{'p'}),
		   qw_text('subdir', 'Apply in package subdir:',
			   default => $opts{'D'}),
		   qw_paragraph('Note:','Hitting next below will first clean your local repositories which could take a bit (watch the console for deails on what it\'s doing at any moment)', doif => sub {!qwparam('noupdate')}),
]),


       check =>
       qw_primary('check','Checking code directory status:', '',
		  [qw_paragraph('removed .rej files:',
			       sub { my $it = captureeachdir('find . -name \*.rej');
				     captureeachdir('find . -name \*.rej | xargs rm -f');
				     return $it;
				 },
				preformatted => 1,
				width => 80,
				height => 60,
			       ),
		   qw_paragraph('svn update:',
				sub {
				    my ($res, $one);
				    foreach my $k (@codetrees) {
					next if (!qwparam($k));
					$res .= "$k:\n";
					$one = capturedir($codetrees{$k}, 
							  "svn update");
					$res .= $one;
					$one = capturedir($codetrees{$k}, 
							  "svn revert -R .");
					$res .= $one;
				    }
				    return $res;
				  },
				preformatted => 1,
				width => 80,
				height => 60,
				doif => sub{!qwparam('noupdate')}
			       )		  ],
		 ),

       applying =>
       qw_primary("applying", 'Applying patches to the code bases', '',
		  [{type => 'table',
		    text => 'Results:',
		    values => sub {
			my @tab;
			foreach my $k (@codetrees) {
			    next if (!qwparam($k));
			    push @tab, [$k,
					qw_paragraph("r$k","", 
						     preformatted => 1,
						     width => 80,
						     height => 20,
						     values => 
						     sub { my $cmd = "patch " . qwparam('patchargs') . " < " . qwparam('patchfile');
							   my $results = "Running on $k: $cmd" . "\n" . capturedir($codetrees{$k},$cmd);
							   return $results})];
			}
			return [\@tab];
		    }}],[],[]),

       edit =>
       qw_primary('edit','Fix the following files:','',
		  [qw_paragraph('Fix these (maybe):',
			       sub {
				   $capfilt = '(.*.rej)';
				   my $res = 
				     captureeachdir('find . -name \*.rej');
				   print Dumper(\%captures);
				   $capfilt = undef;
				   return $res;
				 },
				preformatted => 1,
				width => 80,
				height => 60,
			       ),
		   qw_label('failed files:',
			    sub { $capturenum = 0;
				  map { $capturenum += $#{$captures{$_}} + 1;
				    } (keys(%captures));
				  return $capturenum;
			      }),
		  qw_checkbox('edithem','Open an editor on the failed files?',
			      1, 0, doif => sub { return $capturenum > 0 }),
		  qw_text('editor','Editor:',default => $ENV{'EDITOR'} || 'vi',
			  doif => sub { return $capturenum > 0 })],
		  [sub {
		       if (qwparam('edithem')) {
			   foreach my $k (keys(%captures)) {
			       foreach my $f (@{$captures{$k}}) {
				   my $file = qwparam('basedir') .
				     $codetrees{$k} .
				       qwparam('subdir') . '/' . $f->[0];
				   print STDERR "editing: $file\n";
				   system(qwparam('editor') . " " . $file);
			       }
			   }
		       }
		   }]
		 ),

       maketest =>
       qw_primary("maketest", "Run make?",'',
		 [qw_checkbox('makeit','Run make?', 1, 0),
		  qw_checkbox('maketest', 'Run make test?', 1, 0)
		 ],
 		 [sub {
 		      if (qwparam('makeit') || qwparam('maketest')) {
 			  $_[0]->add_todos(-early, 'domake');
 		      }
 		  }]
		 ),

       domake =>
       qw_primary("domake", "Make results",'',
		  [qw_paragraph('Make results:',
				sub { return captureeachdir('make'); },
				preformatted => 1,
				width => 80,
				height => 20,
				doif => sub { qwparam('makeit') }
			       ),
		   qw_paragraph('Make test results:',
				sub { return captureeachdir('make test'); },
				preformatted => 1,
				width => 80,
				height => 20,
				doif => sub { qwparam('maketest') }
			       )]
		  ),

       commitmsg =>
       qw_primary("commitmsg", 'Commit info:', '',
		  [qw_text('svncommit','Commit message',
			   default => qwparam('svncommit') || $opts{'m'}),
		  {type => 'dynamic',
		   values => sub { my @tops = make_tops(1); return \@tops}}]),

       commit =>
       qw_primary("commit", 'running commit:', '',
		  [qw_paragraph('committing files:',
			       sub { my $msg = qwparam('svncommit');
				     $msg =~ s/\'/\'\"\'\"\'/g; # escape 's
				     return capturedir($opts{'d'},
						       'svn commit -m \'' . $msg . '\' ' . get_codedirs_str()); },
				preformatted => 1,
				width => 80,
				height => 20,
			       )]),

       editing =>
       qw_primary("applying", 'Edit the following files:', '',
		  [{type => 'table',
		    text => 'Results:',
		    values => sub { return [\@captures]},
		   }],[],[])
      }
}

sub capture {
    my $cmd = join(" ",@_);
    my $results = "Running: $cmd\n";
    my @a;
    print $results;
    open(I,"$cmd 2>&1|");
    while (<I>) {
	$results .= $_;
	print $_;
	if ($capfilt) {
	    print "capfilt: $capfilt\n";
	    @a = /$capfilt/;
	    print "  capfilt: @a\n";
	    push @captures, [@a];
	}
    }
    close(I);
    $result = $? >> 8;
    $results .= "RESULT: " . (($result) ? "FAIL" : "SUCCESS") . "($result)\n";
    return $results;
}

sub capturedir {
    my $dir = shift;
    $dir .= "/" if ($dir !~ /\/$/);
    my $basedir = qwparam('basedir');
    $basedir .= "/" if ($basedir !~ /\/$/);
    my $olddir = getcwd();
    my $newdir = "$basedir$dir" . qwparam('subdir');
    my $res = "changing to: $newdir\n";
    print $res;
    chdir($newdir);
    $res .= capture(@_);
    chdir($olddir);
    return $res;
}

sub get_codedirs_str() {
    my $res = "";
    foreach my $k (@codetrees) {
	next if (!qwparam($k));
	$res .= " $opts{'d'}$codetrees{$k}";
    }
    $res =~ s/^ //;
    return $res;
}

sub captureeachdir {
    my $out;
    %captures = ();
    foreach my $k (@codetrees) {
	next if (!qwparam($k));
	$out .= "$k:\n";
	$out .= capturedir($codetrees{$k}, @_) . "\n";
	if ($#captures > -1) {
	    @{$captures{$k}} = @captures;
	    @captures = ();
	}
    }
    return $out;
}

sub dodir {
    my $text = shift;
    return
      [{type => 'table',
	text => $text,
	values => [[sub {
			my @tab;
			foreach my $k (@codetrees) {
			    next if (!qwparam($k));
			    push @tab, [$k,
					qw_paragraph("r$k","", 
						     preformatted => 1,
						     width => 80,
						     height => 20,
						     values => 
						     [[sub { $_->[0]($k)}, 
						       @_]])];
			}
			return [\@tab];
		    }, @_]]
       }];
}