]> piware.de Git - bin.git/blob - install-manifest
install-manifest: drop -y from apt-get install dselect
[bin.git] / install-manifest
1 #!/usr/bin/python
2
3 import urllib, sys, optparse, re, subprocess
4
5 kernel_pkg_re = re.compile('linux-(generic|firmware|headers|image|backports|ec2|source|virtual|preempt)')
6
7 def parse_argv():
8     '''Parse CLI options.
9
10     Return (options, manifest) tuple.
11     '''
12
13     optparser = optparse.OptionParser('''%prog [options] <manifest>
14
15 Install/remove packages according to a CD build manifest.
16 <manifest> can be a local file or URL.''')
17     optparser.add_option('-k', '--keep-kernel',
18             help='Do not change kernel packages', action='store_true',
19             dest='keep_kernel', default=False)
20     options, args = optparser.parse_args()
21
22     if len(args) != 1:
23         optparser.error('You need to specify exactly one manifest argument, see --help')
24         sys.exit(1)
25
26     return (options, args[0])
27
28 #
29 # main
30 #
31
32 (options, manifest) = parse_argv()
33
34 selection = ''
35 for l in urllib.urlopen(manifest):
36     pkg = l.split()[0]
37     if options.keep_kernel and kernel_pkg_re.match(pkg):
38         continue
39     selection += '%s\tinstall\n' % pkg
40
41 # add local kernel packages on --keep-kernel
42 if options.keep_kernel:
43     dpkg = subprocess.Popen(['dpkg', '--get-selections'],
44             stdout=subprocess.PIPE)
45     for l in dpkg.stdout:
46         pkg, status = l.split()
47         if status != 'install':
48             continue
49         if kernel_pkg_re.match(pkg):
50             selection += '%s\tinstall\n' % pkg
51
52 if subprocess.call(['which', 'dselect'], stdout=subprocess.PIPE) != 0:
53     print >> sys.stderr, 'INFO: installing dselect (needed by this script)'
54     assert subprocess.call(['apt-get', 'install', 'dselect']) == 0
55
56 # now apply the new selections
57 assert subprocess.call(['dpkg', '--clear-selections']) == 0
58 dpkg = subprocess.Popen(['dpkg', '--set-selections'], stdin=subprocess.PIPE)
59 dpkg.communicate(selection)
60 assert dpkg.returncode == 0
61
62 # commit!
63 subprocess.call(['dselect', 'install'])