我应该将什么 python 数据结构和解析器与 Apple 的 system_profiler 一起使用?
我的问题就像一个模拟问题 http://my.safaribooksonline.com/0596007973/pythoncook2-CHP- 10-SECT-17 它最终使用 2005 年的过时 xpath 方法进入 Python Cookbook,第二版,我无法使用 10.6 的内置 python(也无法安装旧包),
我想......“每次计算机启动时,使用 system_profiler 检索有关 Mac OS X 系统的详细信息,将其汇总在脚本中(该脚本将在登录时启动)。
我收集的信息因软件版本和硬件配置而异。
示例行是: system_profiler SPSoftwareDataType | 系统分析器grep '启动卷' 它返回启动卷名称。我还打了 15 到 20 个电话以获取信息。
我试图输出完整的“system_profiler >” data' 并使用 cat data | 解析它grep,但这显然效率低下,如果我像上面的示例一样运行每一行,速度会更快。
如果输出到文件和 cat | 18 秒grep。
如果拨打个人电话,则为 13 秒
*我正在努力使其尽可能快。
我推断我可能需要创建一个字典并使用键来引用数据,但我想知道解析和检索数据的最有效方法是什么?我在其他地方看到了使用 system_profiler 输出到 XML 并使用 XML 解析器的建议,但我认为可能有一些缓存和解析方法比首先输出到文件更有效。
My problem is one like a simulated problem from
http://my.safaribooksonline.com/0596007973/pythoncook2-CHP-10-SECT-17
which eventually made its way into Python Cookbook, 2nd Edition using an outdated xpath method from 2005 that I haven't been able to get to work with 10.6's build-in python(nor installing older packages)
I want to ... "retrieve detailed information about a Mac OS X system" using system_profiler to summarize it in a script each time a computer starts up(The script will launch on login).
The information I'm gathering varies from SW versions to HW config.
An example line is,
system_profiler SPSoftwareDataType | grep 'Boot Volume'
which returns the startup volume name. I make 15 to 20 other calls for information.
I've tried to output the full 'system_profiler > data' and parse that using cat data | grep, but that's obviously inefficient to the point where it's been faster if I just run each line like my example above.
18 seconds if ouputting to a file and cat | grep.
13 seconds if making individual calls
*I'm trying to make it as fast as possible.
I deduce that I probably need to create a dictionary and use keys to reference out the data but I'm wondering what's the most efficient way for me to parse and retrieve the data? I've seen a suggestion elsewhere to use system_profiler to output to XML and use a XML parser but I think there's probably some cache and parse method that does it more efficiently than outputting to a file first.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用
system_profiler
的 -xml 选项将输出格式化为标准 OS X plist 格式,然后使用 Python 的内置 plistlib 解析为您可以内省的适当数据结构。一个简单的例子:Use the -xml option to
system_profiler
to format the output in a standard OS X plist format, then use Python's built-in plistlib to parse into an appropriate data structure you can introspect. A simple example: