manifests.py 文件源码

python
阅读 23 收藏 0 点赞 0 评论 0

项目:munki-enrollment-server 作者: gerritdewitt 项目源码 文件源码
def write_metadata_to_manifest(given_computer_manifest_name,given_metadata_key,given_metadata_value):
    '''Modifes the given computer manifest's _metadata dict, adding (overwriting) the given key and value.
        Given manifest must be present and a valid plist.  Returns true if successful, false otherwise.'''
    # Filesystem paths for the manifest:
    given_computer_manifest_name = given_computer_manifest_name.upper() # if not already
    computer_manifest_path = os.path.join(munki_repo.COMPUTER_MANIFESTS_PATH,given_computer_manifest_name)

    # Catch missing manifest:
    if not os.path.exists(computer_manifest_path):
        common.logging_error("Computer manifest not found at %s." % computer_manifest_path)
        return False

    # Load manifest:
    try:
        computer_manifest_dict = plistlib.readPlist(computer_manifest_path)
    except xml.parsers.expat.ExpatError:
        common.logging_error("Computer manifest %s is invalid." % given_computer_manifest_name)
        return False

    # Load manifest metadata or start with blank:
    try:
        manifest_metadata_dict = computer_manifest_dict['_metadata']
    except KeyError:
        manifest_metadata_dict = {}

    # Modify metadata dict:
    common.logging_info("Adding %(key)s to %(manifest)s." % {'key':given_metadata_key,'manifest':given_computer_manifest_name})
    manifest_metadata_dict[given_metadata_key] = given_metadata_value
    computer_manifest_dict['_metadata'] = manifest_metadata_dict

    # Save manifest:
    try:
        plistlib.writePlist(computer_manifest_dict,computer_manifest_path)
        return True
    except TypeError:
        common.logging_error("Failed to write manifest for %s." % given_computer_manifest_name)
        return False
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号