From c636c1db61233cad0a090d22ad285b9cefe68a9e Mon Sep 17 00:00:00 2001 From: Thomas Leonard Date: Mon, 2 Jan 2006 15:43:17 +0000 Subject: [PATCH] Validate interface before saving. git-svn-id: file:///home/talex/Backups/sf.net/Subversion/zero-install/injector/0publish@629 9f8c893c-44ee-0310-b757-c8ca8341c71e --- 0publish | 18 ++++++++++++++++-- validator.py | 24 ++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 2 deletions(-) create mode 100644 validator.py diff --git a/0publish b/0publish index 16e1826..73d5f94 100755 --- a/0publish +++ b/0publish @@ -4,6 +4,7 @@ from optparse import OptionParser import os, sys import signing from logging import info, debug +import edit, validator version = '0.1' @@ -75,10 +76,23 @@ if options.key: print "Changing key from '%s' to '%s'" % (key, options.key) key = options.key if options.edit: - import edit data = edit.edit(data) -# Validate the result... +while True: + # Validate the result... + try: + validator.check(data) + break + except validator.InvalidInterface, ex: + print "Invalid interface: " + str(ex) + + while True: + ans = raw_input("Interface is invalid. (E)dit or (A)bort?").lower() + if ans in ('e', 'edit'): + data = edit.edit(data) + break + if ans in ('a', 'abort'): sys.exit(1) + if old_data == data and sign_fn == old_sign_fn and key == old_key: print "Interface unchanged. Not writing." diff --git a/validator.py b/validator.py new file mode 100644 index 0000000..b7f2561 --- /dev/null +++ b/validator.py @@ -0,0 +1,24 @@ +import os +from zeroinstall.injector import model +from zeroinstall.injector.reader import InvalidInterface, update +import tempfile +from logging import warn + +def check(data): + fd, tmp_name = tempfile.mkstemp(prefix = '0publish-validate-') + os.close(fd) + tmp_iface = model.Interface(tmp_name) + try: + tmp_file = file(tmp_name, 'w') + tmp_file.write(data) + tmp_file.close() + try: + update(tmp_iface, tmp_name, local = True) + return True + except InvalidInterface, ex: + raise + except Exception, ex: + warn("Internal error", ex) + raise InvalidInterface(str(ex)) + finally: + os.unlink(tmp_name) -- 2.11.4.GIT