]> git.ipfire.org Git - pakfire.git/blob - src/pakfire/repository/__init__.py
repository: Add compat "whatprovides" method.
[pakfire.git] / src / pakfire / repository / __init__.py
1 #!/usr/bin/python
2 ###############################################################################
3 # #
4 # Pakfire - The IPFire package management system #
5 # Copyright (C) 2011 Pakfire development team #
6 # #
7 # This program is free software: you can redistribute it and/or modify #
8 # it under the terms of the GNU General Public License as published by #
9 # the Free Software Foundation, either version 3 of the License, or #
10 # (at your option) any later version. #
11 # #
12 # This program is distributed in the hope that it will be useful, #
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of #
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
15 # GNU General Public License for more details. #
16 # #
17 # You should have received a copy of the GNU General Public License #
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. #
19 # #
20 ###############################################################################
21
22 import re
23
24 import logging
25 log = logging.getLogger("pakfire")
26
27 import pakfire.packages as packages
28
29 from pakfire.i18n import _
30
31 from base import RepositoryDummy
32 from local import RepositoryDir, RepositoryBuild
33 from remote import RepositoryRemote
34 from system import RepositorySystem
35
36 class Repositories(object):
37 """
38 Class that loads all repositories from the configuration files.
39
40 This is the place where repositories can be activated or deactivated.
41 """
42
43 def __init__(self, pakfire):
44 self.pakfire = pakfire
45
46 # Place to store the repositories
47 self.__repos = {}
48
49 # Create a dummy repository
50 self.dummy = RepositoryDummy(self.pakfire)
51
52 # Create the local repository.
53 self.local = RepositorySystem(self.pakfire)
54 self.add_repo(self.local)
55
56 # If we running in build mode, we include our local build repository.
57 if self.pakfire.mode == "builder":
58 self.local_build = RepositoryBuild(self.pakfire)
59 self.add_repo(self.local_build)
60
61 # Fetch all repository from the configuration files.
62 for repo_name, repo_args in self.config.get_repos():
63 self._parse(repo_name, repo_args)
64
65 def __iter__(self):
66 repositories = self.__repos.values()
67 repositories.sort()
68
69 return iter(repositories)
70
71 def __len__(self):
72 """
73 Return the count of enabled repositories.
74 """
75 return len([r for r in self if r.enabled])
76
77 @property
78 def initialized(self):
79 """
80 Indicates if all repositories are initialized.
81 """
82 for repo in self:
83 if not repo.opened:
84 return False
85
86 return True
87
88 def initialize(self):
89 # Nothing to do, if everything is already up to date.
90 if self.initialized:
91 return
92
93 log.info(_("Initializing repositories..."))
94 for repo in self:
95 repo.open()
96
97 # Empty line.
98 log.info("")
99
100 def shutdown(self):
101 """
102 Shuts down all repositores.
103 """
104 for repo in self:
105 repo.close()
106
107 @property
108 def config(self):
109 return self.pakfire.config
110
111 @property
112 def distro(self):
113 return self.pakfire.distro
114
115 @property
116 def pool(self):
117 return self.pakfire.pool
118
119 def _parse(self, name, args):
120 _args = {
121 "name" : name,
122 "enabled" : True,
123 "gpgkey" : None,
124 "mirrors" : None,
125 }
126 _args.update(args)
127
128 # Handle variable expansion.
129 replaces = {
130 "name" : name,
131 "arch" : self.distro.arch,
132 }
133
134 for k, v in _args.items():
135 # Skip all non-strings.
136 if not type(v) == type("a"):
137 continue
138
139 while True:
140 m = re.search(packages.lexer.LEXER_VARIABLE, v)
141
142 # If we cannot find a match, we are done.
143 if not m:
144 _args[k] = v
145 break
146
147 # Get the name of the variable.
148 (var,) = m.groups()
149
150 # Replace the variable with its value.
151 v = v.replace("%%{%s}" % var, replaces.get(var, ""))
152
153 repo = RepositoryRemote(self.pakfire, **_args)
154 self.add_repo(repo)
155
156 def add_repo(self, repo):
157 if self.__repos.has_key(repo.name):
158 raise Exception, "Repository with that name does already exist: %s" % repo.name
159
160 self.__repos[repo.name] = repo
161
162 def rem_repo(self, repo):
163 """
164 Remove the given repository from the global repository set.
165 """
166 try:
167 del self.__repos[repo.name]
168 except KeyError:
169 log.debug("Repository that was to be removed does not exist: %s" % repo.name)
170
171 def get_repo(self, name):
172 """
173 Get the repository with the given name, if not available, return
174 the dummy repository.
175 """
176 try:
177 return self.__repos[name]
178 except KeyError:
179 return self.dummy
180
181 def enable_repo(self, name):
182 for repo in self:
183 if repo == self.local:
184 continue
185
186 if repo.name == name or name == "*":
187 repo.enabled = True
188
189 def disable_repo(self, name):
190 for repo in self:
191 if repo == self.local:
192 continue
193
194 if repo.name == name or name == "*":
195 repo.enabled = False
196
197 def whatprovides(self, *args, **kwargs):
198 return self.pool.whatprovides(self.pakfire, *args, **kwargs)
199
200 def clean(self):
201 log.info("Cleaning up all repository caches...")
202
203 for repo in self:
204 repo.clean()