Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/freebsd-ports-kde
Path: blob/main/Tools/scripts/mark_safe.pl
16462 views
1
#!/usr/bin/env perl
2
3
# MAINTAINER= [email protected]
4
# all committers may commit to this file without approval
5
6
## core
7
use strict;
8
use warnings FATAL => 'all';
9
use Carp;
10
11
use File::Find ();
12
use Getopt::Long ();
13
use Pod::Usage ();
14
15
### constants
16
## exit codes
17
use constant EXIT_SUCCESS => 0;
18
use constant EXIT_FAILED_INVALID_ARGS_OR_ENV => 1;
19
20
## other
21
use constant PROGNAME => $0;
22
23
### signal handlers
24
local $SIG{__DIE__} = \&Carp::confess;
25
local $SIG{__WARN__} = \&Carp::cluck;
26
27
### version
28
29
### globals
30
# cmdline options (standard) with defaults
31
my $Help = 0;
32
my $Version = 0;
33
my $Debug = 0;
34
my $Verbose = 0;
35
my $NoExec = 0;
36
37
# cmdline options (custom) with defaults
38
my $Maintainer = "$ENV{USER}\@FreeBSD.org";
39
my $Ports = 0;
40
my $Safe = 1;
41
my $Index = 'INDEX-9';
42
43
# internals
44
my $PORTSDIR = $ENV{PORTSDIR} || '/usr/ports';
45
my %RPARTS = (
46
0 => 'pkg_name',
47
1 => 'dir',
48
2 => 'prefix',
49
3 => 'comment',
50
4 => 'pkg_descr',
51
5 => 'maintainer',
52
6 => 'categories',
53
7 => 'ldep',
54
8 => 'rdep',
55
9 => 'www',
56
);
57
58
my %PARTS = reverse %RPARTS;
59
60
### Utility Functions
61
sub error { print STDERR "ERROR: $_[0]" }
62
sub debug { print STDERR "DEBUG: $_[0]" if $Debug }
63
sub verbose { print STDOUT "VERBOSE($_[0]): $_[1]" if $Verbose > $_[0] }
64
65
### main
66
sub getopts_wrapper {
67
68
my $rv =
69
Getopt::Long::GetOptions(
70
"debug|d" => \$Debug,
71
"verbose=i" => \$Verbose,
72
"help|h" => \$Help,
73
"version|V" => \$Version,
74
"noexec|n" => \$NoExec,
75
76
"maintainer|m=s" => \$Maintainer,
77
"ports|p" => \$Ports,
78
79
"safe|s=i" => \$Safe,
80
"index|i=s" => \$Index,
81
);
82
83
Pod::Usage::pod2usage(-verbose => 1) unless $rv;
84
85
unless ($Help || valid_args()) {
86
$rv = 0;
87
Pod::Usage::pod2usage(-verbose => 1);
88
}
89
90
return $rv ? 1 : 0;
91
}
92
93
sub valid_args {
94
95
my $errors = 0;
96
97
## NoExec implies Verbosity level 1
98
$Verbose = 1 if $NoExec;
99
100
return $errors > 0 ? 0 : 1;
101
}
102
103
sub work {
104
105
my $rv = EXIT_SUCCESS;
106
107
my $ports = ports_get();
108
mark($ports);
109
110
return $rv;
111
}
112
113
sub mark {
114
my ($ports) = @_;
115
116
foreach my $port_dir (@$ports) {
117
my $mfile = "$port_dir/Makefile";
118
print "Mfile: $mfile\n";
119
open my $mk, '<', $mfile or die "Can't open [$mfile] b/c [$!]";
120
my @lines = <$mk>;
121
close $mk or die "Can't close [$mfile] b/c [$!]";
122
123
next if grep { /MAKE_JOBS_(?:UN)?SAFE|NO_BUILD/ } @lines;
124
125
my $i_depends = 0;
126
my $i_comment = 0;
127
my $i_maintainer = 0;
128
my $i = 0;
129
foreach my $line (@lines) {
130
## ORDER MATTERs, lowest in file is last
131
$i_depends = $i if $line =~ /DEPENDS/;
132
$i_comment = $i if $line =~ /COMMENT/;
133
$i_maintainer = $i if $line =~ /MAINTAINER/;
134
++$i;
135
}
136
137
my $loc = $i_depends > 0 ? $i_depends :
138
$i_comment > 0 ? $i_comment :
139
$i_maintainer > 0 ? $i_maintainer : print "Can't find location to insert", next;
140
141
my @newlines = @lines[0..$loc];
142
push @newlines, "\n", "MAKE_JOBS_" . ($Safe ? "SAFE" : "UNSAFE") . "=\tyes\n";
143
push @newlines, @lines[$loc+1..$#lines];
144
145
open my $mk_o, '>', $mfile or die "Can't open [$mfile] b/c [$!]";
146
foreach my $line (@newlines) {
147
print $mk_o $line;
148
}
149
close $mk_o or die "Can't close [$mfile] b/c [$!]";
150
}
151
152
return;
153
}
154
155
sub ports_get {
156
157
my @ports = ();
158
159
if ($Ports) {
160
@ports = map { "$PORTSDIR/$_" } @ARGV;
161
}
162
else {
163
my $index = "$PORTSDIR/$Index";
164
print "Index: $index\n";
165
166
open my $fh, '<', $index or die "Can't open [$index] b/c [$!]";
167
while (<$fh>) {
168
my @parts = split /\|/;
169
my $port_dir = $parts[$PARTS{dir}];
170
$port_dir =~ s!/usr/ports!$PORTSDIR!;
171
my $maintainer = $parts[$PARTS{maintainer}];
172
173
push @ports, $port_dir if $maintainer =~ /^$Maintainer$/io;
174
}
175
close $fh or die "Can't close [$index] b/c [$!]";
176
}
177
178
@ports = grep { !/rubygem-/ } @ports;
179
180
return \@ports;
181
}
182
183
sub main {
184
185
getopts_wrapper() or return EXIT_FAILED_INVALID_ARGS_OR_ENV;
186
187
if ($Help) {
188
Pod::Usage::pod2usage(-verbose => 1);
189
return EXIT_SUCCESS;
190
}
191
192
if ($Version) {
193
print PROGNAME . "\n\n";
194
return EXIT_SUCCESS;
195
}
196
197
return work();
198
}
199
200
MAIN: {
201
exit main();
202
}
203
204
__END__
205
206
=pod
207
208
=head1 NAME
209
210
mark_safe.pl - Mark a port or ports as MAKE_JOBS_(UN)SAFE=yes
211
212
=head1 SYNOPSIS
213
214
mark_safe.pl <options>
215
216
=head1 STD OPTIONS
217
218
=over 4
219
220
=item B<--verbose=1,2,3,4,....>
221
222
Display messages while running on STDOUT. Increasing the level
223
will increase the amount.
224
225
DEFAULT: off/0
226
227
=item B<--debug|d>
228
229
Copious messages not useful unless you are a developer AND
230
debuging this script are output to STDERR
231
232
DEFAULT: off/0
233
234
=item B<--help|h>
235
236
Print this message and exit EXIT_SUCCESS
237
238
DEFAULT: off/0
239
240
=item B<--version|V>
241
242
Output the version and exit with EXIT_SUCCESS
243
244
DEFAULT: off/0
245
246
=item B<--noexec|n>
247
248
Any External commands will simply be echo'd and not run
249
Assume all of them suceed.
250
251
IMPLIES: --verbose=1
252
253
DEFAULT: off/0
254
255
=head1 Dependencies
256
257
=head1 EXIT CODES
258
259
Exits 0 on success
260
Exits > 0 <= 255 on error
261
262
EXIT_SUCCESS => 0
263
EXIT_FAILED_INVALID_ARGS_OR_ENV => 1
264
265
=head1 HISTORY
266
267
20009-04-22 by pgollucci:
268
Created
269
270
=head1 AUTHOR
271
272
Philip M. Gollucci E<lt>[email protected]<gt>
273
274
=cut
275
276
277