summaryrefslogtreecommitdiff
path: root/substitutedeps
blob: 8093d129ba9ef9f6188afac84b8d777b0c628a12 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#!/usr/bin/perl -w

BEGIN {
  unshift @INC, ($::ENV{'BUILD_DIR'} || '/usr/lib/build');
}

use strict;

use Build;

my ($dist, $rpmdeps, $archs, $configdir, $release, $changelog);

while (@ARGV)  {
  if ($ARGV[0] eq '--dist') {
    shift @ARGV;
    $dist = shift @ARGV;
    next;
  }
  if ($ARGV[0] eq '--archpath') {
    shift @ARGV;
    $archs = shift @ARGV;
    next;
  }
  if ($ARGV[0] eq '--configdir') {
    shift @ARGV;
    $configdir = shift @ARGV;
    next;
  }
  if ($ARGV[0] eq '--release') {
    shift @ARGV;
    $release = shift @ARGV;
    next;
  }
  if ($ARGV[0] eq '--changelog') {
    shift @ARGV;
    $changelog = shift @ARGV;
    next;
  }
  last;
}
die("Usage: substitutedeps --dist <dist> --archpath <archpath> [--configdir <configdir>] <specin> <specout>\n") unless @ARGV == 2;
my $spec = $ARGV[0];
my $newspec = $ARGV[1];

my $cf = Build::read_config_dist($dist, $archs, $configdir);

#######################################################################

my $xspec = [];
my $d = Build::read_spec($cf, $spec, $xspec) || {};
my @sdeps = @{$d->{'deps'} || []};
my @neg = map {substr($_, 1)} grep {/^-/} @{$d->{'deps'} || []};
my %neg = map {$_ => 1} @neg;
@sdeps = grep {!$neg{$_}} @sdeps;
@sdeps = Build::do_subst($cf, @sdeps);
@sdeps = grep {!$neg{$_}} @sdeps;
my %sdeps = map {$_ => 1} @sdeps;

open(F, '>', $newspec) || die("$newspec: $!\n");

my $used;
my $inchangelog = 0;
for my $l (@$xspec) {
  $used = 1;
  if (ref($l)) {
    if (!defined($l->[1])) {
      $used = 0;
      $l = $l->[0];
    } else {
      $l = $l->[1];
    }
  }

  if ($inchangelog) {
    $inchangelog = 0 if $l =~ /^\s*%[^%]/;
    next if $inchangelog;
  }
  if ($changelog && ($l =~ /\s*\%changelog\b/)) {
    $inchangelog = 1;
    next;
  }

  $l =~ s/^(Release:\s*).*/$1$release/i if $release;

  if (!$used || ($l !~ /^BuildRequires:/i)) {
    print F "$l\n";
    next;
  }

  my $r = $l;
  $r =~ s/^[^:]*:\s*//;
  my @deps = $r =~ /([^\s\[\(,]+)(\s+[<=>]+\s+[^\s\[,]+)?[\s,]*/g;
  my @ndeps = ();
  my $replace = 0;
  my %f2 = @deps;
  my @f2 = Build::do_subst($cf, grep {!/^-/} keys %f2);
  %f2 = map {$_ => 1} @f2;
  delete $f2{$_} for @neg;
  while (@deps) {
    my ($pack, $vers) = splice(@deps, 0, 2);
    $vers = '' unless defined $vers;
    if ($sdeps{$pack}) {
      push @ndeps, "$pack$vers";
      delete $f2{$pack};
    } else {
      $replace = 1;
    }
  }
  if (%f2) {
    push @ndeps, sort keys %f2;
    $replace = 1
  }
  if ($replace) {
    print F "BuildRequires:  ".join(' ', @ndeps)."\n" if @ndeps;
  } else {
    print F "$l\n";
  }
}

if ($changelog) {
  print F "%changelog\n";
  if (open(CF, '<', $changelog)) {
    while(<CF>) {
      print F $_;
    }
    close CF;
  }
}

close(F) || die("close: $!\n");

exit(0);