summaryrefslogtreecommitdiff
path: root/bileto/lplib.py
blob: c8ad6196a22defbd55778c71511fe516622687ef (plain)
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
"""Bileto Launchpad

Query launchpad for currently supported releases.
"""

from os.path import join
from contextlib import suppress
from functools import lru_cache

from launchpadlib.launchpad import Launchpad

from bileto.settings import Config

EMPTY = ''


class lp:
    """Trigger connections to launchpadlib only when needed."""
    MP_ROOT = 'https://code.launchpad.net/'
    _real_instance = None

    def _get_instance(self):
        """Authenticate to Launchpad."""
        # If you ever have trouble with the token, eg like this traceback:
        # https://bugs.launchpad.net/launchpadlib/+bug/1526563
        # Run 'token_generator.py' to make a new token.
        return Launchpad.login_with(
            application_name='bileto',
            service_root='production',
            allow_access_levels=['WRITE_PRIVATE'],
            version='devel',  # Need devel for copyPackage.
            credentials_file=join(Config.HOME, '.launchpad.credentials'),
        )

    @property
    def _instance(self):
        """Cache LP object."""
        if not self._real_instance:
            self._real_instance = self._get_instance()
        return self._real_instance

    @property
    @lru_cache()
    def _api_root(self):
        """Identify the root URL of the launchpad API."""
        return self._instance.resource_type_link.split('#')[0]

    def __getattr__(self, attr):
        """Wrap launchpadlib so tightly you can't tell the difference."""
        return getattr(self._instance, attr)

    @property
    @lru_cache()
    def ubuntu(self):
        """Shorthand for Ubuntu object."""
        return self.distributions['ubuntu']

    @property
    @lru_cache()
    def ppa_team(self):
        """The team that owns our build PPAs."""
        return self.people[Config.ppa_team]

    @lru_cache()
    def active_series(self):
        """Identify currently supported Ubuntu serieses."""
        return [s.name for s in self.ubuntu.series if s.active]

    def get_irc_nick(self, user, network='freenode'):
        """Find the freenode irc nick for an LP user."""
        ircs = []
        with suppress(KeyError, TypeError):
            ircs = lp.people[user].irc_nicknames
        for irc in ircs:
            if network in (irc.network or EMPTY).lower():
                return irc.nickname
        return user

    def get_teams(self, user):
        """List teams that user belongs to."""
        with suppress(KeyError, TypeError):
            return [
                team.self_link.partition('~')[-1].partition('/')[0]
                for team in lp.people[user].memberships_details]
        return []

    def load(self, url):
        """Return a lp resource from a launchpad url."""
        return self._instance.load(url.replace(self.MP_ROOT, self._api_root))

lp = lp()  # Single global instance.