Path: blob/main/Tools/scripts/mark_safe.pl
16124 views
#!/usr/bin/env perl12# MAINTAINER= [email protected]3# all committers may commit to this file without approval45## core6use strict;7use warnings FATAL => 'all';8use Carp;910use File::Find ();11use Getopt::Long ();12use Pod::Usage ();1314### constants15## exit codes16use constant EXIT_SUCCESS => 0;17use constant EXIT_FAILED_INVALID_ARGS_OR_ENV => 1;1819## other20use constant PROGNAME => $0;2122### signal handlers23local $SIG{__DIE__} = \&Carp::confess;24local $SIG{__WARN__} = \&Carp::cluck;2526### version2728### globals29# cmdline options (standard) with defaults30my $Help = 0;31my $Version = 0;32my $Debug = 0;33my $Verbose = 0;34my $NoExec = 0;3536# cmdline options (custom) with defaults37my $Maintainer = "$ENV{USER}\@FreeBSD.org";38my $Ports = 0;39my $Safe = 1;40my $Index = 'INDEX-9';4142# internals43my $PORTSDIR = $ENV{PORTSDIR} || '/usr/ports';44my %RPARTS = (450 => 'pkg_name',461 => 'dir',472 => 'prefix',483 => 'comment',494 => 'pkg_descr',505 => 'maintainer',516 => 'categories',527 => 'ldep',538 => 'rdep',549 => 'www',55);5657my %PARTS = reverse %RPARTS;5859### Utility Functions60sub error { print STDERR "ERROR: $_[0]" }61sub debug { print STDERR "DEBUG: $_[0]" if $Debug }62sub verbose { print STDOUT "VERBOSE($_[0]): $_[1]" if $Verbose > $_[0] }6364### main65sub getopts_wrapper {6667my $rv =68Getopt::Long::GetOptions(69"debug|d" => \$Debug,70"verbose=i" => \$Verbose,71"help|h" => \$Help,72"version|V" => \$Version,73"noexec|n" => \$NoExec,7475"maintainer|m=s" => \$Maintainer,76"ports|p" => \$Ports,7778"safe|s=i" => \$Safe,79"index|i=s" => \$Index,80);8182Pod::Usage::pod2usage(-verbose => 1) unless $rv;8384unless ($Help || valid_args()) {85$rv = 0;86Pod::Usage::pod2usage(-verbose => 1);87}8889return $rv ? 1 : 0;90}9192sub valid_args {9394my $errors = 0;9596## NoExec implies Verbosity level 197$Verbose = 1 if $NoExec;9899return $errors > 0 ? 0 : 1;100}101102sub work {103104my $rv = EXIT_SUCCESS;105106my $ports = ports_get();107mark($ports);108109return $rv;110}111112sub mark {113my ($ports) = @_;114115foreach my $port_dir (@$ports) {116my $mfile = "$port_dir/Makefile";117print "Mfile: $mfile\n";118open my $mk, '<', $mfile or die "Can't open [$mfile] b/c [$!]";119my @lines = <$mk>;120close $mk or die "Can't close [$mfile] b/c [$!]";121122next if grep { /MAKE_JOBS_(?:UN)?SAFE|NO_BUILD/ } @lines;123124my $i_depends = 0;125my $i_comment = 0;126my $i_maintainer = 0;127my $i = 0;128foreach my $line (@lines) {129## ORDER MATTERs, lowest in file is last130$i_depends = $i if $line =~ /DEPENDS/;131$i_comment = $i if $line =~ /COMMENT/;132$i_maintainer = $i if $line =~ /MAINTAINER/;133++$i;134}135136my $loc = $i_depends > 0 ? $i_depends :137$i_comment > 0 ? $i_comment :138$i_maintainer > 0 ? $i_maintainer : print "Can't find location to insert", next;139140my @newlines = @lines[0..$loc];141push @newlines, "\n", "MAKE_JOBS_" . ($Safe ? "SAFE" : "UNSAFE") . "=\tyes\n";142push @newlines, @lines[$loc+1..$#lines];143144open my $mk_o, '>', $mfile or die "Can't open [$mfile] b/c [$!]";145foreach my $line (@newlines) {146print $mk_o $line;147}148close $mk_o or die "Can't close [$mfile] b/c [$!]";149}150151return;152}153154sub ports_get {155156my @ports = ();157158if ($Ports) {159@ports = map { "$PORTSDIR/$_" } @ARGV;160}161else {162my $index = "$PORTSDIR/$Index";163print "Index: $index\n";164165open my $fh, '<', $index or die "Can't open [$index] b/c [$!]";166while (<$fh>) {167my @parts = split /\|/;168my $port_dir = $parts[$PARTS{dir}];169$port_dir =~ s!/usr/ports!$PORTSDIR!;170my $maintainer = $parts[$PARTS{maintainer}];171172push @ports, $port_dir if $maintainer =~ /^$Maintainer$/io;173}174close $fh or die "Can't close [$index] b/c [$!]";175}176177@ports = grep { !/rubygem-/ } @ports;178179return \@ports;180}181182sub main {183184getopts_wrapper() or return EXIT_FAILED_INVALID_ARGS_OR_ENV;185186if ($Help) {187Pod::Usage::pod2usage(-verbose => 1);188return EXIT_SUCCESS;189}190191if ($Version) {192print PROGNAME . "\n\n";193return EXIT_SUCCESS;194}195196return work();197}198199MAIN: {200exit main();201}202203__END__204205=pod206207=head1 NAME208209mark_safe.pl - Mark a port or ports as MAKE_JOBS_(UN)SAFE=yes210211=head1 SYNOPSIS212213mark_safe.pl <options>214215=head1 STD OPTIONS216217=over 4218219=item B<--verbose=1,2,3,4,....>220221Display messages while running on STDOUT. Increasing the level222will increase the amount.223224DEFAULT: off/0225226=item B<--debug|d>227228Copious messages not useful unless you are a developer AND229debuging this script are output to STDERR230231DEFAULT: off/0232233=item B<--help|h>234235Print this message and exit EXIT_SUCCESS236237DEFAULT: off/0238239=item B<--version|V>240241Output the version and exit with EXIT_SUCCESS242243DEFAULT: off/0244245=item B<--noexec|n>246247Any External commands will simply be echo'd and not run248Assume all of them suceed.249250IMPLIES: --verbose=1251252DEFAULT: off/0253254=head1 Dependencies255256=head1 EXIT CODES257258Exits 0 on success259Exits > 0 <= 255 on error260261EXIT_SUCCESS => 0262EXIT_FAILED_INVALID_ARGS_OR_ENV => 1263264=head1 HISTORY26526620009-04-22 by pgollucci:267Created268269=head1 AUTHOR270271Philip M. Gollucci E<lt>[email protected]<gt>272273=cut274275276277