Added --local to allow creation of feeds from local interfaces.
[0publish.git] / 0publish
blob9335c17da60dc3119444a5d095ef572fdef917a8
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("-l", "--local", help="create feed from local interface")
15 parser.add_option("-r", "--release", help="set versions and dates", action='store_true')
16 parser.add_option("-x", "--xmlsign", help="add an XML signature block", action='store_true')
17 parser.add_option("-v", "--verbose", help="more verbose output", action='count')
18 parser.add_option("-V", "--version", help="display version information", action='store_true')
20 (options, args) = parser.parse_args()
22 force_save = False
24 if options.version:
25 print "0publish (zero-install) " + version
26 print "Copyright (C) 2005 Thomas Leonard"
27 print "This program comes with ABSOLUTELY NO WARRANTY,"
28 print "to the extent permitted by law."
29 print "You may redistribute copies of this program"
30 print "under the terms of the GNU General Public License."
31 print "For more information about these matters, see the file named COPYING."
32 sys.exit(0)
34 if options.verbose:
35 import logging
36 logger = logging.getLogger()
37 if options.verbose == 1:
38 logger.setLevel(logging.INFO)
39 else:
40 logger.setLevel(logging.DEBUG)
42 if len(args) != 1:
43 parser.print_help()
44 sys.exit(1)
45 interface = args[0]
47 def confirm(q):
48 while True:
49 ans = raw_input(q + " [Y/N] ").lower()
50 if ans in ('y', 'yes'): return True
51 if ans in ('n', 'no'): return False
53 # Load or create the starting data...
55 if os.path.exists(interface):
56 if options.local:
57 raise Exception("Can't use --local; file '%s' already exists!" % interface)
58 contents = file(interface).read()
59 data, sign_fn, key = signing.check_signature(interface)
60 elif options.local:
61 import create
62 if os.path.exists(options.local):
63 data = create.create_from_local(options.local)
64 sign_fn = signing.sign_unsigned
65 key = None
66 force_save = True
67 else:
68 raise Exception("File '%s' does not exist." % options.local)
69 else:
70 if confirm("Interface file '%s' does not exist. Create it?" % interface):
71 from create import create
72 data = create(interface)
73 sign_fn = signing.sign_unsigned
74 key = None
75 options.edit = True
76 else:
77 sys.exit(1)
79 debug("Original data: %s", data)
80 info("Original signing method: %s", sign_fn.__name__)
81 info("Original key: %s", key)
83 old_data = data
84 old_sign_fn = sign_fn
85 old_key = key
87 while True:
88 # Validate the input...
89 try:
90 validator.check(data)
91 break
92 except validator.InvalidInterface, ex:
93 print "Invalid interface: " + str(ex)
95 while True:
96 ans = raw_input("Interface is invalid. (E)dit or (A)bort?").lower()
97 if ans in ('e', 'edit'):
98 data = edit.edit(data)
99 options.edit = False # Don't edit twice
100 break
101 if ans in ('a', 'abort'): sys.exit(1)
103 # Process it...
104 if options.xmlsign:
105 sign_fn = signing.sign_xml
106 if options.key:
107 print "Changing key from '%s' to '%s'" % (key, options.key)
108 key = options.key
109 if options.release:
110 import release
111 data = release.make_release(data)
112 if options.edit:
113 data = edit.edit(data)
115 while True:
116 # Validate the result...
117 try:
118 validator.check(data)
119 break
120 except validator.InvalidInterface, ex:
121 print "Invalid interface: " + str(ex)
123 while True:
124 ans = raw_input("Interface is invalid. (E)dit or (A)bort?").lower()
125 if ans in ('e', 'edit'):
126 data = edit.edit(data)
127 break
128 if ans in ('a', 'abort'): sys.exit(1)
131 if (old_data == data and sign_fn == old_sign_fn and key == old_key) and not force_save:
132 print "Interface unchanged. Not writing."
133 sys.exit(1)
135 # Write it back out
136 if not data.endswith('\n'): data += '\n'
137 sign_fn(interface, data, key)
139 print "Wrote", interface