0

在 Ubuntu 12.04 服务器上安装像 nginx 这样的软件时,apache2似乎是作为依赖项之一安装的,这不是我们需要的。

问题:您如何找出安装的软件包apache2作为其依赖项?

4

4 回答 4

1

您可以使用 apt-rdepends 向前或向后遍历依赖项

http://pkgs.org/ubuntu-12.04/ubuntu-universe-i386/apt-rdepends_1.3.0-2_all.deb.html

或者,有

apt-cache rdepends packagename

从这里https://askubuntu.com/questions/128524/how-to-list-package-dependees-reverse-dependencies

于 2013-08-18T09:19:32.000 回答
1

您可以使用

sudo apt-rdepends apache2

这将显示所需的包和版本。

于 2013-08-18T09:22:41.393 回答
0

基于 RPM 的发行版通过简单调用基本的 rpm 实用程序来实现这一点:

rpm -q --whatrequires apache2

我确信 Ubuntu 中使用的 apt 包管理器提供了类似的功能。我建议你阅读它的手册页...

于 2013-08-18T09:04:28.780 回答
0
/var/lib/dpkg/status 

这有 deb 依赖列表。如果此文件中的每个包都有依赖项,则它们都有一个依赖项部分。您可以只提取这些列表并查找 apache2 依赖项。

这是一些糟糕的代码,用于将该状态文件拉入 python 中的可搜索 sqlitedb。

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import json
import os
import re
import sqlite3
import sys
import time
import traceback

from dpkg import parse
from dpkg import poll

def db_init(status_file):
    # poll status file
    status_obj = poll.poll(status_file)

    # initialize sqlite3
    conn = sqlite3.connect(":memory:")
    db_create(conn)

    # parse status fileobject, load into database
    db_status_load(conn, status_obj)

    # kill that big string obj
    del status_obj

    # return ready database handle
    return conn

def db_query(conn, query):
    db_resp = conn.execute(query).fetchall()
    return db_resp

def db_create(conn):
    c = conn.cursor()

    c.execute('PRAGMA temp_store=MEMORY;')
    c.execute('PRAGMA journal_mode=MEMORY;')

    # two tables packages, and a dependency index 
    c.execute('create table if not exists packages (packageid integer primary key autoincrement, packagename text, description blob);')
    c.execute('create table if not exists depends (packageid int, dependencyid int, alternate int);')
    c.execute('create table if not exists alternates (packageid int, dependencyid int, alternateid int);')
    conn.commit()

def db_listPackages(conn):
    c = conn.cursor()
    c.execute('select * from packages')
    rows = c.fetchall()
    return rows

def db_descPackage(conn, package_name):
    c = conn.cursor()
    c.execute('select * from packages where packagename=?', (package_name,))
    rows = c.fetchall()
    return rows

def db_namePackage(conn, package_id):
    c = conn.cursor()
    c.execute('select * from packages where packageid=?', (package_id,))
    rows = c.fetchall()
    if len(rows) < 1:
        # No package name for that ID
        package_name = "NULL"
    else:
        package_name = str(rows[0][1])
    return package_name

def db_idPackage(conn, package_name):
    c = conn.cursor()
    c.execute('select * from packages where packagename=?', (str(package_name),))
    rows = c.fetchall()
    if len(rows) < 1:
        # No corresponding package in our status db by that name
        package_id = 0
    else:
        package_id = int(rows[0][0])
    return package_id

def db_depsPackage(conn, package_id):
    c = conn.cursor()
    c.execute('select * from depends where packageid=?', (package_id,))
    rows = c.fetchall()
    return rows

def db_indepsPackage(conn, package_id):
    c = conn.cursor()
    c.execute('select * from depends where dependencyid=?', (package_id,))
    rows = c.fetchall()
    return rows

def db_getAlternates(conn, package_id, dependency_id):
    c = conn.cursor()
    c.execute('select * from alternates where dependencyid=? AND packageid=?', (dependency_id, package_id))
    rows = c.fetchall()
    return rows

def db_status_load(conn, status_obj):
    c = conn.cursor()
    # render json datastruct from string object
    status_dict = parse.parse(status_obj)
    package_name = package_description = ""
    package_depends = []

    # stage packages info from status_dict
    # populate packages table
    for packages in status_dict['Packages']:
        # define package name
        package_name = packages
        # print "inserting \033[34m %s \033[0m :\n" % package_name
        for idx,stuff in enumerate(status_dict['Packages'][packages]):
            if stuff.has_key("Description"): 
                # define package description
                package_description = stuff['Description']

        # execute insertion query
        b = sqlite3.Binary(package_description)
        c.execute('insert into packages (packagename, description) values (?,?)', (package_name, b) )

    conn.commit()

    # populate dependency table
    for packages in status_dict['Packages']:
        package_name = packages
        # print "inserting depends and alternates for \033[31m %s \033[0m :\n" % package_name
        for stuff in status_dict['Packages'][packages]:
            if stuff.has_key("Depends"):
                dep_string = stuff['Depends']
                dep_string = dep_string.replace(" ", "")
                dep_array = dep_string.split(',')
                package_id = db_idPackage(conn, package_name)
                for dep in dep_array:
                    dep = re.sub(r'\([^)]*\)', '', dep)
                    if "|" in dep: 
                        alternates = dep.split('|')
                        deporig = alternates[0]
                        for idx, alternate in enumerate(alternates):
                            if idx > 0:
                                alternate_id = db_idPackage(conn, alternate)
                                dependency_id = db_idPackage(conn, deporig)
                                # print "\033[33m ALTERNATE: %s, %s, %s \033[0m \n" % (package_id, dependency_id, alternate_id)
                                c.execute('insert into alternates (packageid, dependencyid, alternateid) values (?,?,?)', 
                                          (package_id, dependency_id, alternate_id) )
                            else:
                                dependency_id = db_idPackage(conn, deporig)
                                c.execute('insert into depends (packageid, dependencyid, alternate) values (?, ?, ?)',
                                          (package_id, dependency_id, 1) )
                    else:
                        dependency_id = db_idPackage(conn, dep)
                        c.execute('insert into depends (packageid, dependencyid, alternate) values (?, ?, ?)',
                                  (package_id, dependency_id, 0) ) 

    # commit changes to database
    conn.commit()

    return 0
于 2013-08-18T09:21:26.667 回答