apt-install 4.02 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
#!/usr/bin/python3

# flatdeb — build Flatpak runtimes from Debian packages
#
# Copyright © 2016-2017 Simon McVittie
# Copyright © 2017-2018 Collabora Ltd.
#
# SPDX-License-Identifier: MIT
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

"""
Install the given packages.

32 33
This is basically debos' apt action, but with more debugging, and without
doing `apt-get update`.
34 35 36 37
"""

import argparse
import logging
38
import os
39 40 41 42
import subprocess
import sys
import yaml

43 44 45 46 47 48 49
try:
    import typing
except ImportError:
    pass
else:
    typing  # silence "unused" warnings

50 51 52 53 54

logger = logging.getLogger('flatdeb.apt-install')


def main():
55
    # type: (...) -> None
56 57 58 59 60 61 62 63 64
    parser = argparse.ArgumentParser(
        description='Install the given packages'
    )
    parser.add_argument(
        '--with-recommends', action='store_true', dest='recommends',
        default=False)
    parser.add_argument(
        '--without-recommends', action='store_false', dest='recommends',
        default=False)
65 66
    parser.add_argument(
        '--debug', action='store_true', default=False)
67 68 69 70 71 72 73 74 75
    parser.add_argument('sysroot')
    parser.add_argument('package_files', nargs='+')

    args = parser.parse_args()

    in_chroot = [
        'systemd-nspawn',
        '--directory={}'.format(args.sysroot),
        '--as-pid2',
76
        '--tmpfs=/run/lock',
77
        '--register=no',
78 79 80
        'env', 'DEBIAN_FRONTEND=noninteractive',
    ]

81 82 83 84
    for var in ('ftp_proxy', 'http_proxy', 'https_proxy', 'no_proxy'):
        if var in os.environ:
            in_chroot.append('{}={}'.format(var, os.environ[var]))

85 86
    options = []        # type: typing.List[str]
    packages = []       # type: typing.List[str]
87

88 89 90 91 92 93
    if args.debug:
        options.append('-oDebug::pkgDepCache::AutoInstall=true')
        options.append('-oDebug::pkgDepCache::Marker=true')
        options.append('-oDebug::pkgPolicy=true')
        options.append('-oDebug::pkgProblemResolver=true')
        options.append('-oDebug::pkgProblemResolver::ShowScores=true')
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109

    if not args.recommends:
        options.append('--no-install-recommends')

    for p in args.package_files:
        with open(p, encoding='utf-8') as reader:
            packages.extend(yaml.safe_load(reader))

    subprocess.check_call(in_chroot + [
        'apt-get', '-y',
    ] + options + ['install'] + packages)

    subprocess.check_call(in_chroot + [
        'apt-get', 'clean',
    ])

110

111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
if __name__ == '__main__':
    if sys.stderr.isatty():
        try:
            import colorlog
        except ImportError:
            pass
        else:
            formatter = colorlog.ColoredFormatter(
                '%(log_color)s%(levelname)s:%(name)s:%(reset)s %(message)s')
            handler = logging.StreamHandler()
            handler.setFormatter(formatter)
            logging.getLogger().addHandler(handler)
    else:
        logging.basicConfig()

    logging.getLogger().setLevel(logging.DEBUG)

    try:
        main()
    except KeyboardInterrupt:
        raise SystemExit(130)
    except subprocess.CalledProcessError as e:
        logger.error('%s', e)
        raise SystemExit(1)