Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/freebsd-ports-gnome
Path: blob/main/Tools/scripts/bump_revision.pl
16124 views
1
#!/usr/bin/env -S perl -wT
2
3
#
4
# This script helps with bumping the PORTREVISION of all ports that depend on a
5
# set of ports, for instance, when in the latter set one of the ports bumped the
6
# .so library version.
7
#
8
# The shebang line above includes -T (taint) to be more distrustful
9
# about the environment, for security reasons, and is considered
10
# good Perl practice.
11
#
12
# You can use either the
13
# -l (shaLlow, avoid grandparent dependencies, slower) or
14
# -g option (include grandparent dependencies) option.
15
#
16
# MAINTAINER= [email protected]
17
#
18
19
use strict;
20
use Getopt::Std;
21
use Carp 'verbose';
22
use Cwd;
23
use Data::Dumper;
24
use File::Basename;
25
26
use vars qw/$opt_n $opt_f $opt_i $opt_u $opt_l $opt_g $opt_p/;
27
28
# launder environment
29
delete @ENV{'IFS', 'CDPATH', 'ENV', 'BASH_ENV'};
30
$ENV{'PATH'} = '/bin:/usr/bin:/usr/local/bin';
31
32
sub usage {
33
print <<EOF;
34
Usage: $0 [options] [<category>/]<portname>
35
36
Options:
37
-l - shaLlow, only bump ports with direct dependencies.
38
-g - Grandchildren, also bump for indirect dependencies (default).
39
-n - Check only (dry-run), do not change Makefiles.
40
-f - No tmpdir, just use the directory where INDEX resides.
41
-i <filename> - Use this for INDEX name. Defaults to \${PORTSDIR}/INDEX-n,
42
where n is the major version of the OS, or \${PORTSDIR}/INDEX if missing.
43
-p <dirname> - Set portsdir, if different from /usr/ports.
44
45
Improvements, suggestions, questions -> mandree\@FreeBSD.org
46
EOF
47
exit 1;
48
}
49
50
# flush STDOUT for each and every write even if writing to a pipe.
51
$| = 1;
52
53
sub bumpMakefile {
54
55
my ($p) = @_;
56
57
my $makefile = "$p/Makefile";
58
my $fin;
59
unless(open($fin, $makefile)) {
60
print "-- Cannot open Makefile of $p, ignored.\n";
61
next;
62
}
63
my @lines = <$fin>;
64
if ($!) { die "Error while reading $makefile: $!. Aborting"; }
65
close($fin) or die "Can't close $makefile b/c $!";
66
chomp(@lines);
67
68
my $revision = 1;
69
70
foreach my $line (@lines) {
71
last if ($line =~ /^MAINTAINER/);
72
$revision += $1 if ($line =~ /PORTREVISION\??=[ \t]*(\d+)$/);
73
}
74
75
my $printedrev = 0;
76
open(my $fout, '>', "$makefile.bumped");
77
foreach my $line (@lines) {
78
if (!$printedrev) {
79
if ($line =~ /^CATEGORIES??=/ || $line =~ /^PORTEPOCH??=/) {
80
print $fout "PORTREVISION= $revision\n";
81
$printedrev = 1;
82
# Fall through!
83
}
84
if ($line =~ /^PORTREVISION\?=/) {
85
print $fout "PORTREVISION?= $revision\n";
86
$printedrev = 1;
87
next;
88
}
89
if ($line =~ /^PORTREVISION=/) {
90
print $fout "PORTREVISION= $revision\n";
91
$printedrev = 1;
92
next;
93
}
94
}
95
print $fout "$line\n";
96
}
97
close($fout) or die "Can't close $makefile b/c $!";
98
rename "$makefile.bumped", $makefile or die "Can't rename $makefile.bumped to $makefile: $!";
99
}
100
101
my $osversion = `uname -r`;
102
chomp $osversion;
103
$osversion =~ s/\..*//;
104
105
my $shallow = 0;
106
my ($portsdir, $INDEX);
107
{
108
$opt_i = "";
109
$opt_u = "";
110
getopts("fgi:lnu:p:") or die "Aborting";
111
$shallow = $opt_l if $opt_l;
112
if ($opt_l and $opt_g) {
113
die "Options -g and -l given, which are mutually exclusive. Pick either.";
114
}
115
if (not $opt_l and not $opt_g) {
116
warn "Neither -g nor -l given. Defaulting to -g";
117
$opt_g = 1;
118
}
119
$portsdir = $opt_p ? $opt_p : '/usr/ports';
120
121
$INDEX = "$portsdir/INDEX-$osversion";
122
$INDEX = $opt_i if ($opt_i);
123
if (!-f $INDEX) { $INDEX = "$portsdir/INDEX"; }
124
125
die "$INDEX doesn't seem to exist. Please check the value supplied with -i,\n" .
126
"or use -i /path/to/INDEX, or check your -p PORTSDIR." unless(-f $INDEX);
127
}
128
usage() unless(@ARGV);
129
130
my $TMPDIR = File::Basename::dirname($INDEX);
131
132
#
133
# Sanity checking
134
#
135
if (-d "$TMPDIR/.svn" and not $opt_f and not $opt_n) {
136
die "$TMPDIR/.svn exists, cowardly refusing to proceed.\n";
137
}
138
139
140
# must launder $portsdir (from command line => tainted) first
141
if ($portsdir =~ /^([-\@\w.\/]+)$/) {
142
$portsdir = $1; }
143
else {
144
die "Portsdir \"$portsdir\" contains unsafe characters. Aborting";
145
}
146
147
chdir "$portsdir" or die "cannot cd to $portsdir: $!\nAborting";
148
149
#
150
# Read the index, save some interesting keys
151
#
152
my %index = ();
153
{
154
print "Reading $INDEX\n";
155
open(my $fin, '<', "$INDEX") or die "Cannot open $INDEX for reading.";
156
my @lines = <$fin>;
157
if ($!) { die "Error while reading $INDEX: $! Aborting"; }
158
chomp(@lines);
159
close($fin);
160
161
my @a;
162
my @b;
163
my $port;
164
map {
165
@a = split(/\|/, $_);
166
@b = split(/\//, $a[1]);
167
168
$port = $b[-2]."/".$b[-1];
169
170
@{ $index{$port} }{'portname', 'portnameversion', 'origin', 'comment', 'deps'}
171
= ($b[-1], $a[0], $port, $a[3], ());
172
173
if ($a[8]) {
174
@b = split(" ", $a[8]);
175
@{ $index{$port}{deps} }{@b} = (1) x @b;
176
}
177
178
} @lines;
179
print "- Processed ", scalar keys(%index), " entries.\n";
180
}
181
182
my %DEPPORTS = ();
183
184
foreach my $PORT (@ARGV) {
185
#
186
# See if the port really exists.
187
# If specified as category/portname, that should be enough.
188
# If specified as portname, check all categories for existence or duplicates.
189
#
190
unless (defined $index{$PORT}) {
191
my @found = grep /\/$PORT$/, keys(%index);
192
my $count = @found;
193
194
if ($count == 0) {
195
die "Cannot find ${PORT} in ${INDEX}.";
196
} elsif ($count == 1) {
197
$PORT = $found[0];
198
} else {
199
my $n = join(" ", @found);
200
die "Found ${PORT} more than once in ${INDEX}: $n. Try category/$PORT.\nAborting";
201
}
202
}
203
204
my $PORTNAMEVERSION = $index{$PORT}{portnameversion};
205
print "Found $PORT as $PORTNAMEVERSION\n";
206
207
#
208
# Figure out all the ports depending on this one.
209
#
210
{
211
print "Searching for ports depending on $PORT\n";
212
my $count = 0;
213
214
foreach my $p (keys(%index)) {
215
if (defined $index{$p}{'deps'}{$PORTNAMEVERSION}) {
216
$DEPPORTS{$p} = 1;
217
++$count;
218
}
219
}
220
print "- Found $count ports depending on $PORT.\n";
221
}
222
}
223
224
#
225
# In shallow mode, strip all those who don't have a direct dependency
226
#
227
sub direct_dependency($@) {
228
my ($port, @requisites) = @_;
229
open F, '-|', '/usr/bin/make', '-C', $port, qw/-V _RUN_DEPENDS -V _LIB_DEPENDS/ or die "cannot launch make: $!";
230
my @lines = <F>;
231
chomp @lines;
232
my $deps = join(" ", @lines);
233
my %deps = map { $_ =~ s[/usr/ports/][]; $_ =~ s[$portsdir/][]; ($_ => 1) } split " ", $deps;
234
if ($!) { die "cannot read depends from make: $!"; }
235
close F or die "cannot read depends from make: $!";
236
my $required = grep { $_ } map { defined $deps{$_} } @requisites;
237
return $required;
238
}
239
240
if ($shallow) {
241
my $n = keys %DEPPORTS;
242
my $idx = 1;
243
foreach my $p (keys %DEPPORTS) {
244
print "- Checking requisites of port $idx/$n...\r";
245
++$idx;
246
unless (direct_dependency($p, @ARGV)) {
247
delete $DEPPORTS{$p};
248
}
249
}
250
print "- Found ", scalar keys(%DEPPORTS), " ports depending directly on either of @ARGV.\n";
251
}
252
253
my $ports = join(" ", keys %DEPPORTS);
254
255
#
256
# Create a temp directory and cvs checkout the ports
257
# (don't do error checking, too complicated right now)
258
#
259
unless ($opt_f or $opt_n) {
260
$TMPDIR = ".bump_revision_pl_tmpdir.$$";
261
print "svn checkout into $TMPDIR...\n";
262
mkdir($TMPDIR, 0755);
263
chdir($TMPDIR);
264
system "svn checkout --depth=immediates svn+ssh://repo.freebsd.org/ports/head/ ports" and die "SVN checkout failed (wait value $?), aborting";
265
chdir('ports');
266
system "svn update --set-depth=infinity $ports" and die "SVN checkout failed (wait value $?), aborting";
267
}
268
269
#
270
# Bump portrevisions
271
#
272
{
273
print "Updating Makefiles\n";
274
foreach my $p (sort keys(%DEPPORTS)) {
275
print "- Updating Makefile of $p\n";
276
next if $opt_n;
277
bumpMakefile "$p";
278
}
279
}
280
281
#
282
# Commit the changes. Not automated.
283
#
284
unless ($opt_n) {
285
print <<EOF;
286
All PORTREVISIONs have been updated. You are nearly done, only one
287
thing remains: Committing to the ports tree. This program is not
288
going to do that for you, you have to do it manually.
289
290
\$ cd $TMPDIR
291
\$ svn commit
292
293
Then, remove the temp directory ($TMPDIR).
294
EOF
295
}
296
297