]>
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 os | |
23 | import stat | |
24 | import time | |
25 | ||
a2d1644c MT |
26 | import pakfire.util as util |
27 | from pakfire.constants import * | |
28 | ||
29 | class RepositoryCache(object): | |
30 | """ | |
31 | An object that is able to cache all data that is loaded from a | |
32 | remote repository. | |
33 | """ | |
5a99898b | 34 | path = REPO_CACHE_DIR |
a2d1644c MT |
35 | |
36 | def __init__(self, pakfire, repo): | |
37 | self.pakfire = pakfire | |
38 | self.repo = repo | |
39 | ||
c605d735 | 40 | self.__created = None |
a2d1644c MT |
41 | |
42 | @property | |
c605d735 MT |
43 | def created(self): |
44 | """ | |
45 | Tells us, if the cache was already created. | |
46 | """ | |
47 | if self.__created is None: | |
48 | self.__created = os.path.exists(self.path) | |
49 | ||
50 | return self.__created | |
a2d1644c | 51 | |
c605d735 MT |
52 | def abspath(self, path, create=True): |
53 | if create: | |
54 | self.create() | |
55 | ||
a2d1644c MT |
56 | return os.path.join(self.path, path) |
57 | ||
58 | def create(self): | |
59 | """ | |
60 | Create all necessary directories. | |
61 | """ | |
c605d735 MT |
62 | # Do nothing, if the cache has already been created. |
63 | if self.created: | |
64 | return | |
65 | ||
5a99898b MT |
66 | if not os.path.exists(self.path): |
67 | os.makedirs(self.path) | |
a2d1644c | 68 | |
c605d735 MT |
69 | self.__created = True |
70 | ||
a2d1644c MT |
71 | def exists(self, filename): |
72 | """ | |
73 | Returns True if a file exists and False if it doesn't. | |
74 | """ | |
75 | return os.path.exists(self.abspath(filename)) | |
76 | ||
77 | def age(self, filename): | |
78 | """ | |
79 | Returns the age of a downloaded file in minutes. | |
80 | i.e. the time from download until now. | |
81 | """ | |
82 | if not self.exists(filename): | |
83 | return None | |
84 | ||
85 | filename = self.abspath(filename) | |
86 | ||
87 | # Creation time of the file | |
88 | ctime = os.stat(filename)[stat.ST_CTIME] | |
89 | ||
90 | return (time.time() - ctime) / 60 | |
91 | ||
92 | def open(self, filename, *args, **kwargs): | |
93 | filename = self.abspath(filename) | |
94 | ||
5a99898b MT |
95 | # Create directory if not existant. |
96 | dirname = os.path.dirname(filename) | |
97 | if not os.path.exists(dirname): | |
98 | os.makedirs(dirname) | |
99 | ||
a2d1644c MT |
100 | return open(filename, *args, **kwargs) |
101 | ||
023fece0 MT |
102 | def hash1(self, filename): |
103 | """ | |
104 | Return hash of the file in the cache. | |
105 | """ | |
106 | return util.calc_hash1(self.abspath(filename)) | |
107 | ||
a2d1644c MT |
108 | def verify(self, filename, hash1): |
109 | """ | |
110 | Return a bool that indicates if a file matches the given hash. | |
111 | """ | |
023fece0 | 112 | return self.hash1(filename) == hash1 |
a2d1644c MT |
113 | |
114 | def remove(self, filename): | |
115 | """ | |
116 | Remove a file from cache. | |
117 | """ | |
118 | if not self.exists(filename): | |
119 | return | |
120 | ||
121 | filename = self.abspath(filename) | |
122 | os.unlink(filename) | |
123 | ||
31267a64 MT |
124 | def destroy(self): |
125 | """ | |
126 | Remove all files from this cache. | |
127 | """ | |
128 | if self.created: | |
129 | util.rm(self.path) |