包管理器(aptitude、yum、portage)如何构建它们的依赖关系树?

发布于 2024-08-13 03:21:08 字数 1539 浏览 4 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

来日方长 2024-08-20 03:21:08

依赖树本身的加载很简单,您所需要的只是从键(例如名称)到对象的一些映射。

你没有指定任何语言,所以我选择了Python。预期的输入是一个格式为“[名称]:[空格分隔的依赖项]”的行文件。

def load_depends(file):
  depends = {}
  for line in file:
    line = line.strip()
    if not line or line.startswith("#"):  # allow blanks and comments
      continue
    name, _, deps = line.partition(":")
    deps = deps.strip()
    assert deps, "invalid input"  # most basic input error-checking
    depends[name] = set(deps.split())
  return depends

此代码假设未列出的任何项目都具有零依赖性,并且如果需要,您可以遍历树以添加空条目。至少你应该检查递归依赖关系。

示例:

>>> input = """\
... a: b c
... b: c
... c: d
... e: a
... """.split("\n")
>>> from pprint import pprint
>>> pprint(load_depends(input))
{'a': set(['b', 'c']),
 'b': set(['c']),
 'c': set(['d']),
 'e': set(['a'])}

[注意:我采取了快捷方式,因为我确实不需要行文件,而是需要行的可迭代(文件满足),所以我将行列表传递给函数。]

您可以构建所有行在此基本结构之上的各种功能,并将其和这些概念(例如依赖、推荐、建议,甚至冲突、替换等)封装到特定于您的系统的各种对象中。

The dependency tree itself is trivial to load, all you need is some mapping from keys (such as names) to objects.

You've not specified any language, so I've chosen Python. The expected input is a file of lines in the form "[name]: [space separated dependencies]".

def load_depends(file):
  depends = {}
  for line in file:
    line = line.strip()
    if not line or line.startswith("#"):  # allow blanks and comments
      continue
    name, _, deps = line.partition(":")
    deps = deps.strip()
    assert deps, "invalid input"  # most basic input error-checking
    depends[name] = set(deps.split())
  return depends

This code assumes any item not listed has zero dependencies, and you could traverse the tree to add empty entries if desired. At the very least you should check for recursive dependencies.

Example:

>>> input = """\
... a: b c
... b: c
... c: d
... e: a
... """.split("\n")
>>> from pprint import pprint
>>> pprint(load_depends(input))
{'a': set(['b', 'c']),
 'b': set(['c']),
 'c': set(['d']),
 'e': set(['a'])}

[Note: I take a shortcut, since I really don't require a file of lines, but an iterable of lines (which a file meets), so I pass a list of lines to the function.]

You can build all sorts of functions on top of this basic structure, and encapsulate it and those concepts (like depends vs recommends vs suggests, and even conflicts vs replaces, etc.) into various objects specific to your system.

友欢 2024-08-20 03:21:08

许多其他概念也涉及依赖树,例如SNMP MIB解析、C/C++源代码编译。因此,您可以参考任何其他讨论此问题的材料:)

Many other concepts also involve dependency trees, such as SNMP MIB resolution, C/C++ source code compiling. So you can reference any other materials that talk about this :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文