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

"""Connect feature flags into scopes where they can be used.

The most common is flags scoped by some attribute of a web request, such as
the page ID or the server name.  But other types of scope can also match code
run from cron scripts and potentially also other places.
"""

__all__ = [
    'DefaultScope',
    'default_scopes',
    'FixedScope',
    'HANDLERS',
    'MultiScopeHandler',
    'ScopesForScript',
    'ScopesFromRequest',
    'TeamScope',
    'UserSliceScope',
    'undocumented_scopes',
    ]

__metaclass__ = type

import re

import canonical.config
from lp.registry.interfaces.person import IPerson
from lp.services.propertycache import cachedproperty


undocumented_scopes = set()


class BaseScope():
    """A base class for scope handlers.

    The docstring of subclasses is used on the +feature-info page as
    documentation, so write them accordingly.
    """

    # The regex pattern used to decide if a handler can evaluate a particular
    # scope.  Also used on +feature-info.
    pattern = None

    @cachedproperty
    def compiled_pattern(self):
        """The compiled scope matching regex.  A small optimization."""
        return re.compile(self.pattern)

    def lookup(self, scope_name):
        """Returns true if the given scope name is "active"."""
        raise NotImplementedError('Subclasses of BaseScope must implement '
            'lookup.')


class DefaultScope(BaseScope):
    """The default scope.  Always active."""

    pattern = r'default$'

    def lookup(self, scope_name):
        return True


class PageScope(BaseScope):
    """The current page ID.

    Pageid scopes are written as 'pageid:' + the pageid to match.  Pageids
    are treated as a namespace with : and # delimiters.

    For example, the scope 'pageid:Foo' will be active on pages with pageids:
        Foo
        Foo:Bar
        Foo#quux
    """

    pattern = r'pageid:'

    def __init__(self, request):
        self._request = request

    def lookup(self, scope_name):
        """Is the given scope match the current pageid?"""
        pageid_scope = scope_name[len('pageid:'):]
        scope_segments = self._pageid_to_namespace(pageid_scope)
        request_segments = self._request_pageid_namespace
        # In 2.6, this can be replaced with izip_longest
        for pos, name in enumerate(scope_segments):
            if pos == len(request_segments):
                return False
            if request_segments[pos] != name:
                return False
        return True

    @staticmethod
    def _pageid_to_namespace(pageid):
        """Return a list of namespace elements for pageid."""
        # Normalise delimiters.
        pageid = pageid.replace('#', ':')
        # Create a list to walk, empty namespaces are elided.
        return [name for name in pageid.split(':') if name]

    @cachedproperty
    def _request_pageid_namespace(self):
        return tuple(self._pageid_to_namespace(
            self._request._orig_env.get('launchpad.pageid', '')))


class ScopeWithPerson(BaseScope):
    """An abstract base scope that matches on the current user of the request.

    Intended for subclassing, not direct use.
    """

    def __init__(self, get_person):
        self._get_person = get_person

    @cachedproperty
    def person(self):
        return self._get_person()


class TeamScope(ScopeWithPerson):
    """A user's team memberships.

    Team ID scopes are written as 'team:' + the team name to match.

    The scope 'team:launchpad-beta-users' will match members of the team
    'launchpad-beta-users'.

    The constructor takes a callable that returns the currently logged in
    person because Scopes are set up very early in the request publication
    process -- in particular, before authentication has happened.
    """

    pattern = r'team:'

    def lookup(self, scope_name):
        """Is the given scope a team membership?

        This will do a two queries, so we probably want to keep the number of
        team based scopes in use to a small number. (Person.inTeam could be
        fixed to reduce this to one query).
        """
        if self.person is not None:
            team_name = scope_name[len('team:'):]
            return self.person.inTeam(team_name)


class UserSliceScope(ScopeWithPerson):
    """Selects a slice of all users based on their id.

    Written as 'userslice:a,b' with 0<=a<b will slice the entire user
    population into b approximately equal sub-populations, and then take the
    a'th zero-based index.

    For example, to test a new feature for 1% of users, and a different
    version of it for a different 1% you can use `userslice:10,100` and
    userslice:20,100`.

    You may wish to avoid using the same a or b for different rules so that
    some users don't have all the fun by being in eg 0,100.
    """

    pattern = r'userslice:(\d+),(\d+)'

    def lookup(self, scope_name):
        match = self.compiled_pattern.match(scope_name)
        if not match:
            return  # Shouldn't happen...
        try:
            modulus = int(match.group(1))
            divisor = int(match.group(2))
        except ValueError:
            return
        person_id = self.person.id
        return (person_id % divisor) == modulus


class ServerScope(BaseScope):
    """Matches the current server.

    For example, the scope server.lpnet is active when is_lpnet is set to True
    in the Launchpad configuration.
    """

    pattern = r'server\.'

    def lookup(self, scope_name):
        """Match the current server as a scope."""
        server_name = scope_name.split('.', 1)[1]
        try:
            return canonical.config.config['launchpad']['is_' + server_name]
        except KeyError:
            pass
        return False


class ScriptScope(BaseScope):
    """Matches the name of the currently running script.

    For example, the scope script:embroider is active in a script called
    "embroider."
    """

    pattern = r'script:'

    def __init__(self, script_name):
        self.script_scope = self.pattern + script_name

    def lookup(self, scope_name):
        """Match the running script as a scope."""
        return scope_name == self.script_scope


class FixedScope(BaseScope):
    """A scope that matches an exact value.

    Functionally `ScriptScope` and `DefaultScope` are equivalent to instances
    of this class, but their docstings are used on the +feature-info page.
    """

    def __init__(self, scope):
        self.pattern = re.escape(scope) + '$'

    def lookup(self, scope_name):
        return True


# These are the handlers for all of the allowable scopes, listed here so that
# we can for example show all of them in an admin page.  Any new scope will
# need a scope handler and that scope handler has to be added to this list.
# See BaseScope for hints as to what a scope handler should look like.
HANDLERS = set([DefaultScope, PageScope, TeamScope, ServerScope, ScriptScope])


class MultiScopeHandler():
    """A scope handler that combines multiple `BaseScope`s.

    The ordering in which they're added is arbitrary, because precedence is
    determined by the ordering of rules.
    """

    def __init__(self, scopes):
        self.handlers = scopes

    def _findMatchingHandlers(self, scope_name):
        """Find any handlers that match `scope_name`."""
        return [
            handler
            for handler in self.handlers
                if handler.compiled_pattern.match(scope_name)]

    def lookup(self, scope_name):
        """Determine if scope_name applies.

        This method iterates over the configured scope handlers until it
        either finds one that claims the requested scope name matches,
        or the handlers are exhausted, in which case the
        scope name is not a match.
        """
        matching_handlers = self._findMatchingHandlers(scope_name)
        for handler in matching_handlers:
            if handler.lookup(scope_name):
                return True

        # If we didn't find at least one matching handler, then the
        # requested scope is unknown and we want to record the scope for
        # the +flag-info page to display.
        if len(matching_handlers) == 0:
            undocumented_scopes.add(scope_name)


default_scopes = (DefaultScope(),)


class ScopesFromRequest(MultiScopeHandler):
    """Identify feature scopes based on request state.

    Because the feature controller is constructed very very early in the
    publication process, this needs to be very careful about looking at the
    request -- in particular, this is called before authentication happens.
    """

    def __init__(self, request):
        def person_from_request():
            return IPerson(request.principal, None)
        scopes = list(default_scopes)
        scopes.extend([
            PageScope(request),
            ServerScope(),
            TeamScope(person_from_request),
            UserSliceScope(person_from_request),
            ])
        super(ScopesFromRequest, self).__init__(scopes)


class ScopesForScript(MultiScopeHandler):
    """Identify feature scopes for a given script."""

    def __init__(self, script_name):
        scopes = list(default_scopes)
        scopes.append(ScriptScope(script_name))
        super(ScopesForScript, self).__init__(scopes)