]> git.ipfire.org Git - pakfire.git/blob - pakfire/repository/__init__.py
Bump version 0.9.9.
[pakfire.git] / 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 logging
23
24 import pakfire.packages as packages
25
26 from base import RepositoryDummy
27 from local import RepositoryDir, RepositoryBuild, RepositoryLocal
28 from remote import RepositorySolv
29
30 class Repositories(object):
31 """
32 Class that loads all repositories from the configuration files.
33
34 This is the place where repositories can be activated or deactivated.
35 """
36
37 def __init__(self, pakfire, enable_repos=None, disable_repos=None):
38 self.pakfire = pakfire
39
40 self.config = pakfire.config
41 self.distro = pakfire.distro
42
43 # Place to store the repositories
44 self.__repos = {}
45
46 # Create a dummy repository
47 self.dummy = RepositoryDummy(self.pakfire)
48
49 # Create the local repository
50 self.local = RepositoryLocal(self.pakfire)
51 self.add_repo(self.local)
52
53 # If we running in build mode, we include our local build repository.
54 if self.pakfire.builder:
55 self.local_build = RepositoryBuild(self.pakfire)
56 self.add_repo(self.local_build)
57
58 for repo_name, repo_args in self.config.get_repos():
59 self._parse(repo_name, repo_args)
60
61 # Enable all repositories here as demanded on commandline
62 if enable_repos:
63 for repo in enable_repos:
64 self.enable_repo(repo)
65
66 # Disable all repositories here as demanded on commandline
67 if disable_repos:
68 for repo in disable_repos:
69 self.disable_repo(repo)
70
71 # Update all indexes of the repositories (not force) so that we will
72 # always work with valid data.
73 self.update(offline=self.pakfire.offline)
74
75 def __iter__(self):
76 repositories = self.__repos.values()
77 repositories.sort()
78
79 return iter(repositories)
80
81 def __len__(self):
82 """
83 Return the count of enabled repositories.
84 """
85 return len([r for r in self if r.enabled])
86
87 @property
88 def pool(self):
89 return self.pakfire.pool
90
91 def _parse(self, name, args):
92 # XXX need to make variable expansion
93
94 _args = {
95 "name" : name,
96 "enabled" : True,
97 "gpgkey" : None,
98 "mirrorlist" : None,
99 }
100 _args.update(args)
101
102 repo = RepositorySolv(self.pakfire, **_args)
103
104 self.add_repo(repo)
105
106 def add_repo(self, repo):
107 if self.__repos.has_key(repo.name):
108 raise Exception, "Repository with that name does already exist."
109
110 self.__repos[repo.name] = repo
111
112 def rem_repo(self, repo):
113 """
114 Remove the given repository from the global repository set.
115 """
116 try:
117 del self.__repos[repo.name]
118 except KeyError:
119 logging.debug("Repository that was to be removed does not exist: %s" % repo.name)
120
121 def get_repo(self, name):
122 """
123 Get the repository with the given name, if not available, return
124 the dummy repository.
125 """
126 try:
127 return self.__repos[name]
128 except KeyError:
129 return self.dummy
130
131 def enable_repo(self, name):
132 try:
133 self.__repos[name].enabled = True
134 except KeyError:
135 pass
136
137 def disable_repo(self, name):
138 try:
139 self.__repos[name].enabled = False
140 except KeyError:
141 pass
142
143 def update(self, force=False, offline=False):
144 logging.debug("Updating all repository indexes (force=%s)" % force)
145
146 # update all indexes if necessary or forced
147 for repo in self:
148 repo.update(force=force, offline=offline)
149
150 def whatprovides(self, what):
151 what = self.pakfire.create_relation(what)
152
153 for solv in self.pool.providers(what):
154 yield packages.SolvPackage(self.pakfire, solv)
155
156 def clean(self):
157 logging.info("Cleaning up all repository caches...")
158
159 for repo in self:
160 repo.clean()