2 The B{0install digest} command-line interface.
5 # Copyright (C) 2011, Thomas Leonard
6 # See the README file for details, or visit http://0install.net.
8 from __future__
import print_function
12 from zeroinstall
import SafeException
, _
13 from zeroinstall
.zerostore
import manifest
, unpack
14 from zeroinstall
.cmd
import UsageError
15 from zeroinstall
import support
17 syntax
= "DIRECTORY | ARCHIVE [EXTRACT]"
19 def add_options(parser
):
20 parser
.add_option("", "--algorithm", help=_("the hash function to use"), metavar
="HASH")
22 def handle(config
, options
, args
):
31 alg
= manifest
.algorithms
.get(options
.algorithm
or 'sha1new', None)
33 raise SafeException(_('Unknown algorithm "%s"') % alg
)
36 if extract
is not None:
37 d
= os
.path
.join(d
, extract
)
38 digest
= alg
.new_digest()
39 for line
in alg
.generate_manifest(d
):
40 digest
.update(line
+ '\n')
41 print(alg
.getID(digest
))
43 if os
.path
.isdir(source
):
44 if extract
is not None:
45 raise SafeException("Can't use extract with a directory")
49 tmpdir
= tempfile
.mkdtemp()
51 data
= open(args
[0], 'rb')
52 unpack
.unpack_archive(source
, data
, tmpdir
, extract
)
55 support
.ro_rmtree(tmpdir
)