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
|
# 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 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.
"""
# Enable using with Python 3.7.
from __future__ import annotations
import sqlite3
import threading
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
@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
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
connection: sqlite3.Connection
current_cursor: t.Optional[sqlite3.Cursor] = None
#settings: st.HaketiloGlobalSettings
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]:
"""...."""
start_transaction = transaction and not self.connection.in_transaction
with self.lock:
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
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(
builtin = True,
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 recompute_dependencies(
self,
requirements: t.Iterable[sds.MappingRequirement] = [],
prune_orphans: bool = False
) -> 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) -> None:
"""
Recreation of data structures as done after every recomputation of
dependencies as well as at startup.
"""
...
|