Added Implementation.is_available and Selection.is_available
[zeroinstall.git] / zeroinstall / zerostore / __init__.py
blobf93193933d235ab75a6ab0507ef2e9328958b84e
1 """
2 Code for managing the implementation cache.
3 """
5 # Copyright (C) 2009, Thomas Leonard
6 # See the README file for details, or visit http://0install.net.
8 from zeroinstall import _
9 import os
10 from logging import debug, info, warn
12 from zeroinstall.support import basedir
13 from zeroinstall import SafeException, support
15 class BadDigest(SafeException):
16 """Thrown if a digest is invalid (either syntactically or cryptographically)."""
17 detail = None
19 class NotStored(SafeException):
20 """Throws if a requested implementation isn't in the cache."""
22 class NonwritableStore(SafeException):
23 """Attempt to add to a non-writable store directory."""
25 def _copytree2(src, dst):
26 import shutil
27 names = os.listdir(src)
28 assert os.path.isdir(dst)
29 for name in names:
30 srcname = os.path.join(src, name)
31 dstname = os.path.join(dst, name)
32 if os.path.islink(srcname):
33 linkto = os.readlink(srcname)
34 os.symlink(linkto, dstname)
35 elif os.path.isdir(srcname):
36 os.mkdir(dstname)
37 mtime = int(os.lstat(srcname).st_mtime)
38 _copytree2(srcname, dstname)
39 os.utime(dstname, (mtime, mtime))
40 else:
41 shutil.copy2(srcname, dstname)
43 class Store:
44 """A directory for storing implementations."""
46 def __init__(self, dir, public = False):
47 """Create a new Store.
48 @param dir: directory to contain the implementations
49 @type dir: str
50 @param public: deprecated
51 @type public: bool"""
52 self.dir = dir
54 def __str__(self):
55 return _("Store '%s'") % self.dir
57 def lookup(self, digest):
58 try:
59 alg, value = digest.split('=', 1)
60 except ValueError:
61 raise BadDigest(_("Digest must be in the form ALG=VALUE, not '%s'") % digest)
62 try:
63 assert '/' not in value
64 int(value, 16) # Check valid format
65 except ValueError, ex:
66 raise BadDigest(_("Bad value for digest: %s") % str(ex))
67 dir = os.path.join(self.dir, digest)
68 if os.path.isdir(dir):
69 return dir
70 return None
72 def get_tmp_dir_for(self, required_digest):
73 """Create a temporary directory in the directory where we would store an implementation
74 with the given digest. This is used to setup a new implementation before being renamed if
75 it turns out OK.
76 @raise NonwritableStore: if we can't create it"""
77 try:
78 if not os.path.isdir(self.dir):
79 os.makedirs(self.dir)
80 from tempfile import mkdtemp
81 tmp = mkdtemp(dir = self.dir, prefix = 'tmp-')
82 os.chmod(tmp, 0755) # r-x for all; needed by 0store-helper
83 return tmp
84 except OSError, ex:
85 raise NonwritableStore(str(ex))
87 def add_archive_to_cache(self, required_digest, data, url, extract = None, type = None, start_offset = 0, try_helper = False):
88 import unpack
89 info(_("Caching new implementation (digest %s) in %s"), required_digest, self.dir)
91 if self.lookup(required_digest):
92 info(_("Not adding %s as it already exists!"), required_digest)
93 return
95 tmp = self.get_tmp_dir_for(required_digest)
96 try:
97 unpack.unpack_archive(url, data, tmp, extract, type = type, start_offset = start_offset)
98 except:
99 import shutil
100 shutil.rmtree(tmp)
101 raise
103 try:
104 self.check_manifest_and_rename(required_digest, tmp, extract, try_helper = try_helper)
105 except Exception:
106 #warn(_("Leaving extracted directory as %s"), tmp)
107 support.ro_rmtree(tmp)
108 raise
110 def add_dir_to_cache(self, required_digest, path, try_helper = False):
111 """Copy the contents of path to the cache.
112 @param required_digest: the expected digest
113 @type required_digest: str
114 @param path: the root of the tree to copy
115 @type path: str
116 @param try_helper: attempt to use privileged helper before user cache (since 0.26)
117 @type try_helper: bool
118 @raise BadDigest: if the contents don't match the given digest."""
119 if self.lookup(required_digest):
120 info(_("Not adding %s as it already exists!"), required_digest)
121 return
123 tmp = self.get_tmp_dir_for(required_digest)
124 try:
125 _copytree2(path, tmp)
126 self.check_manifest_and_rename(required_digest, tmp, try_helper = try_helper)
127 except:
128 warn(_("Error importing directory."))
129 warn(_("Deleting %s"), tmp)
130 support.ro_rmtree(tmp)
131 raise
133 def _add_with_helper(self, required_digest, path):
134 """Use 0store-secure-add to copy 'path' to the system store.
135 @param required_digest: the digest for path
136 @type required_digest: str
137 @param path: root of implementation directory structure
138 @type path: str
139 @return: True iff the directory was copied into the system cache successfully
141 if required_digest.startswith('sha1='):
142 return False # Old digest alg not supported
143 helper = support.find_in_path('0store-secure-add-helper')
144 if not helper:
145 info(_("'0store-secure-add-helper' command not found. Not adding to system cache."))
146 return False
147 import subprocess
148 env = os.environ.copy()
149 env['ENV_NOT_CLEARED'] = 'Unclean' # (warn about insecure configurations)
150 env['HOME'] = 'Unclean' # (warn about insecure configurations)
151 dev_null = os.open('/dev/null', os.O_RDONLY)
152 try:
153 info(_("Trying to add to system cache using %s"), helper)
154 child = subprocess.Popen([helper, required_digest],
155 stdin = dev_null,
156 cwd = path,
157 env = env)
158 exit_code = child.wait()
159 finally:
160 os.close(dev_null)
162 if exit_code:
163 warn(_("0store-secure-add-helper failed."))
164 return False
166 info(_("Added succcessfully."))
167 return True
169 def check_manifest_and_rename(self, required_digest, tmp, extract = None, try_helper = False):
170 """Check that tmp[/extract] has the required_digest.
171 On success, rename the checked directory to the digest, and
172 make the whole tree read-only.
173 @param try_helper: attempt to use privileged helper to import to system cache first (since 0.26)
174 @type try_helper: bool
175 @raise BadDigest: if the input directory doesn't match the given digest"""
176 if extract:
177 extracted = os.path.join(tmp, extract)
178 if not os.path.isdir(extracted):
179 raise Exception(_('Directory %s not found in archive') % extract)
180 else:
181 extracted = tmp
183 import manifest
185 manifest.fixup_permissions(extracted)
187 alg, required_value = manifest.splitID(required_digest)
188 actual_digest = alg.getID(manifest.add_manifest_file(extracted, alg))
189 if actual_digest != required_digest:
190 raise BadDigest(_('Incorrect manifest -- archive is corrupted.\n'
191 'Required digest: %(required_digest)s\n'
192 'Actual digest: %(actual_digest)s\n') %
193 {'required_digest': required_digest, 'actual_digest': actual_digest})
195 if try_helper:
196 if self._add_with_helper(required_digest, extracted):
197 support.ro_rmtree(tmp)
198 return
199 info(_("Can't add to system store. Trying user store instead."))
201 final_name = os.path.join(self.dir, required_digest)
202 if os.path.isdir(final_name):
203 raise Exception(_("Item %s already stored.") % final_name) # XXX: not really an error
205 # If we just want a subdirectory then the rename will change
206 # extracted/.. and so we'll need write permission on 'extracted'
208 os.chmod(extracted, 0755)
209 os.rename(extracted, final_name)
210 os.chmod(final_name, 0555)
212 if extract:
213 os.rmdir(tmp)
215 def __repr__(self):
216 return "<store: %s>" % self.dir
218 class Stores(object):
219 """A list of L{Store}s. All stores are searched when looking for an implementation.
220 When storing, we use the first of the system caches (if writable), or the user's
221 cache otherwise."""
222 __slots__ = ['stores']
224 def __init__(self):
225 user_store = os.path.join(basedir.xdg_cache_home, '0install.net', 'implementations')
226 self.stores = [Store(user_store)]
228 impl_dirs = basedir.load_first_config('0install.net', 'injector',
229 'implementation-dirs')
230 debug(_("Location of 'implementation-dirs' config file being used: '%s'"), impl_dirs)
231 if impl_dirs:
232 dirs = file(impl_dirs)
233 else:
234 if os.name == "nt":
235 from win32com.shell import shell, shellcon
236 localAppData = shell.SHGetFolderPath(0, shellcon.CSIDL_LOCAL_APPDATA, 0, 0)
237 commonAppData = shell.SHGetFolderPath(0, shellcon.CSIDL_COMMON_APPDATA, 0, 0)
239 userCache = os.path.join(localAppData, "0install.net", "implementations")
240 sharedCache = os.path.join(commonAppData, "0install.net", "implementations")
241 dirs = [userCache, sharedCache]
243 else:
244 dirs = ['/var/cache/0install.net/implementations']
246 for directory in dirs:
247 directory = directory.strip()
248 if directory and not directory.startswith('#'):
249 debug(_("Added system store '%s'"), directory)
250 self.stores.append(Store(directory))
252 def lookup(self, digest):
253 """@deprecated: use lookup_any instead"""
254 return self.lookup_any([digest])
256 def lookup_any(self, digests):
257 """Search for digest in all stores.
258 @raises NotStored: if not found"""
259 path = self.lookup_maybe(digests)
260 if path:
261 return path
262 raise NotStored(_("Item with digests '%(digests)s' not found in stores. Searched:\n- %(stores)s") %
263 {'digests': digests, 'stores': '\n- '.join([s.dir for s in self.stores])})
265 def lookup_maybe(self, digests):
266 """Like lookup_any, but return None if it isn't found.
267 @since: 0.53"""
268 assert digests
269 for digest in digests:
270 assert digest
271 if '/' in digest or '=' not in digest:
272 raise BadDigest(_('Syntax error in digest (use ALG=VALUE, not %s)') % digest)
273 for store in self.stores:
274 path = store.lookup(digest)
275 if path:
276 return path
277 return None
279 def add_dir_to_cache(self, required_digest, dir):
280 """Add to the best writable cache.
281 @see: L{Store.add_dir_to_cache}"""
282 self._write_store(lambda store, **kwargs: store.add_dir_to_cache(required_digest, dir, **kwargs))
284 def add_archive_to_cache(self, required_digest, data, url, extract = None, type = None, start_offset = 0):
285 """Add to the best writable cache.
286 @see: L{Store.add_archive_to_cache}"""
287 self._write_store(lambda store, **kwargs: store.add_archive_to_cache(required_digest,
288 data, url, extract, type = type, start_offset = start_offset, **kwargs))
290 def _write_store(self, fn):
291 """Call fn(first_system_store). If it's read-only, try again with the user store."""
292 if len(self.stores) > 1:
293 try:
294 fn(self.get_first_system_store())
295 return
296 except NonwritableStore:
297 debug(_("%s not-writable. Trying helper instead."), self.get_first_system_store())
298 pass
299 fn(self.stores[0], try_helper = True)
301 def get_first_system_store(self):
302 """The first system store is the one we try writing to first.
303 @since: 0.30"""
304 try:
305 return self.stores[1]
306 except IndexError:
307 raise SafeException(_("No system stores have been configured"))