Cope with unicode characters in exceptions
[0compile.git] / 0compile
blobdf42feb2fac0abb5b6e626c1b27322742be0d55a
1 #!/usr/bin/env python
2 # Copyright (C) 2006, Thomas Leonard
3 # See the README file for details, or visit http://0install.net.
5 __builtins__._ = lambda x: x
7 import locale
8 from optparse import OptionParser
9 import os, sys
10 from logging import warn
12 zeroinstall_dir = os.environ.get('0COMPILE_ZEROINSTALL', None)
13 if zeroinstall_dir:
14 sys.path.insert(1, zeroinstall_dir)
16 from zeroinstall import SafeException
18 class UsageError(SafeException): pass
20 commands = []
22 import autocompile, setup, clean, copysrc, build, publish, gui, bugs, include_deps
23 import support
25 version = '0.23'
27 try:
28 locale.setlocale(locale.LC_ALL, '')
29 except locale.Error:
30 warn('Error setting locale (eg. Invalid locale)')
32 parser = OptionParser(usage="usage: %prog " +
33 '\n %prog '.join([c.__doc__ for c in commands]))
35 parser.add_option("-c", "--console", help="never use GUI", action='store_true')
36 parser.add_option("-v", "--verbose", help="more verbose output", action='count')
37 parser.add_option("-V", "--version", help="display version information", action='store_true')
39 parser.disable_interspersed_args()
41 (options, args) = parser.parse_args()
43 if options.version:
44 print "0compile (zero-install) " + version
45 print "Copyright (C) 2006 Thomas Leonard"
46 print "This program comes with ABSOLUTELY NO WARRANTY,"
47 print "to the extent permitted by law."
48 print "You may redistribute copies of this program"
49 print "under the terms of the GNU General Public License."
50 print "For more information about these matters, see the file named COPYING."
51 sys.exit(0)
53 if options.verbose:
54 import logging
55 logger = logging.getLogger()
56 if options.verbose == 1:
57 logger.setLevel(logging.INFO)
58 else:
59 logger.setLevel(logging.DEBUG)
61 if options.console:
62 support.launch_prog.append('--console')
64 if len(args) < 1:
65 parser.print_help()
66 sys.exit(1)
68 try:
69 pattern = args[0].lower()
70 matches = [c for c in commands if c.__name__[3:].replace('_', '-').startswith(pattern)]
71 if len(matches) == 0:
72 parser.print_help()
73 sys.exit(1)
74 if len(matches) > 1:
75 raise SafeException("What do you mean by '%s'?\n%s" %
76 (pattern, '\n'.join(['- ' + x.__name__[3:] for x in matches])))
77 matches[0](args[1:])
78 except KeyboardInterrupt, ex:
79 print >>sys.stderr, "Interrupted"
80 sys.exit(1)
81 except OSError, ex:
82 if options.verbose: raise
83 print >>sys.stderr, str(ex)
84 sys.exit(1)
85 except IOError, ex:
86 if options.verbose: raise
87 print >>sys.stderr, str(ex)
88 sys.exit(1)
89 except UsageError, ex:
90 print >>sys.stderr, str(ex)
91 print >>sys.stderr, "usage: " + os.path.basename(sys.argv[0]) + " " + matches[0].__doc__
92 sys.exit(1)
93 except SafeException, ex:
94 if options.verbose: raise
95 try:
96 print >>sys.stderr, unicode(ex)
97 except:
98 print >>sys.stderr, repr(ex)
99 sys.exit(1)