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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
|
#!/opt/bin/perl
use strict;
use warnings;
use Cwd;
use Getopt::Std;
use File::Basename;
use FindBin;
my $Opts = {};
getopts( 'r:p:e:c:vudn', $Opts );
my $Cwd = cwd();
my $Verbose = 1;
my $ExcludeRe = $Opts->{e} ? qr/$Opts->{e}/i : undef;
my $Debug = $Opts->{v} || 0;
my $RunDiff = $Opts->{d} || 0;
my $PkgDir = $Opts->{p} || cwd();
my $Repo = $Opts->{r} or die "Need repository!\n". usage();
my $Changes = $Opts->{c} || 'Changes ChangeLog';
my $NoBranch = $Opts->{n} || 0;
### strip trailing slashes;
$Repo =~ s|/$||;
my $CPV = $Debug ? '-v' : '';
my $TestBin = 'ptardiff';
my $PkgDirRe = quotemeta( $PkgDir .'/' );
my $BranchName = basename( $PkgDir ) . '.' . $$;
my $OrigRepo = $Repo;
### establish working directory, either branch or full copy
if ( $NoBranch ) {
### create a copy of the repo directory
my $RepoCopy = "$Repo-$BranchName";
print "Copying repository to $RepoCopy ..." if $Verbose;
### --archive == -dPpR, but --archive is not portable, and neither
### is -d, so settling for -PpR
system( "cp -PpR -f $Repo $RepoCopy" )
and die "Copying master repo to $RepoCopy failed: $?";
### Going forward, use the copy in place of the original repo
$Repo = $RepoCopy;
print "done\n" if $Verbose;
}
else {
### create a git branch for the new package
print "Setting up a branch from blead called '$BranchName'..." if $Verbose;
chdir $Repo or die "Could not chdir to $Repo: $!";
unless ( -d '.git' ) {
die "\n$Repo is not a git repository\n";
}
my $status = `git status`;
unless ( $status =~ /nothing to commit/ims ) {
die "\nWorking directory not clean. Stopping.\n";
}
system( "git checkout -b $BranchName blead" )
and die "Could not create branch '$BranchName': $?";
print "done\n" if $Verbose;
}
### chdir there
chdir $PkgDir or die "Could not chdir to $PkgDir: $!";
### copy over all files under lib/
my @LibFiles;
{ print "Copying libdir..." if $Verbose;
die "Can't (yet) copy from a repository (found .git or .svn)"
if -d '.git' || -d '.svn';
die "No lib/ directory found\n" unless -d 'lib';
system( "cp -fR $CPV lib $Repo" ) and die "Copy of lib/ failed: $?";
@LibFiles = map { chomp; $_ }
### should we get rid of this file?
grep { $ExcludeRe && $_ =~ $ExcludeRe
? do { warn "Removing $Repo/$_\n";
system("rm $Repo/$_") and die "rm '$Repo/$_' failed: $?";
undef
}
: 1
} `find lib -type f`
or die "Could not detect library files\n";
print "done\n" if $Verbose;
}
### find the directory to put the t/ and bin/ files under
my $RelTopDir; # topdir from the repo root
my $TopDir; # full path to the top dir
my $ModName; # name of the module
my @ModFiles; # the .PMs in this package
{ print "Creating top level dir..." if $Verbose;
### make sure we get the shortest file, so we don't accidentally get
### a subdir
@ModFiles = sort { length($a) <=> length($b) }
map { chomp; $_ }
grep { $ExcludeRe ? $_ !~ $ExcludeRe : 1 }
grep /\.p(?:m|od)$/,
`find $PkgDir/lib -type f`
or die "No TopDir detected\n";
$RelTopDir = $ModFiles[0];
$RelTopDir =~ s/^$PkgDirRe//;
$RelTopDir =~ s/\.p(m|od)$//;
$TopDir = "$Repo/$RelTopDir";
### create the dir if it's not there yet
unless( -d $TopDir ) {
system( "mkdir $TopDir" ) and die "Creating dir $TopDir failed: $?";
}
### the module name, like Foo::Bar
### slice syntax not elegant, but we need to remove the
### leading 'lib/' entry
### stupid temp vars! stupid perl! it doesn't do @{..}[0..-1] :(
{ my @list = @{[split '/', $RelTopDir]};
$ModName = join '::', @list[1 .. $#list];
}
### the .pm files in this package
@ModFiles = map { s|^$PkgDirRe||; $_ } @ModFiles
or die "Could not detect modfiles\n";
print "done\n" if $Verbose;
}
my $TopDirRe = quotemeta( $TopDir . '/' );
### copy over t/ and bin/ directories to the $TopDir
my @TestFiles;
{ print "Copying t/* files to $TopDir..." if $Verbose;
-d 't'
? system( "cp -fR $CPV t $TopDir" ) && die "Copy of t/ failed: $?"
: warn "No t/ directory found\n";
@TestFiles = map { chomp; s|^$TopDirRe||; s|//|/|g; $_ }
### should we get rid of this file?
grep { $ExcludeRe && $_ =~ $ExcludeRe
? do { warn "Removing $_\n";
system("rm $TopDir/$_") and die "rm '$_' failed: $?";
undef
}
: 1
} `find t -type f`
or die "Could not detect testfiles\n";
print "done\n" if $Verbose;
}
my $BinDir;
my @BinFiles;
my $TopBinDir;
BIN: {
$BinDir = -d 'bin' ? 'bin' :
-d 'scripts' ? 'scripts' : undef ;
unless ($BinDir) {
print "No bin/ or scripts/ directory found\n" if $Verbose;
last BIN;
}
my $TopBinDir = "$TopDir/$BinDir/";
print "Copying $BinDir/* files to $TopBinDir..." if $Verbose;
my $CopyCmd = "cp -fR $CPV $BinDir $TopDir";
print "Running '$CopyCmd'..." if $Verbose;
system($CopyCmd) && die "Copy of $BinDir failed: $?";
@BinFiles = map { chomp; s|^$TopDirRe||; s|//|/|g; $_ }
### should we get rid of this file?
grep { $ExcludeRe && $_ =~ $ExcludeRe
? do { warn "Removing $_\n";
system("rm $TopDir/$_") and die "rm '$_' failed: $?";
undef
}
: 1
} `find $BinDir -type f`
or die "Could not detect binfiles\n";
print "done\n" if $Verbose;
}
### copy over change log
my @Changes;
foreach my $cl (split m/\s+/ => $Changes) {
-f $cl or next;
push @Changes, $cl;
print "Copying $cl files to $TopDir..." if $Verbose;
system( "cp -f $CPV $cl $TopDir" )
and die "Copy of $cl failed: $?";
}
### add files where they are required
my @NewFiles;
my @ChangedFiles;
{ for my $bin ( map { basename( $_ ) } @BinFiles ) {
print "Registering $bin with system files...\n";
### fix installperl, so these files get installed by other utils
### ./installperl: return if $name =~
### /^(?:cpan|instmodsh|prove|corelist|ptar|ptardiff)\z/;
{ my $file = 'installperl';
### not there already?
unless( `grep $TestBin $Repo/$file| grep $bin` ) {
print " Adding $bin to $file..." if $Verbose;
### double \\| required --> once for in this script, once
### for the cli
system("$^X -pi -e 's/($TestBin\\|)/$bin|\$1/' $Repo/$file")
and die "Could not add $bin to $file: $?";
print "done\n" if $Verbose;
push @ChangedFiles, $file;
} else {
print " $bin already mentioned in $file\n" if $Verbose;
}
}
### fix utils.lst, so the new tools are mentioned
{ my $file = 'utils.lst';
### not there already?
unless( `grep $bin $Repo/$file` ) {
print " Adding $bin to $file..." if $Verbose;
### double \\| required --> once for in this script, once
### for the cli
system("$^X -pi -e 's!($TestBin)!\$1\nutils/$bin!' $Repo/$file")
and die "Could not add $bin to $file: $?";
print "done\n" if $Verbose;
push @ChangedFiles, $file;
} else {
print " $bin already mentioned in $file\n" if $Verbose;
}
}
### make a $bin.PL file and fix it up
{ my $src = "utils/${TestBin}.PL";
my $file = "utils/${bin}.PL";
### not there already?
unless( -e "$Repo/$file" ) {
print " Creating $file..." if $Verbose;
### important part of the template looks like this
### (we'll need to change it):
# my $script = File::Spec->catfile(
# File::Spec->catdir(
# File::Spec->updir, qw[lib Archive Tar bin]
# ), "module-load.pl");
### copy another template file
system( "cp -f $Repo/$src $Repo/$file" )
and die "Could not create $file from $src: $?";
### change the 'updir' path
### make sure to escape the \[ character classes
my $updir = join ' ', (split('/', $RelTopDir), $BinDir);
system( "$^X -pi -e'".
's/^(.*?File::Spec->updir, qw\[).+?(\].*)$/'.
"\$1 $updir \$2/' $Repo/$file"
) and die "Could not fix updir for $bin in $file: $?";
### change the name of the file from $TestBin to $bin
system( "$^X -pi -e's/$TestBin/$bin/' $Repo/$file" )
and die "Could not update $file with '$bin' as name: $?";
print "done\n" if $Verbose;
} else {
print " $file already exists\n" if $Verbose;
}
### we've may just have created a new file, it will have to
### go into the manifest
push @NewFiles, $file;
}
### add an entry to utils/Makefile.PL for $bin
{ my $file = "utils/Makefile.PL";
### not there already?
unless( `grep $bin $Repo/$file` ) {
print " Adding $bin entries to $file..." if $Verbose;
### $bin appears on 4 lines in this file, so replace all 4
### first, pl =
system( "$^X -pi -e'/^pl\\s+=/ && s/(${TestBin}.PL)/".
"\$1 ${bin}.PL/' $Repo/$file"
) and die "Could not add $bin to the pl = entry: $?";
### next, plextract =
system( "$^X -pi -e'/^plextract\\s+=/ " .
"&& s/(${TestBin})/\$1 $bin/' $Repo/$file"
) and die "Could not add $bin to the plextract = entry: $?";
### third, plextractexe =
system( "$^X -pi -e'/^plextractexe\\s+=/ " .
"&& s!(\./${TestBin})!\$1 ./$bin!' $Repo/$file"
) and die "Could not add $bin to the plextractexe = entry: $?";
### last, the make directive $bin:
system( "$^X -pi -e'/^(${TestBin}:.+)/; \$x=\$1 or next;" .
"\$x =~ s/$TestBin/$bin/g;" . '$_.=$/.$x.$/;' .
"' $Repo/$file"
) and die "Could not add $bin as a make directive: $?";
push @ChangedFiles, $file;
print "done\n" if $Verbose;
} else {
print " $bin already added to $file\n" if $Verbose;
}
}
### add entries to win32/Makefile and win32/makefile.mk
### they contain the following lines:
# ./win32/makefile.mk: ..\utils\ptardiff \
# ./win32/makefile.mk: xsubpp instmodsh prove ptar ptardiff
for my $file ( qw[win32/Makefile win32/makefile.mk] ) {
unless ( `grep $bin $Repo/$file` ) {
print " Adding $bin entries to $file..." if $Verbose;
system( "$^X -pi -e'/^(.+?utils.${TestBin}.+)/;".
'$x=$1 or next;' .
"\$x =~ s/$TestBin/$bin/g;" . '$_.=$x.$/;' .
"' $Repo/$file"
) and die "Could not add $bin to UTILS section in $file: $?\n";
system( "$^X -pi -e's/( $TestBin)/\$1 $bin/' $Repo/$file" )
and die "Could not add $bin to $file: $?\n";
push @ChangedFiles, $file;
print "done\n" if $Verbose;
} else {
print " $bin already added to $file\n" if $Verbose;
}
}
### we need some entries in a vms specific file as well..
### except, I don't understand how it works or what it does, and it
### looks all a bit odd... so lets just print a warning...
### the entries look something like this:
# ./vms/descrip_mms.template:utils4 = [.utils]enc2xs.com
# [.utils]piconv.com [.utils]cpan.com [.utils]prove.com
# [.utils]ptar.com [.utils]ptardiff.com [.utils]shasum.com
# ./vms/descrip_mms.template:[.utils]ptardiff.com : [.utils]ptardiff.PL
# $(ARCHDIR)Config.pm
{ my $file = 'vms/descrip_mms.template';
unless( `grep $bin $Repo/$file` ) {
print $/.$/;
print " WARNING! You should add entries like the following\n"
. " to $file (Using $TestBin as an example)\n"
. " Unfortunately I don't understand what these entries\n"
. " do, so I won't change them automatically:\n\n";
print `grep -nC1 $TestBin $Repo/$file`;
print $/.$/;
} else {
print " $bin already added to $file\n" if $Verbose;
}
}
}
}
### update the manifest
{ my $file = $Repo . '/MANIFEST';
my @manifest;
{ open my $fh, '<', $file or die "Could not open $file: $!";
@manifest = <$fh>;
close $fh;
}
### fill it with files from our package
my %pkg_files;
for ( @ModFiles ) {
$pkg_files{$_} = "$_\t$ModName\n";
}
for ( @TestFiles ) {
$pkg_files{"$RelTopDir/$_"} = "$RelTopDir/$_\t$ModName tests\n"
}
for ( @BinFiles ) {
$pkg_files{"$RelTopDir/$_"} = "$RelTopDir/$_\tthe ".
basename($_) ." utility\n";
}
for ( @Changes ) {
$pkg_files{"$RelTopDir/$_"} = "$RelTopDir/$_\t$ModName change log\n";
}
for ( @NewFiles ) {
$pkg_files{$_} = "$_\tthe ".
do { m/(.+?)\.PL$/; basename($1) } .
" utility\n"
}
### remove all the files that are already in the manifest;
delete $pkg_files{ [split]->[0] } for @manifest;
print "Adding the following entries to the MANIFEST:\n" if $Verbose;
print "\t$_" for sort values %pkg_files;
print $/.$/;
push @manifest, values %pkg_files;
{ chmod 0644, $file;
open my $fh, '>', $file or die "Could not open $file for writing: $!";
#print $fh sort { lc $a cmp lc $b } @manifest;
### XXX stolen from pod/buildtoc:sub do_manifest
print $fh
map { $_->[0] }
sort { $a->[1] cmp $b->[1] || $a->[0] cmp $b->[0] }
map { my $f = lc $_; $f =~ s/[^a-z0-9\s]//g; [ $_, $f ] }
@manifest;
close $fh;
}
push @ChangedFiles, 'MANIFEST';
}
### would you like us to show you a diff?
if( $RunDiff ) {
if ( $NoBranch ) {
my $diff = $Repo; $diff =~ s/$$/patch/;
### weird RV ;(
my $master = basename( $OrigRepo );
my $repo = basename( $Repo );
my $chdir = dirname( $OrigRepo );
### the .patch file is added by an rsync from the APC
### but isn't actually in the p4 repo, so exclude it
my $cmd = "cd $chdir; diff -ruN --exclude=.patch $master $repo > $diff";
print "Running: '$cmd'\n";
print "Generating diff..." if $Verbose;
system( $cmd );
#and die "Could not write diff to '$diff': $?";
die "Could not write diff to '$diff'" unless -e $diff && -s _;
print "done\n" if $Verbose;
print "\nDiff can be applied with patch -p1 in $OrigRepo\n\n";
print " Diff written to: $diff\n\n" if $Verbose;
}
else {
my $diff = "$Repo/$BranchName"; $diff =~ s/$$/patch/;
my $cmd = "cd $Repo; git diff > $diff";
print "Running: '$cmd'\n";
print "Generating diff..." if $Verbose;
system( $cmd );
#and die "Could not write diff to '$diff': $?";
die "Could not write diff to '$diff'" unless -e $diff && -s _;
print "done\n" if $Verbose;
print " Diff written to: $diff\n\n" if $Verbose;
}
}
# add files to git index
unless ( $NoBranch ) {
chdir $Repo;
system( "git add $CPV $_" )
for ( @LibFiles, @NewFiles, @ChangedFiles,
map { "$RelTopDir/$_" } @TestFiles, @BinFiles, @Changes );
}
# return to original directory
chdir $Cwd;
sub usage {
my $me = basename($0);
return qq[
Usage: $me -r PERL_REPO_DIR [-p PACKAGE_DIR] [-v] [-d] [-e REGEX]
Options:
-r Path to perl-core git repository
-v Run verbosely
-c File containing changelog (default 'Changes' or 'ChangeLog')
-e Perl regex matching files that shouldn't be included
-d Create a diff as patch file
-p Path to the package to add. Defaults to cwd()
-n No branching; repository is not a git repo
\n];
}
|