~launchpad-results/launchpad-results/trunk

« back to all changes in this revision

Viewing changes to lib/lpresults/xunit/application.py

  • Committer: Marc Tardif
  • Date: 2011-07-25 17:11:24 UTC
  • Revision ID: marc.tardif@canonical.com-20110725171124-0gg693yorqv2sxbf
Added script for importing submissions from Launchpad.

Show diffs side-by-side

added added

removed removed

Lines of Context:
38
38
from launchpadlib.launchpad import Launchpad
39
39
 
40
40
from lpresults.xunit.functor import Functor
 
41
from lpresults.xunit.logger import get_logger
41
42
from lpresults.xunit.retry import retry_except
42
43
 
43
44
 
47
48
 
48
49
class Application:
49
50
 
50
 
    launchpad_factory = Launchpad.login_with
51
 
 
52
 
    def __init__(self, **kwargs):
53
 
        self.launchpad_functor = Functor(self.launchpad_factory, **kwargs)
 
51
    def __init__(self, launchpad_factory, logger):
 
52
        self.launchpad_factory = launchpad_factory
 
53
        self.logger = logger
54
54
 
55
55
    def getLaunchpad(self, *args, **kwargs):
56
56
        try:
57
 
            return self.launchpad_functor(*args, **kwargs)
 
57
            return self.launchpad_factory(*args, **kwargs)
58
58
        except AttributeError:
59
59
            # In stock httplib2, this exception is raised when failing
60
60
            # to connect to the remote host:
113
113
    default_service_root = "http://localhost:8000/+api/"
114
114
    default_version = "1.0"
115
115
 
 
116
    # Logging defaults
 
117
    default_log_level = "info"
 
118
 
116
119
    def __init__(self, application_factory=None, usage=None):
117
120
        if application_factory is not None:
118
121
            self.application_factory = application_factory
119
122
        if usage is not None:
120
123
            self.usage = usage
121
124
 
122
 
    def getParser(self):
123
 
        parser = OptionParser(usage=self.usage)
 
125
    def getParser(self, **kwargs):
 
126
        parser = OptionParser(usage=self.usage, **kwargs)
124
127
 
125
128
        group = OptionGroup(parser, "Launchpad options")
126
129
        group.add_option("--consumer-name",
127
130
            default=self.default_consumer_name,
 
131
            metavar="NAME",
128
132
            help="The consumer appropriate for this application,"
129
133
                " defaults to: %default")
130
134
        group.add_option("--credentials-file",
 
135
            metavar="FILE",
131
136
            help="If given, the credentials are stored in that file.")
132
137
        group.add_option("--launchpadlib-dir",
 
138
            metavar="DIR",
133
139
            help="The directory for cache and credentials.")
134
140
        group.add_option("--service-root",
135
141
            default=self.default_service_root,
 
142
            metavar="URL",
136
143
            help="The URL to the root of the web service,"
137
144
                " defaults to: %default")
138
145
        group.add_option("--version",
141
148
                " defaults to: %default")
142
149
        parser.add_option_group(group)
143
150
 
 
151
        group = OptionGroup(parser, "Logging options")
 
152
        group.add_option("-l", "--log",
 
153
            metavar="FILE",
 
154
            help="The file to write the log to.")
 
155
        group.add_option("--log-level",
 
156
            default=self.default_log_level,
 
157
            help="One of debug, info, warning, error or critical.")
 
158
        parser.add_option_group(group)
 
159
 
144
160
        return parser
145
161
 
146
162
    def parseArgs(self, args):
147
163
        parser = self.getParser()
148
164
        options, args = parser.parse_args(args)
 
165
        kwargs = options.__dict__
 
166
 
 
167
        # Define logger early
 
168
        log = kwargs.pop("log")
 
169
        if log:
 
170
            if log == "-":
 
171
                log_stream, log_file = sys.stdout, None
 
172
            else:
 
173
                log_stream, log_file = None, log
 
174
        else:
 
175
            log_stream, log_file = sys.stderr, None
 
176
        kwargs["logger"] = get_logger(
 
177
            kwargs.pop("log_level"), log_stream, log_file)
149
178
 
150
179
        # Define launchpadlib_dir and credentials_file if empty
151
 
        if options.launchpadlib_dir is None:
 
180
        launchpadlib_dir = kwargs.pop("launchpadlib_dir")
 
181
        if launchpadlib_dir is None:
152
182
            home_dir = environ["HOME"]
153
183
            launchpadlib_dir = path.join(home_dir, ".lpresults")
154
 
        else:
155
 
            launchpadlib_dir = options.launchpadlib_dir
156
184
 
157
 
        if options.credentials_file is None:
 
185
        credentials_file = kwargs.pop("credentials_file")
 
186
        if credentials_file is None:
158
187
            host_name = urlsplit(options.service_root)[1]
159
188
            credentials_file = path.join(
160
189
                launchpadlib_dir, host_name, "credentials",
161
190
                options.consumer_name)
162
 
        else:
163
 
            credentials_file = options.credentials_file
164
191
 
165
 
        kwargs = options.__dict__
166
 
        kwargs["launchpadlib_dir"] = path.expanduser(launchpadlib_dir)
167
 
        kwargs["credentials_file"] = path.expanduser(credentials_file)
 
192
        kwargs["launchpad_factory"] = Functor(Launchpad.login_with,
 
193
            launchpadlib_dir=path.expanduser(launchpadlib_dir),
 
194
            credentials_file=path.expanduser(credentials_file),
 
195
            consumer_name=kwargs.pop("consumer_name"),
 
196
            service_root=kwargs.pop("service_root"),
 
197
            version=kwargs.pop("version"))
168
198
 
169
199
        return args, kwargs
170
200
 
171
 
    def createApplication(self, args=None):
 
201
    def createApplication(self, args):
172
202
        args, kwargs = self.parseArgs(args)
173
203
 
174
204
        return self.application_factory(*args, **kwargs)
184
214
        This will be called after the application has finished.
185
215
        """
186
216
 
187
 
    def run(self, *args, **kwargs):
 
217
    def run(self, args=None):
188
218
        """Run the application."""
189
219
        self.preApplication()
190
220
 
191
221
        try:
192
 
            application = self.createApplication(*args, **kwargs)
 
222
            application = self.createApplication(args)
193
223
            application.start()
194
224
 
195
225
        except ApplicationError, error: