Skip to content

Instantly share code, notes, and snippets.

@kaymccormick
Created November 17, 2018 22:58
Show Gist options
  • Save kaymccormick/a7001f3b14ae1b4ce9aff08a8d3e29aa to your computer and use it in GitHub Desktop.
Save kaymccormick/a7001f3b14ae1b4ce9aff08a8d3e29aa to your computer and use it in GitHub Desktop.
Use python-apt to extract disttributed config files. This will fill up your disk with package files!
#!/usr/bin/python3
import shutil
import apt_pkg
import apt_inst
import apt
import apt.debfile
import apt.progress
import aptsources.distinfo
import os.path
from pathlib import Path
cache = apt.Cache()
for pkg in cache:
if not pkg.installed:
continue
ver = pkg.installed
try:
x = ver.fetch_binary("pkg")
except:
continue
package = apt.debfile.DebPackage(x)
ar = apt_inst.DebFile(x)
tarfile = ar.data
for file in pkg.installed_files:
if not os.path.isdir(file):
if file.startswith('/etc'):
p = Path("." + file)
p2 = Path("./temp").joinpath(pkg.name)
p3 = p2.joinpath(p)
if not p3.parent.exists():
p3.parent.mkdir(parents=True)
try:
data = tarfile.extractdata(file[1:])
with p3.open('wb') as f:
f.write(data)
except Exception as ex:
print(ex)
# shutil.copyfile(file, p3)
exit
apt_pkg.init()
cache = apt_pkg.Cache(None)
for k in cache.packages:
if k.current_ver:
print(k.name)
ver = k.current_ver
for (file, i) in ver.file_list:
print(i, "\t", file.filename)
if False:
cache = apt.Cache()
cache.update()
cache.open(None)
cache.upgrade(True)
cache.commit()
@paigethylamine
Copy link

paigethylamine commented Dec 12, 2018

la la la pip search * | awk '{print $1}' | parallel pip install --user {} (use a test user acc that way you can rm -rf the ~/.local because it installs a bunch of stuff that, frankly is worth knowing about but isn't conducive to what you want installed in your PATH. It will install packages that will get auto-loaded by python every time you run it without requiring them, etc.) Also it probably shouldn't work because pip iirc doesn't lock it's own package db, so its not a good idea to run other pip instances in parallel when you're already installing.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment