-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapt-local.py
257 lines (202 loc) · 7.22 KB
/
apt-local.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
#!/usr/bin/env python3
# apt-local - manage local apt cache and repository
# Copyright © 2014 J. Victor Martins <jvdm@sdf.org>.
#
# This file is distributed under the "Do What the Fuck You Want To"
# Public License, Version 2 (http://www.wtfpl.net/).
import sys
import os
import errno
import apt_pkg
import apt
from apt.cache import Filter, FilteredCache
from apt.progress.text import OpProgress, AcquireProgress
def init(arch, cache_dir):
# Clear APT and Dir config trees to avoid system configuration:
del apt_pkg.config['APT']
del apt_pkg.config['Dir']
del apt_pkg.config['Dpkg']
# Initialize apt configuration, use our local apt cache hierarchy:
apt_opts = []
for opt, val in { 'APT::Architecture': arch,
'APT::Architectures::': '',
'Dir': cache_dir,
'Dir::State::Status': 'dpkg.status',
'Acquire::Languages': 'none',
}.items():
apt_pkg.config.set(opt, val)
apt_opts.append('%s=%s' % (opt, val))
apt_pkg.init_config()
apt_pkg.init_system()
def cmd_update(opts):
"""Create the APT database hierarchy, and resynchronize it."""
def mkdir_p(path):
try:
os.makedirs(path)
except OSError as exc:
if exc.errno == errno.EEXIST and os.path.isdir(path):
pass
# Create the apt cache directories:
for diropt in ('Dir::Etc::sourceparts',
'Dir::State::lists',
'Dir::State::mirrors',
'Dir::Cache::archives',):
mkdir_p(apt_pkg.config.find_dir(diropt))
# Initialize some required files:
import shutil
for fileopt, content in {
'Dir::State::Status': '',
'Dir::Etc::sourcelist': opts.sourcelist.read()
}.items():
filepath = apt_pkg.config.find_file(fileopt)
with open(filepath, 'w') as stream:
if content:
stream.write(content + '\n')
cache = apt.Cache(progress=OpProgress(outfile=sys.stderr))
cache.update(AcquireProgress(outfile=sys.stderr))
return 0
def iter_pkg_versions(cache, pkg_names):
for name in pkg_names:
for sep in ('=', '_'):
if sep in name:
name, version = name.split(sep)
pkg = cache[name]
pkg.candidate = cache[name].versions[version]
break
else:
pkg = cache[name]
yield pkg
def cmd_install(opts):
class InstallFilter(Filter):
def apply(self, pkg):
return pkg.marked_install
cache = apt.Cache(progress=OpProgress(outfile=sys.stderr))
with cache.actiongroup():
# Install all essential and required by default:
for pkg in cache:
if pkg.essential or pkg.candidate.priority == 'required':
pkg.mark_install()
if opts.file:
for stream in opts.file:
pkgs = [l.strip() for l in list(stream)]
else:
pkgs = opts.packages
for pkg in iter_pkg_versions(cache, pkgs):
pkg.mark_install()
fcache = FilteredCache(cache)
fcache.set_filter(InstallFilter())
data = []
for pkg in fcache:
print('%s=%s' % (pkg.name, pkg.candidate.version), file=opts.output)
return 0
def cmd_fetch(opts):
cache = apt.Cache(progress=OpProgress())
if opts.file:
pkgs = [l.strip() for l in list(open(opts.packages[0], 'r'))]
else:
pkgs = opts.packages
for pkg in iter_pkg_versions(cache, pkgs):
pkg.candidate.fetch_binary(destdir=opts.dest)
return 0
def cmd_show(opts):
cache = apt.Cache()
for pkg in iter_pkg_versions(cache, opts.packages):
if opts.format:
print(opts.format % pkg.candidate.record)
else:
print(pkg.candidate.record)
return 0
def parse_args(args):
import argparse
import subprocess
import re
class CustomFormatter(argparse.ArgumentDefaultsHelpFormatter):
def _format_action_invocation(self, action):
if not action.option_strings:
default = self._get_default_metavar_for_positional(action)
metavar, = self._metavar_formatter(action, default)(1)
return metavar
else:
parts = []
if action.nargs == 0:
parts.extend(action.option_strings)
else:
default = self._get_default_metavar_for_optional(action)
args_string = self._format_args(action, default)
for optstr in action.option_strings:
parts.append(optstr)
parts[-1] += ' %s' % args_string
return '|'.join(parts)
parser = argparse.ArgumentParser(
description='APT cache querying and searching for local databases',
formatter_class=CustomFormatter,
)
# Global options:
parser.add_argument('-a', '--arch',
help='use <arch> instead of APT default', choices=('armhf','amd64'),
metavar='ARCH',
default=apt_pkg.config.get('APT::Architecture'))
parser.add_argument('-c', '--cache',
help='cache directory', metavar='CACHE-DIR',
default=os.path.join(os.environ['HOME'], '.cache/apt-local'))
# Subparsers for commands:
subparser = parser.add_subparsers(
title='Actions',
metavar='ACTION',
)
# http://bugs.python.org/issue9253#msg186387
subparser.required=True
parser_update = subparser.add_parser(
'update',
formatter_class=CustomFormatter,
help='Update the APT database',
)
parser_install = subparser.add_parser(
'install',
formatter_class=CustomFormatter,
help='Return a list of packages to install'
)
parser_fetch = subparser.add_parser(
'fetch',
formatter_class=CustomFormatter,
help='Fetch binary packages',
)
parser_show = subparser.add_parser(
'show',
formatter_class=CustomFormatter,
help='Show package information',
)
# Update command:
parser_update.add_argument(
'sourcelist',
type=argparse.FileType('r'),
help='Sourcelist to use instead of the system default',
)
parser_update.set_defaults(func=cmd_update)
# Install command:
parser_install.add_argument('packages', nargs='*')
parser_install.add_argument(
'-o', '--output',
type=argparse.FileType(mode='w'),
default='-')
parser_install.add_argument(
'-f', '--file',
action='append',
type=argparse.FileType('r'))
parser_install.set_defaults(func=cmd_install)
# Fetch command:
parser_fetch.add_argument('dest')
parser_fetch.add_argument('packages', nargs='+')
parser_fetch.add_argument('-f', '--file', action='store_true')
parser_fetch.set_defaults(func=cmd_fetch)
# Show command:
parser_show.add_argument('packages', nargs='+')
parser_show.add_argument('-f', '--format')
parser_show.set_defaults(func=cmd_show)
return parser.parse_args(args)
def main(args):
opts = parse_args(args)
init(opts.arch, opts.cache)
return opts.func(opts)
if __name__ == '__main__':
sys.exit(main(sys.argv[1:]))