Paste P6466

apt-upgrade.py
ActivePublic

Authored by aborrero on Dec 14 2017, 7:01 PM.
1#!/usr/bin/env python3
2
3import argparse
4import subprocess
5import sys
6import os
7import tempfile
8
9def register_all():
10register_stretchsecurity()
11
12def register_stretchsecurity():
13name = "stretch-security"
14repo = """
15deb http://security.debian.org stretch/updates main contrib non-free
16deb http://deb.debian.org/debian/ stretch main contrib non-free
17"""
18pref = """
19Package: *
20Pin: release l=Debian-Security
21Pin-Priority: 990
22
23Package: *
24Pin: release l=Debian
25Pin-Priority: 99
26"""
27register_info(name, repo, pref)
28
29InfoRegister = {}
30class Info:
31def __init__(self, repo, pref):
32self.repo = repo
33self.pref = pref
34
35def register_info(name, repo, pref):
36new = Info(repo, pref)
37InfoRegister[name] = new
38
39def exec_cmd(cmd, verbose):
40if verbose:
41print(cmd)
42ret = subprocess.call(cmd, shell=True)
43if ret != 0:
44print("something failed")
45sys.exit(ret)
46
47def generate_args(repo_file, pref_file):
48args = " -o Dir::Etc::SourceList=" + repo_file
49args = args + " -o Dir::Etc::Preferences=" + pref_file
50args = args + " -o Dir::Etc::SourceParts=/dev/null"
51args = args + " -q -y"
52args = args + " -o Dpkg::Options::='--force-confdef'"
53args = args + " -o Dpkg::Options::='--force-confold'"
54return args
55
56def exec_apt_update(args, verbose):
57cmd = "DEBIAN_FRONTEND=noninteractive apt-get update" + args
58exec_cmd(cmd, verbose)
59
60def exec_apt_upgrade(args, verbose):
61cmd = "DEBIAN_FRONTEND=noninteractive apt-get upgrade" + args
62exec_cmd(cmd, verbose)
63
64def run(name, verbose, simulate):
65repo_file = tempfile.NamedTemporaryFile(mode='r+', encoding='utf-8')
66repo_file.write(InfoRegister[name].repo)
67pref_file = tempfile.NamedTemporaryFile(mode='r+', encoding='utf-8')
68pref_file.write(InfoRegister[name].pref)
69args = generate_args(repo_file.name, pref_file.name)
70exec_apt_update(args, verbose)
71if simulate:
72args = args + " -s"
73exec_apt_upgrade(args, verbose)
74repo_file.close()
75pref_file.close()
76
77def list_registers():
78for name in InfoRegister:
79print("\t\t\t" + name)
80print("-" * 80)
81print(InfoRegister[name].repo.strip())
82print("-" * 80)
83print(InfoRegister[name].pref.strip())
84print("-" * 80)
85
86def main():
87parser = argparse.ArgumentParser(description="Run a targetted upgrade of packages")
88parser.add_argument('--list', action='store_true', help="list the repos this script can run with")
89parser.add_argument('--verbose', action='store_true', help="be verbose")
90parser.add_argument('-s', action='store_true', help="simulate operations, pass '-s' to apt-get")
91parser.add_argument('target', help="target source repository/channel to upgrade from")
92args = parser.parse_args()
93
94register_all()
95
96if args.list:
97list_registers()
98sys.exit(0)
99
100for name in InfoRegister:
101if name == args.target:
102run(name, args.verbose, args.s)
103sys.exit(0)
104
105print("I don't know how to handle repo: " +args.target)
106print("Use --list to know the repos this script can run with")
107sys.exit(1)
108
109if __name__ == "__main__":
110main()
aborrero created this paste.Dec 14 2017, 7:01 PM
aborrero updated the paste's language from autodetect to python.