summaryrefslogtreecommitdiff
path: root/substitutedeps
blob: 97ba712579e63325d0bfcedcb1be9f40cc31965c (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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
#!/usr/bin/perl -w

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

use strict;

use Build;


sub expand {
  my ($config, $str) = @_;
  my @xspec;
  my %cf = %$config;
  $cf{'save_expanded'} = 1;
  Build::Rpm::parse(\%cf, [ "$str" ], \@xspec);
  return @xspec && ref($xspec[0]) ? $xspec[0]->[1] : '';
}

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

while (@ARGV)  {
  if ($ARGV[0] eq '--root') {
    shift @ARGV;
    $buildroot = shift @ARGV;
    next;
  }
  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 $specdir = $spec;
$specdir =~ s/[^\/]*$//;
$specdir = "./" if $specdir eq '';

my $newspec = $ARGV[1];

my $cf = Build::read_config_dist($dist, $archs, $configdir);
$cf->{'warnings'} = 1;

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

my $xspec = [];
my $d = Build::parse($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;
my $mainpkg = '';
my $pkg;

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;
  }

  if ($l =~ /^Name\s*:\s*(\S+)/i) {
    $pkg = $mainpkg = $1 unless $mainpkg;
  }
  if ($l =~ /^\s*%package\s+(-n\s+)?(\S+)/) {
    if ($1) {
      $pkg = $2;
    } else {
      $pkg = "$mainpkg-$2";
    }
  }

  if ($l =~ /^Release:/i) {
    my $oldl = $l;
    if (defined $release) {
      if (!($l =~ s/<RELEASE\d*>/$release/g)) {
	if ($l =~ /<(?:CI_CNT|B_CNT)>/) {
	  # XXX: should pass ci_cnt/b_cnt instead
	  if ($release =~ /(\d+)\.(\d+)$/) {
	    my ($ci, $b) = ($1, $2);
	    $l =~ s/<CI_CNT>/$ci/;
	    $l =~ s/<B_CNT>/$b/;
	  } elsif ($release =~ /(\d+)$/) {
	    my $b = $1;
	    $l =~ s/<B_CNT>/$b/ unless $l =~ s/<CI_CNT>/$b/;
	  }
	} else {
	  $l =~ s/^(Release:\s*).*/$1$release/i;
	}
      }
    }
    # this is to be compatible to legacy autobuild.
    # you can specify a releaseprg in the project configuration,
    # if your package contains this file it is executed and its
    # output is used as a release.
    # use only if you really must.
    if ($cf->{'releaseprg'} && -f "$specdir$cf->{'releaseprg'}") {
      my $newl = $l;
      $newl =~ s/^Release:\s*//;
      $oldl =~ s/^Release:\s*//;
      my $project = expand($cf, "%?_project") || 'BUILD_BASENAME';
      my $arch = expand($cf, "%?_target_cpu") || 'noarch';
      $::ENV{'BUILD_OLDRELEASE'} = $oldl;
      my @nl;
      my $interpreter = "/bin/bash";
      if (open(RP, '<', "$specdir$cf->{'releaseprg'}")) {
	@nl = <RP>;
        close RP;
	if (@nl && $nl[0] =~ /^#!\s*(\S*)/) {
	  $interpreter = $1;
	}
      }
      if ($buildroot) {
	my $sd = $specdir;
	$sd =~ s/^\Q$buildroot\E//;
        open(RP, "-|", 'chroot', $buildroot, $interpreter, "$sd$cf->{'releaseprg'}", $project, $newl, $pkg, $arch) || die("$cf->{'releaseprg'}: $!\n");
      } else {
        open(RP, "-|", $interpreter, "$specdir$cf->{'releaseprg'}", $project, $newl, $pkg, $arch) || die("$cf->{'releaseprg'}: $!\n");
      }
      @nl = grep {$_ ne ''} <RP>;
      if (!close(RP)) {
	warn("$cf->{'releaseprg'} failed: $?\n");
      }
      # and another compatibility hack: if the prg returns pkg:<package>,
      # the release of the package will be used. yuck...
      if (@nl && $nl[0] =~ s/^pkg://) {
	my $relpkg = $nl[0];
	chomp $relpkg;
	if ($buildroot) {
	  open(RP, "-|", 'chroot', $buildroot, 'rpm', '-q', '--qf', '%{RELEASE}', $relpkg) || die("rpm: $!\n");
	} else {
	  open(RP, "-|", 'rpm', '-q', '--qf', '%{RELEASE}', $relpkg) || die("rpm: $!\n");
	}
        @nl = grep {$_ ne ''} <RP>;
        if (!close(RP)) {
	  warn("rpm package query of '$relpkg' failed: $?\n");
        }
      }
      if ($nl[0]) {
	chomp $nl[0];
	$l =~ s/^(Release:\s*).*/$1$nl[0]/i;
        if (defined $release) {
	  if (!($l =~ s/<RELEASE\d*>/$release/g)) {
	    if ($l =~ /<(?:CI_CNT|B_CNT)>/) {
	      # XXX: should pass ci_cnt/b_cnt instead
	      if ($release =~ /(\d+)\.(\d+)$/) {
		my ($ci, $b) = ($1, $2);
		$l =~ s/<CI_CNT>/$ci/;
		$l =~ s/<B_CNT>/$b/;
	      } elsif ($release =~ /(\d+)$/) {
		my $b = $1;
		$l =~ s/<B_CNT>/$b/ unless $l =~ s/<CI_CNT>/$b/;
	      }
	    }
	  }
	}
      }
    }
    # all compat stuff done. we return to your scheduled program
  }

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

  my $isbuildrequires = 0;
  $isbuildrequires = 1 if $l =~ /^BuildRequires:/i;
  my $r = $l;
  $r =~ s/^[^:]*:\s*//;
  my @deps = $r =~ /([^\s\[,]+)(\s+[<=>]+\s+[^\s\[,]+)?[\s,]*/g;
  my @ndeps = ();
  my $replace = 0;
  my @f2 = Build::do_subst_vers($cf, @deps);
  my %f2 = @f2;
  if ($isbuildrequires) {
    delete $f2{$_} for @neg;
    delete $f2{$_} for grep {/^-/} keys %f2;
  }
  while (@deps) {
    my ($pack, $vers) = splice(@deps, 0, 2);
    $vers = '' unless defined $vers;
    if (($isbuildrequires && $sdeps{$pack}) || exists($f2{$pack})) {
      push @ndeps, "$pack$vers";
      delete $f2{$pack};
    } else {
      $replace = 1;
    }
  }
  if (%f2) {
    while (@f2) {
      my ($pack, $vers) = splice(@f2, 0, 2);
      next unless exists $f2{$pack};
      $vers = '' unless defined $vers;
      push @ndeps, "$pack$vers";
    }
    $replace = 1
  }
  if ($replace) {
    $l =~ /^(.*?:\s*)/;
    print F $1.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);