Logo Search packages:      
Sourcecode: zeroinstall-injector version File versions


00001 """Code for the B{0store} command-line interface."""

# Copyright (C) 2006, Thomas Leonard
# See the README file for details, or visit http://0install.net.

import sys, os
from zeroinstall.zerostore.manifest import generate_manifest, verify, get_algorithm, copy_tree_with_verify
from zeroinstall import zerostore, SafeException

stores = None

def init_stores():
      global stores
      assert stores is None
      if stores is None:
            stores = zerostore.Stores()

class UsageError(SafeException): pass

00020 def do_manifest(args):
      """manifest DIRECTORY [ALGORITHM]"""
      if len(args) < 1 or len(args) > 2: raise UsageError("Wrong number of arguments")
      if len(args) == 2:
            alg = get_algorithm(args[1])
            # If no algorithm was given, guess from the directory name
            name = os.path.basename(args[0])
            if '=' in name:
                  alg = get_algorithm(name.split('=', 1)[0])
                  alg = get_algorithm('sha1')
      digest = alg.new_digest()
      for line in alg.generate_manifest(args[0]):
            print line
            digest.update(line + '\n')
      print alg.getID(digest)

00039 def do_find(args):
      """find DIGEST"""
      if len(args) != 1: raise UsageError("Wrong number of arguments")
            print stores.lookup(args[0])
      except zerostore.BadDigest, ex:
            print >>sys.stderr, ex
      except zerostore.NotStored, ex:
            print >>sys.stderr, ex

00051 def do_add(args):
      from zeroinstall.zerostore import unpack
      if len(args) < 2: raise UsageError("Missing arguments")
      digest = args[0]
      if os.path.isdir(args[1]):
            if len(args) > 2: raise UsageError("Too many arguments")
            stores.add_dir_to_cache(digest, args[1])
      elif os.path.isfile(args[1]):
            if len(args) > 3: raise UsageError("Too many arguments")
            if len(args) > 2:
                  extract = args[2]
                  extract = None

            type = unpack.type_from_url(args[1])
            if not type:
                  raise SafeException("Unknown extension in '%s' - can't guess MIME type" % args[1])

            stores.add_archive_to_cache(digest, file(args[1]), args[1], extract, type = type)
            raise UsageError("No such file or directory '%s'" % args[1])

00075 def do_verify(args):
      """verify (DIGEST | (DIRECTORY [DIGEST])"""
      if len(args) == 2:
            required_digest = args[1]
            root = args[0]
      elif len(args) == 1:
            root = get_stored(args[0])
            required_digest = None        # Get from name
           raise UsageError("Missing DIGEST or DIRECTORY")

      print "Verifying", root
            verify(root, required_digest)
            print "OK"
      except zerostore.BadDigest, ex:
            print str(ex)
            if ex.detail:
                  print ex.detail

def show_changes(actual, saved):
      import difflib
      for line in difflib.unified_diff(saved, actual, 'Recorded', 'Actual'):
            print line,

00102 def do_list(args):
      if args: raise UsageError("List takes no arguments")
      print "User store (writable) : " + stores.stores[0].dir
      for s in stores.stores[1:]:
            print "System store          : " + s.dir
      if len(stores.stores) < 2:
            print "No system stores."

def get_stored(dir_or_digest):
      if os.path.isdir(dir_or_digest):
            return dir_or_digest
                  return stores.lookup(dir_or_digest)
            except zerostore.NotStored, ex:
                  print >>sys.stderr, ex

00121 def do_copy(args):
      """copy SOURCE [ TARGET ]"""
      if len(args) == 2:
            source, target = args
      elif len(args) == 1:
            source = args[0]
            target = stores.stores[0].dir
            raise UsageError("Wrong number of arguments.")

      if not os.path.isdir(source):
            raise UsageError("Source directory '%s' not found" % source)
      if not os.path.isdir(target):
            raise UsageError("Target directory '%s' not found" % target)
      manifest_path = os.path.join(source, '.manifest')
      if not os.path.isfile(manifest_path):
            raise UsageError("Source manifest '%s' not found" % manifest_path)
      required_digest = os.path.basename(source)
      manifest_data = file(manifest_path).read()

      copy_tree_with_verify(source, target, manifest_data, required_digest)

commands = [do_add, do_copy, do_find, do_list, do_manifest, do_verify]

Generated by  Doxygen 1.6.0   Back to index