~launchpad-pqm/launchpad/devel

8687.15.17 by Karl Fogel
Add the copyright header block to the rest of the files under lib/lp/.
1
# Copyright 2009 Canonical Ltd.  This software is licensed under the
2
# GNU Affero General Public License version 3 (see the file LICENSE).
7334.1.1 by Muharem Hrnjadovic
imported from dead branch
3
4
__metaclass__ = type
5
__all__ = ['PackageCopyRequest', 'PackageCopyRequestSet']
6
7334.1.2 by Muharem Hrnjadovic
content class now defined using storm constructs
7
import itertools
7334.1.1 by Muharem Hrnjadovic
imported from dead branch
8
11403.1.4 by Henning Eggers
Reformatted imports using format-imports script r32.
9
from storm.locals import (
10
    Bool,
11
    DateTime,
12
    Enum,
13
    Int,
14
    Reference,
15
    Storm,
16
    Unicode,
17
    )
7334.1.1 by Muharem Hrnjadovic
imported from dead branch
18
from zope.component import getUtility
19
from zope.interface import implements
20
14606.3.1 by William Grant
Merge canonical.database into lp.services.database.
21
from lp.registry.interfaces.person import validate_public_person
22
from lp.registry.interfaces.pocket import PackagePublishingPocket
23
from lp.services.database.constants import UTC_NOW
14600.2.2 by Curtis Hovey
Moved webapp to lp.services.
24
from lp.services.webapp.interfaces import (
11403.1.4 by Henning Eggers
Reformatted imports using format-imports script r32.
25
    DEFAULT_FLAVOR,
26
    IStoreSelector,
27
    MAIN_STORE,
28
    )
11411.6.7 by Julian Edwards
Move PackageCopyStatus
29
from lp.soyuz.enums import PackageCopyStatus
8294.6.1 by Julian Edwards
First stab at code-reorg. Still got a discrepancy on stuff I assigned to registry but not migrated yet.
30
from lp.soyuz.interfaces.packagecopyrequest import (
11403.1.4 by Henning Eggers
Reformatted imports using format-imports script r32.
31
    IPackageCopyRequest,
32
    IPackageCopyRequestSet,
33
    )
7334.1.2 by Muharem Hrnjadovic
content class now defined using storm constructs
34
35
36
def _construct_enum_mapping(db_item_cls):
7334.1.4 by Muharem Hrnjadovic
Brad's review comments
37
    """Helper function, constructs DBItem to storm enumeration mappings."""
7334.1.6 by Muharem Hrnjadovic
minor improvement
38
    return dict(zip(db_item_cls.items, itertools.count(1)))
7334.1.2 by Muharem Hrnjadovic
content class now defined using storm constructs
39
40
41
class PackageCopyRequest(Storm):
7334.1.4 by Muharem Hrnjadovic
Brad's review comments
42
    """See `IPackageCopyRequest`."""
7334.1.1 by Muharem Hrnjadovic
imported from dead branch
43
    implements(IPackageCopyRequest)
7334.1.2 by Muharem Hrnjadovic
content class now defined using storm constructs
44
    __storm_table__ = 'PackageCopyRequest'
45
    id = Int(primary=True)
46
47
    target_archive_id = Int(name='target_archive', allow_none=False)
48
    target_archive = Reference(target_archive_id, 'Archive.id')
49
    target_distroseries_id = Int(name='target_distroseries', allow_none=True)
50
    target_distroseries = Reference(target_distroseries_id, 'DistroSeries.id')
51
    target_component_id = Int(name='target_component', allow_none=True)
52
    target_component = Reference(target_component_id, 'Component.id')
53
    target_pocket = Enum(map=_construct_enum_mapping(PackagePublishingPocket))
54
55
    copy_binaries = Bool(allow_none=False, default=False)
56
57
    source_archive_id = Int(name='source_archive', allow_none=False)
58
    source_archive = Reference(source_archive_id, 'Archive.id')
59
    source_distroseries_id = Int(name='source_distroseries', allow_none=True)
60
    source_distroseries = Reference(source_distroseries_id, 'DistroSeries.id')
61
    source_component_id = Int(name='source_component', allow_none=True)
62
    source_component = Reference(source_component_id, 'Component.id')
63
    source_pocket = Enum(map=_construct_enum_mapping(PackagePublishingPocket))
64
65
    requester_id = Int(name='requester', allow_none=False)
66
    requester = Reference(requester_id, 'Person.id')
67
68
    requester_id = Int(
69
        name='requester', allow_none=False, validator=validate_public_person)
70
    requester = Reference(requester_id, 'Person.id')
71
72
    status = Enum(
73
        allow_none=False, map=_construct_enum_mapping(PackageCopyStatus))
74
    reason = Unicode(allow_none=True)
75
76
    date_created = DateTime(allow_none=False, default=UTC_NOW)
77
    date_started = DateTime(allow_none=True)
78
    date_completed = DateTime(allow_none=True)
7334.1.1 by Muharem Hrnjadovic
imported from dead branch
79
80
    def __str__(self):
7334.1.4 by Muharem Hrnjadovic
Brad's review comments
81
        """See `IPackageCopyRequest`."""
7334.1.1 by Muharem Hrnjadovic
imported from dead branch
82
7334.1.4 by Muharem Hrnjadovic
Brad's review comments
83
        def get_name_or_nothing(property_name, nothing='-'):
84
            """Helper method, returns property value if set or 'nothing'."""
7334.1.1 by Muharem Hrnjadovic
imported from dead branch
85
            property = getattr(self, property_name, None)
86
87
            # Return straight-away if property is not set.
88
            if property is None:
7334.1.4 by Muharem Hrnjadovic
Brad's review comments
89
                return nothing
7334.1.1 by Muharem Hrnjadovic
imported from dead branch
90
91
            # Does the property have a name?
92
            name = getattr(property, 'name', None)
93
            if name is not None:
7334.1.2 by Muharem Hrnjadovic
content class now defined using storm constructs
94
                return str(name)
7334.1.1 by Muharem Hrnjadovic
imported from dead branch
95
96
            # Does the property have a title?
97
            title = getattr(property, 'title', None)
98
            if title is not None:
7334.1.2 by Muharem Hrnjadovic
content class now defined using storm constructs
99
                return str(title)
7334.1.1 by Muharem Hrnjadovic
imported from dead branch
100
101
            # Return the string representation of the property as a last
102
            # resort.
103
            return str(property)
104
105
        result = (
106
            "Package copy request\n"
107
            "source = %s/%s/%s/%s\ntarget = %s/%s/%s/%s\n"
108
            "copy binaries: %s\nrequester: %s\nstatus: %s\n"
109
            "date created: %s\ndate started: %s\ndate completed: %s" %
110
            (get_name_or_nothing('source_archive'),
111
             get_name_or_nothing('source_distroseries'),
112
             get_name_or_nothing('source_component'),
113
             get_name_or_nothing('source_pocket'),
114
             get_name_or_nothing('target_archive'),
115
             get_name_or_nothing('target_distroseries'),
116
             get_name_or_nothing('target_component'),
117
             get_name_or_nothing('target_pocket'),
118
             get_name_or_nothing('copy_binaries'),
119
             get_name_or_nothing('requester'),
120
             get_name_or_nothing('status'),
121
             get_name_or_nothing('date_created'),
122
             get_name_or_nothing('date_started'),
123
             get_name_or_nothing('date_completed')))
124
        return result
125
126
    def markAsInprogress(self):
7334.1.4 by Muharem Hrnjadovic
Brad's review comments
127
        """See `IPackageCopyRequest`."""
7334.1.1 by Muharem Hrnjadovic
imported from dead branch
128
        self.status = PackageCopyStatus.INPROGRESS
129
        self.date_started = UTC_NOW
130
7334.1.4 by Muharem Hrnjadovic
Brad's review comments
131
    def markAsCompleted(self):
132
        """See `IPackageCopyRequest`."""
7334.1.1 by Muharem Hrnjadovic
imported from dead branch
133
        self.status = PackageCopyStatus.COMPLETE
134
        self.date_completed = UTC_NOW
135
136
    def markAsFailed(self):
7334.1.4 by Muharem Hrnjadovic
Brad's review comments
137
        """See `IPackageCopyRequest`."""
7334.1.1 by Muharem Hrnjadovic
imported from dead branch
138
        self.status = PackageCopyStatus.FAILED
139
        self.date_completed = UTC_NOW
140
141
    def markAsCanceling(self):
7334.1.4 by Muharem Hrnjadovic
Brad's review comments
142
        """See `IPackageCopyRequest`."""
7334.1.1 by Muharem Hrnjadovic
imported from dead branch
143
        self.status = PackageCopyStatus.CANCELING
144
145
    def markAsCancelled(self):
7334.1.4 by Muharem Hrnjadovic
Brad's review comments
146
        """See `IPackageCopyRequest`."""
7334.1.1 by Muharem Hrnjadovic
imported from dead branch
147
        self.status = PackageCopyStatus.CANCELLED
148
        self.date_completed = UTC_NOW
149
150
7334.1.2 by Muharem Hrnjadovic
content class now defined using storm constructs
151
def _set_location_data(pcr, location, prefix):
152
    """Copies source/target package location data to copy requests."""
153
    # Set the archive first, must be present.
154
    assert location.archive is not None, (
155
        '%s archive must be set in package location' % prefix)
156
    setattr(pcr, '%s_archive' % prefix, location.archive)
157
    # Now set the optional data if present.
158
    optional_location_data = ('distroseries', 'component', 'pocket')
159
    for datum_name in optional_location_data:
160
        value = getattr(location, datum_name, None)
161
        if value is not None:
162
            setattr(pcr, '%s_%s' % (prefix, datum_name), value)
163
164
7334.1.1 by Muharem Hrnjadovic
imported from dead branch
165
class PackageCopyRequestSet:
7334.1.4 by Muharem Hrnjadovic
Brad's review comments
166
    """See `IPackageCopyRequestSet`."""
7334.1.1 by Muharem Hrnjadovic
imported from dead branch
167
    implements(IPackageCopyRequestSet)
168
169
    def new(
170
        self, source, target, requester, copy_binaries=False, reason=None):
7334.1.4 by Muharem Hrnjadovic
Brad's review comments
171
        """See `IPackageCopyRequestSet`."""
7334.1.2 by Muharem Hrnjadovic
content class now defined using storm constructs
172
        store = getUtility(IStoreSelector).get(MAIN_STORE, DEFAULT_FLAVOR)
173
        pcr = PackageCopyRequest()
174
        for location_data in ((source, 'source'), (target, 'target')):
175
            _set_location_data(pcr, *location_data)
176
        pcr.requester = requester
177
        if copy_binaries == True:
178
            pcr.copy_binaries = True
179
        if reason is not None:
180
            pcr.reason = reason
181
182
        pcr.status = PackageCopyStatus.NEW
183
        store.add(pcr)
184
        return pcr
7334.1.1 by Muharem Hrnjadovic
imported from dead branch
185
186
    def getByPersonAndStatus(self, requester, status=None):
7334.1.4 by Muharem Hrnjadovic
Brad's review comments
187
        """See `IPackageCopyRequestSet`."""
7334.1.1 by Muharem Hrnjadovic
imported from dead branch
188
        store = getUtility(IStoreSelector).get(MAIN_STORE, DEFAULT_FLAVOR)
189
        base_clauses = (PackageCopyRequest.requester == requester,)
190
        if status is not None:
191
            optional_clauses = (PackageCopyRequest.status == status,)
192
        else:
193
            optional_clauses = ()
194
        return store.find(
195
            PackageCopyRequest, *(base_clauses + optional_clauses))
196
197
    def getByTargetDistroSeries(self, distroseries):
7334.1.4 by Muharem Hrnjadovic
Brad's review comments
198
        """See `IPackageCopyRequestSet`."""
7334.1.1 by Muharem Hrnjadovic
imported from dead branch
199
        store = getUtility(IStoreSelector).get(MAIN_STORE, DEFAULT_FLAVOR)
200
        return store.find(
201
            PackageCopyRequest,
202
            PackageCopyRequest.target_distroseries == distroseries)
203
204
    def getBySourceDistroSeries(self, distroseries):
7334.1.4 by Muharem Hrnjadovic
Brad's review comments
205
        """See `IPackageCopyRequestSet`."""
7334.1.1 by Muharem Hrnjadovic
imported from dead branch
206
        store = getUtility(IStoreSelector).get(MAIN_STORE, DEFAULT_FLAVOR)
207
        return store.find(
208
            PackageCopyRequest,
209
            PackageCopyRequest.source_distroseries == distroseries)
210
211
    def getByTargetArchive(self, archive):
7334.1.4 by Muharem Hrnjadovic
Brad's review comments
212
        """See `IPackageCopyRequestSet`."""
7334.1.1 by Muharem Hrnjadovic
imported from dead branch
213
        store = getUtility(IStoreSelector).get(MAIN_STORE, DEFAULT_FLAVOR)
214
        return store.find(
215
            PackageCopyRequest,
216
            PackageCopyRequest.target_archive == archive)