-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapt_repo.py
277 lines (229 loc) · 8.73 KB
/
apt_repo.py
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
#!/usr/bin/python3
# Copyright (C) 2018 Jelmer Vernooij <jelmer@jelmer.uk>
#
# 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 2 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, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
import errno
import os
import pwd
import shutil
import subprocess
import tempfile
from threading import Semaphore
from typing import Optional
from debian.deb822 import Dsc, Deb822
from breezy.errors import DependencyNotPresent
class NoAptSources(Exception):
"""No apt sources were configured."""
class AptSourceError(Exception):
"""An error occured while running 'apt source'."""
def __init__(self, reason):
self.reason = reason
def _convert_apt_pkg_error(e):
if '28: No space left on device':
return IOError(errno.ENOSPC, str(e))
return e
class Apt:
def __enter__(self):
raise NotImplementedError(self.__enter__)
def __exit__(self, exc_tp, exc_val, exc_tb):
raise NotImplementedError(self.__exit__)
def iter_source_by_name(self, source_name):
for source in self.iter_sources():
if source['Package'] == source_name:
yield source
def iter_sources(self):
raise NotImplementedError(self.iter_sources)
def iter_binaries(self):
raise NotImplementedError(self.iter_binaries)
def iter_binary_by_name(self, binary_name):
for binary in self.iter_binaries():
if binary['Package'] == binary_name:
yield binary
def retrieve_orig(self, source_name, target_directory,
orig_version=None):
raise NotImplementedError(self.retrieve_orig)
def retrieve_source(self, source_name, target_directory,
source_version=None):
raise NotImplementedError(self.retrieve_source)
_apt_semaphore = Semaphore()
class LocalApt(Apt):
def __init__(self, rootdir=None):
self.apt_pkg = None
self._rootdir = rootdir
def __repr__(self):
return "{}({!r})".format(type(self).__name__, self._rootdir)
def __enter__(self):
try:
import apt_pkg
except ImportError as e:
raise DependencyNotPresent('apt_pkg', e) from e
import apt
self.apt_pkg = apt_pkg
self.apt_pkg.init()
try:
self.cache = apt.Cache(rootdir=self._rootdir)
except apt_pkg.Error as e:
raise _convert_apt_pkg_error(e) from e
return self
def _set_dir(self):
if self._rootdir is not None:
self.apt_pkg.config.set("Dir", self._rootdir)
else:
self.apt_pkg.config.set("Dir", '/')
def __exit__(self, exc_tp, exc_val, exc_tb):
return False
def iter_sources(self):
with _apt_semaphore:
self._set_dir()
try:
sources = self.apt_pkg.SourceRecords()
except SystemError as e:
raise NoAptSources() from e
sources.restart()
while sources.step():
yield Dsc(sources.record)
def iter_source_by_name(self, source_name):
with _apt_semaphore:
self._set_dir()
try:
sources = self.apt_pkg.SourceRecords()
except SystemError as e:
raise NoAptSources() from e
sources.restart()
while sources.lookup(source_name):
yield Dsc(sources.record)
def iter_binaries(self):
with _apt_semaphore:
self._set_dir()
for pkg in self.cache:
for version in pkg.versions:
yield Deb822(version._records.record)
def iter_binary_by_name(self, binary_name):
with _apt_semaphore:
self._set_dir()
try:
pkg = self.cache[binary_name]
except KeyError:
pass
else:
for version in pkg.versions:
yield Deb822(version._records.record)
def retrieve_source(self, package_name, target, source_version=None,
tar_only=False):
self._run_apt_source(package_name, target, source_version,
tar_only=tar_only)
def _get_command(self, package, version_str=None, tar_only=False):
args = ['apt', 'source', '-d']
if self._rootdir is not None:
args.append('-oDir=%s' % self._rootdir)
if tar_only:
args.append('--tar-only')
args.extend([
'-y', '--only-source',
('{}={}'.format(package, version_str))
if version_str is not None else package])
return args
def _run_apt_source(self, package: str, target_dir,
version_str: Optional[str] = None,
tar_only: bool = False):
command = self._get_command(package, version_str, tar_only=tar_only)
try:
subprocess.run(
command, cwd=target_dir,
capture_output=True,
check=True)
except subprocess.CalledProcessError as e:
stderr = e.stderr.splitlines()
if stderr[-1] == (
b"E: You must put some 'source' URIs in your sources.list"
):
raise NoAptSources() from e
CS = b"\x1b[1;31mE: \x1b[0m"
CE = b"\x1b[0m"
if stderr[-1] == (
CS + b"You must put some 'deb-src' URIs in your sources.list" +
CE
):
raise NoAptSources() from e
if stderr[-1].startswith(b"E: "):
raise AptSourceError(stderr[-1][3:].decode()) from e
if stderr[-1].startswith(CS):
raise AptSourceError(
stderr[-1][len(CS): -len(CE)].decode()) from e
raise AptSourceError(
[line.decode("utf-8", "surrogateescape") for line in stderr]
) from e
class RemoteApt(LocalApt):
def __init__(self, mirror_uri, distribution=None, components=None,
key_path=None):
super().__init__()
self.mirror_uri = mirror_uri
self.distribution = distribution
self.components = components
self.key_path = key_path
self._rootdir = None
def __repr__(self):
return (
"{}({!r}, distribution={!r}, components={!r}, key_path={!r})"
.format(
type(self).__name__, self.mirror_uri, self.distribution,
self.components, self.key_path))
def __enter__(self):
self._rootdir = tempfile.mkdtemp()
aptdir = os.path.join(self._rootdir, 'etc', 'apt')
os.makedirs(aptdir)
if self.key_path:
tag = "[signed-by=%s]" % self.key_path
else:
tag = "[trusted=yes]"
with open(os.path.join(aptdir, 'sources.list'), 'w') as f:
f.write('deb {} {} {} {}\n'.format(
tag, self.mirror_uri, self.distribution,
' '.join(self.components)))
f.write('deb-src {} {} {} {}\n'.format(
tag, self.mirror_uri, self.distribution,
' '.join(self.components)))
try:
import apt
except ImportError as e:
raise DependencyNotPresent('apt', e) from e
try:
import apt_pkg
except ImportError as e:
raise DependencyNotPresent('apt_pkg', e) from e
self.apt_pkg = apt_pkg
self.apt_pkg.init()
try:
self.cache = apt.Cache(rootdir=self._rootdir)
except apt_pkg.Error as e:
raise _convert_apt_pkg_error(e) from e
self._set_dir()
self.cache.update()
return self
def _set_dir(self):
try:
username = pwd.getpwuid(os.getuid()).pw_name
except KeyError:
pass
else:
self.apt_pkg.config.set("APT::Sandbox::User", username)
self.apt_pkg.config.set("Dir", self._rootdir)
def __exit__(self, exc_tp, exc_val, exc_tb):
shutil.rmtree(self._rootdir)
return False
@classmethod
def from_string(cls, text, key_path=None):
(mirror_uri, distribution, rest) = text.split(' ', 2)
return cls(mirror_uri, distribution, rest.split(), key_path=key_path)