Merge lp:~jelmer/bzr/lp-plugin-lazy into lp:bzr
- lp-plugin-lazy
- Merge into bzr.dev
Status: | Merged |
---|---|
Approved by: | Martin Packman |
Approved revision: | no longer in the source branch. |
Merged at revision: | 6496 |
Proposed branch: | lp:~jelmer/bzr/lp-plugin-lazy |
Merge into: | lp:bzr |
Diff against target: |
851 lines (+422/-403) 2 files modified
bzrlib/plugins/launchpad/__init__.py (+12/-403) bzrlib/plugins/launchpad/cmds.py (+410/-0) |
To merge this branch: | bzr merge lp:~jelmer/bzr/lp-plugin-lazy |
Related bugs: |
Reviewer | Review Type | Date Requested | Status |
---|---|---|---|
Martin Packman (community) | Approve | ||
Review via email: mp+96892@code.launchpad.net |
Commit message
Lazily load launchpad plugin commands.
Description of the change
Lazily load launchpad plugin commands.
Jelmer Vernooij (jelmer) wrote : | # |
Am 12/03/12 00:46, schrieb Martin Packman:
> Review: Needs Information
>
> Looks fine, though unlikely to have a big impact.
>
> Seems like _register_hooks should be replaced with a install_
I guess, although branch gets imported anyway by other bits of the code
so I don't think that would be all that useful.
>
> # Since we are a built-in plugin we share the bzrlib version
> + trace,
> version_info,
>
> Addition split the comment from the version_info line it's talking about. Mixing up module and object imports is a bit ick, but that's python.
Whoops, thanks.
Cheers,
jelmer
Martin Packman (gz) wrote : | # |
Sort of underlines the number of different lazy registration things we have...
Preview Diff
1 | === modified file 'bzrlib/plugins/launchpad/__init__.py' | |||
2 | --- bzrlib/plugins/launchpad/__init__.py 2012-03-06 17:17:27 +0000 | |||
3 | +++ bzrlib/plugins/launchpad/__init__.py 2012-03-12 14:38:29 +0000 | |||
4 | @@ -42,420 +42,29 @@ | |||
5 | 42 | 42 | ||
6 | 43 | # see http://wiki.bazaar.canonical.com/Specs/BranchRegistrationTool | 43 | # see http://wiki.bazaar.canonical.com/Specs/BranchRegistrationTool |
7 | 44 | 44 | ||
8 | 45 | from bzrlib.lazy_import import lazy_import | ||
9 | 46 | lazy_import(globals(), """ | ||
10 | 47 | from bzrlib import ( | ||
11 | 48 | ui, | ||
12 | 49 | trace, | ||
13 | 50 | ) | ||
14 | 51 | from bzrlib.i18n import gettext | ||
15 | 52 | """) | ||
16 | 53 | |||
17 | 54 | from bzrlib import ( | 45 | from bzrlib import ( |
18 | 55 | branch as _mod_branch, | 46 | branch as _mod_branch, |
19 | 56 | config as _mod_config, | 47 | config as _mod_config, |
20 | 57 | controldir, | ||
21 | 58 | lazy_regex, | 48 | lazy_regex, |
22 | 59 | # Since we are a built-in plugin we share the bzrlib version | 49 | # Since we are a built-in plugin we share the bzrlib version |
23 | 50 | trace, | ||
24 | 60 | version_info, | 51 | version_info, |
25 | 61 | ) | 52 | ) |
26 | 62 | from bzrlib.commands import ( | 53 | from bzrlib.commands import ( |
29 | 63 | Command, | 54 | plugin_cmds, |
28 | 64 | register_command, | ||
30 | 65 | ) | 55 | ) |
31 | 66 | from bzrlib.directory_service import directories | 56 | from bzrlib.directory_service import directories |
32 | 67 | from bzrlib.errors import ( | ||
33 | 68 | BzrCommandError, | ||
34 | 69 | InvalidRevisionSpec, | ||
35 | 70 | InvalidURL, | ||
36 | 71 | NoPublicBranch, | ||
37 | 72 | NotBranchError, | ||
38 | 73 | ) | ||
39 | 74 | from bzrlib.help_topics import topic_registry | 57 | from bzrlib.help_topics import topic_registry |
424 | 75 | from bzrlib.option import ( | 58 | |
425 | 76 | Option, | 59 | for klsname, aliases in [ |
426 | 77 | ListOption, | 60 | ("cmd_register_branch", []), |
427 | 78 | ) | 61 | ("cmd_launchpad_open", ["lp-open"]), |
428 | 79 | 62 | ("cmd_launchpad_login", ["lp-login"]), | |
429 | 80 | 63 | ("cmd_launchpad_mirror", ["lp-mirror"]), | |
430 | 81 | class cmd_register_branch(Command): | 64 | ("cmd_lp_propose_merge", ["lp-submit", "lp-propose"]), |
431 | 82 | __doc__ = """Register a branch with launchpad.net. | 65 | ("cmd_lp_find_proposal", [])]: |
432 | 83 | 66 | plugin_cmds.register_lazy(klsname, aliases, | |
433 | 84 | This command lists a bzr branch in the directory of branches on | 67 | "bzrlib.plugins.launchpad.cmds") |
50 | 85 | launchpad.net. Registration allows the branch to be associated with | ||
51 | 86 | bugs or specifications. | ||
52 | 87 | |||
53 | 88 | Before using this command you must register the project to which the | ||
54 | 89 | branch belongs, and create an account for yourself on launchpad.net. | ||
55 | 90 | |||
56 | 91 | arguments: | ||
57 | 92 | public_url: The publicly visible url for the branch to register. | ||
58 | 93 | This must be an http or https url (which Launchpad can read | ||
59 | 94 | from to access the branch). Local file urls, SFTP urls, and | ||
60 | 95 | bzr+ssh urls will not work. | ||
61 | 96 | If no public_url is provided, bzr will use the configured | ||
62 | 97 | public_url if there is one for the current branch, and | ||
63 | 98 | otherwise error. | ||
64 | 99 | |||
65 | 100 | example: | ||
66 | 101 | bzr register-branch http://foo.com/bzr/fooproject.mine \\ | ||
67 | 102 | --project fooproject | ||
68 | 103 | """ | ||
69 | 104 | takes_args = ['public_url?'] | ||
70 | 105 | takes_options = [ | ||
71 | 106 | Option('project', | ||
72 | 107 | 'Launchpad project short name to associate with the branch.', | ||
73 | 108 | unicode), | ||
74 | 109 | Option('product', | ||
75 | 110 | 'Launchpad product short name to associate with the branch.', | ||
76 | 111 | unicode, | ||
77 | 112 | hidden=True), | ||
78 | 113 | Option('branch-name', | ||
79 | 114 | 'Short name for the branch; ' | ||
80 | 115 | 'by default taken from the last component of the url.', | ||
81 | 116 | unicode), | ||
82 | 117 | Option('branch-title', | ||
83 | 118 | 'One-sentence description of the branch.', | ||
84 | 119 | unicode), | ||
85 | 120 | Option('branch-description', | ||
86 | 121 | 'Longer description of the purpose or contents of the branch.', | ||
87 | 122 | unicode), | ||
88 | 123 | Option('author', | ||
89 | 124 | "Branch author's email address, if not yourself.", | ||
90 | 125 | unicode), | ||
91 | 126 | Option('link-bug', | ||
92 | 127 | 'The bug this branch fixes.', | ||
93 | 128 | int), | ||
94 | 129 | Option('dry-run', | ||
95 | 130 | 'Prepare the request but don\'t actually send it.') | ||
96 | 131 | ] | ||
97 | 132 | |||
98 | 133 | |||
99 | 134 | def run(self, | ||
100 | 135 | public_url=None, | ||
101 | 136 | project='', | ||
102 | 137 | product=None, | ||
103 | 138 | branch_name='', | ||
104 | 139 | branch_title='', | ||
105 | 140 | branch_description='', | ||
106 | 141 | author='', | ||
107 | 142 | link_bug=None, | ||
108 | 143 | dry_run=False): | ||
109 | 144 | from bzrlib.plugins.launchpad.lp_registration import ( | ||
110 | 145 | BranchRegistrationRequest, BranchBugLinkRequest, | ||
111 | 146 | DryRunLaunchpadService, LaunchpadService) | ||
112 | 147 | if public_url is None: | ||
113 | 148 | try: | ||
114 | 149 | b = _mod_branch.Branch.open_containing('.')[0] | ||
115 | 150 | except NotBranchError: | ||
116 | 151 | raise BzrCommandError(gettext( | ||
117 | 152 | 'register-branch requires a public ' | ||
118 | 153 | 'branch url - see bzr help register-branch.')) | ||
119 | 154 | public_url = b.get_public_branch() | ||
120 | 155 | if public_url is None: | ||
121 | 156 | raise NoPublicBranch(b) | ||
122 | 157 | if product is not None: | ||
123 | 158 | project = product | ||
124 | 159 | trace.note(gettext( | ||
125 | 160 | '--product is deprecated; please use --project.')) | ||
126 | 161 | |||
127 | 162 | |||
128 | 163 | rego = BranchRegistrationRequest(branch_url=public_url, | ||
129 | 164 | branch_name=branch_name, | ||
130 | 165 | branch_title=branch_title, | ||
131 | 166 | branch_description=branch_description, | ||
132 | 167 | product_name=project, | ||
133 | 168 | author_email=author, | ||
134 | 169 | ) | ||
135 | 170 | linko = BranchBugLinkRequest(branch_url=public_url, | ||
136 | 171 | bug_id=link_bug) | ||
137 | 172 | if not dry_run: | ||
138 | 173 | service = LaunchpadService() | ||
139 | 174 | # This gives back the xmlrpc url that can be used for future | ||
140 | 175 | # operations on the branch. It's not so useful to print to the | ||
141 | 176 | # user since they can't do anything with it from a web browser; it | ||
142 | 177 | # might be nice for the server to tell us about an html url as | ||
143 | 178 | # well. | ||
144 | 179 | else: | ||
145 | 180 | # Run on service entirely in memory | ||
146 | 181 | service = DryRunLaunchpadService() | ||
147 | 182 | service.gather_user_credentials() | ||
148 | 183 | rego.submit(service) | ||
149 | 184 | if link_bug: | ||
150 | 185 | linko.submit(service) | ||
151 | 186 | print 'Branch registered.' | ||
152 | 187 | |||
153 | 188 | register_command(cmd_register_branch) | ||
154 | 189 | |||
155 | 190 | |||
156 | 191 | class cmd_launchpad_open(Command): | ||
157 | 192 | __doc__ = """Open a Launchpad branch page in your web browser.""" | ||
158 | 193 | |||
159 | 194 | aliases = ['lp-open'] | ||
160 | 195 | takes_options = [ | ||
161 | 196 | Option('dry-run', | ||
162 | 197 | 'Do not actually open the browser. Just say the URL we would ' | ||
163 | 198 | 'use.'), | ||
164 | 199 | ] | ||
165 | 200 | takes_args = ['location?'] | ||
166 | 201 | |||
167 | 202 | def _possible_locations(self, location): | ||
168 | 203 | """Yield possible external locations for the branch at 'location'.""" | ||
169 | 204 | yield location | ||
170 | 205 | try: | ||
171 | 206 | branch = _mod_branch.Branch.open_containing(location)[0] | ||
172 | 207 | except NotBranchError: | ||
173 | 208 | return | ||
174 | 209 | branch_url = branch.get_public_branch() | ||
175 | 210 | if branch_url is not None: | ||
176 | 211 | yield branch_url | ||
177 | 212 | branch_url = branch.get_push_location() | ||
178 | 213 | if branch_url is not None: | ||
179 | 214 | yield branch_url | ||
180 | 215 | |||
181 | 216 | def _get_web_url(self, service, location): | ||
182 | 217 | from bzrlib.plugins.launchpad.lp_registration import ( | ||
183 | 218 | NotLaunchpadBranch) | ||
184 | 219 | for branch_url in self._possible_locations(location): | ||
185 | 220 | try: | ||
186 | 221 | return service.get_web_url_from_branch_url(branch_url) | ||
187 | 222 | except (NotLaunchpadBranch, InvalidURL): | ||
188 | 223 | pass | ||
189 | 224 | raise NotLaunchpadBranch(branch_url) | ||
190 | 225 | |||
191 | 226 | def run(self, location=None, dry_run=False): | ||
192 | 227 | from bzrlib.plugins.launchpad.lp_registration import ( | ||
193 | 228 | LaunchpadService) | ||
194 | 229 | if location is None: | ||
195 | 230 | location = u'.' | ||
196 | 231 | web_url = self._get_web_url(LaunchpadService(), location) | ||
197 | 232 | trace.note(gettext('Opening %s in web browser') % web_url) | ||
198 | 233 | if not dry_run: | ||
199 | 234 | import webbrowser # this import should not be lazy | ||
200 | 235 | # otherwise bzr.exe lacks this module | ||
201 | 236 | webbrowser.open(web_url) | ||
202 | 237 | |||
203 | 238 | register_command(cmd_launchpad_open) | ||
204 | 239 | |||
205 | 240 | |||
206 | 241 | class cmd_launchpad_login(Command): | ||
207 | 242 | __doc__ = """Show or set the Launchpad user ID. | ||
208 | 243 | |||
209 | 244 | When communicating with Launchpad, some commands need to know your | ||
210 | 245 | Launchpad user ID. This command can be used to set or show the | ||
211 | 246 | user ID that Bazaar will use for such communication. | ||
212 | 247 | |||
213 | 248 | :Examples: | ||
214 | 249 | Show the Launchpad ID of the current user:: | ||
215 | 250 | |||
216 | 251 | bzr launchpad-login | ||
217 | 252 | |||
218 | 253 | Set the Launchpad ID of the current user to 'bob':: | ||
219 | 254 | |||
220 | 255 | bzr launchpad-login bob | ||
221 | 256 | """ | ||
222 | 257 | aliases = ['lp-login'] | ||
223 | 258 | takes_args = ['name?'] | ||
224 | 259 | takes_options = [ | ||
225 | 260 | 'verbose', | ||
226 | 261 | Option('no-check', | ||
227 | 262 | "Don't check that the user name is valid."), | ||
228 | 263 | ] | ||
229 | 264 | |||
230 | 265 | def run(self, name=None, no_check=False, verbose=False): | ||
231 | 266 | # This is totally separate from any launchpadlib login system. | ||
232 | 267 | from bzrlib.plugins.launchpad import account | ||
233 | 268 | check_account = not no_check | ||
234 | 269 | |||
235 | 270 | if name is None: | ||
236 | 271 | username = account.get_lp_login() | ||
237 | 272 | if username: | ||
238 | 273 | if check_account: | ||
239 | 274 | account.check_lp_login(username) | ||
240 | 275 | if verbose: | ||
241 | 276 | self.outf.write(gettext( | ||
242 | 277 | "Launchpad user ID exists and has SSH keys.\n")) | ||
243 | 278 | self.outf.write(username + '\n') | ||
244 | 279 | else: | ||
245 | 280 | self.outf.write(gettext('No Launchpad user ID configured.\n')) | ||
246 | 281 | return 1 | ||
247 | 282 | else: | ||
248 | 283 | name = name.lower() | ||
249 | 284 | if check_account: | ||
250 | 285 | account.check_lp_login(name) | ||
251 | 286 | if verbose: | ||
252 | 287 | self.outf.write(gettext( | ||
253 | 288 | "Launchpad user ID exists and has SSH keys.\n")) | ||
254 | 289 | account.set_lp_login(name) | ||
255 | 290 | if verbose: | ||
256 | 291 | self.outf.write(gettext("Launchpad user ID set to '%s'.\n") % | ||
257 | 292 | (name,)) | ||
258 | 293 | |||
259 | 294 | register_command(cmd_launchpad_login) | ||
260 | 295 | |||
261 | 296 | |||
262 | 297 | # XXX: cmd_launchpad_mirror is untested | ||
263 | 298 | class cmd_launchpad_mirror(Command): | ||
264 | 299 | __doc__ = """Ask Launchpad to mirror a branch now.""" | ||
265 | 300 | |||
266 | 301 | aliases = ['lp-mirror'] | ||
267 | 302 | takes_args = ['location?'] | ||
268 | 303 | |||
269 | 304 | def run(self, location='.'): | ||
270 | 305 | from bzrlib.plugins.launchpad import lp_api | ||
271 | 306 | from bzrlib.plugins.launchpad.lp_registration import LaunchpadService | ||
272 | 307 | branch, _ = _mod_branch.Branch.open_containing(location) | ||
273 | 308 | service = LaunchpadService() | ||
274 | 309 | launchpad = lp_api.login(service) | ||
275 | 310 | lp_branch = lp_api.LaunchpadBranch.from_bzr(launchpad, branch, | ||
276 | 311 | create_missing=False) | ||
277 | 312 | lp_branch.lp.requestMirror() | ||
278 | 313 | |||
279 | 314 | |||
280 | 315 | register_command(cmd_launchpad_mirror) | ||
281 | 316 | |||
282 | 317 | |||
283 | 318 | class cmd_lp_propose_merge(Command): | ||
284 | 319 | __doc__ = """Propose merging a branch on Launchpad. | ||
285 | 320 | |||
286 | 321 | This will open your usual editor to provide the initial comment. When it | ||
287 | 322 | has created the proposal, it will open it in your default web browser. | ||
288 | 323 | |||
289 | 324 | The branch will be proposed to merge into SUBMIT_BRANCH. If SUBMIT_BRANCH | ||
290 | 325 | is not supplied, the remembered submit branch will be used. If no submit | ||
291 | 326 | branch is remembered, the development focus will be used. | ||
292 | 327 | |||
293 | 328 | By default, the SUBMIT_BRANCH's review team will be requested to review | ||
294 | 329 | the merge proposal. This can be overriden by specifying --review (-R). | ||
295 | 330 | The parameter the launchpad account name of the desired reviewer. This | ||
296 | 331 | may optionally be followed by '=' and the review type. For example: | ||
297 | 332 | |||
298 | 333 | bzr lp-propose-merge --review jrandom --review review-team=qa | ||
299 | 334 | |||
300 | 335 | This will propose a merge, request "jrandom" to perform a review of | ||
301 | 336 | unspecified type, and request "review-team" to perform a "qa" review. | ||
302 | 337 | """ | ||
303 | 338 | |||
304 | 339 | takes_options = [Option('staging', | ||
305 | 340 | help='Propose the merge on staging.'), | ||
306 | 341 | Option('message', short_name='m', type=unicode, | ||
307 | 342 | help='Commit message.'), | ||
308 | 343 | Option('approve', | ||
309 | 344 | help='Mark the proposal as approved immediately.'), | ||
310 | 345 | Option('fixes', 'The bug this proposal fixes.', str), | ||
311 | 346 | ListOption('review', short_name='R', type=unicode, | ||
312 | 347 | help='Requested reviewer and optional type.')] | ||
313 | 348 | |||
314 | 349 | takes_args = ['submit_branch?'] | ||
315 | 350 | |||
316 | 351 | aliases = ['lp-submit', 'lp-propose'] | ||
317 | 352 | |||
318 | 353 | def run(self, submit_branch=None, review=None, staging=False, | ||
319 | 354 | message=None, approve=False, fixes=None): | ||
320 | 355 | from bzrlib.plugins.launchpad import lp_propose | ||
321 | 356 | tree, branch, relpath = controldir.ControlDir.open_containing_tree_or_branch( | ||
322 | 357 | '.') | ||
323 | 358 | if review is None: | ||
324 | 359 | reviews = None | ||
325 | 360 | else: | ||
326 | 361 | reviews = [] | ||
327 | 362 | for review in review: | ||
328 | 363 | if '=' in review: | ||
329 | 364 | reviews.append(review.split('=', 2)) | ||
330 | 365 | else: | ||
331 | 366 | reviews.append((review, '')) | ||
332 | 367 | if submit_branch is None: | ||
333 | 368 | submit_branch = branch.get_submit_branch() | ||
334 | 369 | if submit_branch is None: | ||
335 | 370 | target = None | ||
336 | 371 | else: | ||
337 | 372 | target = _mod_branch.Branch.open(submit_branch) | ||
338 | 373 | proposer = lp_propose.Proposer(tree, branch, target, message, | ||
339 | 374 | reviews, staging, approve=approve, | ||
340 | 375 | fixes=fixes) | ||
341 | 376 | proposer.check_proposal() | ||
342 | 377 | proposer.create_proposal() | ||
343 | 378 | |||
344 | 379 | |||
345 | 380 | register_command(cmd_lp_propose_merge) | ||
346 | 381 | |||
347 | 382 | |||
348 | 383 | class cmd_lp_find_proposal(Command): | ||
349 | 384 | |||
350 | 385 | __doc__ = """Find the proposal to merge this revision. | ||
351 | 386 | |||
352 | 387 | Finds the merge proposal(s) that discussed landing the specified revision. | ||
353 | 388 | This works only if the selected branch was the merge proposal target, and | ||
354 | 389 | if the merged_revno is recorded for the merge proposal. The proposal(s) | ||
355 | 390 | are opened in a web browser. | ||
356 | 391 | |||
357 | 392 | Any revision involved in the merge may be specified-- the revision in | ||
358 | 393 | which the merge was performed, or one of the revisions that was merged. | ||
359 | 394 | |||
360 | 395 | So, to find the merge proposal that reviewed line 1 of README:: | ||
361 | 396 | |||
362 | 397 | bzr lp-find-proposal -r annotate:README:1 | ||
363 | 398 | """ | ||
364 | 399 | |||
365 | 400 | takes_options = ['revision'] | ||
366 | 401 | |||
367 | 402 | def run(self, revision=None): | ||
368 | 403 | from bzrlib.plugins.launchpad import lp_api | ||
369 | 404 | import webbrowser | ||
370 | 405 | b = _mod_branch.Branch.open_containing('.')[0] | ||
371 | 406 | pb = ui.ui_factory.nested_progress_bar() | ||
372 | 407 | b.lock_read() | ||
373 | 408 | try: | ||
374 | 409 | revno = self._find_merged_revno(revision, b, pb) | ||
375 | 410 | merged = self._find_proposals(revno, b, pb) | ||
376 | 411 | if len(merged) == 0: | ||
377 | 412 | raise BzrCommandError(gettext('No review found.')) | ||
378 | 413 | trace.note(gettext('%d proposals(s) found.') % len(merged)) | ||
379 | 414 | for mp in merged: | ||
380 | 415 | webbrowser.open(lp_api.canonical_url(mp)) | ||
381 | 416 | finally: | ||
382 | 417 | b.unlock() | ||
383 | 418 | pb.finished() | ||
384 | 419 | |||
385 | 420 | def _find_merged_revno(self, revision, b, pb): | ||
386 | 421 | if revision is None: | ||
387 | 422 | return b.revno() | ||
388 | 423 | pb.update(gettext('Finding revision-id')) | ||
389 | 424 | revision_id = revision[0].as_revision_id(b) | ||
390 | 425 | # a revno spec is necessarily on the mainline. | ||
391 | 426 | if self._is_revno_spec(revision[0]): | ||
392 | 427 | merging_revision = revision_id | ||
393 | 428 | else: | ||
394 | 429 | graph = b.repository.get_graph() | ||
395 | 430 | pb.update(gettext('Finding merge')) | ||
396 | 431 | merging_revision = graph.find_lefthand_merger( | ||
397 | 432 | revision_id, b.last_revision()) | ||
398 | 433 | if merging_revision is None: | ||
399 | 434 | raise InvalidRevisionSpec(revision[0].user_spec, b) | ||
400 | 435 | pb.update(gettext('Finding revno')) | ||
401 | 436 | return b.revision_id_to_revno(merging_revision) | ||
402 | 437 | |||
403 | 438 | def _find_proposals(self, revno, b, pb): | ||
404 | 439 | launchpad = lp_api.login(lp_registration.LaunchpadService()) | ||
405 | 440 | pb.update(gettext('Finding Launchpad branch')) | ||
406 | 441 | lpb = lp_api.LaunchpadBranch.from_bzr(launchpad, b, | ||
407 | 442 | create_missing=False) | ||
408 | 443 | pb.update(gettext('Finding proposals')) | ||
409 | 444 | return list(lpb.lp.getMergeProposals(status=['Merged'], | ||
410 | 445 | merged_revnos=[revno])) | ||
411 | 446 | |||
412 | 447 | |||
413 | 448 | @staticmethod | ||
414 | 449 | def _is_revno_spec(spec): | ||
415 | 450 | try: | ||
416 | 451 | int(spec.user_spec) | ||
417 | 452 | except ValueError: | ||
418 | 453 | return False | ||
419 | 454 | else: | ||
420 | 455 | return True | ||
421 | 456 | |||
422 | 457 | |||
423 | 458 | register_command(cmd_lp_find_proposal) | ||
434 | 459 | 68 | ||
435 | 460 | 69 | ||
436 | 461 | def _register_directory(): | 70 | def _register_directory(): |
437 | 462 | 71 | ||
438 | === added file 'bzrlib/plugins/launchpad/cmds.py' | |||
439 | --- bzrlib/plugins/launchpad/cmds.py 1970-01-01 00:00:00 +0000 | |||
440 | +++ bzrlib/plugins/launchpad/cmds.py 2012-03-12 14:38:29 +0000 | |||
441 | @@ -0,0 +1,410 @@ | |||
442 | 1 | # Copyright (C) 2006-2012 Canonical Ltd | ||
443 | 2 | # | ||
444 | 3 | # This program is free software; you can redistribute it and/or modify | ||
445 | 4 | # it under the terms of the GNU General Public License as published by | ||
446 | 5 | # the Free Software Foundation; either version 2 of the License, or | ||
447 | 6 | # (at your option) any later version. | ||
448 | 7 | # | ||
449 | 8 | # This program is distributed in the hope that it will be useful, | ||
450 | 9 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
451 | 10 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
452 | 11 | # GNU General Public License for more details. | ||
453 | 12 | # | ||
454 | 13 | # You should have received a copy of the GNU General Public License | ||
455 | 14 | # along with this program; if not, write to the Free Software | ||
456 | 15 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
457 | 16 | |||
458 | 17 | """Launchpad plugin commands.""" | ||
459 | 18 | |||
460 | 19 | from __future__ import absolute_import | ||
461 | 20 | |||
462 | 21 | from bzrlib import ( | ||
463 | 22 | branch as _mod_branch, | ||
464 | 23 | controldir, | ||
465 | 24 | trace, | ||
466 | 25 | ) | ||
467 | 26 | from bzrlib.commands import ( | ||
468 | 27 | Command, | ||
469 | 28 | ) | ||
470 | 29 | from bzrlib.errors import ( | ||
471 | 30 | BzrCommandError, | ||
472 | 31 | InvalidRevisionSpec, | ||
473 | 32 | InvalidURL, | ||
474 | 33 | NoPublicBranch, | ||
475 | 34 | NotBranchError, | ||
476 | 35 | ) | ||
477 | 36 | from bzrlib.i18n import gettext | ||
478 | 37 | from bzrlib.option import ( | ||
479 | 38 | Option, | ||
480 | 39 | ListOption, | ||
481 | 40 | ) | ||
482 | 41 | |||
483 | 42 | |||
484 | 43 | class cmd_register_branch(Command): | ||
485 | 44 | __doc__ = """Register a branch with launchpad.net. | ||
486 | 45 | |||
487 | 46 | This command lists a bzr branch in the directory of branches on | ||
488 | 47 | launchpad.net. Registration allows the branch to be associated with | ||
489 | 48 | bugs or specifications. | ||
490 | 49 | |||
491 | 50 | Before using this command you must register the project to which the | ||
492 | 51 | branch belongs, and create an account for yourself on launchpad.net. | ||
493 | 52 | |||
494 | 53 | arguments: | ||
495 | 54 | public_url: The publicly visible url for the branch to register. | ||
496 | 55 | This must be an http or https url (which Launchpad can read | ||
497 | 56 | from to access the branch). Local file urls, SFTP urls, and | ||
498 | 57 | bzr+ssh urls will not work. | ||
499 | 58 | If no public_url is provided, bzr will use the configured | ||
500 | 59 | public_url if there is one for the current branch, and | ||
501 | 60 | otherwise error. | ||
502 | 61 | |||
503 | 62 | example: | ||
504 | 63 | bzr register-branch http://foo.com/bzr/fooproject.mine \\ | ||
505 | 64 | --project fooproject | ||
506 | 65 | """ | ||
507 | 66 | takes_args = ['public_url?'] | ||
508 | 67 | takes_options = [ | ||
509 | 68 | Option('project', | ||
510 | 69 | 'Launchpad project short name to associate with the branch.', | ||
511 | 70 | unicode), | ||
512 | 71 | Option('product', | ||
513 | 72 | 'Launchpad product short name to associate with the branch.', | ||
514 | 73 | unicode, | ||
515 | 74 | hidden=True), | ||
516 | 75 | Option('branch-name', | ||
517 | 76 | 'Short name for the branch; ' | ||
518 | 77 | 'by default taken from the last component of the url.', | ||
519 | 78 | unicode), | ||
520 | 79 | Option('branch-title', | ||
521 | 80 | 'One-sentence description of the branch.', | ||
522 | 81 | unicode), | ||
523 | 82 | Option('branch-description', | ||
524 | 83 | 'Longer description of the purpose or contents of the branch.', | ||
525 | 84 | unicode), | ||
526 | 85 | Option('author', | ||
527 | 86 | "Branch author's email address, if not yourself.", | ||
528 | 87 | unicode), | ||
529 | 88 | Option('link-bug', | ||
530 | 89 | 'The bug this branch fixes.', | ||
531 | 90 | int), | ||
532 | 91 | Option('dry-run', | ||
533 | 92 | 'Prepare the request but don\'t actually send it.') | ||
534 | 93 | ] | ||
535 | 94 | |||
536 | 95 | |||
537 | 96 | def run(self, | ||
538 | 97 | public_url=None, | ||
539 | 98 | project='', | ||
540 | 99 | product=None, | ||
541 | 100 | branch_name='', | ||
542 | 101 | branch_title='', | ||
543 | 102 | branch_description='', | ||
544 | 103 | author='', | ||
545 | 104 | link_bug=None, | ||
546 | 105 | dry_run=False): | ||
547 | 106 | from bzrlib.plugins.launchpad.lp_registration import ( | ||
548 | 107 | BranchRegistrationRequest, BranchBugLinkRequest, | ||
549 | 108 | DryRunLaunchpadService, LaunchpadService) | ||
550 | 109 | if public_url is None: | ||
551 | 110 | try: | ||
552 | 111 | b = _mod_branch.Branch.open_containing('.')[0] | ||
553 | 112 | except NotBranchError: | ||
554 | 113 | raise BzrCommandError(gettext( | ||
555 | 114 | 'register-branch requires a public ' | ||
556 | 115 | 'branch url - see bzr help register-branch.')) | ||
557 | 116 | public_url = b.get_public_branch() | ||
558 | 117 | if public_url is None: | ||
559 | 118 | raise NoPublicBranch(b) | ||
560 | 119 | if product is not None: | ||
561 | 120 | project = product | ||
562 | 121 | trace.note(gettext( | ||
563 | 122 | '--product is deprecated; please use --project.')) | ||
564 | 123 | |||
565 | 124 | |||
566 | 125 | rego = BranchRegistrationRequest(branch_url=public_url, | ||
567 | 126 | branch_name=branch_name, | ||
568 | 127 | branch_title=branch_title, | ||
569 | 128 | branch_description=branch_description, | ||
570 | 129 | product_name=project, | ||
571 | 130 | author_email=author, | ||
572 | 131 | ) | ||
573 | 132 | linko = BranchBugLinkRequest(branch_url=public_url, | ||
574 | 133 | bug_id=link_bug) | ||
575 | 134 | if not dry_run: | ||
576 | 135 | service = LaunchpadService() | ||
577 | 136 | # This gives back the xmlrpc url that can be used for future | ||
578 | 137 | # operations on the branch. It's not so useful to print to the | ||
579 | 138 | # user since they can't do anything with it from a web browser; it | ||
580 | 139 | # might be nice for the server to tell us about an html url as | ||
581 | 140 | # well. | ||
582 | 141 | else: | ||
583 | 142 | # Run on service entirely in memory | ||
584 | 143 | service = DryRunLaunchpadService() | ||
585 | 144 | service.gather_user_credentials() | ||
586 | 145 | rego.submit(service) | ||
587 | 146 | if link_bug: | ||
588 | 147 | linko.submit(service) | ||
589 | 148 | self.outf.write('Branch registered.\n') | ||
590 | 149 | |||
591 | 150 | |||
592 | 151 | class cmd_launchpad_open(Command): | ||
593 | 152 | __doc__ = """Open a Launchpad branch page in your web browser.""" | ||
594 | 153 | |||
595 | 154 | aliases = ['lp-open'] | ||
596 | 155 | takes_options = [ | ||
597 | 156 | Option('dry-run', | ||
598 | 157 | 'Do not actually open the browser. Just say the URL we would ' | ||
599 | 158 | 'use.'), | ||
600 | 159 | ] | ||
601 | 160 | takes_args = ['location?'] | ||
602 | 161 | |||
603 | 162 | def _possible_locations(self, location): | ||
604 | 163 | """Yield possible external locations for the branch at 'location'.""" | ||
605 | 164 | yield location | ||
606 | 165 | try: | ||
607 | 166 | branch = _mod_branch.Branch.open_containing(location)[0] | ||
608 | 167 | except NotBranchError: | ||
609 | 168 | return | ||
610 | 169 | branch_url = branch.get_public_branch() | ||
611 | 170 | if branch_url is not None: | ||
612 | 171 | yield branch_url | ||
613 | 172 | branch_url = branch.get_push_location() | ||
614 | 173 | if branch_url is not None: | ||
615 | 174 | yield branch_url | ||
616 | 175 | |||
617 | 176 | def _get_web_url(self, service, location): | ||
618 | 177 | from bzrlib.plugins.launchpad.lp_registration import ( | ||
619 | 178 | NotLaunchpadBranch) | ||
620 | 179 | for branch_url in self._possible_locations(location): | ||
621 | 180 | try: | ||
622 | 181 | return service.get_web_url_from_branch_url(branch_url) | ||
623 | 182 | except (NotLaunchpadBranch, InvalidURL): | ||
624 | 183 | pass | ||
625 | 184 | raise NotLaunchpadBranch(branch_url) | ||
626 | 185 | |||
627 | 186 | def run(self, location=None, dry_run=False): | ||
628 | 187 | from bzrlib.plugins.launchpad.lp_registration import ( | ||
629 | 188 | LaunchpadService) | ||
630 | 189 | if location is None: | ||
631 | 190 | location = u'.' | ||
632 | 191 | web_url = self._get_web_url(LaunchpadService(), location) | ||
633 | 192 | trace.note(gettext('Opening %s in web browser') % web_url) | ||
634 | 193 | if not dry_run: | ||
635 | 194 | import webbrowser # this import should not be lazy | ||
636 | 195 | # otherwise bzr.exe lacks this module | ||
637 | 196 | webbrowser.open(web_url) | ||
638 | 197 | |||
639 | 198 | |||
640 | 199 | class cmd_launchpad_login(Command): | ||
641 | 200 | __doc__ = """Show or set the Launchpad user ID. | ||
642 | 201 | |||
643 | 202 | When communicating with Launchpad, some commands need to know your | ||
644 | 203 | Launchpad user ID. This command can be used to set or show the | ||
645 | 204 | user ID that Bazaar will use for such communication. | ||
646 | 205 | |||
647 | 206 | :Examples: | ||
648 | 207 | Show the Launchpad ID of the current user:: | ||
649 | 208 | |||
650 | 209 | bzr launchpad-login | ||
651 | 210 | |||
652 | 211 | Set the Launchpad ID of the current user to 'bob':: | ||
653 | 212 | |||
654 | 213 | bzr launchpad-login bob | ||
655 | 214 | """ | ||
656 | 215 | aliases = ['lp-login'] | ||
657 | 216 | takes_args = ['name?'] | ||
658 | 217 | takes_options = [ | ||
659 | 218 | 'verbose', | ||
660 | 219 | Option('no-check', | ||
661 | 220 | "Don't check that the user name is valid."), | ||
662 | 221 | ] | ||
663 | 222 | |||
664 | 223 | def run(self, name=None, no_check=False, verbose=False): | ||
665 | 224 | # This is totally separate from any launchpadlib login system. | ||
666 | 225 | from bzrlib.plugins.launchpad import account | ||
667 | 226 | check_account = not no_check | ||
668 | 227 | |||
669 | 228 | if name is None: | ||
670 | 229 | username = account.get_lp_login() | ||
671 | 230 | if username: | ||
672 | 231 | if check_account: | ||
673 | 232 | account.check_lp_login(username) | ||
674 | 233 | if verbose: | ||
675 | 234 | self.outf.write(gettext( | ||
676 | 235 | "Launchpad user ID exists and has SSH keys.\n")) | ||
677 | 236 | self.outf.write(username + '\n') | ||
678 | 237 | else: | ||
679 | 238 | self.outf.write(gettext('No Launchpad user ID configured.\n')) | ||
680 | 239 | return 1 | ||
681 | 240 | else: | ||
682 | 241 | name = name.lower() | ||
683 | 242 | if check_account: | ||
684 | 243 | account.check_lp_login(name) | ||
685 | 244 | if verbose: | ||
686 | 245 | self.outf.write(gettext( | ||
687 | 246 | "Launchpad user ID exists and has SSH keys.\n")) | ||
688 | 247 | account.set_lp_login(name) | ||
689 | 248 | if verbose: | ||
690 | 249 | self.outf.write(gettext("Launchpad user ID set to '%s'.\n") % | ||
691 | 250 | (name,)) | ||
692 | 251 | |||
693 | 252 | |||
694 | 253 | # XXX: cmd_launchpad_mirror is untested | ||
695 | 254 | class cmd_launchpad_mirror(Command): | ||
696 | 255 | __doc__ = """Ask Launchpad to mirror a branch now.""" | ||
697 | 256 | |||
698 | 257 | aliases = ['lp-mirror'] | ||
699 | 258 | takes_args = ['location?'] | ||
700 | 259 | |||
701 | 260 | def run(self, location='.'): | ||
702 | 261 | from bzrlib.plugins.launchpad import lp_api | ||
703 | 262 | from bzrlib.plugins.launchpad.lp_registration import LaunchpadService | ||
704 | 263 | branch, _ = _mod_branch.Branch.open_containing(location) | ||
705 | 264 | service = LaunchpadService() | ||
706 | 265 | launchpad = lp_api.login(service) | ||
707 | 266 | lp_branch = lp_api.LaunchpadBranch.from_bzr(launchpad, branch, | ||
708 | 267 | create_missing=False) | ||
709 | 268 | lp_branch.lp.requestMirror() | ||
710 | 269 | |||
711 | 270 | |||
712 | 271 | class cmd_lp_propose_merge(Command): | ||
713 | 272 | __doc__ = """Propose merging a branch on Launchpad. | ||
714 | 273 | |||
715 | 274 | This will open your usual editor to provide the initial comment. When it | ||
716 | 275 | has created the proposal, it will open it in your default web browser. | ||
717 | 276 | |||
718 | 277 | The branch will be proposed to merge into SUBMIT_BRANCH. If SUBMIT_BRANCH | ||
719 | 278 | is not supplied, the remembered submit branch will be used. If no submit | ||
720 | 279 | branch is remembered, the development focus will be used. | ||
721 | 280 | |||
722 | 281 | By default, the SUBMIT_BRANCH's review team will be requested to review | ||
723 | 282 | the merge proposal. This can be overriden by specifying --review (-R). | ||
724 | 283 | The parameter the launchpad account name of the desired reviewer. This | ||
725 | 284 | may optionally be followed by '=' and the review type. For example: | ||
726 | 285 | |||
727 | 286 | bzr lp-propose-merge --review jrandom --review review-team=qa | ||
728 | 287 | |||
729 | 288 | This will propose a merge, request "jrandom" to perform a review of | ||
730 | 289 | unspecified type, and request "review-team" to perform a "qa" review. | ||
731 | 290 | """ | ||
732 | 291 | |||
733 | 292 | takes_options = [Option('staging', | ||
734 | 293 | help='Propose the merge on staging.'), | ||
735 | 294 | Option('message', short_name='m', type=unicode, | ||
736 | 295 | help='Commit message.'), | ||
737 | 296 | Option('approve', | ||
738 | 297 | help='Mark the proposal as approved immediately.'), | ||
739 | 298 | Option('fixes', 'The bug this proposal fixes.', str), | ||
740 | 299 | ListOption('review', short_name='R', type=unicode, | ||
741 | 300 | help='Requested reviewer and optional type.')] | ||
742 | 301 | |||
743 | 302 | takes_args = ['submit_branch?'] | ||
744 | 303 | |||
745 | 304 | aliases = ['lp-submit', 'lp-propose'] | ||
746 | 305 | |||
747 | 306 | def run(self, submit_branch=None, review=None, staging=False, | ||
748 | 307 | message=None, approve=False, fixes=None): | ||
749 | 308 | from bzrlib.plugins.launchpad import lp_propose | ||
750 | 309 | tree, branch, relpath = controldir.ControlDir.open_containing_tree_or_branch( | ||
751 | 310 | '.') | ||
752 | 311 | if review is None: | ||
753 | 312 | reviews = None | ||
754 | 313 | else: | ||
755 | 314 | reviews = [] | ||
756 | 315 | for review in review: | ||
757 | 316 | if '=' in review: | ||
758 | 317 | reviews.append(review.split('=', 2)) | ||
759 | 318 | else: | ||
760 | 319 | reviews.append((review, '')) | ||
761 | 320 | if submit_branch is None: | ||
762 | 321 | submit_branch = branch.get_submit_branch() | ||
763 | 322 | if submit_branch is None: | ||
764 | 323 | target = None | ||
765 | 324 | else: | ||
766 | 325 | target = _mod_branch.Branch.open(submit_branch) | ||
767 | 326 | proposer = lp_propose.Proposer(tree, branch, target, message, | ||
768 | 327 | reviews, staging, approve=approve, | ||
769 | 328 | fixes=fixes) | ||
770 | 329 | proposer.check_proposal() | ||
771 | 330 | proposer.create_proposal() | ||
772 | 331 | |||
773 | 332 | |||
774 | 333 | class cmd_lp_find_proposal(Command): | ||
775 | 334 | |||
776 | 335 | __doc__ = """Find the proposal to merge this revision. | ||
777 | 336 | |||
778 | 337 | Finds the merge proposal(s) that discussed landing the specified revision. | ||
779 | 338 | This works only if the selected branch was the merge proposal target, and | ||
780 | 339 | if the merged_revno is recorded for the merge proposal. The proposal(s) | ||
781 | 340 | are opened in a web browser. | ||
782 | 341 | |||
783 | 342 | Any revision involved in the merge may be specified-- the revision in | ||
784 | 343 | which the merge was performed, or one of the revisions that was merged. | ||
785 | 344 | |||
786 | 345 | So, to find the merge proposal that reviewed line 1 of README:: | ||
787 | 346 | |||
788 | 347 | bzr lp-find-proposal -r annotate:README:1 | ||
789 | 348 | """ | ||
790 | 349 | |||
791 | 350 | takes_options = ['revision'] | ||
792 | 351 | |||
793 | 352 | def run(self, revision=None): | ||
794 | 353 | from bzrlib import ui | ||
795 | 354 | from bzrlib.plugins.launchpad import lp_api | ||
796 | 355 | import webbrowser | ||
797 | 356 | b = _mod_branch.Branch.open_containing('.')[0] | ||
798 | 357 | pb = ui.ui_factory.nested_progress_bar() | ||
799 | 358 | b.lock_read() | ||
800 | 359 | try: | ||
801 | 360 | revno = self._find_merged_revno(revision, b, pb) | ||
802 | 361 | merged = self._find_proposals(revno, b, pb) | ||
803 | 362 | if len(merged) == 0: | ||
804 | 363 | raise BzrCommandError(gettext('No review found.')) | ||
805 | 364 | trace.note(gettext('%d proposals(s) found.') % len(merged)) | ||
806 | 365 | for mp in merged: | ||
807 | 366 | webbrowser.open(lp_api.canonical_url(mp)) | ||
808 | 367 | finally: | ||
809 | 368 | b.unlock() | ||
810 | 369 | pb.finished() | ||
811 | 370 | |||
812 | 371 | def _find_merged_revno(self, revision, b, pb): | ||
813 | 372 | if revision is None: | ||
814 | 373 | return b.revno() | ||
815 | 374 | pb.update(gettext('Finding revision-id')) | ||
816 | 375 | revision_id = revision[0].as_revision_id(b) | ||
817 | 376 | # a revno spec is necessarily on the mainline. | ||
818 | 377 | if self._is_revno_spec(revision[0]): | ||
819 | 378 | merging_revision = revision_id | ||
820 | 379 | else: | ||
821 | 380 | graph = b.repository.get_graph() | ||
822 | 381 | pb.update(gettext('Finding merge')) | ||
823 | 382 | merging_revision = graph.find_lefthand_merger( | ||
824 | 383 | revision_id, b.last_revision()) | ||
825 | 384 | if merging_revision is None: | ||
826 | 385 | raise InvalidRevisionSpec(revision[0].user_spec, b) | ||
827 | 386 | pb.update(gettext('Finding revno')) | ||
828 | 387 | return b.revision_id_to_revno(merging_revision) | ||
829 | 388 | |||
830 | 389 | def _find_proposals(self, revno, b, pb): | ||
831 | 390 | from bzrlib.plugins.launchpad import (lp_api, lp_registration) | ||
832 | 391 | launchpad = lp_api.login(lp_registration.LaunchpadService()) | ||
833 | 392 | pb.update(gettext('Finding Launchpad branch')) | ||
834 | 393 | lpb = lp_api.LaunchpadBranch.from_bzr(launchpad, b, | ||
835 | 394 | create_missing=False) | ||
836 | 395 | pb.update(gettext('Finding proposals')) | ||
837 | 396 | return list(lpb.lp.getMergeProposals(status=['Merged'], | ||
838 | 397 | merged_revnos=[revno])) | ||
839 | 398 | |||
840 | 399 | |||
841 | 400 | @staticmethod | ||
842 | 401 | def _is_revno_spec(spec): | ||
843 | 402 | try: | ||
844 | 403 | int(spec.user_spec) | ||
845 | 404 | except ValueError: | ||
846 | 405 | return False | ||
847 | 406 | else: | ||
848 | 407 | return True | ||
849 | 408 | |||
850 | 409 | |||
851 | 410 |
Looks fine, though unlikely to have a big impact.
Seems like _register_hooks should be replaced with a install_ lazy_named_ hook call, which would mean the _mod_branch import could go as well?
# Since we are a built-in plugin we share the bzrlib version
+ trace,
version_info,
Addition split the comment from the version_info line it's talking about. Mixing up module and object imports is a bit ick, but that's python.