fc_resolve_deps.pl 6.6 KB
Newer Older
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 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
#! /usr/bin/perl

use XML::Parser;
use Data::Dumper;
use IO::Zlib;

use strict;

my %packages;
my %provides;
my %files;

my $current_data;
my $current_arch;
my $current_location;
my $current_package;

my $in_provides = 0;
my $in_requires = 0;

sub usage
{
	my $progname = (split(/\//, $0))[-1];

	print STDERR "Usage: $progname media_path package1 [package2 ...]\n";
}

sub is_same_version
{
	my ($a, $b) = @_;
	my $rc = 0;

	if (($$a{'version'} eq $$b{'version'}) &&
	    ($$a{'release'} eq $$b{'release'}) &&
	    ($$a{'epoch'} eq $$b{'epoch'})) {
		$rc = 1;
    	}

	return $rc;
}

sub merge_package_entries
{
	my ($a, $b) = @_;
	for (keys %{$$b{'location'}}) {
		$$a{'location'}{$_} =
		       $$b{'location'}{$_};
	}
}

sub hash_provides
{
	my ($package) = @_;

	# add to hash of 'who provides what'
	for my $symbol (keys %{$$package{'provides'}}) {
		my $ptr = $provides{$symbol};
		if (ref $ptr eq 'ARRAY') {
			push @$ptr, $package;
		}
		elsif (defined $ptr) {
			$provides{$symbol} = [ $ptr, $package ];
		}
		else {
			$provides{$symbol} = $package;
		}
	}
}

sub hash_files
{
	my ($package) = @_;

	# add to hash of 'who provides this file'
	for my $file (@{$$package{'files'}}) {
		my $ptr = $files{$file};
		if (ref $ptr eq 'ARRAY') {
			push @$ptr, $package;
		}
		elsif (defined $ptr) {
			$files{$file} = [ $ptr, $package ];
		}
		else {
			$files{$file} = $package;
		}
	}

}

sub start_tag
{
	my ($parser, $element, %attrs) = @_;

	if ($element eq 'package') {
		$current_package = {};
	}
	elsif ($element eq 'version') {
		$$current_package{'version'} = $attrs{'ver'};
		$$current_package{'release'} = $attrs{'rel'};
		$$current_package{'epoch'}   = $attrs{'epoch'};
	}
	elsif ($element eq 'location') {
		$current_location   = $attrs{'href'};
	}
	elsif ($element eq 'rpm:provides') { $in_provides = 1; }
	elsif ($element eq 'rpm:requires') { $in_requires = 1; }
	elsif ($element eq 'rpm:entry') {
		my $name = $attrs{'name'};

		# rpmlib dependencies only pertain to the version of rpm used
		# to install the package.  We don't care about these, so ignore
		# them.
		return if ($name =~ /^rpmlib\(/);

		my $entry = { 'name' => $name };

		$$entry{'flags'} = $attrs{'flags'} if $attrs{'flags'};
		$$entry{'version'} = $attrs{'ver'} if $attrs{'ver'};
		$$entry{'release'} = $attrs{'rel'} if $attrs{'rel'};
		$$entry{'epoch'} = $attrs{'epoch'} if $attrs{'epoch'};
		#$$entry{'pre'} = $attrs{'pre'} if $attrs{'pre'};

		if ($in_provides) {
			my $provides = $$current_package{'provides'};
			if (not defined $provides) {
				$$current_package{'provides'} = {};
			}
			$$current_package{'provides'}{$name} = $entry;
		}
		elsif ($in_requires) {
			my $requires = $$current_package{'requires'};
			if (not defined $requires) {
				$$current_package{'requires'} = {};
			}
			$$current_package{'requires'}{$name} = $entry;
		}
		else {
			#print STDERR "WARNING: entry tag not in provides or requires tag\n";
		}
	}
}

sub end_tag
{
	my ($parser, $element) = @_;

	$current_data =~ s/^\s*(\S.*\S)\s*$/$1/;

	if ($element eq 'package') {
		my $name = $$current_package{'name'};
		my $ptr = $packages{$name};
		my $is_duplicate = 0;

		$$current_package{'location'} = { $current_arch =>
		                                  $current_location };
		if (ref $ptr eq 'HASH') {
			if (is_same_version($ptr, $current_package)) {
				merge_package_entries($ptr, $current_package);
				$is_duplicate = 1;
			}
			else {
				$packages{$name} = [ $ptr,
				                     $current_package ];
			}
		}
		elsif (ref $ptr eq 'ARRAY') {
			for (@$ptr) {
				if (is_same_version($ptr, $current_package)) {
					merge_package_entries($ptr,
					                      $current_package);
					$is_duplicate = 1;
				}
			}
			push @$ptr, $current_package if (!$is_duplicate)

		}
		else {
			$packages{$name} = $current_package;
		}

		if (!$is_duplicate) {
			hash_provides($current_package);
			hash_files($current_package);
		}

		$current_package = undef;
		$current_location = undef;
		$current_arch = undef;
	}
	elsif ($element eq 'name') {
		$$current_package{'name'} = $current_data;
	}
	elsif ($element eq 'arch') {
		$current_arch = $current_data;
	}
	elsif ($element eq 'rpm:provides') { $in_provides = 0; }
	elsif ($element eq 'rpm:requires') { $in_requires = 0; }
	elsif ($element eq 'file') {
		my $file_list = $$current_package{'files'};
		if (not defined $file_list) {
			$$current_package{'files'} = [];
			$file_list = $$current_package{'files'};
		}
		push @$file_list, $current_data;
	}

	$current_data = undef;
}

sub tag_data
{
	my ($parser, $data) = @_;

	$current_data .= $data;
}

my $base_dir = $ARGV[0];

if (! $base_dir) {
	usage();
	exit 1;
}

my $parser = new XML::Parser(Handlers => {Start => \&start_tag,
                                          End => \&end_tag,
                                          Char => \&tag_data});

my $handle = new IO::Zlib;
$handle->open("$base_dir/repodata/primary.xml.gz", "rb") || 
	die "Couldn't open primay.xml.gz: $!\n";
$parser->parse($handle);

$handle->close();

my %pkgdeps;
my @pkgqueue = @ARGV;
while (@pkgqueue) {
	my $package = shift @pkgqueue;

	next if (not exists $packages{$package});
	next if (exists $pkgdeps{$package});

	$pkgdeps{$package} = 1;

	for my $req (keys %{$packages{$package}{'requires'}}) {
		my $req_info = $packages{$package}{'required'}{$req};
		my $provider;
		if ($req =~ /^\//) {
			if (exists $files{$req}) {
				$provider = $files{$req};
			}
		}
		elsif (exists $packages{$req}) {
			$provider = $packages{$req};
		}
		elsif (exists $provides{$req}) {
			$provider = $provides{$req};
		}
		else {
			print STDERR "WARNING: could not resolve dependency \"$req\" for package $package\n";
			next;
		}

		# XXX HACK If multiple packages (and package versions) work,
		# assume the first one is what we want. No package or symbol
		# version checking is done.  This is very, very wrong, but
		# right now I'm too lazy to do it correctly. Also, no conflict
		# checking is done currently.  Also bad, but I don't want to
		# deal with it right now.  It really shouldn't be an issue
		# anyway, as this is only meant to bootstrap a system in lieu
		# of the Fedora installer.  None of the core packages have
		# conflicts or relevant version dependencies.
		if (ref $provider eq 'ARRAY') {
			print STDERR "WARNING: multiple candidates can satisfy \"$req\" for package $package\n";
			$provider = @$provider[0];
		}

		push @pkgqueue, $$provider{'name'} if ($provider);
	}
}

#print "$_\n" for (sort keys %pkgdeps);
for my $package (sort keys %pkgdeps) {
	my $locations = $packages{$package}{'location'};
	for my $arch qw/i686 i586 i386 noarch/ {
		if (exists $$locations{$arch}) {
			print $base_dir . "/". $$locations{$arch} . "\n";
			last;
		}
	}
}