PackageLocator.pm revision 1.65
1# ex:ts=8 sw=4:
2# $OpenBSD: PackageLocator.pm,v 1.65 2007/05/13 12:34:32 espie Exp $
3#
4# Copyright (c) 2003-2007 Marc Espie <espie@openbsd.org>
5#
6# Permission to use, copy, modify, and distribute this software for any
7# purpose with or without fee is hereby granted, provided that the above
8# copyright notice and this permission notice appear in all copies.
9#
10# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17
18use strict;
19use warnings;
20
21package OpenBSD::PackageLocator;
22
23use OpenBSD::PackageRepositoryList;
24use OpenBSD::PackageRepository;
25
26# this returns an archive handle from an uninstalled package name, currently
27# There is a cache available.
28
29my %packages;
30my $pkgpath = OpenBSD::PackageRepositoryList->new;
31
32if (defined $ENV{PKG_PATH}) {
33	my $v = $ENV{PKG_PATH};
34	$v =~ s/^\:+//;
35	$v =~ s/\:+$//;
36	my @tentative = split /\/\:/, $v;
37	while (my $i = shift @tentative) {
38		$i =~ m|/$| or $i.='/';
39		$pkgpath->add(OpenBSD::PackageRepository->new($i));
40	}
41} else {
42	$pkgpath->add(OpenBSD::PackageRepository->new("./"));
43}
44
45sub path_parse
46{
47	use File::Basename;
48
49	my ($pkgname, $path) = fileparse($_);
50	my $repository = OpenBSD::PackageRepository->new($path);
51	return ($repository, $path, $pkgname);
52}
53
54sub find
55{
56	my $class = shift;
57	local $_ = shift;
58	my $arch = shift;
59	my $srcpath = shift;
60
61	if ($_ eq '-') {
62		my $repository = OpenBSD::PackageRepository::Local::Pipe->_new('./');
63		my $package = $repository->find(undef, $arch, $srcpath);
64		return $package;
65	}
66	if (exists $packages{$_}) {
67		return $packages{$_};
68	}
69	my $package;
70	if (m/\//) {
71		my ($repository, undef, $pkgname) = path_parse($_);
72		$package = $repository->find($pkgname, $arch, $srcpath);
73		if (defined $package) {
74			$pkgpath->add($repository);
75		}
76	} else {
77		$package = $pkgpath->find($_, $arch, $srcpath);
78	}
79	$packages{$_} = $package if defined($package);
80	return $package;
81}
82
83sub available
84{
85	return $pkgpath->available();
86}
87
88sub grabPlist
89{
90	my $class = shift;
91	local $_ = shift;
92	my $arch = shift;
93	my $code = shift;
94
95	if ($_ eq '-') {
96		my $repository = OpenBSD::PackageRepository::Local::Pipe->_new('./');
97		my $plist = $repository->grabPlist(undef, $arch, $code);
98		return $plist;
99	}
100	my $plist;
101	if (m/\//) {
102		my ($repository, undef, $pkgname) = path_parse($_);
103		$plist = $repository->grabPlist($pkgname, $arch, $code);
104		if (defined $plist) {
105			$pkgpath->add($repository);
106		}
107	} else {
108		$plist = $pkgpath->grabPlist($_, $arch, $code);
109	}
110	return $plist;
111}
112
113sub cleanup
114{
115	$pkgpath->cleanup;
116}
117
118sub match_spec
119{
120	my ($class, $spec, $filter) = @_;
121	return $pkgpath->match_spec($spec, $filter);
122}
123
124sub findstem
125{
126	my ($class, $stem, $filter) = @_;
127	return $pkgpath->findstem($stem, $filter);
128}
129
130sub find_partialstem
131{
132	my ($class, $partial, $filter) = @_;
133	return $pkgpath->find_partialstem($partial, $filter);
134}
135
1361;
137