]> git.pld-linux.org Git - projects/cleanbuild.git/blob - addbr
_binary_payload w1.gzdio
[projects/cleanbuild.git] / addbr
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 my $fname = shift @ARGV or die;
7 my $add = shift @ARGV or die;
8 my $msg = shift @ARGV || "requested";
9
10 $SIG{__WARN__} = sub
11 {
12         local $_ = shift;
13         chomp;
14         print STDERR "\033[32;1m" . $_ . "\033[0m\n"
15 };
16
17 warn "Adding: $add\n";
18
19 my $file = $fname;
20 $file = "$ENV{HOME}/rpm/packages/$fname/$fname.spec"
21         unless -r $file;
22 -r $file or die "Can't read $file";
23
24 open FILE, "<", $file;
25 my @in = <FILE>;
26 close FILE;
27
28 my $brm = qr/#?(?:%\{!?\?with_\S+:)?\s*BuildRequires:\s*(\S+)\s*(?:(>|>=|==|<=|<).*)?\}?/i;
29 foreach ( @in ) {
30         if ( /^$brm$/o ) {
31                 if ( $1 eq $add ) {
32                         warn "$add already on the list\n";
33                         exit(1);
34                 }
35         }
36
37 }
38
39 my @out;
40 while ( $_ = shift @in ) {
41         if ( /^\s*(build(requires|root|conflicts|arch)|requires(\(.*\))?|provides|conflicts|obsoletes|excludearch):/i ) {
42                 unshift @in, $_;
43                 last;
44         }
45         push @out, $_;
46 }
47
48 my @buf;
49 while ( $_ = shift @in ) {
50         if ( /^%if/ ) {
51                 push @buf, $_;
52                 my $i = 1;
53                 while ( $_ = shift @in ) {
54                         push @buf, $_;
55                         if ( /^%if/ ) {
56                                 $i++;
57                         } elsif ( /^%endif/ ) {
58                                 $i--;
59                                 last unless $i;
60                         }
61                 }
62
63         } elsif ( /^$brm$/o ) {
64                 my $name = $1;
65                 push @buf, $_;
66
67                 if ( $add lt $name ) {
68                         push @out, "# AUTO: $msg\n";
69                         push @out, "BuildRequires:\t$add\n";
70                         $add = undef;
71                 }
72                 push @out, @buf;
73                 @buf = ();
74         } elsif ( /^\s*$/ or /^\s*#/ ) {
75                 push @buf, $_;
76         } else {
77                 push @buf, $_;
78                 push @out, "# AUTO: $msg\n";
79                 push @out, "BuildRequires:\t$add\n";
80                 last;
81
82         }
83         last unless $add;
84 }
85
86 push @out, @buf;
87 push @out, @in;
88
89
90 open FILE, ">", $file;
91 print FILE @out;
92 close FILE;
This page took 0.066951 seconds and 3 git commands to generate.