aboutsummaryrefslogtreecommitdiff
path: root/src/hydrilla/proxy/state_impl/base.py
blob: 7437d520310b540907999b2d1262161cd38cb59b (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
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
# SPDX-License-Identifier: GPL-3.0-or-later

# Haketilo proxy data and configuration (definition of fields of a class that
# will implement HaketiloState).
#
# This file is part of Hydrilla&Haketilo.
#
# Copyright (C) 2022 Wojtek Kosior
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.
#
#
# I, Wojtek Kosior, thereby promise not to sue for violation of this
# file's license. Although I request that you do not make use of this
# code in a proprietary program, I am not going to enforce this in
# court.

"""
This module defines fields that will later be part of a concrete HaketiloState
subtype.
"""

import sqlite3
import threading
import secrets
import webbrowser
import dataclasses as dc
import typing as t

from pathlib import Path
from contextlib import contextmanager
from abc import abstractmethod

from ... import url_patterns
from ... import pattern_tree
from .. import simple_dependency_satisfying as sds
from .. import state as st
from .. import policies


@contextmanager
def temporary_ids_tables(
        cursor: sqlite3.Cursor,
        tables: t.Iterable[tuple[str, t.Iterable[int]]]
) -> t.Iterator[None]:
    created: set[str] = set()

    try:
        for name, ids in tables:
            cursor.execute(
                f'CREATE TEMPORARY TABLE "{name}"(id INTEGER PRIMARY KEY);'
            )
            created.add(name)

            for id in ids:
                cursor.execute(f'INSERT INTO "{name}" VALUES(?);', (id,))

        yield
    finally:
        for name in created:
            cursor.execute(f'DROP TABLE "{name}";')


@dc.dataclass(frozen=True)
class PolicyTree(pattern_tree.PatternTree[policies.PolicyFactory]):
    SelfType = t.TypeVar('SelfType', bound='PolicyTree')

    def register_payload(
            self:        'SelfType',
            pattern:     url_patterns.ParsedPattern,
            payload_key: st.PayloadKey,
            token:       str
    ) -> 'SelfType':
        payload_policy_factory = policies.PayloadPolicyFactory(
            builtin     = False,
            payload_key = payload_key
        )

        policy_tree = self.register(pattern, payload_policy_factory)

        resource_policy_factory = policies.PayloadResourcePolicyFactory(
            builtin     = False,
            payload_key = payload_key
        )

        policy_tree = policy_tree.register(
            pattern.path_append(token, '***'),
            resource_policy_factory
        )

        return policy_tree

def mark_failed_file_installs(
        cursor:      sqlite3.Cursor,
        file_sha256: str,
        repo_id:     int
) -> None:
    cursor.execute(
        '''
        WITH failed_items AS (
                SELECT DISTINCT
                        item_version_id
                FROM
                             files               AS f
                        JOIN file_uses           AS fu USING (file_id)
                        JOIN item_versions_extra AS ive USING (item_version_id)
                WHERE
                        f.sha256 = ? AND f.data IS NULL AND ive.repo_id = ?
        )
        UPDATE
                item_versions
        SET
                installed = 'F'
        WHERE
                item_version_id IN failed_items;
        ''',
        (file_sha256, repo_id)
    )

NoLockArg = t.Union[t.Sequence[int], t.Literal['all_mappings_unlocked']]

PayloadsData = t.Mapping[st.PayloadRef, st.PayloadData]

# mypy needs to be corrected:
# https://stackoverflow.com/questions/70999513/conflict-between-mix-ins-for-abstract-dataclasses/70999704#70999704
@dc.dataclass # type: ignore[misc]
class HaketiloStateWithFields(st.HaketiloState):
    """...."""
    store_dir:      Path
    _listen_host:   str
    _listen_port:   int
    _logger:        st.Logger
    connection:     sqlite3.Connection
    settings:       st.HaketiloGlobalSettings
    current_cursor: t.Optional[sqlite3.Cursor] = None

    secret: bytes = dc.field(default_factory=(lambda: secrets.token_bytes(16)))

    policy_tree:   PolicyTree   = PolicyTree()
    payloads_data: PayloadsData = dc.field(default_factory=dict)

    lock: threading.RLock = dc.field(default_factory=threading.RLock)

    @contextmanager
    def cursor(self, transaction: bool = False) \
        -> t.Iterator[sqlite3.Cursor]:
        with self.lock:
            start_transaction = \
                transaction and not self.connection.in_transaction

            if self.current_cursor is not None:
                yield self.current_cursor
                return

            try:
                self.current_cursor = self.connection.cursor()

                if start_transaction:
                    self.current_cursor.execute('BEGIN TRANSACTION;')

                try:
                    yield self.current_cursor

                    if start_transaction:
                        assert self.connection.in_transaction
                        self.current_cursor.execute('COMMIT TRANSACTION;')
                except:
                    if start_transaction:
                        self.current_cursor.execute('ROLLBACK TRANSACTION;')
                    raise
            except st.FileInstallationError as ex:
                if start_transaction:
                    assert self.current_cursor is not None
                    mark_failed_file_installs(
                        cursor      = self.current_cursor,
                        file_sha256 = ex.sha256,
                        repo_id     = int(ex.repo_id)
                    )
                raise
            finally:
                self.current_cursor = None

    def select_policy(self, url: url_patterns.ParsedUrl) -> policies.Policy:
        """...."""
        with self.lock:
            policy_tree = self.policy_tree

        try:
            best_priority: int                         = 0
            best_policy:   t.Optional[policies.Policy] = None

            for factories_set in policy_tree.search(url):
                for stored_factory in sorted(factories_set):
                    factory = stored_factory.item

                    policy = factory.make_policy(self)

                    if policy.priority > best_priority:
                        best_priority = policy.priority
                        best_policy   = policy
        except Exception as e:
            return policies.ErrorBlockPolicy(error=e)

        if best_policy is not None:
            return best_policy

        if self.get_settings().default_allow_scripts:
            return policies.FallbackAllowPolicy()
        else:
            return policies.FallbackBlockPolicy()

    @abstractmethod
    def import_items(self, malcontent_path: Path, repo_id: int = 1) -> None:
        ...

    @abstractmethod
    def soft_prune_orphan_items(self) -> None:
        ...

    @abstractmethod
    def recompute_dependencies(
            self,
            unlocked_required_mappings: NoLockArg = []
    ) -> None:
        ...

    @abstractmethod
    def pull_missing_files(self) -> None:
        """
        This function checks which packages marked as installed are missing
        files in the database. It attempts to restore integrity by downloading
        the files from their respective repositories.
        """
        ...

    @abstractmethod
    def rebuild_structures(self, *, payloads: bool = True, rules: bool = True) \
        -> None:
        """
        Recreation of data structures as done after every recomputation of
        dependencies as well as at startup.
        """
        ...

    @property
    def listen_host(self) -> str:
        if self._listen_host != '0.0.0.0':
            return '127.0.0.1'

        return self._listen_host

    @property
    def listen_port(self) -> int:
        return self._listen_port

    @property
    def efective_listen_addr(self) -> str:
        effective_host = self._listen_host
        if self._listen_host == '0.0.0.0':
            effective_host = '127.0.0.1'

        return f'http://{effective_host}:{self._listen_port}'

    def launch_browser(self) -> bool:
        return webbrowser.open(self.efective_listen_addr)

    @property
    def logger(self) -> st.Logger:
        return self._logger