Skip to content

Instantly share code, notes, and snippets.

@Treborium
Created December 23, 2019 09:24
Show Gist options
  • Save Treborium/aacca45a19ed64689642f1e8dc47aaad to your computer and use it in GitHub Desktop.
Save Treborium/aacca45a19ed64689642f1e8dc47aaad to your computer and use it in GitHub Desktop.
Update all packages of a given package manager sequentially
#!/usr/bin/env python3
import subprocess
import platform
import sys
class Update:
def __init__(self, list_packages_cmd: str, update_package_cmd: str):
self.list_packages_cmd = list_packages_cmd
self.update_package_cmd = update_package_cmd
def fetch_upgradable_packages(self):
output = subprocess.run(self.list_packages_cmd.split(), capture_output=True).stdout.decode()
self.packages = [line.split(" ")[0] for line in output.split("\n")]
self.package_count = len(self.packages)
return self
def upgrade_all(self):
for package in self.packages:
self.upgrade(package)
def upgrade(self, package: str):
subprocess.run(f"{self.update_package_cmd} {package}".split())
if __name__ == '__main__':
if len(sys.argv) <= 1:
print("Please pass the command to list upgradable packages and the command to upgrade a single package.")
print("If your package manager is pacman then a valid call would be: ./update.py 'pacman -Qu' 'sudo pacman -S'")
exit("ERROR: Not enough arguments!")
update = Update(sys.argv[1], sys.argv[2])
update.fetch_upgradable_packages().upgrade_all()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment