| 1 |
#!/usr/bin/perl |
| 2 |
|
| 3 |
use Getopt::GUI::Long; |
| 4 |
use QWizard; |
| 5 |
use QWizard::API; |
| 6 |
use Data::Dumper; |
| 7 |
use Cwd; |
| 8 |
|
| 9 |
use Getopt::Std; |
| 10 |
Getopt::GUI::Long::Configure(qw(display_help no_ignore_case)); |
| 11 |
|
| 12 |
use strict; |
| 13 |
|
| 14 |
our %opts = |
| 15 |
( |
| 16 |
'd' => $ENV{'HOME'} . "/src/snmp/patme/", |
| 17 |
'b' => 'main,5.6,5.5,5.4,5.3', |
| 18 |
'p' => '-p0', |
| 19 |
); |
| 20 |
|
| 21 |
# sets the order shown |
| 22 |
our @codetrees = ('main', |
| 23 |
'5.6', |
| 24 |
'5.5', |
| 25 |
'5.4', |
| 26 |
'5.3', |
| 27 |
'5.2', |
| 28 |
'5.1', |
| 29 |
'5.0', |
| 30 |
'UCD'); |
| 31 |
|
| 32 |
our %codetrees = ('5.0' => 'V5-0-patches', |
| 33 |
'5.1' => 'V5-1-patches', |
| 34 |
'5.2' => 'V5-2-patches', |
| 35 |
'5.3' => 'V5-3-patches', |
| 36 |
'5.4' => 'V5-4-patches', |
| 37 |
'5.5' => 'V5-5-patches', |
| 38 |
'5.6' => 'V5-6-patches', |
| 39 |
'main' => 'net-snmp', |
| 40 |
'UCD' => 'V4-2-patches'); |
| 41 |
|
| 42 |
our (@captures, $capfilt, $result, %captures, $capturenum); |
| 43 |
|
| 44 |
GetOptions(\%opts, |
| 45 |
['f|file=s', 'Patch file'], |
| 46 |
['d|base-directory=s', 'Base directory of checkouts'], |
| 47 |
['p|patch-args=s', 'Default patch arguments (-p1)'], |
| 48 |
|
| 49 |
['GUI:separator', 'Patch application specifics;'], |
| 50 |
['b|braches=s', 'Branches to apply to (eg 5.1,5.2,...)'], |
| 51 |
['m|commit-msg=s', 'Default commit message to use'], |
| 52 |
['D|subdir=s', 'Apply patches to a subdirectory'], |
| 53 |
['u|no-update', 'Do not run svn status/update in the directory first. Only use this if it\'s known clean.'], |
| 54 |
); |
| 55 |
|
| 56 |
my %bs; |
| 57 |
if ($opts{'b'}) { |
| 58 |
map { $bs{$_} = 1; } split(/,\s*/,$opts{'b'}); |
| 59 |
} |
| 60 |
$opts{'d'} .= "/" if ($opts{'d'} !~ /\/$/); |
| 61 |
|
| 62 |
my $qw = new QWizard(); |
| 63 |
my $pris = load_primaries(); |
| 64 |
$qw->{'primaries'} = $pris; |
| 65 |
|
| 66 |
$qw->qwparam('svncommit',$opts{'m'}) if ($opts{'m'}); |
| 67 |
|
| 68 |
$qw->magic('top'); |
| 69 |
|
| 70 |
sub make_tops { |
| 71 |
my @tops; |
| 72 |
foreach my $k (@codetrees) { |
| 73 |
push @tops, |
| 74 |
qw_checkbox($k, "Apply to $k", 1, 0, |
| 75 |
default => $qw->qwparam($k) || $bs{$k}, |
| 76 |
override => 1); |
| 77 |
} |
| 78 |
return @tops; |
| 79 |
} |
| 80 |
|
| 81 |
sub load_primaries { |
| 82 |
my @tops = make_tops(); |
| 83 |
return |
| 84 |
{ |
| 85 |
top => |
| 86 |
qw_primary('top','Select packages to apply the patch to:', '', |
| 87 |
[@tops, |
| 88 |
qw_text('basedir', 'Base code directory:', |
| 89 |
default => $opts{'d'}), |
| 90 |
qw_hidden('no_confirm',1), |
| 91 |
qw_text('patchfile','Patch file:', default => $opts{f}, |
| 92 |
check_value => sub { |
| 93 |
return "patch file doesn't exist" if (! -f qwparam('patchfile')) |
| 94 |
}), |
| 95 |
qw_checkbox('noupdate','Don\'t run svn update/revert first:', |
| 96 |
1, 0, default => $opts{'u'} || 0)], |
| 97 |
[],[],sub_modules => ['commit', 'commitmsg', 'maketest', |
| 98 |
'edit', 'applying', 'check', |
| 99 |
'patch_info']), |
| 100 |
|
| 101 |
patch_info => |
| 102 |
qw_primary('check','Checking code directory status:', '', |
| 103 |
[qw_paragraph('patch pieces:', |
| 104 |
sub { capture("egrep '^(---|\\+\\+\\+)' " . |
| 105 |
qwparam('patchfile'))}, |
| 106 |
width => 80, |
| 107 |
height => 30), |
| 108 |
qw_text('patchargs','Patch arguments', |
| 109 |
default => $opts{'p'}), |
| 110 |
qw_text('subdir', 'Apply in package subdir:', |
| 111 |
default => $opts{'D'}), |
| 112 |
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')}), |
| 113 |
]), |
| 114 |
|
| 115 |
|
| 116 |
check => |
| 117 |
qw_primary('check','Checking code directory status:', '', |
| 118 |
[qw_paragraph('removed .rej files:', |
| 119 |
sub { my $it = captureeachdir('find . -name \*.rej'); |
| 120 |
captureeachdir('find . -name \*.rej | xargs rm -f'); |
| 121 |
return $it; |
| 122 |
}, |
| 123 |
preformatted => 1, |
| 124 |
width => 80, |
| 125 |
height => 60, |
| 126 |
), |
| 127 |
qw_paragraph('svn update:', |
| 128 |
sub { |
| 129 |
my ($res, $one); |
| 130 |
foreach my $k (@codetrees) { |
| 131 |
next if (!qwparam($k)); |
| 132 |
$res .= "$k:\n"; |
| 133 |
$one = capturedir($codetrees{$k}, |
| 134 |
"svn update"); |
| 135 |
$res .= $one; |
| 136 |
$one = capturedir($codetrees{$k}, |
| 137 |
"svn revert -R ."); |
| 138 |
$res .= $one; |
| 139 |
} |
| 140 |
return $res; |
| 141 |
}, |
| 142 |
preformatted => 1, |
| 143 |
width => 80, |
| 144 |
height => 60, |
| 145 |
doif => sub{!qwparam('noupdate')} |
| 146 |
) ], |
| 147 |
), |
| 148 |
|
| 149 |
applying => |
| 150 |
qw_primary("applying", 'Applying patches to the code bases', '', |
| 151 |
[{type => 'table', |
| 152 |
text => 'Results:', |
| 153 |
values => sub { |
| 154 |
my @tab; |
| 155 |
foreach my $k (@codetrees) { |
| 156 |
next if (!qwparam($k)); |
| 157 |
push @tab, [$k, |
| 158 |
qw_paragraph("r$k","", |
| 159 |
preformatted => 1, |
| 160 |
width => 80, |
| 161 |
height => 20, |
| 162 |
values => |
| 163 |
sub { my $cmd = "patch " . qwparam('patchargs') . " < " . qwparam('patchfile'); |
| 164 |
my $results = "Running on $k: $cmd" . "\n" . capturedir($codetrees{$k},$cmd); |
| 165 |
return $results})]; |
| 166 |
} |
| 167 |
return [\@tab]; |
| 168 |
}}],[],[]), |
| 169 |
|
| 170 |
edit => |
| 171 |
qw_primary('edit','Fix the following files:','', |
| 172 |
[qw_paragraph('Fix these (maybe):', |
| 173 |
sub { |
| 174 |
$capfilt = '(.*.rej)'; |
| 175 |
my $res = |
| 176 |
captureeachdir('find . -name \*.rej'); |
| 177 |
print Dumper(\%captures); |
| 178 |
$capfilt = undef; |
| 179 |
return $res; |
| 180 |
}, |
| 181 |
preformatted => 1, |
| 182 |
width => 80, |
| 183 |
height => 60, |
| 184 |
), |
| 185 |
qw_label('failed files:', |
| 186 |
sub { $capturenum = 0; |
| 187 |
map { $capturenum += $#{$captures{$_}} + 1; |
| 188 |
} (keys(%captures)); |
| 189 |
return $capturenum; |
| 190 |
}), |
| 191 |
qw_checkbox('edithem','Open an editor on the failed files?', |
| 192 |
1, 0, doif => sub { return $capturenum > 0 }), |
| 193 |
qw_text('editor','Editor:',default => $ENV{'EDITOR'} || 'vi', |
| 194 |
doif => sub { return $capturenum > 0 })], |
| 195 |
[sub { |
| 196 |
if (qwparam('edithem')) { |
| 197 |
foreach my $k (keys(%captures)) { |
| 198 |
foreach my $f (@{$captures{$k}}) { |
| 199 |
my $file = qwparam('basedir') . |
| 200 |
$codetrees{$k} . |
| 201 |
qwparam('subdir') . '/' . $f->[0]; |
| 202 |
print STDERR "editing: $file\n"; |
| 203 |
system(qwparam('editor') . " " . $file); |
| 204 |
} |
| 205 |
} |
| 206 |
} |
| 207 |
}] |
| 208 |
), |
| 209 |
|
| 210 |
maketest => |
| 211 |
qw_primary("maketest", "Run make?",'', |
| 212 |
[qw_checkbox('makeit','Run make?', 1, 0), |
| 213 |
qw_checkbox('maketest', 'Run make test?', 1, 0) |
| 214 |
], |
| 215 |
[sub { |
| 216 |
if (qwparam('makeit') || qwparam('maketest')) { |
| 217 |
$_[0]->add_todos(-early, 'domake'); |
| 218 |
} |
| 219 |
}] |
| 220 |
), |
| 221 |
|
| 222 |
domake => |
| 223 |
qw_primary("domake", "Make results",'', |
| 224 |
[qw_paragraph('Make results:', |
| 225 |
sub { return captureeachdir('make'); }, |
| 226 |
preformatted => 1, |
| 227 |
width => 80, |
| 228 |
height => 20, |
| 229 |
doif => sub { qwparam('makeit') } |
| 230 |
), |
| 231 |
qw_paragraph('Make test results:', |
| 232 |
sub { return captureeachdir('make test'); }, |
| 233 |
preformatted => 1, |
| 234 |
width => 80, |
| 235 |
height => 20, |
| 236 |
doif => sub { qwparam('maketest') } |
| 237 |
)] |
| 238 |
), |
| 239 |
|
| 240 |
commitmsg => |
| 241 |
qw_primary("commitmsg", 'Commit info:', '', |
| 242 |
[qw_text('svncommit','Commit message', |
| 243 |
default => qwparam('svncommit') || $opts{'m'}), |
| 244 |
{type => 'dynamic', |
| 245 |
values => sub { my @tops = make_tops(1); return \@tops}}]), |
| 246 |
|
| 247 |
commit => |
| 248 |
qw_primary("commit", 'running commit:', '', |
| 249 |
[qw_paragraph('committing files:', |
| 250 |
sub { my $msg = qwparam('svncommit'); |
| 251 |
$msg =~ s/\'/\'\"\'\"\'/g; # escape 's |
| 252 |
return capturedir($opts{'d'}, |
| 253 |
'svn commit -m \'' . $msg . '\' ' . get_codedirs_str()); }, |
| 254 |
preformatted => 1, |
| 255 |
width => 80, |
| 256 |
height => 20, |
| 257 |
)]), |
| 258 |
|
| 259 |
editing => |
| 260 |
qw_primary("applying", 'Edit the following files:', '', |
| 261 |
[{type => 'table', |
| 262 |
text => 'Results:', |
| 263 |
values => sub { return [\@captures]}, |
| 264 |
}],[],[]) |
| 265 |
} |
| 266 |
} |
| 267 |
|
| 268 |
sub capture { |
| 269 |
my $cmd = join(" ",@_); |
| 270 |
my $results = "Running: $cmd\n"; |
| 271 |
my @a; |
| 272 |
print $results; |
| 273 |
open(I,"$cmd 2>&1|"); |
| 274 |
while (<I>) { |
| 275 |
$results .= $_; |
| 276 |
print $_; |
| 277 |
if ($capfilt) { |
| 278 |
print "capfilt: $capfilt\n"; |
| 279 |
@a = /$capfilt/; |
| 280 |
print " capfilt: @a\n"; |
| 281 |
push @captures, [@a]; |
| 282 |
} |
| 283 |
} |
| 284 |
close(I); |
| 285 |
$result = $? >> 8; |
| 286 |
$results .= "RESULT: " . (($result) ? "FAIL" : "SUCCESS") . "($result)\n"; |
| 287 |
return $results; |
| 288 |
} |
| 289 |
|
| 290 |
sub capturedir { |
| 291 |
my $dir = shift; |
| 292 |
$dir .= "/" if ($dir !~ /\/$/); |
| 293 |
my $basedir = qwparam('basedir'); |
| 294 |
$basedir .= "/" if ($basedir !~ /\/$/); |
| 295 |
my $olddir = getcwd(); |
| 296 |
my $newdir = "$basedir$dir" . qwparam('subdir'); |
| 297 |
my $res = "changing to: $newdir\n"; |
| 298 |
print $res; |
| 299 |
chdir($newdir); |
| 300 |
$res .= capture(@_); |
| 301 |
chdir($olddir); |
| 302 |
return $res; |
| 303 |
} |
| 304 |
|
| 305 |
sub get_codedirs_str() { |
| 306 |
my $res = ""; |
| 307 |
foreach my $k (@codetrees) { |
| 308 |
next if (!qwparam($k)); |
| 309 |
$res .= " $opts{'d'}$codetrees{$k}"; |
| 310 |
} |
| 311 |
$res =~ s/^ //; |
| 312 |
return $res; |
| 313 |
} |
| 314 |
|
| 315 |
sub captureeachdir { |
| 316 |
my $out; |
| 317 |
%captures = (); |
| 318 |
foreach my $k (@codetrees) { |
| 319 |
next if (!qwparam($k)); |
| 320 |
$out .= "$k:\n"; |
| 321 |
$out .= capturedir($codetrees{$k}, @_) . "\n"; |
| 322 |
if ($#captures > -1) { |
| 323 |
@{$captures{$k}} = @captures; |
| 324 |
@captures = (); |
| 325 |
} |
| 326 |
} |
| 327 |
return $out; |
| 328 |
} |
| 329 |
|
| 330 |
sub dodir { |
| 331 |
my $text = shift; |
| 332 |
return |
| 333 |
[{type => 'table', |
| 334 |
text => $text, |
| 335 |
values => [[sub { |
| 336 |
my @tab; |
| 337 |
foreach my $k (@codetrees) { |
| 338 |
next if (!qwparam($k)); |
| 339 |
push @tab, [$k, |
| 340 |
qw_paragraph("r$k","", |
| 341 |
preformatted => 1, |
| 342 |
width => 80, |
| 343 |
height => 20, |
| 344 |
values => |
| 345 |
[[sub { $_->[0]($k)}, |
| 346 |
@_]])]; |
| 347 |
} |
| 348 |
return [\@tab]; |
| 349 |
}, @_]] |
| 350 |
}]; |
| 351 |
} |