All pastes #163653 Raw Edit

epackageuse

public text v1 · immutable
·published 2006-09-07 18:58 UTC
rendered paste body
#!/usr/bin/perl 
#===============================================================================
#
#         FILE:  epu.pl
#
#        USAGE:  ./epu.pl <package/name> <useflags>
#
#  DESCRIPTION:  This script is able to manage the /etc/portage/package.use file
#
#      OPTIONS:  <package/name> must be a valid portage package e.g. "net-im/gaim",
#      			 <useflag> must be a valid useflag e.g. "-perl" to disable
#      			 or "perl" to enable or add
#      			 (without qoutes (" ") of course ;).
# REQUIREMENTS:  Gentoo (emerge)
#         BUGS:  none reportet
#        NOTES:  none
#       AUTHOR:  ViLeDa (Tristan Leo), <filecorpse@gmail.com>
#      COMPANY:  Schmerzerzeuger
#      VERSION:  1.0
#      CREATED:  07.09.2006 19:27:33 CEST
#     REVISION:  2
#
#===============================================================================

use strict;
use Tie::File;

#Check commandline
unless ( @ARGV > 1 && $ARGV[0] =~ m/\w+\/\w+/ ) {
    print "Syntax: <package> <useflags>
For example:
	to enable a flag:     epu net-im/gaim perl
	or to disable a flag: epu net-im/gaim -perl\n";
    die "More help informations soon.\n";
}

#some declarations
my $package  = shift @ARGV;
my @useflags = sort @ARGV;
my $usefile  = './package.use';
my $fuseflags;
my $fpackage;
my $haspackage;
my $i = 0;

#read the file into an array
tie my @uselines, 'Tie::File', $usefile;

#look if package already in there
for my $fuseline (@uselines) {

    #regexp for package match and useflag extraction
    if ( $fuseline =~ m/^($package)[ ](.*)$/sg )
    {    #package is already in the file
            #save useflags
        $fpackage   = $1;
        $fuseflags  = $2;
        $haspackage = 1;
        last;
    }
    $i++;
}

$uselines[@uselines] = $package . " " . join( " ", @useflags )
  if not $haspackage;    #write new line to file

for my $flags (@useflags) {
    next if $fuseflags =~ m/^($flags)/;
    next if $fuseflags =~ m/[ ]($flags)/;
    next if $fuseflags =~ s/[-]$flags/$flags/;
    if ( $flags =~ m/[-](.+)/s ) {
        if ( undef ne $1 && $fuseflags =~ m/$1/s ) {
            $fuseflags =~ s/$1/$flags/;
        }
    }
    else {
        print "Adding useflag: ", $flags, "\n";
        $fuseflags .= " " . $flags;
    }
}

$uselines[$i] = $package . " " . join( " ", sort split( " ", $fuseflags ) );

print "New line: ", $uselines[$i], "\n";

untie @uselines;

# vim:ts=8:sw=4:ft=perl