补丁容器patchrecord.py

# -*- coding: utf-8 -*-
# pylint: disable=missing-docstring
import json
import os
import sys
import xml.etree.ElementTree as ET


def main():
    backup_file_path = AGENT_HOME + '/works/patchFrame/backup/dapmanager-agent.bak'
    agent_patch_record = PATCH_PERSIST_DICT['agent']
    if os.path.isdir(backup_file_path) and agent_patch_record:
        recover_patch = [{'"time"': agent_patch_record[-1]['time'],
                          '"patchName"': '"' + str(agent_patch_record[-1]['patchName']) + '"',
                          '"service"': '"agent"'}]
        os.system('echo ' + str({'"service"': '"agent"',
                                 '"patchHistory"': query_patch_history(),
                                 '"recoverPatch"': recover_patch}))
    else:
        os.system('echo ' + str({'"service"': '"agent"',
                                 '"patchHistory"': query_patch_history()}))
    sys.exit(0)


def query_patch_history():
    patch_history = []
    for product in PATCH_PERSIST_DICT:
        for persist in PATCH_PERSIST_DICT[product]:
            patch_history.append(
                {'"time"': persist['time'],
                 '"patchName"': '"' + str(persist['patchName']) + '"',
                 '"service"': '"agent"'})
    return patch_history


def query_patch_persist():
    patch_persist_dict = {}
    for product in PRODUCT_LIST:
        product_persist = ARCHIVE_PATH + '/' + product + '.json'
        if os.path.isfile(product_persist):
            _f = open(product_persist)
            patch_persist_dict[product] = json.load(_f)
            _f.close()
        else:
            patch_persist_dict[product] = []
    return patch_persist_dict


def query_patch_product():
    patch_product_xml = ARCHIVE_PATH + '/agent-patch-product.xml'
    tree = ET.parse(patch_product_xml)
    root = tree.getroot()
    product_list = []
    for product in root.findall('product'):
        product_list.append(product.text)
    product_list.sort()
    return product_list


if __name__ == '__main__':
    AGENT_HOME = sys.argv[1]
    ARCHIVE_PATH = AGENT_HOME + '/service/patch/archive'
    PRODUCT_LIST = query_patch_product()
    PATCH_PERSIST_DICT = query_patch_persist()

    main()

你可能感兴趣的:(python)