From 9fe1a1fe89bc97cef39a48595838f2abb01f64a4 Mon Sep 17 00:00:00 2001 From: Thomas Leonard Date: Sat, 9 May 2009 11:38:28 +0100 Subject: [PATCH] Allow multiple --manifest-algorithm options The first digest becomes the 'id', the others get added to a element. By default, sha1new is used for the id, and sha256 is added as an extra, if available. --- 0publish | 10 ++++++++-- archive.py | 31 +++++++++++++++++++------------ 2 files changed, 27 insertions(+), 14 deletions(-) diff --git a/0publish b/0publish index dc142ca..661431a 100755 --- a/0publish +++ b/0publish @@ -20,7 +20,7 @@ parser.add_option("-e", "--edit", help="edit with $EDITOR", action='store_true') parser.add_option("-g", "--gpgsign", help="add a GPG signature block", action='store_true') parser.add_option("-k", "--key", help="key to use for signing") parser.add_option("-l", "--local", help="create feed from local interface") -parser.add_option("--manifest-algorithm", help="select algorithm for manifests", action='store', metavar='ALG') +parser.add_option("--manifest-algorithm", help="select algorithm for manifests", action='append', metavar='ALG') parser.add_option("--set-interface-uri", help="set interface URI", action='store', metavar='URI') parser.add_option("--set-id", help="set implementation ID", action='store', metavar='DIGEST') parser.add_option("--set-main", help="set main executable", action='store', metavar='EXEC') @@ -145,7 +145,13 @@ try: data = stable.mark_stable(data) if options.archive_url: import archive - data = archive.add_archive(data, options.archive_url, options.archive_file, options.archive_extract, options.manifest_algorithm) + algs = options.manifest_algorithm + if algs is None: + algs = ['sha1new'] + import hashlib + if hasattr(hashlib, 'sha256'): + algs.append('sha256') + data = archive.add_archive(data, options.archive_url, options.archive_file, options.archive_extract, algs) elif options.archive_file or options.archive_extract: raise Exception('Must use --archive-uri option') if options.local: diff --git a/archive.py b/archive.py index 7965776..fbdfbf7 100644 --- a/archive.py +++ b/archive.py @@ -9,6 +9,9 @@ except ImportError: from zeroinstall.injector import namespaces import os, shutil, tempfile +import digest +import xmltools + def ro_rmtree(root): """Like shutil.rmtree, except that we also delete with read-only items. @param root: the root of the subtree to remove @@ -41,7 +44,7 @@ def autopackage_get_start_offset(package): return os.path.getsize(package) - int(line.split('"', 2)[1]) raise Exception("Can't find payload in autopackage (missing 'dataSize')") -def add_archive(data, url, local_file, extract, alg): +def add_archive(data, url, local_file, extract, algs): if local_file is None: local_file = os.path.abspath(os.path.basename(url)) if not os.path.exists(local_file): @@ -50,9 +53,8 @@ def add_archive(data, url, local_file, extract, alg): doc = minidom.parseString(data) - if alg is None: - alg = 'sha1new' - + assert algs + if local_file.endswith('.package'): start_offset = autopackage_get_start_offset(local_file) type = 'application/x-bzip-compressed-tar' @@ -72,7 +74,8 @@ def add_archive(data, url, local_file, extract, alg): else: extracted = tmpdir - archive_id = manifest_for_dir(extracted, alg) + archive_id = manifest_for_dir(extracted, algs[0]) + extra_digests = set([manifest_for_dir(extracted, a) for a in algs[1:]]) finally: ro_rmtree(tmpdir) @@ -93,11 +96,7 @@ def add_archive(data, url, local_file, extract, alg): assert impl.getAttribute('id') == archive_id - nl = doc.createTextNode('\n ') - impl.appendChild(nl) - - archive = doc.createElementNS(namespaces.XMLNS_IFACE, 'archive') - impl.appendChild(archive) + archive = xmltools.create_element(impl, 'archive') archive.setAttribute('href', url) archive.setAttribute('size', str(os.stat(local_file).st_size - start_offset)) if extract is not None: @@ -107,7 +106,15 @@ def add_archive(data, url, local_file, extract, alg): if type: archive.setAttribute('type', type) - nl = doc.createTextNode('\n ') - impl.appendChild(nl) + # Remove digests we already + for x in xmltools.children(impl, 'manifest-digest'): + digest_element = x + break + else: + digest_element = doc.createElementNS(namespaces.XMLNS_IFACE, 'manifest-digest') + xmltools.insert_before(digest_element, archive) + for x in extra_digests - set([digest.digests(impl)]): + name, value = x.split('=') + digest_element.setAttribute(name, value) return doc.toxml() -- 2.11.4.GIT