3 # bump-perl-version, DAPM 14 Jul 2009
5 # A utility to find, and optionally bump, references to the perl version
6 # number in various files within the perl source
8 # It's designed to work in two phases. First, when run with -s (scan),
9 # it searches all the files in MANIFEST looking for strings that appear to
10 # match the current perl version (or which it knows are *supposed* to
11 # contain the current version), and produces a list of them to stdout,
12 # along with a suggested edit. For example:
14 # $ Porting/bump-perl-version -s 5.10.0 5.10.1 > /tmp/scan
18 # 52: -archlib='/opt/perl/lib/5.10.0/i686-linux-64int'
19 # +archlib='/opt/perl/lib/5.10.1/i686-linux-64int'
22 # At this point there will be false positives. Edit the file to remove
23 # those changes you don't want made. Then in the second phase, feed that
24 # list in, and it will change those lines in the files:
26 # $ Porting/bump-perl-version -u < /tmp/scan
28 # (so line 52 of Porting/config.sh is now updated)
30 # The -i option can be used to combine these two steps (if you prefer to make
31 # all of the changes at once and then edit the results via git).
33 # This utility 'knows' about certain files and formats, and so can spot
34 # 'hidden' version numbers, like PERL_SUBVERSION=9.
36 # A third variant makes use of this knowledge to check that all the things
37 # it knows about are at the current version:
39 # $ Porting/bump-perl-version -c 5.10.0
41 # XXX this script hasn't been tested against a major version bump yet,
42 # eg 5.11.0 to 5.12.0; there may be things it missed - DAPM 14 Jul 09
44 # Note there are various files and directories that it skips; these are
45 # ones that are unlikely to contain anything needing bumping, but which
46 # will generate lots of false positives (eg pod/*). These are listed on
47 # STDERR as they are skipped.
53 use ExtUtils::Manifest;
56 sub usage { die <<EOF }
65 -c check files and warn if any known string values (eg
66 PERL_SUBVERSION) don't match the specified version
68 -s scan files and produce list of possible change lines to stdout
70 -u read in the scan file from stdin, and change all the lines specified
72 -i scan files and make changes inplace
74 C.C.C the current perl version, eg 5.10.0
75 N.N.N the new perl version, eg 5.10.1
79 getopts('csui', \%opts) or usage;
81 @ARGV == 0 or usage('no version version numbers should be specified');
82 # fake to stop warnings when calculating $oldx etc
83 @ARGV = qw(99.99.99 99.99.99);
86 @ARGV == 1 or usage('required one version number');
90 @ARGV == 2 or usage('require two version numbers');
92 usage('only one of -c, -s, -u and -i') if keys %opts > 1;
94 my ($oldx, $oldy, $oldz) = $ARGV[0] =~ /^(\d+)\.(\d+)\.(\d+)$/
95 or usage("bad version: $ARGV[0]");
96 my ($newx, $newy, $newz) = $ARGV[1] =~ /^(\d+)\.(\d+)\.(\d+)$/
97 or usage("bad version: $ARGV[1]");
99 my $old_decimal = sprintf "%d.%03d%03d", $oldx, $oldy, $oldz; # 5.011001
102 # 0 a regexp that matches strings that might contain versions;
103 # 1 a sub that returns two strings based on $1 etc values:
104 # * string containing captured values (for -c)
105 # * a string containing the replacement value
106 # 2 what we expect the sub to return as its first arg; undef implies
108 # 3 a regex restricting which files this applies to (undef is all files)
110 # Note that @maps entries are checks in order, and only the first to match
115 qr{^((?:api_)?version(?:=|\s+)'?) (\d+) ('?) (?!\.)}x,
116 sub { $2, "$1$newy$3" },
121 qr{^(subversion(?:=|\s+)'?) (\d+) ('?) (?!\.)}x,
122 sub { $2, "$1$newz$3" },
127 qr{^(api_subversion(?:=|\s+)'?) (\d+) ('?) (?!\.)}x,
128 sub { $2, ($newy % 2) ? "$1$newz$3" : "${1}0$3" },
129 ($oldy % 2) ? $oldz : 0,
133 qr{^(api_versionstring(?:=|\s+)'?) ([\d\.]+) ('?) (?!\.)}x,
134 sub { $2, ($newy % 2) ? "$1$newx.$newy.$newz$3": "$1$newx.$newy.0$3" },
135 ($oldy % 2) ? "$oldx.$oldy.$oldz" : "$oldx.$oldy.0",
139 qr{(version\s+'?) (\d+) ('?\s+subversion\s+'?) (\d+) ('?) (?!\.)}x,
140 sub { "$2-$4", "$1$newy$3$newz$5" },
145 qr{\b (PERL_(?:API_)?VERSION(?:=|\s+)'?) (\d+) ('?) (?!\.)}x,
146 sub { $2, "$1$newy$3"},
150 qr{\b (PERL_SUBVERSION(?:=|\s+)'?) (\d+) ('?) (?!\.)}x,
151 sub { $2, "$1$newz$3"},
152 ($oldy % 2) ? $oldz : 0,
155 qr{\b (PERL_API_SUBVERSION(?:=|\s+)'?) (\d+) ('?) (?!\.)}x,
156 sub { $2, ($newy % 2) ? "$1$newz$3" : "${1}0$3" },
159 # these two formats are in README.vms
161 qr{\b perl-(\d+\^\.\d+\^\.\d+) \b}x,
162 sub { $1, "perl-$newx^.$newy^.$newz"},
166 qr{\b ($oldx _ $oldy _$oldz) \b}x,
167 sub { $1, ($newx . '_' . $newy . '_' . $newz)},
172 qr{ $oldx\.$oldy\.$oldz \b}x,
173 sub {"", "$newx.$newy.$newz"},
179 qr{ $old_decimal \b}x,
180 sub {"", sprintf "%d.%03d%03d", $newx, $newy, $newz },
184 # perl511, perl511.dll, perl511.lib, perl511s.lib, libperl511.a
186 qr{\b ((?:lib)?) perl (\d\d\d) (s?) \b }x,
187 sub {$2, "$1perl$newx$newy$3" },
189 qr/win32|hints/, # README.win32, win32/*, hints/*
192 # microperl locations should be bumped for major versions
194 qr{(/)(\d\.\d{2})(["'/])},
195 sub { $2, "$1$newx.$newy$3" },
202 qr/(PV\s*=\s*)(\d\d{2})\b$/,
203 sub { $2, "$1$newx$newy" },
210 # files and dirs that we likely don't want to change version numbers on.
212 my %SKIP_FILES = map { ($_ => 1) } qw(
215 Porting/Maintainers.pl
216 Porting/acknowledgements.pl
217 Porting/corelist-perldelta.pl
218 Porting/epigraphs.pod
219 Porting/how_to_write_a_perldelta.pod
220 Porting/release_managers_guide.pod
221 Porting/release_schedule.pod
222 Porting/bump-perl-version
234 my @mani_files = sort keys %{ExtUtils::Manifest::maniread('MANIFEST')};
235 my %mani_files = map { ($_ => 1) } @mani_files;
236 die "No entries found in MANIFEST; aborting\n" unless @mani_files;
238 if ($opts{c} or $opts{s} or $opts{i}) {
245 usage('one of -c, -s or -u must be specified');
253 for my $file (@mani_files) {
254 next if grep $file =~ m{^$_/}, @SKIP_DIRS;
255 if ($SKIP_FILES{$file}) {
256 warn "(skipping $file)\n";
259 open my $fh, '<', $file;
261 my @stat = stat $file;
263 my $file_changed = 0;
264 my $new_contents = '';
266 while (my $line = <$fh>) {
268 for my $map (@maps) {
269 my ($pat, $sub, $expected, $file_pat) = @$map;
271 next if defined $file_pat and $file !~ $file_pat;
272 next unless $line =~ $pat;
273 my ($got, $replacement) = $sub->();
276 # only report unexpected
277 next unless defined $expected and $got ne $expected;
279 $line =~ s/$pat/$replacement/
280 or die "Internal error: substitution failed: [$pat]\n";
282 $new_contents .= $line if $opts{i};
283 if ($line ne $oldline) {
286 print "\n$file\n" unless $header;
288 printf "\n%5d: -%s +%s", $., $oldline, $line;
292 if ($opts{i} && $file_changed) {
293 warn "Updating $file inplace\n";
294 open my $fh, '>', $file;
296 print $fh $new_contents;
298 chmod $mode & 0777, $file;
301 warn "(skipped $_/*)\n" for @SKIP_DIRS;
316 die "No such file in MANIFEST: '$file'\n" unless $mani_files{$file};
317 die "file already seen; '$file'\n" if exists $changes{$file};
320 elsif (/^\s+(\d+): -(.*)/) {
322 ($line, $old) = ($1,$2);
323 die "$.: old line without preceding filename\n"
324 unless defined $file;
325 die "Dup line number: $line\n" if exists $changes{$file}{$line};
326 $changes{$file}{$line}[0] = $old;
328 elsif (/^\s+\+(.*)/) {
330 die "$.: replacement line seen without old line\n" unless $line;
331 $changes{$file}{$line}[1] = $new;
335 die "Unexpected line at ;line $.: $_\n";
339 # suck in file contents to memory, then update that in-memory copy
342 for my $file (sort keys %changes) {
343 open my $fh, '<', $file;
345 $contents{$file} = [ <$fh> ];
346 chomp @{$contents{$file}};
349 my $entries = $changes{$file};
350 for my $line (keys %$entries) {
351 die "$file: no such line: $line\n"
352 unless defined $contents{$file}[$line-1];
353 if ($contents{$file}[$line-1] ne $entries->{$line}[0]) {
354 die "$file: line mismatch at line $line:\n"
355 . "File: [$contents{$file}[$line-1]]\n"
356 . "Config: [$entries->{$line}[0]]\n"
358 $contents{$file}[$line-1] = $entries->{$line}[1];
362 # check the temp files don't already exist
364 for my $file (sort keys %contents) {
365 my $nfile = "$file-new";
366 die "$nfile already exists in MANIFEST; aborting\n"
367 if $mani_files{$nfile};
370 # write out the new files
372 for my $file (sort keys %contents) {
373 my $nfile = "$file-new";
374 open my $fh, '>', $nfile;
376 print $fh $_, "\n" for @{$contents{$file}};
379 my @stat = stat $file;
381 die "stat $file fgailed to give a mode!\n" unless defined $mode;
382 chmod $mode & 0777, $nfile;
387 for my $file (sort keys %contents) {
388 my $nfile = "$file-new";
389 warn "updating $file ...\n";
390 rename $nfile, $file;