Fixed usage of tab
[jhbuild.git] / jhbuild / main.py
blob147ec2352fd735fef36ac00bb02004bd229819a5
1 # jhbuild - a build script for GNOME 1.x and 2.x
2 # Copyright (C) 2001-2006 James Henstridge
4 # main.py: parses command line arguments and starts the build
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 import sys, os, errno
21 import optparse
22 import traceback
23 import logging
25 import gettext
26 import __builtin__
27 __builtin__.__dict__['N_'] = lambda x: x
29 import jhbuild.config
30 import jhbuild.commands
31 from jhbuild.errors import UsageError, FatalError
32 from jhbuild.utils.cmds import get_output
33 from jhbuild.moduleset import warn_local_modulesets
36 if sys.platform == 'darwin':
37 # work around locale.getpreferredencoding() returning an empty string in
38 # Mac OS X, see http://bugzilla.gnome.org/show_bug.cgi?id=534650 and
39 # http://bazaar-vcs.org/DarwinCommandLineArgumentDecoding
40 sys.platform = 'posix'
41 try:
42 import locale
43 finally:
44 sys.platform = 'darwin'
45 else:
46 import locale
48 try:
49 _encoding = locale.getpreferredencoding()
50 assert _encoding
51 except (locale.Error, AssertionError):
52 _encoding = 'ascii'
54 def uencode(s):
55 if type(s) is unicode:
56 return s.encode(_encoding, 'replace')
57 else:
58 return s
60 def uprint(*args):
61 '''Print Unicode string encoded for the terminal'''
62 for s in args[:-1]:
63 print uencode(s),
64 s = args[-1]
65 print uencode(s)
67 __builtin__.__dict__['uprint'] = uprint
68 __builtin__.__dict__['uencode'] = uencode
70 class LoggingFormatter(logging.Formatter):
71 def __init__(self):
72 logging.Formatter.__init__(self, '%(level_name_initial)s: %(message)s')
74 def format(self, record):
75 record.level_name_initial = record.levelname[0]
76 return logging.Formatter.format(self, record)
78 def print_help(parser):
79 parser.print_help()
80 print
81 jhbuild.commands.print_help()
82 parser.exit()
84 def main(args):
85 localedir = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', 'mo'))
86 if not os.path.exists(localedir):
87 localedir = None
88 gettext.install('jhbuild', localedir=localedir, unicode=True)
90 logging.getLogger().setLevel(logging.INFO)
91 logging_handler = logging.StreamHandler()
92 logging_handler.setFormatter(LoggingFormatter())
93 logging.getLogger().addHandler(logging_handler)
94 parser = optparse.OptionParser(
95 usage=_('%prog [ -f config ] command [ options ... ]'),
96 add_help_option=False,
97 description=_('Build a set of modules from diverse repositories in correct dependency order (such as GNOME).'))
98 parser.disable_interspersed_args()
99 parser.add_option('-h', '--help', action='callback',
100 callback=lambda *args: print_help(parser),
101 help=_("Display this help and exit"))
102 parser.add_option('--help-commands', action='callback',
103 callback=lambda *args: print_help(parser),
104 help=optparse.SUPPRESS_HELP)
105 parser.add_option('-f', '--file', action='store', metavar='CONFIG',
106 type='string', dest='configfile',
107 default=os.environ.get("JHBUILDRC", os.path.join(os.environ['HOME'], '.jhbuildrc')),
108 help=_('use a non default configuration file'))
109 parser.add_option('-m', '--moduleset', action='store', metavar='URI',
110 type='string', dest='moduleset', default=None,
111 help=_('use a non default module set'))
112 parser.add_option('--no-interact', action='store_true',
113 dest='nointeract', default=False,
114 help=_('do not prompt for input'))
116 options, args = parser.parse_args(args)
118 try:
119 config = jhbuild.config.Config(options.configfile)
120 except FatalError, exc:
121 sys.stderr.write('jhbuild: %s\n' % exc.args[0].encode(_encoding, 'replace'))
122 sys.exit(1)
124 if options.moduleset: config.moduleset = options.moduleset
125 if options.nointeract: config.interact = False
127 if not args or args[0][0] == '-':
128 command = 'build' # default to cvs update + compile
129 else:
130 command = args[0]
131 args = args[1:]
133 warn_local_modulesets(config)
135 try:
136 rc = jhbuild.commands.run(command, config, args, help=lambda: print_help(parser))
137 except UsageError, exc:
138 sys.stderr.write('jhbuild %s: %s\n' % (command, exc.args[0].encode(_encoding, 'replace')))
139 parser.print_usage()
140 sys.exit(1)
141 except FatalError, exc:
142 sys.stderr.write('jhbuild %s: %s\n' % (command, exc.args[0].encode(_encoding, 'replace')))
143 sys.exit(1)
144 except KeyboardInterrupt:
145 uprint(_('Interrupted'))
146 sys.exit(1)
147 except EOFError:
148 uprint(_('EOF'))
149 sys.exit(1)
150 except IOError, e:
151 if e.errno != errno.EPIPE:
152 raise
153 sys.exit(0)
154 if rc:
155 sys.exit(rc)