~launchpad-pqm/launchpad/devel

10637.3.1 by Guilherme Salgado
Use the default python version instead of a hard-coded version
1
#!/usr/bin/python -S
7675.584.6 by William Grant
Add and test parse-ppa-apache-access-logs.py.
2
#
3
# Copyright 2010 Canonical Ltd.  This software is licensed under the
4
# GNU Affero General Public License version 3 (see the file LICENSE).
5
6
"""Parse PPA apache logs to find out download counts for each file."""
7
8
__metaclass__ = type
9
10
import _pythonpath
11
12
import functools
13
14
from zope.component import getUtility
15
16
from canonical.config import config
7675.584.17 by William Grant
XXXify circular import comment.
17
# XXX: wgrant 2010-03-16 bug=539496: Importing directly from
18
# lp.registry.interfaces.person results in a circular import.
7675.584.6 by William Grant
Add and test parse-ppa-apache-access-logs.py.
19
from canonical.launchpad.interfaces import IPersonSet
20
from lp.soyuz.interfaces.archive import NoSuchPPA
21
from lp.soyuz.scripts.ppa_apache_log_parser import DBUSER, get_ppa_file_key
22
from lp.services.apachelogparser.script import ParseApacheLogs
23
24
25
class ParsePPAApacheLogs(ParseApacheLogs):
26
    """An Apache log parser for PPA downloads."""
27
28
    def setUpUtilities(self):
29
        """See `ParseApacheLogs`."""
30
        self.person_set = getUtility(IPersonSet)
31
32
    @property
33
    def root(self):
34
        """See `ParseApacheLogs`."""
35
        return config.ppa_apache_log_parser.logs_root
36
37
    def getDownloadKey(self, path):
38
        """See `ParseApacheLogs`."""
39
        return get_ppa_file_key(path)
40
41
    def getDownloadCountUpdater(self, file_id):
7675.584.13 by William Grant
Remove mistakenly placed hash.
42
        """See `ParseApacheLogs`."""
7675.584.6 by William Grant
Add and test parse-ppa-apache-access-logs.py.
43
        person = self.person_set.getByName(file_id[0])
44
        if person is None:
45
            return
46
        try:
47
            archive = person.getPPAByName(file_id[1])
48
        except NoSuchPPA:
49
            return None
50
        # file_id[2] (distro) isn't used yet, since getPPAByName
51
        # hardcodes Ubuntu.
52
        bpr = archive.getBinaryPackageReleaseByFileName(file_id[3])
53
        if bpr is None:
54
            return None
55
56
        return functools.partial(archive.updatePackageDownloadCount, bpr)
57
58
59
if __name__ == '__main__':
60
    script = ParsePPAApacheLogs('parse-ppa-apache-logs', DBUSER)
61
    script.lock_and_run()