If interface is incorrectly signed, offer to load it anyway.
[0publish.git] / 0publish
blob56f93b915b7f75372ca13f92b26f039be81cfaad
1 #!/usr/bin/env python
2 from zeroinstall.injector import gpg
3 from optparse import OptionParser
4 import os, sys
5 import signing
6 from logging import info, debug
7 import edit, validator
9 version = '0.1'
11 parser = OptionParser(usage="usage: %prog [options] interface")
12 parser.add_option("-k", "--key", help="key to use for signing")
13 parser.add_option("-e", "--edit", help="edit with $EDITOR", action='store_true')
14 parser.add_option("-r", "--release", help="set versions and dates", action='store_true')
15 parser.add_option("-x", "--xmlsign", help="add an XML signature block", action='store_true')
16 parser.add_option("-v", "--verbose", help="more verbose output", action='count')
17 parser.add_option("-V", "--version", help="display version information", action='store_true')
19 (options, args) = parser.parse_args()
21 if options.version:
22 print "0publish (zero-install) " + version
23 print "Copyright (C) 2005 Thomas Leonard"
24 print "This program comes with ABSOLUTELY NO WARRANTY,"
25 print "to the extent permitted by law."
26 print "You may redistribute copies of this program"
27 print "under the terms of the GNU General Public License."
28 print "For more information about these matters, see the file named COPYING."
29 sys.exit(0)
31 if options.verbose:
32 import logging
33 logger = logging.getLogger()
34 if options.verbose == 1:
35 logger.setLevel(logging.INFO)
36 else:
37 logger.setLevel(logging.DEBUG)
39 if len(args) != 1:
40 parser.print_help()
41 sys.exit(1)
42 interface = args[0]
44 def confirm(q):
45 while True:
46 ans = raw_input(q + " [Y/N] ").lower()
47 if ans in ('y', 'yes'): return True
48 if ans in ('n', 'no'): return False
50 # Load or create the starting data...
52 if os.path.exists(interface):
53 contents = file(interface).read()
54 data, sign_fn, key = signing.check_signature(interface)
55 else:
56 if confirm("Interface file '%s' does not exist. Create it?" % interface):
57 from create import create
58 data = create(interface)
59 sign_fn = signing.sign_unsigned
60 key = None
61 options.edit = True
62 else:
63 sys.exit(1)
65 debug("Original data: %s", data)
66 info("Original signing method: %s", sign_fn.__name__)
67 info("Original key: %s", key)
69 old_data = data
70 old_sign_fn = sign_fn
71 old_key = key
73 while True:
74 # Validate the input...
75 try:
76 validator.check(data)
77 break
78 except validator.InvalidInterface, ex:
79 print "Invalid interface: " + str(ex)
81 while True:
82 ans = raw_input("Interface is invalid. (E)dit or (A)bort?").lower()
83 if ans in ('e', 'edit'):
84 data = edit.edit(data)
85 options.edit = False # Don't edit twice
86 break
87 if ans in ('a', 'abort'): sys.exit(1)
89 # Process it...
90 if options.xmlsign:
91 sign_fn = signing.sign_xml
92 if options.key:
93 print "Changing key from '%s' to '%s'" % (key, options.key)
94 key = options.key
95 if options.release:
96 import release
97 data = release.make_release(data)
98 if options.edit:
99 data = edit.edit(data)
101 while True:
102 # Validate the result...
103 try:
104 validator.check(data)
105 break
106 except validator.InvalidInterface, ex:
107 print "Invalid interface: " + str(ex)
109 while True:
110 ans = raw_input("Interface is invalid. (E)dit or (A)bort?").lower()
111 if ans in ('e', 'edit'):
112 data = edit.edit(data)
113 break
114 if ans in ('a', 'abort'): sys.exit(1)
117 if old_data == data and sign_fn == old_sign_fn and key == old_key:
118 print "Interface unchanged. Not writing."
119 sys.exit(1)
121 # Write it back out
122 sign_fn(interface, data, key)
124 print "Wrote", interface