识别使用Python的Mac OS X中的软件包目录 [英] Identify directories that are packages in Mac OS X with Python

查看:88
本文介绍了识别使用Python的Mac OS X中的软件包目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Mac OS X Finder使用包"的概念来使某些文件夹的内容对用户不透明.我正在使用os.walk()枚举目录树,并且想跳过对诸如应用程序捆绑包之类的软件包的枚举.

The Mac OS X Finder uses the concept of "packages" to make the contents of certain folders opaque to the user. I'm using os.walk() to enumerate a directory tree and I want to skip enumeration on packages such as application bundles.

mdls命令行实用程序可用于检查kMDItemContentTypeTree属性中是否包含com.apple.package.在检测到操作系统确实是darwin之后,检测文件夹是否是要放入os.system并使用mdls的软件包的唯一/最佳方法是吗?

The mdls commandline utility can be used to check whether com.apple.package is in the kMDItemContentTypeTree attribute. Is the only/best way to detect whether a folder is a package to drop into os.system and use mdls after detecting that the OS is indeed darwin?

顺便说一句,此解决方案似乎依赖于Spotlight元数据,据我了解,该元数据是从文件/目录本身中填充的.这使我想知道是否有一种方法可以检查目录是否为mdls之外的软件包.也许我想念一些东西.

As an aside, this solution seems to depend on Spotlight metadata which I understand is populated from the files/directories themselves. This makes me wonder whether there is a method to check whether a directory is a package outside of mdls. Perhaps I'm missing something.

推荐答案

OS X packages (and bundles) are usually defined by their extension. Simply create a directory with an .app extension to see it be presented as a (broken) application in the Finder.

官方文档列出了以下定义包的方法:

The official documentation lists the following ways to define bundles:

如果满足以下任一条件,则Finder会将目录视为软件包:

The Finder considers a directory to be a package if any of the following conditions are true:

  • 该目录具有已知的文件扩展名:.app,.bundle,.framework,.plugin,.kext等.
  • 该目录具有一个扩展名,其他一些应用程序则声称该扩展名代表程序包类型;请参阅文档包".
  • 该目录的软件包位已设置.
  • The directory has a known filename extension: .app, .bundle, .framework, .plugin, .kext, and so on.
  • The directory has an extension that some other application claims represents a package type; see "Document Packages."
  • The directory has its package bit set.

指定软件包的首选方法是给软件包目录一个已知的文件扩展名.在大多数情况下,Xcode通过提供应用正确扩展名的模板来为您解决这一问题.您要做的就是创建适当类型的Xcode项目.

The preferred way to specify a package is to give the package directory a known filename extension. For the most part, Xcode takes care of this for you by providing templates that apply the correct extension. All you have to do is create an Xcode project of the appropriate type.

然后,检测包的最简单方法是检测那些扩展名.快速而肮脏的方法是,使用上述文档作为指导,简单地查找扩展的硬编码列表.

The simplest way to detect packages then is to detect those extensions. The quick and dirty way is to simply look for a hard-coded list of extensions, using the above documentation as your guide.

下一步是查询操作系统,是否已将给定的扩展名注册为文档包.请参阅如何检查Finder是否将具有给定扩展名的目录显示为一个包?

The next step up is to query the OS if a given extension has been registered as a Document Package. See How to check whether directories with a given extension are shown by the Finder as a package?

要检测目录中的软件包位,您必须使用 xattr检索u'com.apple.FinderInfo'键,然后使用 Finder.h标头信息,用于解码返回的二进制数据; kHasBundle标志为0x2000:

To detect the package bit on directories, you'll have to use the xattr library to retrieve the u'com.apple.FinderInfo' key and then use the Finder.h header info to decode the binary data returned; the kHasBundle flag is 0x2000:

attrs = xattr.getxattr('/path/to/dir', u'com.apple.FinderInfo')
ispackage = bool(ord(attrs[8]) & 0x20)  # I *think* this is correct; works for hidden dirs and & 0x40

这篇关于识别使用Python的Mac OS X中的软件包目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆