Skip to content

Instantly share code, notes, and snippets.

@NBonaparte
Last active August 13, 2020 22:12
Show Gist options
  • Save NBonaparte/5bcde22022a0f9269e5dd50f52c46aaf to your computer and use it in GitHub Desktop.
Save NBonaparte/5bcde22022a0f9269e5dd50f52c46aaf to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
import os
pacman_path = "/var/lib/pacman/local"
pkgs = []
class bcolors:
HEADER = '\033[95m'
OKBLUE = '\033[94m'
OKGREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
def file_units(size):
if size > 2**30:
return f"{size/2**30:>5.1f} GiB"
elif size > 2**20:
return f"{size/2**20:>5.0f} MiB"
elif size > 2**10:
return f"{size/2**10:>5.0f} KiB"
else:
return f"{size:>5d} B"
for pkg in os.listdir(pacman_path):
path = os.path.join(pacman_path, pkg)
if os.path.isdir(path):
with open(os.path.join(path, "desc")) as f:
last_line = ""
for line in f:
if last_line.rstrip() == "%NAME%":
name = line.rstrip()
elif last_line.rstrip() == "%VERSION%":
version = line.rstrip()
elif last_line.rstrip() == "%SIZE%":
size = int(line.rstrip())
break
last_line = line
pkgs.append((name, version, size))
pkgs.sort(key=lambda x: x[2])
for pkg in pkgs:
print(f"{file_units(pkg[2])}: {bcolors.BOLD}{pkg[0]} {bcolors.OKGREEN}{pkg[1]}{bcolors.ENDC}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment