also look up for system-installed translations
[jhbuild.git] / jhbuild / main.py
blob55a7ee0758b73eace85decd00dd71f8481f117df
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 help_commands(option, opt_str, value, parser):
79 thisdir = os.path.abspath(os.path.dirname(__file__))
81 # import all available commands
82 for fname in os.listdir(os.path.join(thisdir, 'commands')):
83 name, ext = os.path.splitext(fname)
84 if not ext == '.py':
85 continue
86 try:
87 __import__('jhbuild.commands.%s' % name)
88 except ImportError:
89 pass
91 uprint(_('JHBuild commands are:'))
92 commands = [(x.name, x.doc) for x in jhbuild.commands.get_commands().values()]
93 commands.sort()
94 for name, description in commands:
95 uprint(' %-15s %s' % (name, description))
96 print
97 uprint(_('For more information run "jhbuild <command> --help"'))
98 parser.exit()
100 def main(args):
101 localedir = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', 'mo'))
102 if not os.path.exists(localedir):
103 localedir = None
104 gettext.install('jhbuild', localedir=localedir, unicode=True)
106 logging.getLogger().setLevel(logging.INFO)
107 logging_handler = logging.StreamHandler()
108 logging_handler.setFormatter(LoggingFormatter())
109 logging.getLogger().addHandler(logging_handler)
110 parser = optparse.OptionParser(
111 usage=_('%prog [ -f config ] command [ options ... ]'),
112 description=_('Build a set of modules from diverse repositories in correct dependency order (such as GNOME).'))
113 parser.disable_interspersed_args()
114 parser.add_option('--help-commands', action='callback',
115 callback=help_commands,
116 help=_('Information about available jhbuild commands'))
117 parser.add_option('-f', '--file', action='store', metavar='CONFIG',
118 type='string', dest='configfile',
119 default=os.environ.get("JHBUILDRC", os.path.join(os.environ['HOME'], '.jhbuildrc')),
120 help=_('use a non default configuration file'))
121 parser.add_option('-m', '--moduleset', action='store', metavar='URI',
122 type='string', dest='moduleset', default=None,
123 help=_('use a non default module set'))
124 parser.add_option('--no-interact', action='store_true',
125 dest='nointeract', default=False,
126 help=_('do not prompt for input'))
128 options, args = parser.parse_args(args)
130 try:
131 config = jhbuild.config.Config(options.configfile)
132 except FatalError, exc:
133 sys.stderr.write('jhbuild: %s\n' % exc.args[0].encode(_encoding, 'replace'))
134 sys.exit(1)
136 if options.moduleset: config.moduleset = options.moduleset
137 if options.nointeract: config.interact = False
139 if not args or args[0][0] == '-':
140 command = 'build' # default to cvs update + compile
141 else:
142 command = args[0]
143 args = args[1:]
145 warn_local_modulesets(config)
147 try:
148 rc = jhbuild.commands.run(command, config, args)
149 except UsageError, exc:
150 sys.stderr.write('jhbuild %s: %s\n' % (command, exc.args[0].encode(_encoding, 'replace')))
151 parser.print_usage()
152 sys.exit(1)
153 except FatalError, exc:
154 sys.stderr.write('jhbuild %s: %s\n' % (command, exc.args[0].encode(_encoding, 'replace')))
155 sys.exit(1)
156 except KeyboardInterrupt:
157 uprint(_('Interrupted'))
158 sys.exit(1)
159 except EOFError:
160 uprint(_('EOF'))
161 sys.exit(1)
162 except IOError, e:
163 if e.errno != errno.EPIPE:
164 raise
165 sys.exit(0)
166 if rc:
167 sys.exit(rc)