]>
Commit | Line | Data |
---|---|---|
a2d1644c | 1 | #!/usr/bin/python |
b792d887 MT |
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 | ############################################################################### | |
a2d1644c | 21 | |
1a47c04f MT |
22 | import re |
23 | ||
a2d1644c | 24 | import logging |
8b6bc023 | 25 | log = logging.getLogger("pakfire") |
a2d1644c | 26 | |
c605d735 MT |
27 | import pakfire.packages as packages |
28 | ||
89fac8cf | 29 | from base import RepositoryDummy |
0f8d6745 MT |
30 | from local import RepositoryDir, RepositoryBuild |
31 | from remote import RepositoryRemote | |
32 | from system import RepositorySystem | |
a2d1644c MT |
33 | |
34 | class Repositories(object): | |
35 | """ | |
36 | Class that loads all repositories from the configuration files. | |
37 | ||
38 | This is the place where repositories can be activated or deactivated. | |
39 | """ | |
40 | ||
36b328f2 | 41 | def __init__(self, pakfire): |
a2d1644c MT |
42 | self.pakfire = pakfire |
43 | ||
a2d1644c | 44 | # Place to store the repositories |
c605d735 | 45 | self.__repos = {} |
a2d1644c MT |
46 | |
47 | # Create a dummy repository | |
c605d735 | 48 | self.dummy = RepositoryDummy(self.pakfire) |
a2d1644c | 49 | |
0f8d6745 MT |
50 | # Create the local repository. |
51 | self.local = RepositorySystem(self.pakfire) | |
a2d1644c MT |
52 | self.add_repo(self.local) |
53 | ||
54 | # If we running in build mode, we include our local build repository. | |
36b328f2 | 55 | if self.pakfire.mode == "builder": |
c605d735 | 56 | self.local_build = RepositoryBuild(self.pakfire) |
a2d1644c MT |
57 | self.add_repo(self.local_build) |
58 | ||
0f8d6745 | 59 | # Fetch all repository from the configuration files. |
a2d1644c MT |
60 | for repo_name, repo_args in self.config.get_repos(): |
61 | self._parse(repo_name, repo_args) | |
62 | ||
c605d735 MT |
63 | def __iter__(self): |
64 | repositories = self.__repos.values() | |
65 | repositories.sort() | |
66 | ||
67 | return iter(repositories) | |
ae20b05f | 68 | |
a2d1644c MT |
69 | def __len__(self): |
70 | """ | |
71 | Return the count of enabled repositories. | |
72 | """ | |
c605d735 | 73 | return len([r for r in self if r.enabled]) |
a2d1644c | 74 | |
36b328f2 MT |
75 | @property |
76 | def initialized(self): | |
77 | """ | |
78 | Indicates if all repositories are initialized. | |
79 | """ | |
80 | for repo in self: | |
81 | if not repo.opened: | |
82 | return False | |
83 | ||
84 | return True | |
85 | ||
86 | def initialize(self): | |
87 | # Nothing to do, if everything is already up to date. | |
88 | if self.initialized: | |
89 | return | |
90 | ||
91 | for repo in self: | |
92 | repo.open() | |
93 | ||
94 | def shutdown(self): | |
95 | """ | |
96 | Shuts down all repositores. | |
97 | """ | |
98 | for repo in self: | |
99 | repo.close() | |
100 | ||
101 | @property | |
102 | def config(self): | |
103 | return self.pakfire.config | |
104 | ||
105 | @property | |
106 | def distro(self): | |
107 | return self.pakfire.distro | |
108 | ||
c605d735 MT |
109 | @property |
110 | def pool(self): | |
111 | return self.pakfire.pool | |
a2d1644c MT |
112 | |
113 | def _parse(self, name, args): | |
a2d1644c MT |
114 | _args = { |
115 | "name" : name, | |
116 | "enabled" : True, | |
117 | "gpgkey" : None, | |
a6bd96bc | 118 | "mirrors" : None, |
a2d1644c MT |
119 | } |
120 | _args.update(args) | |
121 | ||
1a47c04f MT |
122 | # Handle variable expansion. |
123 | replaces = { | |
124 | "name" : name, | |
125 | "arch" : self.distro.arch, | |
126 | } | |
127 | ||
128 | for k, v in _args.items(): | |
129 | # Skip all non-strings. | |
130 | if not type(v) == type("a"): | |
131 | continue | |
a2d1644c | 132 | |
1a47c04f MT |
133 | while True: |
134 | m = re.search(packages.lexer.LEXER_VARIABLE, v) | |
135 | ||
136 | # If we cannot find a match, we are done. | |
137 | if not m: | |
138 | _args[k] = v | |
139 | break | |
140 | ||
141 | # Get the name of the variable. | |
142 | (var,) = m.groups() | |
143 | ||
144 | # Replace the variable with its value. | |
145 | v = v.replace("%%{%s}" % var, replaces.get(var, "")) | |
146 | ||
0f8d6745 | 147 | repo = RepositoryRemote(self.pakfire, **_args) |
a2d1644c MT |
148 | self.add_repo(repo) |
149 | ||
150 | def add_repo(self, repo): | |
c605d735 MT |
151 | if self.__repos.has_key(repo.name): |
152 | raise Exception, "Repository with that name does already exist." | |
a2d1644c | 153 | |
c605d735 | 154 | self.__repos[repo.name] = repo |
67bc4528 | 155 | |
c157d1e2 MT |
156 | def rem_repo(self, repo): |
157 | """ | |
158 | Remove the given repository from the global repository set. | |
159 | """ | |
160 | try: | |
161 | del self.__repos[repo.name] | |
162 | except KeyError: | |
8b6bc023 | 163 | log.debug("Repository that was to be removed does not exist: %s" % repo.name) |
c157d1e2 | 164 | |
c605d735 MT |
165 | def get_repo(self, name): |
166 | """ | |
167 | Get the repository with the given name, if not available, return | |
168 | the dummy repository. | |
169 | """ | |
170 | try: | |
171 | return self.__repos[name] | |
172 | except KeyError: | |
173 | return self.dummy | |
a2d1644c | 174 | |
c605d735 | 175 | def enable_repo(self, name): |
013eb9f2 MT |
176 | for repo in self: |
177 | if repo == self.local: | |
178 | continue | |
179 | ||
180 | if repo.name == name or name == "*": | |
181 | repo.enabled = True | |
a2d1644c MT |
182 | |
183 | def disable_repo(self, name): | |
013eb9f2 MT |
184 | for repo in self: |
185 | if repo == self.local: | |
186 | continue | |
187 | ||
188 | if repo.name == name or name == "*": | |
189 | repo.enabled = False | |
a2d1644c | 190 | |
c605d735 | 191 | def whatprovides(self, what): |
c49a002d | 192 | what = self.pakfire.pool.create_relation(what) |
376eb555 | 193 | |
c605d735 MT |
194 | for solv in self.pool.providers(what): |
195 | yield packages.SolvPackage(self.pakfire, solv) | |
31267a64 MT |
196 | |
197 | def clean(self): | |
8b6bc023 | 198 | log.info("Cleaning up all repository caches...") |
31267a64 MT |
199 | |
200 | for repo in self: | |
201 | repo.clean() |