【问题标题】:Is there a python grub.cfg parser?是否有 python grub.cfg 解析器?
【发布时间】:2012-03-04 03:23:51
【问题描述】:

有人知道用于 grub2 的 grub.cfg 文件的 python 解析器吗?

我正在尝试通过设备分区(根)获取“菜单项”,例如

hd0,msdos1: ['Ubuntu, with Linux 3.0.0-15-generic',
            'Ubuntu, with Linux 3.0.0-15-generic (recovery mode)',
            'Ubuntu, with Linux 3.0.0-14-generic']
hd2,msdos1: ["Windows 7 (on /dev/sdc1)"]

等等

解决方案:

re.findall("menuentry ['\"](.*?)['\"].*?set root='(.*?)'", x, re.S)

[('Ubuntu, with Linux 3.0.0-15-generic', '(hd0,msdos1)'), ('Ubuntu, 使用 Linux 3.0.0-15-generic (恢复模式)', '(hd0,msdos1)'), ('Ubuntu, with Linux 3.0.0-14-generic', '(hd0,msdos1)'), ('Ubuntu, 使用 Linux 3.0.0-14-generic (恢复模式)', '(hd0,msdos1)'), ('Ubuntu, with Linux 3.0.0-13-generic', '(hd0,msdos1)'), ('Ubuntu, 使用 Linux 3.0.0-13-generic (恢复模式)', '(hd0,msdos1)'), ('Ubuntu,使用 Linux 3.0.0-12-generic', '(hd0,msdos1)'), ('Ubuntu, 使用 Linux 3.0.0-12-generic (恢复模式)', '(hd0,msdos1)'), ('内存测试 (memtest86+)', '(hd0,msdos1)'), ('内存测试 (memtest86+, 串行控制台 115200)', '(hd0,msdos1)'), ('Windows 7 (在 /dev/sdc1)', '(hd2,msdos1)')]

【问题讨论】:

  • 哦,你们希望一切都为您完成...只要制作那个解析器,您就会有很多乐趣!
  • 不,我只是问有没有。无论如何,找到了正则表达式和 re.S (DOTALL) 的替代方法。

标签: python parsing grub


【解决方案1】:

我不知道用于 grub.cfg 的 Python 解析器,但您不需要解析整个文件以获取该信息。这是您要查找的数据的格式:

menuentry "<name>" [options] {
  ...
  set root='<root>'
  ...
}

因此,查找以menuentry 开头的行,从该行解析名称,然后扫描到带有} 的下一行set root=

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-12-03
    • 1970-01-01
    • 2011-06-06
    • 1970-01-01
    • 2013-02-06
    • 2010-12-11
    • 2016-05-16
    • 1970-01-01
    相关资源
    最近更新 更多