Do you understand the details of the apt command in Linux mint?

13582 ワード

1概要


Linux Mint 18.0システムではaptコマンドがUbuntu 16.04 LTSシステムと大きく異なることがよく見られますが、今日はその違いを解読します.
後続の説明を容易にするために、まず約束をします.ここでは、次のように仮定します.
システム
ホスト名
説明
Ubuntu 16.04 LTS
ubuntu
仮想マシン
Linux Mint 18.0 LTS
qwfys
マイコンピュータ
まず、Linux Mint 18.0のaptコマンドを見てみましょう.
lwk@qwfys ~ $ lsb_release -a
No LSB modules are available.
Distributor ID:	LinuxMint
Description:	Linux Mint 18.3 Sylvia
Release:	18.3
Codename:	sylvia
lwk@qwfys ~ $ 
lwk@qwfys ~ $ apt
apt
Usage: apt command [options]
       apt help command [options]

Commands:
  add-repository   - Add entries to apt sources.list
  autoclean        - Erase old downloaded archive files
  autoremove       - Remove automatically all unused packages
  build            - Build binary or source packages from sources
  build-dep        - Configure build-dependencies for source packages
  changelog        - View a package's changelog
  check            - Verify that there are no broken dependencies
  clean            - Erase downloaded archive files
  contains         - List packages containing a file
  content          - List files contained in a package
  deb              - Install a .deb package
  depends          - Show raw dependency information for a package
  dist-upgrade     - Upgrade the system by removing/installing/upgrading packages
  download         - Download the .deb file for a package
  edit-sources     - Edit /etc/apt/sources.list with your preferred text editor
  dselect-upgrade  - Follow dselect selections
  full-upgrade     - Same as 'dist-upgrade'
  held             - List all held packages
  help             - Show help for a command
  hold             - Hold a package
  install          - Install/upgrade packages
  list             - List packages based on package names
  policy           - Show policy settings
  purge            - Remove packages and their configuration files
  recommends       - List missing recommended packages for a particular package
  rdepends         - Show reverse dependency information for a package
  reinstall        - Download and (possibly) reinstall a currently installed package
  remove           - Remove packages
  search           - Search for a package by name and/or expression
  show             - Display detailed information about a package
  showhold         - Same as 'held'
  source           - Download source archives
  sources          - Same as 'edit-sources'
  unhold           - Unhold a package
  update           - Download lists of new/upgradable packages
  upgrade          - Perform a safe upgrade
  version          - Show the installed version of a package

lwk@qwfys ~ $ 


次に、Ubuntu Server 16.04 LTSのaptコマンドを見てみましょう.
lwk@qwfys ~ $ ssh [email protected]
Welcome to Ubuntu 16.04.4 LTS (GNU/Linux 4.4.0-116-generic x86_64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/advantage

27 packages can be updated.
0 updates are security updates.


Last login: Fri Apr 27 10:42:27 2018 from 192.168.56.1
lwk@ubuntu:~$ lsb_release -a
No LSB modules are available.
Distributor ID:	Ubuntu
Description:	Ubuntu 16.04.4 LTS
Release:	16.04
Codename:	xenial
lwk@ubuntu:~$ apt
apt 1.2.25 (amd64)
Usage: apt [options] command

apt is a commandline package manager and provides commands for
searching and managing as well as querying information about packages.
It provides the same functionality as the specialized APT tools,
like apt-get and apt-cache, but enables options more suitable for
interactive use by default.

Most used commands:
  list - list packages based on package names
  search - search in package descriptions
  show - show package details
  install - install packages
  remove - remove packages
  autoremove - Remove automatically all unused packages
  update - update list of available packages
  upgrade - upgrade the system by installing/upgrading packages
  full-upgrade - upgrade the system by removing/installing/upgrading packages
  edit-sources - edit the source information file

See apt(8) for more information about the available commands.
Configuration options and syntax is detailed in apt.conf(5).
Information about how to configure sources can be found in sources.list(5).
Package and version choices can be expressed via apt_preferences(5).
Security details are available in apt-secure(8).
                                        This APT has Super Cow Powers.
lwk@ubuntu:~$ 


では、両者の間にはどんな違いがありますか.次に、一緒に分析してみましょう.

2 aptプローブ


ここでは、上記の2つのシステムでのaptの違いをwhereisコマンドとwhichコマンドで確認する実験を行います.
Linux Mint 18.0に次のコマンドを入力します.
lwk@qwfys ~ $ whereis apt
apt: /usr/bin/apt /usr/lib/apt /etc/apt /usr/local/bin/apt /usr/share/man/man8/apt.8.gz
lwk@qwfys ~ $ which apt
/usr/local/bin/apt
lwk@qwfys ~ $ 

 Ubuntu 16.04 LTSで、次のコマンドを入力します.
lwk@ubuntu:~$ whereis apt
apt: /usr/bin/apt /usr/lib/apt /etc/apt /usr/share/man/man8/apt.8.gz
lwk@ubuntu:~$ which apt
/usr/bin/apt
lwk@ubuntu:~$ 


Linux Mint 18.0システムでは、ファイル/usr/local/bin/aptが優先されています.では、このファイルには何か奇妙な点がありますか.
lwk@qwfys ~ $ ll /usr/local/bin/apt 
-rwxr-xr-x 1 root root 6482 Nov 23 17:49 /usr/local/bin/apt*
lwk@qwfys ~ $ file /usr/local/bin/apt 
/usr/local/bin/apt: Python script, ASCII text executable
lwk@qwfys ~ $ 

ファイルがpythonファイルであることを発見しました.このファイルの内容を見てみましょう.
lwk@qwfys ~ $ cat /usr/local/bin/apt 
#!/usr/bin/python3

import sys
import os
import subprocess

def usage():
    print("apt")
    print("Usage: apt command [options]")
    print("       apt help command [options]")
    print("")
    print("Commands:")
    print("  add-repository   - Add entries to apt sources.list")
    print("  autoclean        - Erase old downloaded archive files")
    print("  autoremove       - Remove automatically all unused packages")
    print("  build            - Build binary or source packages from sources")
    print("  build-dep        - Configure build-dependencies for source packages")
    print("  changelog        - View a package's changelog")
    print("  check            - Verify that there are no broken dependencies")
    print("  clean            - Erase downloaded archive files")
    print("  contains         - List packages containing a file")
    print("  content          - List files contained in a package")
    print("  deb              - Install a .deb package")
    print("  depends          - Show raw dependency information for a package")
    print("  dist-upgrade     - Upgrade the system by removing/installing/upgrading packages")
    print("  download         - Download the .deb file for a package")
    print("  edit-sources     - Edit /etc/apt/sources.list with your preferred text editor")
    print("  dselect-upgrade  - Follow dselect selections")
    print("  full-upgrade     - Same as 'dist-upgrade'")
    print("  held             - List all held packages")
    print("  help             - Show help for a command")
    print("  hold             - Hold a package")
    print("  install          - Install/upgrade packages")
    print("  list             - List packages based on package names")
    print("  policy           - Show policy settings")
    print("  purge            - Remove packages and their configuration files")
    print("  recommends       - List missing recommended packages for a particular package")
    print("  rdepends         - Show reverse dependency information for a package")
    print("  reinstall        - Download and (possibly) reinstall a currently installed package")
    print("  remove           - Remove packages")
    print("  search           - Search for a package by name and/or expression")
    print("  show             - Display detailed information about a package")
    print("  showhold         - Same as 'held'")
    print("  source           - Download source archives")
    print("  sources          - Same as 'edit-sources'")
    print("  unhold           - Unhold a package")
    print("  update           - Download lists of new/upgradable packages")
    print("  upgrade          - Perform a safe upgrade")
    print("  version          - Show the installed version of a package")
    print("")
    sys.exit(1)

aliases = {
    "dist-upgrade": "full-upgrade",
    "sources": "edit-sources",
    "held": "showhold"
}

if len(sys.argv) < 2:
    usage()

argcommand = sys.argv[1]
argoptions = " ".join(sys.argv[2:])

command = ""

show_help = False
sort = False
highlight = False

rows, columns = os.popen('stty size', 'r').read().split()

if argcommand == "help":
    if len(sys.argv) < 3:
        usage()
    show_help = True
    argcommand = sys.argv[2]
    argoptions = " ".join(sys.argv[3:])

if argcommand in aliases.keys():
    argcommand = aliases[argcommand]

if argcommand in ("autoremove", "list", "show", "install", "remove", "purge", "update", "upgrade", "full-upgrade", "edit-sources"):
    # apt
    command = "/usr/bin/apt %s %s" % (argcommand, argoptions)
elif argcommand in ("clean", "dselect-upgrade", "build-dep", "check", "autoclean", "source", "moo"):
    # apt-get
    command = "apt-get %s %s" % (argcommand, argoptions)
elif argcommand in ("changelog", "reinstall"):
    # aptitude
    command = "aptitude %s %s" % (argcommand, argoptions)
elif argcommand in ("stats", "depends", "rdepends", "policy"):
    # apt-cache
    command = "apt-cache %s %s" % (argcommand, argoptions)
elif argcommand in ("recommends", ):
    command = "/usr/lib/linuxmint/mintsystem/mint-apt-recommends.py " + argoptions
elif argcommand in ("showhold", "hold", "unhold"):
    # apt-mark
    command = "apt-mark %s %s" % (argcommand, argoptions)
elif argcommand in ("markauto", "markmanual"):
    # apt-mark
    command = "apt-mark %s %s" % (argcommand[4:], argoptions)
elif argcommand == "contains":
    command = "dpkg -S %s" % argoptions
elif argcommand == "content":
    command = "dpkg -L %s" % argoptions
elif argcommand == "deb":
    command = "dpkg -i %s" % argoptions
elif argcommand == "build":
    command = "dpkg-buildpackage %s" % argoptions
elif argcommand == "version":
    command = "/usr/lib/linuxmint/common/version.py %s" % argoptions
elif argcommand == "download":
    command = "/usr/lib/linuxmint/mintsystem/mint-apt-download.py " + argoptions
elif argcommand == "add-repository":
    command = "add-apt-repository %s" % argoptions
elif argcommand == "search":
    command = "aptitude -w %s %s %s" % (columns, argcommand, argoptions)
else:
    usage()
    sys.exit(1)

# Sudo prefix
if os.getuid() != 0 and argcommand in ("autoremove", "install", "remove", "purge", "update", "upgrade", "full-upgrade", "edit-sources", "clean", "dselect-upgrade", "build-dep", "check", "autoclean", "reinstall", "deb", "hold", "unhold", "add-repository", "markauto", "markmanual"):
    command = "sudo %s" % command

# Color highlighting
if argcommand in ("content", "version", "policy", "depends", "rdepends", "search") and len(argoptions.strip()) > 1:
    highlight = True

# Sorting
if argcommand in ("content", "contains"):
    sort = True

if show_help:
    print("\"apt " + argcommand + " " + argoptions + "\" is equivalent to \"" + command + "\"")
else:
    command = command.split()
    if sort and highlight:
        ps1 = subprocess.Popen(command, stdout=subprocess.PIPE)
        ps2 = subprocess.Popen(('sort'), stdin=ps1.stdout, stdout=subprocess.PIPE)
        ps3 = subprocess.Popen(('highlight', argoptions), stdin=ps2.stdout)
        ps3.wait()
    elif sort:
        ps1 = subprocess.Popen(command, stdout=subprocess.PIPE)
        ps2 = subprocess.Popen(('sort'), stdin=ps1.stdout)
        ps2.wait()
    elif highlight:
        ps1 = subprocess.Popen(command, stdout=subprocess.PIPE)
        ps2 = subprocess.Popen(('highlight', argoptions), stdin=ps1.stdout)
        ps2.wait()
    else:
        return_code = subprocess.call(command)
        sys.exit(return_code)
lwk@qwfys ~ $ 


ファイル/usr/local/bin/aptは、次のコマンドのパッケージであることがわかります.
/usr/bin/apt
/usr/bin/apt-get
/usr/bin/aptitude
/usr/bin/apt-mark
/usr/bin/dpkg
/usr/lib/linuxmint/common/version.py
/usr/lib/linuxmint/mintsystem/mint-apt-download.py

  私たちにとって、Linux Mint 18.0システムのaptコマンドを熟知すれば、日常のシステム管理を満たすことができます.