-
Notifications
You must be signed in to change notification settings - Fork 0
/
aptli
288 lines (248 loc) · 9.43 KB
/
aptli
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
#!/usr/bin/env python3
import os
import glob
import json
import shutil
import argparse
import subprocess
import collections
cwd = os.getcwd()
local_home = os.path.expanduser('~/.local')
aptli_home = os.path.join(local_home, 'aptli')
db_path = os.path.join(aptli_home, 'db.json')
db_stage_path = os.path.join(aptli_home, 'db-stage.json')
cache_path = os.path.join(aptli_home, 'cache')
path_table = [
'usr/local/sbin',
'usr/local/bin',
'usr/sbin',
'usr/bin',
'sbin',
'bin',
'usr/games',
'usr/local/games'
]
ld_table = [
x.split(':')[0]
for x in
subprocess.check_output(['ldconfig', '-vN']).decode().strip().splitlines()
if ':' in x
]
force_ld_paths = ['/usr/lib', '/lib', '/lib64', '/usr/lib64']
for force_path in force_ld_paths:
if force_path not in ld_table:
ld_table.append(force_path)
ld_table = [x[1:] if x.startswith('/') else x for x in ld_table]
warnings = []
class Database:
def __init__(self, filepath=None) -> None:
self.packages = []
self.file_refs = collections.defaultdict(list)
self.journal_files = []
self.install_state = False
if filepath is not None:
self.load(filepath)
def load(self, filepath):
with open(filepath) as fi:
sdict = json.load(fi)
self.packages.extend(sdict['packages'])
self.file_refs.update(sdict['file_refs'])
self.journal_files.extend(sdict['journal_files'])
self.install_state = sdict['install_state']
def write(self):
with open(db_stage_path, 'w') as fo:
json.dump(self.__dict__, fo, indent=2)
# W1
if os.path.exists(db_path):
os.unlink(db_path)
# W2
os.rename(db_stage_path, db_path)
def take_care_of_journal(db):
# type: (Database) -> None
for file in db.journal_files:
if file not in db.file_refs:
fullpath = os.path.abspath(os.path.join(local_home, file))
if os.path.isfile(fullpath):
os.unlink(fullpath)
cpath = os.path.dirname(fullpath)
while not os.listdir(cpath):
os.rmdir(cpath)
cpath = os.path.dirname(cpath)
if len(db.journal_files):
db.journal_files.clear()
db.write()
def recover_database():
if os.path.exists(db_stage_path):
# <= W1 or at W2.
if not os.path.exists(db_path):
# at W2. recover from staging file.
os.rename(db_stage_path, db_path)
else:
# <= W1, discard.
os.unlink(db_stage_path)
if os.path.exists(db_path):
db = Database(db_path)
else:
db = Database()
take_care_of_journal(db)
return db
def install_aptli():
install_target = os.path.expanduser('~/.local/usr/bin')
install_path = os.path.join(install_target, 'aptli')
os.makedirs(install_target, exist_ok=True)
shutil.copyfile(__file__, install_path)
os.chmod(install_path, 0o755)
print("Installed aptli file.")
def install_paths():
appendee = '\nexport PATH=' + ':'.join([os.path.join(local_home, p) for p in path_table]) + ':$PATH'
appendee += '\nexport LD_LIBRARY_PATH=$LD_LIBRARY_PATH:' + ':'.join([os.path.join(local_home, p) for p in ld_table])
appendee += '\n'
with open(os.path.expanduser('~/.bashrc'), 'a+') as fo:
fo.write(appendee)
print('Configuration of aptli is done. You may need to re-login into the shell to see the changes.')
def download_deb(package):
subprocess.check_call(['apt-get', 'download', package], cwd=cache_path)
def packages_from_packlist(pliststr):
# type: (str) -> list[str]
if len(pliststr.strip()) == 0:
return []
return [
spec.split()[0] for spec in
pliststr.split(',')
]
def get_contents(package):
fsys_stream = subprocess.Popen(['dpkg-deb', '--fsys-tarfile', package], stdout=subprocess.PIPE)
contents_list = subprocess.check_output(['tar', 't'], stdin=fsys_stream.stdout).decode().strip().splitlines()
return [os.path.relpath(os.path.join(local_home, c), local_home) for c in contents_list]
def do_install_packages(db, syspkglist, packages):
# type: (Database, list, list) -> None
print("Installing:")
for package in packages:
print('\t', os.path.basename(package))
assert len(db.journal_files) == 0, 'internal error: non-empty journal before unit install'
for package in packages:
db.journal_files.extend(get_contents(package))
db.write()
for package in packages:
subprocess.check_call(['dpkg-deb', '-x', package, local_home])
db.journal_files.clear()
package_names = []
for package in packages:
package_name = subprocess.check_output(['dpkg-deb', '-f', package, 'Package']).decode().strip()
package_names.append(package_name)
for file in get_contents(package):
db.file_refs[file].append(package_name)
db.packages.append(package_name)
db.write()
print('Installed:', ', '.join(package_names))
for package_name, package in zip(package_names, packages):
recommends = packages_from_packlist(subprocess.check_output(['dpkg-deb', '-f', package, 'Recommends']).decode())
for rec in recommends:
if rec not in syspkglist and rec not in db.packages:
warnings.append(
'recommended package %s from %s does not exist. This may indicate problems of the system.'
% (rec, package_name)
)
def install_prepare(db, syspkglist, package, stage):
# type: (Database, list, str, list) -> list
if package in syspkglist:
print('Already installed (system):', package)
return []
if package in db.packages:
print('Already installed (aptli):', package)
return []
if not os.path.isfile(package):
package_cand = glob.glob(os.path.join(cache_path, package + '_*.deb'))
if len(package_cand) == 0:
download_deb(package)
package_cand = glob.glob(os.path.join(cache_path, package + '_*.deb'))
assert len(package_cand) > 0, 'internal error: deb file not found after downloading'
package = max(package_cand, key=lambda x: os.stat(x).st_mtime)
package = os.path.abspath(package)
print('Installing file:', os.path.basename(package))
pre_depends = packages_from_packlist(subprocess.check_output(['dpkg-deb', '-f', package, 'Pre-Depends']).decode())
for depend in pre_depends:
print('Installing pre-dependency:', os.path.basename(package), '->', depend)
install(db, syspkglist, depend)
to_install = [package]
depends = packages_from_packlist(subprocess.check_output(['dpkg-deb', '-f', package, 'Depends']).decode())
for depend in depends:
if depend in stage:
continue
stage.append(depend)
print('Preparing dependency:', os.path.basename(package), '->', depend)
to_install.extend(install_prepare(db, syspkglist, depend, stage))
to_install = [t for t in to_install if t not in db.packages] # filter installed pre-depends
return to_install
def install(db, syspkglist, package):
do_install_packages(
db, syspkglist,
install_prepare(db, syspkglist, package, [])
)
def remove(db, syspkglist, package):
# type: (Database, list, str) -> None
assert len(db.journal_files) == 0, 'internal error: non-empty journal before remove'
if package not in db.packages:
if package not in syspkglist:
raise ValueError('Attempting to remove non-installed package', package)
raise ValueError('Package to remove is not installed by aptli', package)
print('Removing:', package)
print('Files to remove:')
for file, pkgsublist in db.file_refs.items():
if package in pkgsublist:
pkgsublist.remove(package)
if len(pkgsublist) == 0:
db.journal_files.append(file)
print('\t', file)
for file in db.journal_files:
popped = db.file_refs.pop(file)
assert len(popped) == 0, 'internal error: removing referenced files'
db.packages.remove(package)
db.write()
take_care_of_journal(db)
print('Removed:', package)
def main():
argp = argparse.ArgumentParser('aptli')
argp.add_argument(
'packages', metavar='pkg', type=str, nargs='*',
help='package name(s) or `.deb` file(s) to be installed'
)
argp.add_argument(
'--remove', action='store_true',
help='remove instead of installing packages (only packages installed with `aptli` can be removed)'
)
argp.add_argument(
'--upgrade-aptli', action='store_true',
help='replace installed aptli with the executing aptli'
)
argp.add_argument(
'-y', '--yes', action='store_true'
)
args = argp.parse_args()
os.makedirs(cache_path, exist_ok=True)
db = recover_database()
if not db.install_state:
db.install_state = True
db.write()
install_aptli()
install_paths()
elif args.upgrade_aptli:
install_aptli()
pkglist = [
x.split()[0]
for x in
subprocess.check_output(['dpkg', '--get-selections']).decode().strip().splitlines()
if 'deinstall' not in x
]
for pkg in args.packages:
if args.remove:
remove(db, pkglist, pkg)
else:
install(db, pkglist, pkg)
if __name__ == '__main__':
try:
main()
for warning in warnings:
print('Warning:', warning)
finally:
os.chdir(cwd)