]> git.ipfire.org Git - pakfire.git/blob - python/pakfire/repository/cache.py
Bump version 0.9.9.
[pakfire.git] / python / pakfire / repository / cache.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 os
23 import stat
24 import time
25
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 """
34
35 def __init__(self, pakfire, repo):
36 self.pakfire = pakfire
37 self.repo = repo
38
39 self.__created = None
40
41 @property
42 def created(self):
43 """
44 Tells us, if the cache was already created.
45 """
46 if self.__created is None:
47 self.__created = os.path.exists(self.path)
48
49 return self.__created
50
51 @property
52 def path(self):
53 return os.path.join(REPO_CACHE_DIR, self.pakfire.distro.release, \
54 self.repo.name, self.repo.arch)
55
56 def abspath(self, path, create=True):
57 if create:
58 self.create()
59
60 return os.path.join(self.path, path)
61
62 def create(self):
63 """
64 Create all necessary directories.
65 """
66 # Do nothing, if the cache has already been created.
67 if self.created:
68 return
69
70 for path in ("mirrors", "packages", "repodata"):
71 path = self.abspath(path, create=False)
72
73 if not os.path.exists(path):
74 os.makedirs(path)
75
76 self.__created = True
77
78 def exists(self, filename):
79 """
80 Returns True if a file exists and False if it doesn't.
81 """
82 return os.path.exists(self.abspath(filename))
83
84 def age(self, filename):
85 """
86 Returns the age of a downloaded file in minutes.
87 i.e. the time from download until now.
88 """
89 if not self.exists(filename):
90 return None
91
92 filename = self.abspath(filename)
93
94 # Creation time of the file
95 ctime = os.stat(filename)[stat.ST_CTIME]
96
97 return (time.time() - ctime) / 60
98
99 def open(self, filename, *args, **kwargs):
100 filename = self.abspath(filename)
101
102 return open(filename, *args, **kwargs)
103
104 def verify(self, filename, hash1):
105 """
106 Return a bool that indicates if a file matches the given hash.
107 """
108 return util.calc_hash1(self.abspath(filename)) == hash1
109
110 def remove(self, filename):
111 """
112 Remove a file from cache.
113 """
114 if not self.exists(filename):
115 return
116
117 filename = self.abspath(filename)
118 os.unlink(filename)
119
120 def destroy(self):
121 """
122 Remove all files from this cache.
123 """
124 if self.created:
125 util.rm(self.path)