]>
git.ipfire.org Git - pakfire.git/blob - pakfire/repository/cache.py
3d6665d3feba339f8e4a96b8e0e7bb9207f9972c
2 ###############################################################################
4 # Pakfire - The IPFire package management system #
5 # Copyright (C) 2011 Pakfire development team #
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. #
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. #
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/>. #
20 ###############################################################################
26 import pakfire
.util
as util
27 from pakfire
.constants
import *
29 class RepositoryCache(object):
31 An object that is able to cache all data that is loaded from a
35 def __init__(self
, pakfire
, repo
):
36 self
.pakfire
= pakfire
44 Tells us, if the cache was already created.
46 if self
.__created
is None:
47 self
.__created
= os
.path
.exists(self
.path
)
53 return os
.path
.join(REPO_CACHE_DIR
, self
.pakfire
.distro
.release
, \
54 self
.repo
.name
, self
.repo
.arch
)
56 def abspath(self
, path
, create
=True):
60 return os
.path
.join(self
.path
, path
)
64 Create all necessary directories.
66 # Do nothing, if the cache has already been created.
70 for path
in ("mirrors", "packages", "repodata"):
71 path
= self
.abspath(path
, create
=False)
73 if not os
.path
.exists(path
):
78 def exists(self
, filename
):
80 Returns True if a file exists and False if it doesn't.
82 return os
.path
.exists(self
.abspath(filename
))
84 def age(self
, filename
):
86 Returns the age of a downloaded file in minutes.
87 i.e. the time from download until now.
89 if not self
.exists(filename
):
92 filename
= self
.abspath(filename
)
94 # Creation time of the file
95 ctime
= os
.stat(filename
)[stat
.ST_CTIME
]
97 return (time
.time() - ctime
) / 60
99 def open(self
, filename
, *args
, **kwargs
):
100 filename
= self
.abspath(filename
)
102 return open(filename
, *args
, **kwargs
)
104 def verify(self
, filename
, hash1
):
106 Return a bool that indicates if a file matches the given hash.
108 return util
.calc_hash1(self
.abspath(filename
)) == hash1
110 def remove(self
, filename
):
112 Remove a file from cache.
114 if not self
.exists(filename
):
117 filename
= self
.abspath(filename
)
122 Remove all files from this cache.