~launchpad-pqm/launchpad/devel

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
# Copyright 2009 Canonical Ltd.  This software is licensed under the
# GNU Affero General Public License version 3 (see the file LICENSE).

# pylint: disable-msg=E0211,E0213

"""Product release interfaces."""

__metaclass__ = type

__all__ = [
    'IProductRelease',
    'IProductReleaseEditRestricted',
    'IProductReleaseFile',
    'IProductReleaseFileAddForm',
    'IProductReleaseFileEditRestricted',
    'IProductReleaseFilePublic',
    'IProductReleasePublic',
    'IProductReleaseSet',
    'UpstreamFileType',
    ]

from lazr.enum import (
    DBEnumeratedType,
    DBItem,
    )
from lazr.restful.declarations import (
    call_with,
    export_as_webservice_entry,
    export_factory_operation,
    export_operation_as,
    export_write_operation,
    exported,
    operation_parameters,
    REQUEST_USER,
    )
from lazr.restful.fields import (
    CollectionField,
    Reference,
    ReferenceChoice,
    )
from lazr.restful.interface import copy_field
from zope.component import getUtility
from zope.interface import Interface
from zope.schema import (
    Bytes,
    Choice,
    Datetime,
    Int,
    Text,
    TextLine,
    )

from lp.services.config import config
from lp import _
from lp.app.validators import LaunchpadValidationError
from lp.app.validators.version import sane_version
from lp.services.fields import (
    ContentNameField,
    PersonChoice,
    )


def file_size_constraint(value, max_size):
    """Check constraints.

    The file cannot be empty and must be <= max_size.
    """
    size = len(value)
    if size == 0:
        raise LaunchpadValidationError(u'Cannot upload empty file.')
    elif max_size > 0 and size > max_size:
        raise LaunchpadValidationError(
            u'Cannot upload files larger than %i bytes' % max_size)
    else:
        return True


def productrelease_file_size_constraint(value):
    """Constraint for a product release file's size."""
    max_size = config.launchpad.max_productrelease_file_size
    return file_size_constraint(value, max_size)


def productrelease_signature_size_constraint(value):
    """Constraint for a product release signature's size."""
    max_size = config.launchpad.max_productrelease_signature_size
    return file_size_constraint(value, max_size)


class UpstreamFileType(DBEnumeratedType):
    """Upstream File Type

    When upstream open source project release a product they will
    include several files in the release. All of these files are
    stored in Launchpad (we throw nothing away ;-). This schema
    gives the type of files that we know about.
    """

    CODETARBALL = DBItem(1, """
        Code Release Tarball

        This file contains code in a compressed package like
        a tar.gz or tar.bz or .zip file.
        """)

    README = DBItem(2, """
        README File

        This is a README associated with the upstream
        release. It might be in .txt or .html format, the
        filename would be an indicator.
        """)

    RELEASENOTES = DBItem(3, """
        Release Notes

        This file contains the release notes of the new
        upstream release. Again this could be in .txt or
        in .html format.
        """)

    CHANGELOG = DBItem(4, """
        ChangeLog File

        This file contains information about changes in this
        release from the previous release in the series. This
        is usually not a detailed changelog, but a high-level
        summary of major new features and fixes.
        """)

    INSTALLER = DBItem(5, """
        Installer file

        This file contains an installer for a product.  It may
        be a Debian package, an RPM file, an OS X disk image, a
        Windows installer, or some other type of installer.
        """)


class ProductReleaseVersionField(ContentNameField):

    errormessage = _(
        "%s is already in use by another version in this release series.")

    @property
    def _content_iface(self):
        return IProductRelease

    def _getByName(self, version):
        """Return the content object for the specified version.

        The version is specified either by the context directly or by the
        context's referenced productseries.  Overridden from
        `ContentFieldName`.
        """
        # Import locally to avoid circular imports.
        from lp.registry.interfaces.productseries import (
            IProductSeries)
        if IProductSeries.providedBy(self.context):
            productseries = self.context
        else:
            productseries = self.context.productseries
        releaseset = getUtility(IProductReleaseSet)
        release = releaseset.getBySeriesAndVersion(productseries, version)
        if release == self.context:
            # A ProductRelease may edit itself; do not report that another
            # ProductRelease exists with the same version.
            return None
        return release


class IProductReleaseFileEditRestricted(Interface):
    """`IProductReleaseFile` properties which require `launchpad.Edit`."""

    @export_write_operation()
    @export_operation_as('delete')
    def destroySelf():
        """Delete the product release file."""


class IProductReleaseFilePublic(Interface):
    """Public properties for `IProductReleaseFile`."""

    id = Int(title=_('ID'), required=True, readonly=True)
    productrelease = exported(
        ReferenceChoice(title=_('Project release'),
                        description=_("The parent product release."),
                        schema=Interface, # Defined later.
                        required=True,
                        vocabulary='ProductRelease'),
        exported_as='project_release')
    libraryfile = exported(
        Bytes(title=_("File"),
              description=_("The file contents."),
              readonly=True,
              required=True),
        exported_as='file')
    signature = exported(
        Bytes(title=_("File signature"),
              description=_("The file signature."),
              readonly=True,
              required=False))
    filetype = exported(
        Choice(title=_("Upstream file type"), required=True,
               vocabulary=UpstreamFileType,
               default=UpstreamFileType.CODETARBALL),
        exported_as='file_type')
    description = exported(
        Text(title=_("Description"), required=False,
             description=_('A detailed description of the file contents')))
    date_uploaded = exported(
        Datetime(title=_('Upload date'),
                 description=_('The date this file was uploaded'),
                 required=True, readonly=True))


class IProductReleaseFile(IProductReleaseFileEditRestricted,
                          IProductReleaseFilePublic):
    """A file associated with a ProductRelease."""
    export_as_webservice_entry("project_release_file", publish_web_link=False)


class IProductReleaseEditRestricted(Interface):
    """`IProductRelease` properties which require `launchpad.Edit`."""

    @call_with(uploader=REQUEST_USER)
    @operation_parameters(
        filename=TextLine(),
        signature_filename=TextLine(),
        content_type=TextLine(),
        file_content=Bytes(constraint=productrelease_file_size_constraint),
        signature_content=Bytes(
            constraint=productrelease_signature_size_constraint),
        file_type=copy_field(IProductReleaseFile['filetype'], required=False)
        )
    @export_factory_operation(
        IProductReleaseFile, ['description'])
    @export_operation_as('add_file')
    def addReleaseFile(filename, file_content, content_type,
                       uploader, signature_filename=None,
                       signature_content=None,
                       file_type=UpstreamFileType.CODETARBALL,
                       description=None):
        """Add file to the library and link to this `IProductRelease`.

        The signature file will also be added if available.

        :param filename: Name of the file being uploaded.
        :param file_content: StringIO or file object.
        :param content_type: A MIME content type string.
        :param uploader: The person who uploaded the file.
        :param signature_filename: Name of the uploaded gpg signature file.
        :param signature_content: StringIO or file object.
        :param file_type: An `UpstreamFileType` enum value.
        :param description: Info about the file.
        :returns: `IProductReleaseFile` object.
        """

    @export_write_operation()
    @export_operation_as('delete')
    def destroySelf():
        """Delete this product release.

        This method must not be used if this product release has any
        release files associated with it.
        """


class IProductReleasePublic(Interface):
    """Public `IProductRelease` properties."""

    id = Int(title=_('ID'), required=True, readonly=True)

    datereleased = exported(
        Datetime(
            title=_('Date released'), required=True,
            readonly=False,
            description=_('The date this release was published. Before '
                          'release, this should have an estimated '
                          'release date.')),
        exported_as="date_released"
        )

    version = exported(
        ProductReleaseVersionField(
            title=_('Version'),
            description= u'The specific version number assigned to this '
            'release. Letters and numbers are acceptable, for releases like '
            '"1.2rc3".',
            constraint=sane_version)
        )

    owner = exported(
        PersonChoice(
            title=u"The owner of this release.",
            required=True,
            vocabulary='ValidOwner',
            description=_("The person or team who owns  his product release.")
            )
        )

    productseries = Choice(
        title=_('Release series'), readonly=True,
        vocabulary='FilteredProductSeries')

    codename = TextLine(
        title=u'Code name', required=False, readonly=True,
        description=_('The release code-name. This is deprecated, '
                      'since it was moved to the milestone.'))

    summary = Text(
        title=_("Summary"), required=False, readonly=True,
        description=_('A brief summary of the release highlights, to '
                      'be shown at the top of the release page, and in '
                      'listings.'))

    release_notes = exported(
        Text(
            title=_("Release notes"), required=False,
            description=_('A description of important new features '
                          '(though the changelog below might repeat some of '
                          'this information).'))
        )

    changelog = exported(
        Text(
            title=_('Changelog'), required=False,
            description=_('A description of every change in the release.'))
        )

    datecreated = exported(
        Datetime(title=_('Date Created'),
                 description=_("The date this project release was created in "
                               "Launchpad."),
                 required=True, readonly=True),
        exported_as="date_created")

    displayname = exported(
        Text(title=u'Constructed display name for a project release.',
             readonly=True),
        exported_as="display_name")

    title = exported(
        Text(title=u'Constructed title for a project release.', readonly=True)
        )

    product = exported(
        Reference(title=u'The upstream project of this release.',
                  schema=Interface, readonly=True),
         exported_as="project")

    files = exported(
        CollectionField(
            title=_('Project release files'),
            description=_('A list of files for this release.'),
            readonly=True,
            value_type=Reference(schema=IProductReleaseFile)))

    milestone = exported(
        ReferenceChoice(
            title=u"Milestone for this release",
            description=_("A release requires a corresponding milestone "
                          "that is not attached to another release."),
            # Schema is set to IMilestone in interfaces/milestone.py.
            schema=Interface,
            vocabulary='Milestone',
            required=True))

    def getFileAliasByName(name):
        """Return the `LibraryFileAlias` by file name.

        Raises a NotFoundError if no matching ProductReleaseFile exists.
        """

    def getProductReleaseFileByName(name):
        """Return the `ProductReleaseFile` by file name.

        Raises a NotFoundError if no matching ProductReleaseFile exists.
        """


class IProductRelease(IProductReleaseEditRestricted,
                      IProductReleasePublic):
    """A specific release (i.e. version) of a product.

    For example: Mozilla 1.7.2 or Apache 2.0.48.
    """

    export_as_webservice_entry('project_release')


# Set the schema for IProductReleaseFile now that IProductRelease is defined.
IProductReleaseFile['productrelease'].schema = IProductRelease


class IProductReleaseFileAddForm(Interface):
    """Schema for adding ProductReleaseFiles to a project."""
    description = Text(title=_("Description"), required=True,
        description=_('A short description of the file contents'))

    filecontent = Bytes(
        title=u"File", required=True,
        constraint=productrelease_file_size_constraint)

    signature = Bytes(
        title=u"GPG signature (recommended)", required=False,
        constraint=productrelease_signature_size_constraint)

    contenttype = Choice(title=_("File content type"), required=True,
                         vocabulary=UpstreamFileType,
                         default=UpstreamFileType.CODETARBALL)


class IProductReleaseSet(Interface):
    """Auxiliary class for ProductRelease handling."""

    def getBySeriesAndVersion(productseries, version, default=None):
        """Get a release by its version and productseries.

        If no release is found, default will be returned.
        """

    def getReleasesForSeries(series):
        """Get all releases for the series."""

    def getFilesForReleases(releases):
        """Get all files for the releases."""