Paste P6466

apt-upgrade.py
ActivePublic

Authored by aborrero on Dec 14 2017, 7:01 PM.
#!/usr/bin/env python3
import argparse
import subprocess
import sys
import os
import tempfile
def register_all():
register_stretchsecurity()
def register_stretchsecurity():
name = "stretch-security"
repo = """
deb http://security.debian.org stretch/updates main contrib non-free
deb http://deb.debian.org/debian/ stretch main contrib non-free
"""
pref = """
Package: *
Pin: release l=Debian-Security
Pin-Priority: 990
Package: *
Pin: release l=Debian
Pin-Priority: 99
"""
register_info(name, repo, pref)
InfoRegister = {}
class Info:
def __init__(self, repo, pref):
self.repo = repo
self.pref = pref
def register_info(name, repo, pref):
new = Info(repo, pref)
InfoRegister[name] = new
def exec_cmd(cmd, verbose):
if verbose:
print(cmd)
ret = subprocess.call(cmd, shell=True)
if ret != 0:
print("something failed")
sys.exit(ret)
def generate_args(repo_file, pref_file):
args = " -o Dir::Etc::SourceList=" + repo_file
args = args + " -o Dir::Etc::Preferences=" + pref_file
args = args + " -o Dir::Etc::SourceParts=/dev/null"
args = args + " -q -y"
args = args + " -o Dpkg::Options::='--force-confdef'"
args = args + " -o Dpkg::Options::='--force-confold'"
return args
def exec_apt_update(args, verbose):
cmd = "DEBIAN_FRONTEND=noninteractive apt-get update" + args
exec_cmd(cmd, verbose)
def exec_apt_upgrade(args, verbose):
cmd = "DEBIAN_FRONTEND=noninteractive apt-get upgrade" + args
exec_cmd(cmd, verbose)
def run(name, verbose, simulate):
repo_file = tempfile.NamedTemporaryFile(mode='r+', encoding='utf-8')
repo_file.write(InfoRegister[name].repo)
pref_file = tempfile.NamedTemporaryFile(mode='r+', encoding='utf-8')
pref_file.write(InfoRegister[name].pref)
args = generate_args(repo_file.name, pref_file.name)
exec_apt_update(args, verbose)
if simulate:
args = args + " -s"
exec_apt_upgrade(args, verbose)
repo_file.close()
pref_file.close()
def list_registers():
for name in InfoRegister:
print("\t\t\t" + name)
print("-" * 80)
print(InfoRegister[name].repo.strip())
print("-" * 80)
print(InfoRegister[name].pref.strip())
print("-" * 80)
def main():
parser = argparse.ArgumentParser(description="Run a targetted upgrade of packages")
parser.add_argument('--list', action='store_true', help="list the repos this script can run with")
parser.add_argument('--verbose', action='store_true', help="be verbose")
parser.add_argument('-s', action='store_true', help="simulate operations, pass '-s' to apt-get")
parser.add_argument('target', help="target source repository/channel to upgrade from")
args = parser.parse_args()
register_all()
if args.list:
list_registers()
sys.exit(0)
for name in InfoRegister:
if name == args.target:
run(name, args.verbose, args.s)
sys.exit(0)
print("I don't know how to handle repo: " +args.target)
print("Use --list to know the repos this script can run with")
sys.exit(1)
if __name__ == "__main__":
main()
aborrero created this paste.Dec 14 2017, 7:01 PM
aborrero updated the paste's language from autodetect to python.