3 # Copyright 2003-2004, Gentoo Foundation
4 # Distributed under the terms of the GNU General Public License v2
5 # Written by Aron Griffis <agriffis@gentoo.org>
7 # ekeyword: Update the KEYWORDS in an ebuild. For example:
9 # $ ekeyword ~alpha oaf-0.6.8-r1.ebuild
11 # < KEYWORDS="x86 ppc sparc"
13 # > KEYWORDS="x86 ppc sparc ~alpha"
16 my ($kw_re) = '^(?:([-~^]?)(\w[\w-]*)|([-^]\*))$';
19 # make sure the cmdline consists of keywords and ebuilds
21 die "syntax: ekeyword { arch | ~[arch] | -[arch] } ebuild...\n"
24 $a = '~all' if $a eq '~' or $a eq $ENV{'HOME'}; # for vapier
25 next if $a =~ /$kw_re/o; # keyword
26 next if $a =~ /^\S+\.ebuild$/; # ebuild
27 die "I don't understand $a\n";
31 if ($f =~ /$kw_re/o) {
38 open I, "<$f" or die "Can't read $f: $!\n";
39 open O, ">$f.new" or die "Can't create $f.new: $!\n";
43 /^KEYWORDS/ or print, next;
45 # extract the quoted section from KEYWORDS
46 (my $quoted = $_) =~ s/^.*?["'](.*?)["'].*/$1/s;
48 # replace -* with -STAR for our convenience below
49 $quoted =~ s/-\*/-STAR/;
52 my ($leader, $arch, $star) = ($k =~ /$kw_re/o);
56 $leader = substr $star,0,1;
65 $quoted =~ s/[-~]?\Q$arch\E\b//;
69 # add or modify keywords
72 # modify all non-masked keywords in the list
73 $quoted =~ s/(^|\s)~?(?=\w)/$1$leader/g;
75 # modify or add keyword
76 unless ($quoted =~ s/[-~]?\Q$arch\E(\s|$)/$leader$arch$1/) {
77 # modification failed, need to add
78 if ($arch eq 'STAR') {
79 $quoted = "$leader$arch $quoted";
81 $quoted .= " $leader$arch";
88 # replace -STAR with -*
89 $quoted =~ s/-STAR\b/-*/;
91 # sort keywords and fix spacing
92 $quoted = join " ", sort {
93 (my $sa = $a) =~ s/^\W//;
94 (my $sb = $b) =~ s/^\W//;
95 return -1 if $sa eq '*';
96 return +1 if $sb eq '*';
100 # re-insert quoted to KEYWORDS
101 s/(["']).*?["']/$1$quoted$1/;
103 print $_, <I> or die "Can't write $f.new: $!\n";
110 system "diff $f $f.new | grep -v '^[0-1]'";
111 rename "$f.new", "$f" or die "Can't rename: $!\n";