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

# pylint: disable-msg=E0611,W0212

__metaclass__ = type
__all__ = [
    'BinaryPackageFile',
    'BinaryPackageFileSet',
    'SourceFileMixin',
    'SourcePackageReleaseFile',
    ]

from sqlobject import ForeignKey
from zope.interface import implements

from lp.registry.interfaces.sourcepackage import SourcePackageFileType
from lp.services.database.bulk import load_related
from lp.services.database.enumcol import EnumCol
from lp.services.database.sqlbase import (
    SQLBase,
    sqlvalues,
    )
from lp.services.librarian.model import (
    LibraryFileAlias,
    LibraryFileContent,
    )
from lp.soyuz.enums import BinaryPackageFileType
from lp.soyuz.interfaces.files import (
    IBinaryPackageFile,
    IBinaryPackageFileSet,
    ISourcePackageReleaseFile,
    ISourcePackageReleaseFileSet,
    )


class BinaryPackageFile(SQLBase):
    """See IBinaryPackageFile """
    implements(IBinaryPackageFile)
    _table = 'BinaryPackageFile'

    binarypackagerelease = ForeignKey(dbName='binarypackagerelease',
                                      foreignKey='BinaryPackageRelease',
                                      notNull=True)
    libraryfile = ForeignKey(dbName='libraryfile',
                             foreignKey='LibraryFileAlias', notNull=True)
    filetype = EnumCol(dbName='filetype',
                       schema=BinaryPackageFileType)


class BinaryPackageFileSet:
    """See `IBinaryPackageFileSet`."""
    implements(IBinaryPackageFileSet)

    def getByPackageUploadIDs(self, package_upload_ids):
        """See `IBinaryPackageFileSet`."""
        if package_upload_ids is None or len(package_upload_ids) == 0:
            return []
        return BinaryPackageFile.select("""
            PackageUploadBuild.packageupload = PackageUpload.id AND
            PackageUpload.id IN %s AND
            BinaryPackageBuild.id = PackageUploadBuild.build AND
            BinaryPackageRelease.build = BinaryPackageBuild.id AND
            BinaryPackageFile.binarypackagerelease = BinaryPackageRelease.id
            """ % sqlvalues(package_upload_ids),
            clauseTables=["PackageUpload", "PackageUploadBuild",
                          "BinaryPackageBuild", "BinaryPackageRelease"],
            prejoins=["binarypackagerelease", "binarypackagerelease.build",
                      "binarypackagerelease.binarypackagename"])

    def loadLibraryFiles(self, binary_files):
        """See `IBinaryPackageFileSet`."""
        lfas = load_related(LibraryFileAlias, binary_files, ['libraryfileID'])
        load_related(LibraryFileContent, lfas, ['contentID'])
        return lfas


class SourceFileMixin:
    """Mix-in class for common functionality between source file classes."""

    @property
    def is_orig(self):
        return self.filetype in (
            SourcePackageFileType.ORIG_TARBALL,
            SourcePackageFileType.COMPONENT_ORIG_TARBALL
            )


class SourcePackageReleaseFile(SourceFileMixin, SQLBase):
    """See ISourcePackageFile"""

    implements(ISourcePackageReleaseFile)

    sourcepackagerelease = ForeignKey(foreignKey='SourcePackageRelease',
                                      dbName='sourcepackagerelease')
    libraryfile = ForeignKey(foreignKey='LibraryFileAlias',
                             dbName='libraryfile')
    filetype = EnumCol(schema=SourcePackageFileType)


class SourcePackageReleaseFileSet:
    """See `ISourcePackageReleaseFileSet`."""
    implements(ISourcePackageReleaseFileSet)

    def getByPackageUploadIDs(self, package_upload_ids):
        """See `ISourcePackageReleaseFileSet`."""
        if package_upload_ids is None or len(package_upload_ids) == 0:
            return []
        return SourcePackageReleaseFile.select("""
            PackageUploadSource.packageupload = PackageUpload.id AND
            PackageUpload.id IN %s AND
            SourcePackageReleaseFile.sourcepackagerelease =
                PackageUploadSource.sourcepackagerelease
            """ % sqlvalues(package_upload_ids),
            clauseTables=["PackageUpload", "PackageUploadSource"],
            prejoins=["libraryfile", "libraryfile.content",
                      "sourcepackagerelease",
                      "sourcepackagerelease.sourcepackagename"])