Package zeroinstall :: Package cmd :: Module digest
[frames] | no frames]

Source Code for Module zeroinstall.cmd.digest

 1  """ 
 2  The B{0install digest} command-line interface. 
 3  """ 
 4   
 5  # Copyright (C) 2011, Thomas Leonard 
 6  # See the README file for details, or visit http://0install.net. 
 7   
 8  from __future__ import print_function 
 9   
10  import os, tempfile 
11   
12  from zeroinstall import SafeException, _ 
13  from zeroinstall.zerostore import manifest, unpack 
14  from zeroinstall.cmd import UsageError 
15  from zeroinstall import support 
16   
17  syntax = "DIRECTORY | ARCHIVE [EXTRACT]" 
18   
19 -def add_options(parser):
20 parser.add_option("", "--algorithm", help=_("the hash function to use"), metavar="HASH") 21 parser.add_option("-m", "--manifest", help=_("print the manifest"), action='store_true') 22 parser.add_option("-d", "--digest", help=_("print the digest"), action='store_true')
23
24 -def handle(config, options, args):
25 """@type args: [str]""" 26 if len(args) == 1: 27 extract = None 28 elif len(args) == 2: 29 extract = args[1] 30 else: 31 raise UsageError() 32 33 source = args[0] 34 alg = manifest.algorithms.get(options.algorithm or 'sha1new', None) 35 if alg is None: 36 raise SafeException(_('Unknown algorithm "%s"') % alg) 37 38 show_manifest = bool(options.manifest) 39 show_digest = bool(options.digest) or not show_manifest 40 41 def do_manifest(d): 42 if extract is not None: 43 d = os.path.join(d, extract) 44 digest = alg.new_digest() 45 for line in alg.generate_manifest(d): 46 if show_manifest: 47 print(line) 48 digest.update((line + '\n').encode('utf-8')) 49 if show_digest: 50 print(alg.getID(digest))
51 52 if os.path.isdir(source): 53 if extract is not None: 54 raise SafeException("Can't use extract with a directory") 55 do_manifest(source) 56 else: 57 data = None 58 tmpdir = tempfile.mkdtemp() 59 try: 60 data = open(args[0], 'rb') 61 unpack.unpack_archive(source, data, tmpdir, extract) 62 do_manifest(tmpdir) 63 finally: 64 support.ro_rmtree(tmpdir) 65 if data: 66 data.close() 67
68 -def complete(completion, args, cword):
69 """@type completion: L{zeroinstall.cmd._Completion} 70 @type args: [str] 71 @type cword: int""" 72 if len(args) != 1: return 73 completion.expand_files()
74