]> git.ipfire.org Git - pakfire.git/blob - src/pakfire/repository/__init__.py
libpakfire: Drop Pool
[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 glob
23 import os
24 import re
25
26 import logging
27 log = logging.getLogger("pakfire")
28
29 from .. import config
30 from .. import packages
31
32 from .base import RepositoryDummy
33 from .local import RepositoryDir, RepositoryBuild
34 from .remote import RepositoryRemote
35 from .system import RepositorySystem
36
37 from ..i18n import _
38
39 class Repositories(object):
40 """
41 Class that loads all repositories from the configuration files.
42
43 This is the place where repositories can be activated or deactivated.
44 """
45
46 def __init__(self, pakfire):
47 self.pakfire = pakfire
48
49 # Place to store the repositories
50 self.__repos = {}
51
52 # Create a dummy repository
53 from . import base
54 self.dummy = base.RepositoryDummy(self.pakfire)
55
56 # Create the local repository.
57 self.local = self.pakfire.installed_repo = RepositorySystem(self.pakfire)
58 self.add_repo(self.local)
59
60 # If we running in build mode, we include our local build repository.
61 if self.pakfire.mode == "builder":
62 self.local_build = RepositoryBuild(self.pakfire)
63 self.add_repo(self.local_build)
64
65 self._load_from_configuration(self.pakfire.config)
66
67 def __iter__(self):
68 repositories = list(self.__repos.values())
69 repositories.sort()
70
71 return iter(repositories)
72
73 def __len__(self):
74 """
75 Return the count of enabled repositories.
76 """
77 return len([r for r in self if r.enabled])
78
79 def refresh(self):
80 """
81 Refreshes all repositories
82 """
83 for repo in self:
84 if repo.enabled:
85 repo.refresh()
86
87 @property
88 def distro(self):
89 return self.pakfire.distro
90
91 def load_configuration(self, *paths):
92 c = config.Config()
93
94 for path in paths:
95 # Read directories
96 if os.path.isdir(path):
97 for file in glob.glob("%s/*.repo" % path):
98 c.read(file)
99
100 # Read files
101 else:
102 c.read(path)
103
104 self._load_from_configuration(c)
105
106 def _load_from_configuration(self, config):
107 # Add all repositories that have been found
108 for name, settings in config.get_repos():
109 self._parse(name, settings)
110
111 def _parse(self, name, args):
112 _args = {
113 "name" : name,
114 "enabled" : True,
115 "gpgkey" : None,
116 "mirrors" : None,
117 }
118 _args.update(args)
119
120 # Handle variable expansion.
121 replaces = {
122 "name" : name,
123 "arch" : self.pakfire.arch,
124 }
125
126 for k, v in list(_args.items()):
127 # Skip all non-strings.
128 if not type(v) == type("a"):
129 continue
130
131 while True:
132 m = re.search(packages.lexer.LEXER_VARIABLE, v)
133
134 # If we cannot find a match, we are done.
135 if not m:
136 _args[k] = v
137 break
138
139 # Get the name of the variable.
140 (var,) = m.groups()
141
142 # Replace the variable with its value.
143 v = v.replace("%%{%s}" % var, replaces.get(var, ""))
144
145 repo = RepositoryRemote(self.pakfire, **_args)
146 self.add_repo(repo)
147
148 def add_repo(self, repo):
149 if repo.name in self.__repos:
150 raise Exception("Repository with that name does already exist: %s" % repo.name)
151
152 self.__repos[repo.name] = repo
153
154 def rem_repo(self, repo):
155 """
156 Remove the given repository from the global repository set.
157 """
158 try:
159 del self.__repos[repo.name]
160 except KeyError:
161 log.debug("Repository that was to be removed does not exist: %s" % repo.name)
162
163 def get_repo(self, name):
164 """
165 Get the repository with the given name, if not available, return
166 the dummy repository.
167 """
168 try:
169 return self.__repos[name]
170 except KeyError:
171 return self.dummy
172
173 def enable_repo(self, name):
174 for repo in self:
175 if repo == self.local:
176 continue
177
178 if repo.name == name or name == "*":
179 repo.enabled = True
180
181 def disable_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 = False
188
189 def whatprovides(self, *args, **kwargs):
190 return self.pakfire.whatprovides(*args, **kwargs)
191
192 def clean(self):
193 log.info("Cleaning up all repository caches...")
194
195 for repo in self:
196 repo.clean()