summaryrefslogtreecommitdiffstats
path: root/doc/source/examples/apt-cdrom.py
blob: 13bfb97841638fafa0eb1fdfc22e2d8f9bac421a (plain)
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#!/usr/bin/python3
import sys

import apt_pkg

import apt


def show_help():
    print(f"apt {apt_pkg.VERSION} compiled on {apt_pkg.DATE} {apt_pkg.TIME}")
    if apt_pkg.config.find_b("version"):
        return 0

    # Copied from apt-cdrom
    print(
        "Usage: apt-cdrom [options] command\n"
        "\n"
        "apt-cdrom is a tool to add CDROM's to APT's source list. The\n"
        "CDROM mount point and device information is taken from apt.conf\n"
        "and /etc/fstab.\n"
        "\n"
        "Commands:\n"
        "   add - Add a CDROM\n"
        "   ident - Report the identity of a CDROM\n"
        "\n"
        "Options:\n"
        "  -h   This help text\n"
        "  -d   CD-ROM mount point\n"
        "  -r   Rename a recognized CD-ROM\n"
        "  -m   No mounting\n"
        "  -f   Fast mode, don't check package files\n"
        "  -a   Thorough scan mode\n"
        "  -c=? Read this configuration file\n"
        "  -o=? Set an arbitrary configuration option, eg -o "
        "dir::cache=/tmp\n"
        "See fstab(5)"
    )
    return 0


def main(args):
    arguments = apt_pkg.parse_commandline(
        apt_pkg.config,
        [
            ("h", "help", "help"),
            ("v", "version", "version"),
            ("d", "cdrom", "Acquire::cdrom::mount", "HasArg"),
            ("r", "rename", "APT::CDROM::Rename"),
            ("m", "no-mount", "APT::CDROM::NoMount"),
            ("f", "fast", "APT::CDROM::Fast"),
            ("n", "just-print", "APT::CDROM::NoAct"),
            ("n", "recon", "APT::CDROM::NoAct"),
            ("n", "no-act", "APT::CDROM::NoAct"),
            ("a", "thorough", "APT::CDROM::Thorough"),
            ("c", "config-file", "", "ConfigFile"),
            ("o", "option", "", "ArbItem"),
        ],
        args,
    )

    if apt_pkg.config.find_b("help") or apt_pkg.config.find_b("version"):
        return show_help()

    progress = apt.progress.text.CdromProgress()
    cdrom = apt_pkg.Cdrom()

    if not arguments:
        return show_help()
    elif arguments[0] == "add":
        cdrom.add(progress)
    elif arguments[0] == "ident":
        cdrom.ident(progress)
    else:
        sys.stderr.write("E: Invalid operation %s\n" % arguments[0])
        return 1


if __name__ == "__main__":
    sys.exit(main(sys.argv))