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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
|
# Copyright 2009 Canonical Ltd. This software is licensed under the
# GNU Affero General Public License version 3 (see the file LICENSE).
"""Branch targets."""
__metaclass__ = type
__all__ = [
'branch_to_target',
'PackageBranchTarget',
'PersonBranchTarget',
'ProductBranchTarget',
]
from operator import attrgetter
from zope.component import getUtility
from zope.interface import implements
from zope.security.proxy import isinstance as zope_isinstance
from lp.code.errors import NoLinkedBranch
from lp.code.interfaces.branchcollection import IAllBranches
from lp.code.interfaces.branchtarget import (
check_default_stacked_on,
IBranchTarget,
)
from lp.code.interfaces.codeimport import ICodeImportSet
from lp.code.interfaces.linkedbranch import get_linked_to_branch
from lp.registry.interfaces.pocket import PackagePublishingPocket
from lp.registry.interfaces.series import SeriesStatus
from lp.services.webapp.authorization import check_permission
from lp.services.webapp.interfaces import ICanonicalUrlData
from lp.services.webapp.sorting import sorted_version_numbers
def branch_to_target(branch):
"""Adapt an IBranch to an IBranchTarget."""
return branch.target
class _BaseBranchTarget:
def __eq__(self, other):
return self.context == other.context
def __ne__(self, other):
return self.context != other.context
def newCodeImport(self, registrant, branch_name, rcs_type, url=None,
cvs_root=None, cvs_module=None, owner=None):
"""See `IBranchTarget`."""
return getUtility(ICodeImportSet).new(
registrant, self, branch_name, rcs_type, url=url,
cvs_root=cvs_root, cvs_module=cvs_module, owner=owner)
def getRelatedSeriesBranchInfo(self, parent_branch, limit_results=None):
"""See `IBranchTarget`."""
return []
def getRelatedPackageBranchInfo(self, parent_branch, limit_results=None):
"""See `IBranchTarget`."""
return []
class PackageBranchTarget(_BaseBranchTarget):
implements(IBranchTarget)
def __init__(self, sourcepackage):
self.sourcepackage = sourcepackage
@property
def name(self):
"""See `IBranchTarget`."""
return self.sourcepackage.path
@property
def components(self):
"""See `IBranchTarget`."""
return [
self.sourcepackage.distribution,
self.sourcepackage.distroseries,
self.sourcepackage,
]
@property
def context(self):
"""See `IBranchTarget`."""
return self.sourcepackage
def getNamespace(self, owner):
"""See `IBranchTarget`."""
from lp.code.model.branchnamespace import (
PackageNamespace)
return PackageNamespace(owner, self.sourcepackage)
@property
def collection(self):
"""See `IBranchTarget`."""
return getUtility(IAllBranches).inSourcePackage(self.sourcepackage)
@property
def default_stacked_on_branch(self):
"""See `IBranchTarget`."""
return check_default_stacked_on(
self.sourcepackage.development_version.getBranch(
PackagePublishingPocket.RELEASE))
@property
def default_merge_target(self):
"""See `IBranchTarget`."""
return self.sourcepackage.getBranch(PackagePublishingPocket.RELEASE)
@property
def displayname(self):
"""See `IBranchTarget`."""
return self.sourcepackage.displayname
@property
def supports_merge_proposals(self):
"""See `IBranchTarget`."""
return True
@property
def supports_short_identites(self):
"""See `IBranchTarget`."""
return True
@property
def supports_code_imports(self):
"""See `IBranchTarget`."""
return True
def areBranchesMergeable(self, other_target):
"""See `IBranchTarget`."""
# Branches are mergable into a PackageTarget if the source package
# name is the same, or the branch is associated with the linked
# product.
if zope_isinstance(other_target, PackageBranchTarget):
my_sourcepackagename = self.context.sourcepackagename
other_sourcepackagename = other_target.context.sourcepackagename
return my_sourcepackagename == other_sourcepackagename
elif zope_isinstance(other_target, ProductBranchTarget):
# If the sourcepackage has a related product, then branches of
# that product are mergeable.
product_series = self.sourcepackage.productseries
if product_series is None:
return False
else:
return other_target.context == product_series.product
else:
return False
def assignKarma(self, person, action_name, date_created=None):
"""See `IBranchTarget`."""
return person.assignKarma(
action_name,
distribution=self.context.distribution,
sourcepackagename=self.context.sourcepackagename,
datecreated=date_created)
def getBugTask(self, bug):
"""See `IBranchTarget`."""
# XXX: rockstar - See bug 397251. Basically, source packages may have
# specific bug tasks. This should return those specific bugtasks in
# those cases.
return bug.default_bugtask
def _retargetBranch(self, branch):
"""Set the branch target to refer to this target.
This only updates the target related attributes of the branch, and
expects a branch without a security proxy as a parameter.
"""
branch.product = None
branch.distroseries = self.sourcepackage.distroseries
branch.sourcepackagename = self.sourcepackage.sourcepackagename
def getRelatedPackageBranchInfo(self, parent_branch, limit_results=None):
"""See `IBranchTarget`."""
result = []
linked_branches = self.sourcepackage.linkedBranches()
distroseries = self.sourcepackage.distroseries
result.extend(
[(branch, distroseries)
for branch in linked_branches.values()
if (check_permission('launchpad.View', branch) and
branch != parent_branch and
distroseries.status != SeriesStatus.OBSOLETE)])
result = sorted_version_numbers(result, key=lambda branch_info: (
getattr(branch_info[1], 'name')))
if limit_results is not None:
# We only want the most recent branches
result = result[:limit_results]
return result
class PersonBranchTarget(_BaseBranchTarget):
implements(IBranchTarget)
name = u'+junk'
default_stacked_on_branch = None
default_merge_target = None
def __init__(self, person):
self.person = person
@property
def components(self):
"""See `IBranchTarget`."""
return [self.person]
@property
def context(self):
"""See `IBranchTarget`."""
return self.person
@property
def displayname(self):
"""See `IBranchTarget`."""
return "~%s/+junk" % self.person.name
def getNamespace(self, owner):
"""See `IBranchTarget`."""
from lp.code.model.branchnamespace import (
PersonalNamespace)
return PersonalNamespace(owner)
@property
def collection(self):
"""See `IBranchTarget`."""
return getUtility(IAllBranches).ownedBy(self.person).isJunk()
@property
def supports_merge_proposals(self):
"""See `IBranchTarget`."""
return False
@property
def supports_short_identites(self):
"""See `IBranchTarget`."""
return False
@property
def supports_code_imports(self):
"""See `IBranchTarget`."""
return False
def areBranchesMergeable(self, other_target):
"""See `IBranchTarget`."""
return False
def assignKarma(self, person, action_name, date_created=None):
"""See `IBranchTarget`."""
# Does nothing. No karma for +junk.
return None
def getBugTask(self, bug):
"""See `IBranchTarget`."""
return bug.default_bugtask
def _retargetBranch(self, branch):
"""Set the branch target to refer to this target.
This only updates the target related attributes of the branch, and
expects a branch without a security proxy as a parameter.
"""
branch.product = None
branch.distroseries = None
branch.sourcepackagename = None
class ProductBranchTarget(_BaseBranchTarget):
implements(IBranchTarget)
def __init__(self, product):
self.product = product
@property
def components(self):
"""See `IBranchTarget`."""
return [self.product]
@property
def context(self):
"""See `IBranchTarget`."""
return self.product
@property
def displayname(self):
"""See `IBranchTarget`."""
return self.product.displayname
@property
def name(self):
"""See `IBranchTarget`."""
return self.product.name
@property
def default_stacked_on_branch(self):
"""See `IBranchTarget`."""
return check_default_stacked_on(self.product.development_focus.branch)
@property
def default_merge_target(self):
"""See `IBranchTarget`."""
return self.product.development_focus.branch
def getNamespace(self, owner):
"""See `IBranchTarget`."""
from lp.code.model.branchnamespace import (
ProductNamespace)
return ProductNamespace(owner, self.product)
@property
def collection(self):
"""See `IBranchTarget`."""
return getUtility(IAllBranches).inProduct(self.product)
@property
def supports_merge_proposals(self):
"""See `IBranchTarget`."""
return True
@property
def supports_short_identites(self):
"""See `IBranchTarget`."""
return True
@property
def supports_code_imports(self):
"""See `IBranchTarget`."""
return True
def areBranchesMergeable(self, other_target):
"""See `IBranchTarget`."""
# Branches are mergable into a PackageTarget if the source package
# name is the same, or the branch is associated with the linked
# product.
if zope_isinstance(other_target, ProductBranchTarget):
return self.product == other_target.context
elif zope_isinstance(other_target, PackageBranchTarget):
# If the sourcepackage has a related product, and that product is
# the same as ours, then the branches are mergeable.
product_series = other_target.context.productseries
if product_series is None:
return False
else:
return self.product == product_series.product
else:
return False
def assignKarma(self, person, action_name, date_created=None):
"""See `IBranchTarget`."""
return person.assignKarma(
action_name, product=self.product, datecreated=date_created)
def getBugTask(self, bug):
"""See `IBranchTarget`."""
task = bug.getBugTask(self.product)
if task is None:
# Just choose the first task for the bug.
task = bug.bugtasks[0]
return task
def _retargetBranch(self, branch):
"""Set the branch target to refer to this target.
This only updates the target related attributes of the branch, and
expects a branch without a security proxy as a parameter.
"""
branch.product = self.product
branch.distroseries = None
branch.sourcepackagename = None
def getRelatedSeriesBranchInfo(self, parent_branch, limit_results=None):
"""See `IBranchTarget`."""
sorted_series = []
for series in self.product.series:
if (series.status != SeriesStatus.OBSOLETE
and series != self.product.development_focus):
sorted_series.append(series)
# Now sort the list by name with newer versions before older.
sorted_series = sorted_version_numbers(sorted_series,
key=attrgetter('name'))
# Add the development focus first.
sorted_series.insert(0, self.product.development_focus)
result = []
for series in sorted_series:
try:
branch = get_linked_to_branch(series).branch
if (branch not in result and branch != parent_branch and
check_permission('launchpad.View', branch)):
result.append((branch, series))
except NoLinkedBranch:
# If there's no branch for a particular series, we don't care.
pass
if limit_results is not None:
# We only want the most recent branches
result = result[:limit_results]
return result
def getRelatedPackageBranchInfo(self, parent_branch, limit_results=None):
"""See `IBranchTarget`."""
result = []
for distrosourcepackage in self.product.distrosourcepackages:
try:
branch = get_linked_to_branch(distrosourcepackage).branch
series = distrosourcepackage.distribution.currentseries
if (branch != parent_branch and series is not None and
check_permission('launchpad.View', branch)):
result.append((branch, series))
except NoLinkedBranch:
# If there's no branch for a particular source package,
# we don't care.
pass
result = sorted_version_numbers(result, key=lambda branch_info: (
getattr(branch_info[1], 'name')))
if limit_results is not None:
# We only want the most recent branches
result = result[:limit_results]
return result
def get_canonical_url_data_for_target(branch_target):
"""Return the `ICanonicalUrlData` for an `IBranchTarget`."""
return ICanonicalUrlData(branch_target.context)
def product_series_to_branch_target(product_series):
"""The Product itself is the branch target given a ProductSeries."""
return ProductBranchTarget(product_series.product)
def distribution_sourcepackage_to_branch_target(distro_sourcepackage):
"""The development version of the distro sourcepackage is the target."""
dev_version = distro_sourcepackage.development_version
# It is possible for distributions to not have any series, and if that is
# the case, the dev_version is None.
if dev_version is None:
return None
return PackageBranchTarget(dev_version)
|