]>
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 MT |
21 | |
22 | import logging | |
23 | import os | |
24 | ||
c605d735 | 25 | import base |
a2d1644c MT |
26 | import index |
27 | ||
8656150e MT |
28 | import pakfire.downloader as downloader |
29 | ||
c605d735 | 30 | from pakfire.constants import * |
6a509182 | 31 | from pakfire.i18n import _ |
a2d1644c | 32 | |
c605d735 MT |
33 | class RepositorySolv(base.RepositoryFactory): |
34 | def __init__(self, pakfire, name, description, url, mirrorlist, gpgkey, enabled=True): | |
a2d1644c MT |
35 | # Parse arguments. |
36 | self.url = url | |
37 | self.gpgkey = gpgkey | |
38 | self.mirrorlist = mirrorlist | |
39 | ||
a7cd7a38 MT |
40 | base.RepositoryFactory.__init__(self, pakfire, name, description) |
41 | ||
a2d1644c MT |
42 | # Initialize mirror servers. |
43 | self.mirrors = downloader.MirrorList(self.pakfire, self) | |
44 | ||
c605d735 MT |
45 | # Create index, which is always SOLV. |
46 | self.index = index.IndexSolv(self.pakfire, self) | |
a2d1644c | 47 | |
c605d735 MT |
48 | # Save enabled/disabled flag at the end. |
49 | if enabled in ("1", "yes", "on", True, 1): | |
50 | self.enabled = True | |
51 | else: | |
52 | self.enabled = False | |
a2d1644c MT |
53 | |
54 | @property | |
55 | def priority(self): | |
56 | priority = 100 | |
57 | ||
58 | url2priority = { | |
59 | "file://" : 50, | |
60 | "http://" : 75, | |
61 | } | |
62 | ||
63 | for url, prio in url2priority.items(): | |
64 | if self.url.startswith(url): | |
65 | priority = prio | |
66 | break | |
67 | ||
68 | return priority | |
69 | ||
811d22ad | 70 | def download(self, pkg, text=""): |
c605d735 MT |
71 | """ |
72 | Downloads 'filename' from repository and returns the local filename. | |
73 | """ | |
811d22ad MT |
74 | filename, hash1 = pkg.filename, pkg.hash1 |
75 | ||
c605d735 MT |
76 | # Marker, if we need to download the package. |
77 | download = True | |
78 | ||
79 | cache_prefix = "" | |
80 | if filename.endswith(PACKAGE_EXTENSION): | |
81 | cache_prefix = "packages" | |
82 | elif filename == METADATA_DOWNLOAD_FILE: | |
83 | cache_prefix = "repodata" | |
84 | elif filename.endswith(METADATA_DATABASE_FILE): | |
85 | cache_prefix = "repodata" | |
86 | ||
87 | cache_filename = os.path.join(cache_prefix, os.path.basename(filename)) | |
88 | ||
89 | # Check if file already exists in cache. | |
90 | if self.cache.exists(cache_filename): | |
91 | logging.debug("File exists in cache: %s" % filename) | |
92 | ||
93 | # If the file does already exist, we check if the hash1 matches. | |
94 | if hash1 and self.cache.verify(cache_filename, hash1): | |
95 | # We already got the right file. Skip download. | |
96 | download = False | |
97 | else: | |
98 | # The file in cache has a wrong hash. Remove it and repeat download. | |
99 | cache.remove(cache_filename) | |
100 | ||
101 | if download: | |
102 | logging.debug("Going to download %s" % filename) | |
103 | ||
6a509182 MT |
104 | # If we are in offline mode, we cannot download any files. |
105 | if self.pakfire.offline and not self.url.startswith("file://"): | |
106 | raise OfflineModeError, _("Cannot download this file in offline mode: %s") \ | |
107 | % filename | |
108 | ||
c605d735 MT |
109 | # Make sure filename is of type string (and not unicode) |
110 | filename = str(filename) | |
111 | ||
112 | # Get a package grabber and add mirror download capabilities to it. | |
113 | grabber = downloader.PackageDownloader( | |
cfc16a71 | 114 | self.pakfire, |
c605d735 MT |
115 | text=text + os.path.basename(filename), |
116 | ) | |
117 | grabber = self.mirrors.group(grabber) | |
118 | ||
119 | i = grabber.urlopen(filename) | |
120 | ||
121 | # Open input and output files and download the file. | |
122 | o = self.cache.open(cache_filename, "w") | |
123 | ||
124 | buf = i.read(BUFFER_SIZE) | |
125 | while buf: | |
126 | o.write(buf) | |
127 | buf = i.read(BUFFER_SIZE) | |
128 | ||
129 | i.close() | |
130 | o.close() | |
131 | ||
132 | # Verify if the download was okay. | |
133 | if hash1 and not self.cache.verify(cache_filename, hash1): | |
134 | raise Exception, "XXX this should never happen..." | |
135 | ||
136 | return os.path.join(self.cache.path, cache_filename) |