summaryrefslogtreecommitdiff
path: root/cerbero/main.py
blob: 5a503be58a1ae8ca7127598baad65492850f4c66 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# cerbero - a multi-platform build system for Open Source software
# Copyright (C) 2012 Andoni Morales Alastruey <ylatuya@gmail.com>
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Library General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Library General Public License for more details.
#
# You should have received a copy of the GNU Library General Public
# License along with this library; if not, write to the
# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.

from cerbero import hacks

try:
    import argparse
except ImportError, e:
    print "Could not import argparse. Try installing it with "\
          "'sudo easy_install argparse"
    raise e

import sys
import errno
import logging
import traceback
import os

from cerbero import config, commands
from cerbero.errors import UsageError, FatalError, BuildStepError, \
    ConfigurationError, CerberoException, AbortedError
from cerbero.utils import _, N_, user_is_root
from cerbero.utils import messages as m

description = N_('Build and package a set of modules to distribute them in '
                 'a SDK')

class Main(object):

    def __init__(self, args):
        if user_is_root():
            m.warning(_("Running as root"))

        self.check_in_cerbero_shell()
        self.init_logging()
        self.create_parser()
        self.load_commands()
        self.parse_arguments(args)
        self.load_config()
        self.run_command()

    def check_in_cerbero_shell(self):
        if os.environ.get('CERBERO_PREFIX', '') != '':
            self.log_error(_("ERROR: cerbero can't be run "
                             "from a cerbero shell"))

    def log_error(self, msg, print_usage=False, command=None):
        ''' Log an error and exit '''
        if command is not None:
            m.error("***** Error running '%s' command:" % command)
        m.error('%s' % msg)
        if print_usage:
            self.parser.print_usage()
        sys.exit(1)

    def init_logging(self):
        ''' Initialize logging '''
        logging.getLogger().setLevel(logging.INFO)
        logging.getLogger().addHandler(logging.StreamHandler())

    def create_parser(self):
        ''' Creates the arguments parser '''
        self.parser = argparse.ArgumentParser(description=_(description))
        self.parser.add_argument('-c', '--config', action='append', type=str, default=None,
                help=_('Configuration file used for the build'))

    def parse_arguments(self, args):
        ''' Parse the command line arguments '''
        # If no commands, make it show the help by default
        if len(args) == 0:
            args = ["-h"]
        self.args = self.parser.parse_args(args)

    def load_commands(self):
        subparsers = self.parser.add_subparsers(help=_('sub-command help'),
                                                dest='command')
        commands.load_commands(subparsers)

    def load_config(self):
        ''' Load the configuration '''
        try:
            self.config = config.Config()
            self.config.load(self.args.config)
        except ConfigurationError, exc:
            self.log_error(exc, False)

    def run_command(self):
        command = self.args.command
        try:
            res = commands.run(command, self.config, self.args)
        except UsageError, exc:
            self.log_error(exc, True, command)
            sys.exit(1)
        except FatalError, exc:
            traceback.print_exc()
            self.log_error(exc, True, command)
        except BuildStepError, exc:
            self.log_error(exc.msg, False, command)
        except AbortedError, exc:
            self.log_error('', False, command)
        except CerberoException, exc:
            self.log_error(exc, False, command)
        except KeyboardInterrupt:
            self.log_error(_('Interrupted'))
        except IOError, e:
            if e.errno != errno.EPIPE:
                raise
            sys.exit(0)

        if res:
            sys.exit(res)


def main():
    Main(sys.argv[1:])


if __name__ == "__main__":
    main()