~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).
3
4983.1.2 by Curtis Hovey
Added pylint exceptions to database classes.
4
# pylint: disable-msg=E0611,W0212
1670 by Canonical.com Patch Queue Manager
Big lot of database clean-up r=stub except for resolution of conflicts.
5
6
__metaclass__ = type
3691.315.7 by kiko
Move package name vocabularies into the database classes, in preparation for some smart description-sniffing
7
__all__ = [
8
    'SourcePackageName',
9
    'SourcePackageNameSet',
11637.3.20 by j.c.sackett
Lint fixes.
10
    'getSourcePackageDescriptions',
11
    ]
1670 by Canonical.com Patch Queue Manager
Big lot of database clean-up r=stub except for resolution of conflicts.
12
11403.1.4 by Henning Eggers
Reformatted imports using format-imports script r32.
13
from sqlobject import (
14
    SQLMultipleJoin,
15
    SQLObjectNotFound,
16
    StringCol,
17
    )
1433 by Canonical.com Patch Queue Manager
Got classes related with source and binary packages into diferent files on database, interfaces and zcml directories.
18
from zope.interface import implements
19
11270.1.3 by Tim Penhey
Changed NotFoundError imports - gee there were a lot of them.
20
from lp.app.errors import NotFoundError
13662.5.2 by Aaron Bentley
SourcePackageNameSet.new may raise InvalidName.
21
from lp.app.validators.name import valid_name
22
from lp.registry.errors import (
23
    InvalidName,
24
    NoSuchSourcePackageName,
25
    )
7675.110.3 by Curtis Hovey
Ran the migration script to move registry code to lp.registry.
26
from lp.registry.interfaces.sourcepackagename import (
11403.1.4 by Henning Eggers
Reformatted imports using format-imports script r32.
27
    ISourcePackageName,
28
    ISourcePackageNameSet,
29
    )
14606.3.1 by William Grant
Merge canonical.database into lp.services.database.
30
from lp.services.database.sqlbase import (
31
    cursor,
32
    quote_like,
33
    SQLBase,
34
    sqlvalues,
35
    )
36
from lp.services.helpers import ensure_unicode
3691.315.10 by kiko
Finally fix bug #40173:
37
1433 by Canonical.com Patch Queue Manager
Got classes related with source and binary packages into diferent files on database, interfaces and zcml directories.
38
39
class SourcePackageName(SQLBase):
40
    implements(ISourcePackageName)
41
    _table = 'SourcePackageName'
42
2048 by Canonical.com Patch Queue Manager
debbugssync, hct enabling, and ui fixes. r=jamesh
43
    name = StringCol(dbName='name', notNull=True, unique=True,
44
        alternateID=True)
1433 by Canonical.com Patch Queue Manager
Got classes related with source and binary packages into diferent files on database, interfaces and zcml directories.
45
8879.5.5 by Edwin Grubbs
Fixed lint errors.
46
    potemplates = SQLMultipleJoin(
47
        'POTemplate', joinColumn='sourcepackagename')
4065.1.1 by Celso Providelo
Fixing #102545 (OOPS on SP/+packaging page). It fix template issues and add an effective pagetest for Packaging features.
48
    packagings = SQLMultipleJoin(
8879.5.5 by Edwin Grubbs
Fixed lint errors.
49
        'Packaging', joinColumn='sourcepackagename', orderBy='Packaging.id')
4065.1.1 by Celso Providelo
Fixing #102545 (OOPS on SP/+packaging page). It fix template issues and add an effective pagetest for Packaging features.
50
1433 by Canonical.com Patch Queue Manager
Got classes related with source and binary packages into diferent files on database, interfaces and zcml directories.
51
    def __unicode__(self):
52
        return self.name
53
7675.520.1 by Barry Warsaw
Add some useful reprs.
54
    def __repr__(self):
7675.520.3 by Barry Warsaw
Fix some failing tests and add some tests for the new reprs.
55
        return "<%s '%s'>" % (self.__class__.__name__, self.name)
7675.520.1 by Barry Warsaw
Add some useful reprs.
56
1670 by Canonical.com Patch Queue Manager
Big lot of database clean-up r=stub except for resolution of conflicts.
57
    def ensure(klass, name):
1433 by Canonical.com Patch Queue Manager
Got classes related with source and binary packages into diferent files on database, interfaces and zcml directories.
58
        try:
59
            return klass.byName(name)
60
        except SQLObjectNotFound:
61
            return klass(name=name)
1670 by Canonical.com Patch Queue Manager
Big lot of database clean-up r=stub except for resolution of conflicts.
62
    ensure = classmethod(ensure)
63
64
65
class SourcePackageNameSet:
1439 by Canonical.com Patch Queue Manager
Some more file organization
66
    implements(ISourcePackageNameSet)
67
68
    def __getitem__(self, name):
14583.1.19 by Curtis Hovey
Fixed doc.
69
        """See `ISourcePackageNameSet`."""
7675.166.315 by Stuart Bishop
Be more careful about casting to Unicode
70
        name = ensure_unicode(name)
1439 by Canonical.com Patch Queue Manager
Some more file organization
71
        try:
72
            return SourcePackageName.byName(name)
73
        except SQLObjectNotFound:
7362.9.2 by Jonathan Lange
Raise all sorts of errors if we can't find things.
74
            raise NoSuchSourcePackageName(name)
1439 by Canonical.com Patch Queue Manager
Some more file organization
75
76
    def get(self, sourcepackagenameid):
14583.1.19 by Curtis Hovey
Fixed doc.
77
        """See `ISourcePackageNameSet`."""
1439 by Canonical.com Patch Queue Manager
Some more file organization
78
        try:
79
            return SourcePackageName.get(sourcepackagenameid)
80
        except SQLObjectNotFound:
81
            raise NotFoundError(sourcepackagenameid)
82
1716.3.57 by Christian Reis
Fix up failing tests, and updating a last forgotten callsite
83
    def getAll(self):
14583.1.19 by Curtis Hovey
Fixed doc.
84
        """See `ISourcePackageNameSet`."""
1716.3.57 by Christian Reis
Fix up failing tests, and updating a last forgotten callsite
85
        return SourcePackageName.select()
86
1439 by Canonical.com Patch Queue Manager
Some more file organization
87
    def findByName(self, name):
1670 by Canonical.com Patch Queue Manager
Big lot of database clean-up r=stub except for resolution of conflicts.
88
        """Find sourcepackagenames by its name or part of it."""
1716.5.60 by kiko
Fix LIKE/ILIKE queries that were incorrectly using quote() instead of quote_like. Also fix uses of urljoin that should have been urlappends. Delintifies in places
89
        query = "name ILIKE '%%' || %s || '%%'" % quote_like(name)
1439 by Canonical.com Patch Queue Manager
Some more file organization
90
        return SourcePackageName.select(query)
1670 by Canonical.com Patch Queue Manager
Big lot of database clean-up r=stub except for resolution of conflicts.
91
2393 by Canonical.com Patch Queue Manager
[r=SteveA] cleanup BugTaskReleaseTargetingView warnings. add
92
    def queryByName(self, name):
14583.1.19 by Curtis Hovey
Fixed doc.
93
        """See `ISourcePackageNameSet`."""
2393 by Canonical.com Patch Queue Manager
[r=SteveA] cleanup BugTaskReleaseTargetingView warnings. add
94
        return SourcePackageName.selectOneBy(name=name)
95
2090 by Canonical.com Patch Queue Manager
[r=SteveA] another round of import fascism and other cleanups
96
    def new(self, name):
13662.5.2 by Aaron Bentley
SourcePackageNameSet.new may raise InvalidName.
97
        if not valid_name(name):
98
            raise InvalidName(
99
                "%s is not a valid name for a source package." % name)
2090 by Canonical.com Patch Queue Manager
[r=SteveA] another round of import fascism and other cleanups
100
        return SourcePackageName(name=name)
101
102
    def getOrCreateByName(self, name):
103
        try:
104
            return self[name]
3023.3.49 by Daniel Silverstone
A bit more debugging for when NEW happens in the uploader
105
        except NotFoundError:
2090 by Canonical.com Patch Queue Manager
[r=SteveA] another round of import fascism and other cleanups
106
            return self.new(name)
107
3691.315.7 by kiko
Move package name vocabularies into the database classes, in preparation for some smart description-sniffing
108
8879.5.5 by Edwin Grubbs
Fixed lint errors.
109
def getSourcePackageDescriptions(
110
    results, use_names=False, max_title_length=50):
3691.315.11 by kiko
Reorganize BinaryAndSourcePackage* into its own module, and kill PackageNameIterator in favor of just using two functions to accomplish what's needed cleanly.
111
    """Return a dictionary with descriptions keyed on source package names.
112
113
    Takes an ISelectResults of a *PackageName query. The use_names
114
    flag is a hack that allows this method to work for the
115
    BinaryAndSourcePackageName view, which lacks IDs.
116
117
    WARNING: this function assumes that there is little overlap and much
118
    coherence in how package names are used, in particular across
119
    distributions if derivation is implemented. IOW, it does not make a
120
    promise to provide The Correct Description, but a pretty good guess
121
    at what the description should be.
122
    """
4664.1.1 by Curtis Hovey
Normalized comments for bug 3732.
123
    # XXX: kiko, 2007-01-17:
124
    # Use_names could be removed if we instead added IDs to the
3691.315.11 by kiko
Reorganize BinaryAndSourcePackage* into its own module, and kill PackageNameIterator in favor of just using two functions to accomplish what's needed cleanly.
125
    # BinaryAndSourcePackageName view, but we'd still need to find
126
    # out how to specify the attribute, since it would be
127
    # sourcepackagename_id and binarypackagename_id depending on
128
    # whether the row represented one or both of those cases.
129
    if use_names:
8879.5.5 by Edwin Grubbs
Fixed lint errors.
130
        clause = ("SourcePackageName.name in %s" %
3691.315.11 by kiko
Reorganize BinaryAndSourcePackage* into its own module, and kill PackageNameIterator in favor of just using two functions to accomplish what's needed cleanly.
131
                 sqlvalues([pn.name for pn in results]))
132
    else:
8879.5.5 by Edwin Grubbs
Fixed lint errors.
133
        clause = ("SourcePackageName.id in %s" %
3691.315.11 by kiko
Reorganize BinaryAndSourcePackage* into its own module, and kill PackageNameIterator in favor of just using two functions to accomplish what's needed cleanly.
134
                 sqlvalues([spn.id for spn in results]))
135
136
    cur = cursor()
137
    cur.execute("""SELECT DISTINCT BinaryPackageName.name,
138
                          SourcePackageName.name
7675.687.147 by Michael Nelson
vocabularies.txt
139
                     FROM BinaryPackageRelease, SourcePackageName,
140
                          BinaryPackageBuild, SourcePackageRelease,
141
                          BinaryPackageName
3691.315.11 by kiko
Reorganize BinaryAndSourcePackage* into its own module, and kill PackageNameIterator in favor of just using two functions to accomplish what's needed cleanly.
142
                    WHERE
3691.337.1 by kiko
Fix typo in SourcePackageName vocabulary iterator that caused everything to go haywire; updated test that now actually makes sense.
143
                       BinaryPackageName.id =
144
                           BinaryPackageRelease.binarypackagename AND
7675.687.147 by Michael Nelson
vocabularies.txt
145
                       BinaryPackageRelease.build = BinaryPackageBuild.id AND
3691.315.11 by kiko
Reorganize BinaryAndSourcePackage* into its own module, and kill PackageNameIterator in favor of just using two functions to accomplish what's needed cleanly.
146
                       SourcePackageRelease.sourcepackagename =
147
                           SourcePackageName.id AND
7675.687.147 by Michael Nelson
vocabularies.txt
148
                       BinaryPackageBuild.source_package_release =
3691.315.11 by kiko
Reorganize BinaryAndSourcePackage* into its own module, and kill PackageNameIterator in favor of just using two functions to accomplish what's needed cleanly.
149
                           SourcePackageRelease.id AND
150
                       %s
151
                   ORDER BY BinaryPackageName.name,
152
                            SourcePackageName.name"""
153
                    % clause)
154
155
    descriptions = {}
156
    for binarypackagename, sourcepackagename in cur.fetchall():
7675.166.315 by Stuart Bishop
Be more careful about casting to Unicode
157
        if not sourcepackagename in descriptions:
3691.315.11 by kiko
Reorganize BinaryAndSourcePackage* into its own module, and kill PackageNameIterator in favor of just using two functions to accomplish what's needed cleanly.
158
            descriptions[sourcepackagename] = (
159
                "Source of: %s" % binarypackagename)
160
        else:
161
            if len(descriptions[sourcepackagename]) > max_title_length:
162
                description = "..."
163
            else:
164
                description = ", %s" % binarypackagename
165
            descriptions[sourcepackagename] += description
166
    return descriptions