Skip to content

Instantly share code, notes, and snippets.

@AllanRPereira
Last active October 4, 2024 23:46
Show Gist options
  • Save AllanRPereira/13c83aab80bb6bebc651fb570e6b9094 to your computer and use it in GitHub Desktop.
Save AllanRPereira/13c83aab80bb6bebc651fb570e6b9094 to your computer and use it in GitHub Desktop.
Cálcula espaço gasto por pacotes instalados utilizando o gerenciador apt
"""
Software para listar o tamanho de todos os softwares instalados
com o gerenciador de pacotes apt
"""
import subprocess
from json import dump
pacotes = subprocess.run(["dpkg", "-l"], capture_output=True)
lista_pacotes = []
for pacote in pacotes.stdout.decode("utf-8").split("\n")[5:]:
try:
tmp = []
for elemento in pacote.split(" "):
if (elemento != ""):
tmp.append(elemento)
if len(tmp) > 1:
lista_pacotes.append(tmp[1])
except:
pass
tamanhos = {}
for pacote in lista_pacotes:
dpkg_process = subprocess.Popen(["dpkg", "-s", pacote], stdout=subprocess.PIPE, text=True)
dados = subprocess.Popen(["grep", "Size"], stdin=dpkg_process.stdout, stdout=subprocess.PIPE, text=True)
output, error = dados.communicate()
output = output.split(" ")[1]
tamanhos[pacote] = round(int(output) / 1024)
with open("dados.json", "w") as file:
dump(tamanhos, file)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment