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
|
#!/usr/bin/env perl
# -*- perl -*-
# %CopyrightBegin%
#
# Copyright Ericsson AB 2024. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# %CopyrightEnd%
use strict;
use warnings;
my %updated_apps;
my %pre_used_app_vsns;
my %post_used_app_vsns;
my %src_app_vsns;
my %unchanged_app_replace;
my %changed_app_replace;
my $src_otp_vsn;
my $otp_release;
my $src_root;
my $rel_root;
my $old_app_vsns_file;
my $new_app_vsns_file;
while (@ARGV) {
my $opt = shift @ARGV;
if ($opt eq '-s') {
if (@ARGV) {
$src_root = shift @ARGV;
}
else {
usage("Missing source root");
}
}
elsif ($opt eq '-r') {
if (@ARGV) {
$rel_root = shift @ARGV;
}
else {
usage("Missing release root");
}
}
elsif ($opt eq '-a') {
if (@ARGV) {
$old_app_vsns_file = shift @ARGV;
}
else {
usage("Missing app versions file");
}
}
}
$src_root or usage("Missing source root");
$rel_root or usage("Missing release root");
$old_app_vsns_file or usage("Missing app versions file");
open(OTP_VSN_FILE, "$src_root/OTP_VERSION")
or fail("Failed to open $src_root/OTP_VERSION");
$src_otp_vsn = <OTP_VSN_FILE>;
chomp($src_otp_vsn);
close(OTP_VSN_FILE);
if ($src_otp_vsn !~ /^(\d+)(?:\.\d)+$/) {
fail("Invalid OTP version: $src_otp_vsn");
}
$otp_release=$1;
open(OTP_VSN_FILE, "$rel_root/releases/$otp_release/OTP_VERSION")
or fail("Failed to open $rel_root/releases/$otp_release/OTP_VERSION");
my $rel_otp_vsn = <OTP_VSN_FILE>;
chomp($rel_otp_vsn);
close(OTP_VSN_FILE);
$new_app_vsns_file = "$rel_root/releases/$otp_release/installed_application_versions";
# Mappings from application to used application versions before and
# after patching
read_used_app_vsns(\%pre_used_app_vsns, $old_app_vsns_file);
read_used_app_vsns(\%post_used_app_vsns, $new_app_vsns_file);
# Mapping from application to application versions in the source tree
open(VSN_TAB, "$src_root/otp_versions.table")
or fail("Failed to open: $src_root/otp_versions.table");
# First line contains all application versions of this OTP version...
my $vsn_tab_line = <VSN_TAB>;
close(VSN_TAB);
foreach my $app_vsn (split(/ /, $vsn_tab_line)) {
chomp($app_vsn);
if ($app_vsn !~ /^([\w_]+)-(\d+(?:\.\d+)+)$/) {
next if (($app_vsn eq ':') or ($app_vsn eq '#'));
fail("Invalid data in versions table: $app_vsn");
}
else {
my $app = $1;
my $vsn = $2;
$src_app_vsns{$app} = $vsn;
}
}
my $no_pre_apps = keys %pre_used_app_vsns;
my $no_post_apps = keys %post_used_app_vsns;
if (!$no_pre_apps) {
fail("No old applications found");
}
if ($no_pre_apps != $no_post_apps) {
fail("Amount of applications changed from $no_pre_apps to $no_post_apps");
}
# Create mappings for replacing links between application versions.
#
# The mapping for changed (newly patched) applications is from
# application versions in source tree to used application versions
# after patching installation.
#
# The mapping for unchanged applications is from previously used
# application versions (before this patching) to used application
# versions after patching installation.
foreach my $app (keys %pre_used_app_vsns) {
if (!defined $post_used_app_vsns{$app}) {
fail("Application '$app' missing after applying patch");
}
my $pre_vsn = $pre_used_app_vsns{$app};
my $post_vsn = $post_used_app_vsns{$app};
my $src_vsn = $src_app_vsns{$app};
if ($pre_vsn ne $post_vsn) {
$updated_apps{$app} = 1;
}
else {
$updated_apps{$app} = 0;
}
$unchanged_app_replace{"$app-$pre_vsn"} = "$app-$post_vsn";
$changed_app_replace{"$app-$src_vsn"} = "$app-$post_vsn";
}
# Create patterns matching the keys in mappings
my $unchanged_app_replace_pattern = '';
my $changed_app_replace_pattern = '';
foreach my $app_vsn (keys %unchanged_app_replace) {
if ($unchanged_app_replace_pattern ne '') {
$unchanged_app_replace_pattern .= "|";
}
$app_vsn =~ s/\./\\./g;
$unchanged_app_replace_pattern .= $app_vsn;
}
foreach my $app_vsn (keys %changed_app_replace) {
if ($changed_app_replace_pattern ne '') {
$changed_app_replace_pattern .= "|";
}
$app_vsn =~ s/\./\\./g;
$changed_app_replace_pattern .= $app_vsn;
}
# Update links in each application of the installation
# using the mappings createded above
foreach my $app (keys %post_used_app_vsns) {
my $dir = "$rel_root/";
my $vsn = $post_used_app_vsns{$app};
if ($app eq 'erts') {
$dir .= 'erts';
}
else {
$dir .= "lib/$app";
}
$dir .= "-$vsn/doc/html";
chdir $dir or fail("Failed to cd into $dir");
my @files = glob("*.html");
foreach my $file (@files) {
update_html_links($file, $updated_apps{$app}, 0);
}
}
# Also update top and system links. Here we also need to update
# the OTP version to match the base version for the installation.
foreach my $dir (("$rel_root/doc", "$rel_root/doc/system")) {
chdir $dir or fail("Failed to cd into $dir");
my @files = glob("*.html");
foreach my $file (@files) {
update_html_links($file, 1, $file eq 'man_index.html');
update_html_otp_vsn($file, $src_app_vsns{'OTP'}, $rel_otp_vsn);
}
}
# and OTP version in scripts
chdir "$rel_root/doc/dist" or fail("Failed to cd into $rel_root/doc/dist");
foreach my $file (glob("*.js")) {
my $src_vsn_pattern = $src_otp_vsn;
$src_vsn_pattern =~ s/\./\\./g;
my $modified = 0;
my $data = '';
open(JS, $file)
or fail("Failed to open file: $file");
while (<JS>) {
my $line = $_;
$line =~ s|Erlang/OTP v$src_vsn_pattern|Erlang/OTP v$rel_otp_vsn|g
and $modified = 1;
$line =~ s|Erlang/OTP $src_vsn_pattern|Erlang/OTP $rel_otp_vsn|g
and $modified = 1;
$data .= $line;
}
close(JS);
if ($modified) {
open(JS, '>', $file)
or fail("Failed to open file for writing: $file");
print JS $data;
close(JS);
}
}
# done
exit 0;
sub update_html_links {
my ($file, $changed_app, $replace_all_app_vsns) = @_;
my $data = '';
my $pattern;
my %replace;
my $modified = 0;
my $suffix = '/doc/html';
if ($replace_all_app_vsns) {
$suffix = '';
}
if ($changed_app) {
$pattern = $changed_app_replace_pattern;
%replace = %changed_app_replace;
}
else {
$pattern = $unchanged_app_replace_pattern;
%replace = %unchanged_app_replace;
}
open(HTML, $file)
or fail("Failed to open file: $file");
while (<HTML>) {
my $line = $_;
if ($changed_app) {
$line =~ s|($changed_app_replace_pattern)$suffix|$changed_app_replace{$1}$suffix|g
and $modified = 1;
}
else {
$line =~ s|($unchanged_app_replace_pattern)$suffix|$unchanged_app_replace{$1}$suffix|g
and $modified = 1;
}
$data .= $line;
}
close(HTML);
if ($modified) {
open(HTML, '>', $file)
or fail("Failed to open file for writing: $file");
print HTML $data;
close(HTML);
}
}
sub update_html_otp_vsn {
my ($file, $src_otp_vsn, $rel_otp_vsn) = @_;
my $src_vsn_pattern = $src_otp_vsn;
$src_vsn_pattern =~ s/\./\\./g;
my $in_proj_ver = 0;
my $modified = 0;
my $data = '';
open(HTML, $file)
or fail("Failed to open file: $file");
while (<HTML>) {
my $line = $_;
if (!$in_proj_ver) {
$line =~ s|Erlang/OTP v$src_vsn_pattern|Erlang/OTP v$rel_otp_vsn|g
and $modified = 1;
$line =~ s|Erlang/OTP $src_vsn_pattern|Erlang/OTP $rel_otp_vsn|g
and $modified = 1;
}
else {
if ($line =~ s|^(\s*v)$src_vsn_pattern(\s*</div>)|$1$rel_otp_vsn$2|g) {
$modified = 1;
$in_proj_ver = 0;
}
if ($line =~ s|^(\s*v)$src_vsn_pattern|$1$rel_otp_vsn|g) {
$modified = 1;
$in_proj_ver = 0;
}
}
$line =~ s|(<div[^>]+projectVersion[^>]+>\s*v)$src_vsn_pattern(\s*</div>)|$1$rel_otp_vsn$2|g
and $modified = 1;
$line =~ /<div[^>]+projectVersion[^>]+>\s*$/
and $in_proj_ver = 1;
$data .= $line;
}
close(HTML);
if ($modified) {
open(HTML, '>', $file)
or fail("Failed to open file for writing: $file");
print HTML $data;
close(HTML);
}
}
sub read_used_app_vsns {
my ($hash, $file) = @_;
open(AV_FILE, $file)
or fail("Failed to open file: $file");
while (<AV_FILE>) {
my $app_vsn = $_;
chomp($app_vsn);
/^(\w+)-(\d+(?:\.\d+)+)$/
or fail("Invalid application version $app_vsn in file $file");
my $app = $1;
my $vsn = $2;
if (!defined $hash->{$app}) {
$hash->{$app} = $vsn;
}
else {
my $res = cmp_vsn($vsn, $hash->{$app});
if ($res eq '>') {
$hash->{$app} = $vsn;
}
elsif ($res eq 'error') {
fail("Internal error");
}
}
}
close(AV_FILE);
}
sub cmp_vsn {
my ($xs, $ys) = @_;
my @xa;
my @ya;
my $x;
my $y;
return 'error' if ($xs !~ /^\d+(?:\.\d+)+$/);
return '=' if ($xs eq $ys);
return 'error' if ($ys !~ /^\d+(?:\.\d+)+$/);
@xa = split(/\./, $xs);
@ya = split(/\./, $ys);
while (@xa and @ya) {
$x = shift @xa;
$y = shift @ya;
last if ($x != $y);
}
return '<' if ($x < $y);
return '>' if ($x > $y);
return '=' if (!@xa and !@ya);
return '>' if (@xa);
return '<';
}
sub fail {
my $err = shift;
print STDERR "ERROR: $err\n";
exit 1;
}
sub usage {
my $err = shift;
print STDERR "ERROR: $err\n Usage: $0 -s <source root> -r <release root> -a <old app versions file>\n";
exit 1;
}
|