# Path: FileHandling.py
# def updatePilotErrorReport(pilotErrorCode, pilotErrorDiag, priority, jobID, workdir):
#     """ Write pilot error info to file """
#     # Report format:
#     # { jobID1: { priority1: [{ pilotErrorCode1:<nr>, pilotErrorDiag1:<str> }, .. ], .. }, .. }
#     # The pilot will report only the first of the highest priority error when it reports the error at the end of the job
#     # Use the following priority convention:
#     # "0": highest priority [e.g. errors that originate from the main pilot module (unless otherwise necessary)]
#     # "1": high priority [e.g. errors that originate from the Monitor module (-||-)]
#     # "2": normal priority [errors that originate from other modules (-||-)]
#     # etc
# 
#     # Convert to string if integer is sent for priority
#     if type(priority) != str:
#         priority = str(priority)
# 
#     filename = getPilotErrorReportFilename(workdir)
#     if os.path.exists(filename):
#         # The file already exists, read it back (with unicode to utf-8 conversion)
#         dictionary = getJSONDictionary(filename)
#     else:
#         dictionary = {}
# 
#     # Sort the new error
#     if dictionary.has_key(jobID):
#         jobID_dictionary = dictionary[jobID]
# 
#         # Update with the latest error info
#         if not jobID_dictionary.has_key(priority):
#             dictionary[jobID][priority] = []
# 
#         new_dictionary = { 'pilotErrorCode':pilotErrorCode, 'pilotErrorDiag':pilotErrorDiag }
# 
#         # Update the dictionary with the new info
#         dictionary[jobID][priority].append(new_dictionary)
#         print dictionary
# 
#     else:
#         # Create a first entry into the error report
#         dictionary[jobID] = {}
#         dictionary[jobID][priority] = []
#         dictionary[jobID][priority].append({})
#         dictionary[jobID][priority][0] = { 'pilotErrorCode':pilotErrorCode, 'pilotErrorDiag':pilotErrorDiag }
# 
#     # Finally update the file
#     status = writeJSON(filename, dictionary)


import os
import json
import time
import traceback
import pUtil
from SocketServer import BaseRequestHandler 
from Configuration import Configuration
from FileHandling import updatePilotErrorReport
    
                        if jobinfo.has_key("output_fields"):
                            self.__env['jobDic'][k][1].output_fields = pUtil.stringToFields(jobinfo["output_fields"])
                            pUtil.tolog("Got output_fields=%s" % str(self.__env['jobDic'][k][1].output_fields))
                            pUtil.tolog("Converted from output_fields=%s" % str(jobinfo["output_fields"]))

                        # corrupted files
                        if jobinfo.has_key("corruptedFiles"):
                            self.__env['jobDic'][k][1].corruptedFiles = jobinfo["corruptedFiles"]

                        # hpc status
                        if jobinfo.has_key("mode"):
                            self.__env['jobDic'][k][1].mode = jobinfo['mode']
                        if jobinfo.has_key("hpcStatus"):
                            self.__env['jobDic'][k][1].hpcStatus = jobinfo['hpcStatus']
                        if jobinfo.has_key("yodaJobMetrics"):
                            self.__env['jobDic'][k][1].yodaJobMetrics = json.loads(jobinfo['yodaJobMetrics'])
                        if jobinfo.has_key("coreCount"):
                            self.__env['jobDic'][k][1].coreCount = jobinfo['coreCount']
                        if jobinfo.has_key("HPCJobId"):
                            self.__env['jobDic'][k][1].HPCJobId = jobinfo['HPCJobId']

                        # zip output
                        if jobinfo.has_key("outputZipName"):
                            self.__env['jobDic'][k][1].outputZipName = jobinfo['outputZipName']
                        if jobinfo.has_key("outputZipBucketID"):
                            self.__env['jobDic'][k][1].outputZipBucketID = jobinfo['outputZipBucketID']

                        if (self.__env['jobDic'][k][1].result[2] and self.__env['jobDic'][k][1].result[2] != old_pilotecode) or\
                           (self.__env['jobDic'][k][1].pilotErrorDiag and len(self.__env['jobDic'][k][1].pilotErrorDiag) and self.__env['jobDic'][k][1].pilotErrorDiag != old_pilotErrorDiag):

# Path: FileHandling.py
# def updatePilotErrorReport(pilotErrorCode, pilotErrorDiag, priority, jobID, workdir):
#     """ Write pilot error info to file """
#     # Report format:
#     # { jobID1: { priority1: [{ pilotErrorCode1:<nr>, pilotErrorDiag1:<str> }, .. ], .. }, .. }
#     # The pilot will report only the first of the highest priority error when it reports the error at the end of the job
#     # Use the following priority convention:
#     # "0": highest priority [e.g. errors that originate from the main pilot module (unless otherwise necessary)]
#     # "1": high priority [e.g. errors that originate from the Monitor module (-||-)]
#     # "2": normal priority [errors that originate from other modules (-||-)]
#     # etc
# 
#     # Convert to string if integer is sent for priority
#     if type(priority) != str:
#         priority = str(priority)
# 
#     filename = getPilotErrorReportFilename(workdir)
#     if os.path.exists(filename):
#         # The file already exists, read it back (with unicode to utf-8 conversion)
#         dictionary = getJSONDictionary(filename)
#     else:
#         dictionary = {}
# 
#     # Sort the new error
#     if dictionary.has_key(jobID):
#         jobID_dictionary = dictionary[jobID]
# 
#         # Update with the latest error info
#         if not jobID_dictionary.has_key(priority):
#             dictionary[jobID][priority] = []
# 
#         new_dictionary = { 'pilotErrorCode':pilotErrorCode, 'pilotErrorDiag':pilotErrorDiag }
# 
#         # Update the dictionary with the new info
#         dictionary[jobID][priority].append(new_dictionary)
#         print dictionary
# 
#     else:
#         # Create a first entry into the error report
#         dictionary[jobID] = {}
#         dictionary[jobID][priority] = []
#         dictionary[jobID][priority].append({})
#         dictionary[jobID][priority][0] = { 'pilotErrorCode':pilotErrorCode, 'pilotErrorDiag':pilotErrorDiag }
# 
#     # Finally update the file
#     status = writeJSON(filename, dictionary)


# Path: UpdateHandler.py
import os
import json
import time
import traceback
import pUtil
from SocketServer import BaseRequestHandler 
from Configuration import Configuration
from FileHandling import updatePilotErrorReport

    
                        if jobinfo.has_key("output_fields"):
                            self.__env['jobDic'][k][1].output_fields = pUtil.stringToFields(jobinfo["output_fields"])
                            pUtil.tolog("Got output_fields=%s" % str(self.__env['jobDic'][k][1].output_fields))
                            pUtil.tolog("Converted from output_fields=%s" % str(jobinfo["output_fields"]))

                        # corrupted files
                        if jobinfo.has_key("corruptedFiles"):
                            self.__env['jobDic'][k][1].corruptedFiles = jobinfo["corruptedFiles"]

                        # hpc status
                        if jobinfo.has_key("mode"):
                            self.__env['jobDic'][k][1].mode = jobinfo['mode']
                        if jobinfo.has_key("hpcStatus"):
                            self.__env['jobDic'][k][1].hpcStatus = jobinfo['hpcStatus']
                        if jobinfo.has_key("yodaJobMetrics"):
                            self.__env['jobDic'][k][1].yodaJobMetrics = json.loads(jobinfo['yodaJobMetrics'])
                        if jobinfo.has_key("coreCount"):
                            self.__env['jobDic'][k][1].coreCount = jobinfo['coreCount']
                        if jobinfo.has_key("HPCJobId"):
                            self.__env['jobDic'][k][1].HPCJobId = jobinfo['HPCJobId']

                        # zip output
                        if jobinfo.has_key("outputZipName"):
                            self.__env['jobDic'][k][1].outputZipName = jobinfo['outputZipName']
                        if jobinfo.has_key("outputZipBucketID"):
                            self.__env['jobDic'][k][1].outputZipBucketID = jobinfo['outputZipBucketID']

                        if (self.__env['jobDic'][k][1].result[2] and self.__env['jobDic'][k][1].result[2] != old_pilotecode) or\
                           (self.__env['jobDic'][k][1].pilotErrorDiag and len(self.__env['jobDic'][k][1].pilotErrorDiag) and self.__env['jobDic'][k][1].pilotErrorDiag != old_pilotErrorDiag):

                            updatePilotErrorReport(self.__env['jobDic'][k][1].result[2], self.__env['jobDic'][k][1].pilotErrorDiag, "2",  self.__env['jobDic'][k][1].jobId, self.__env['pilot_initdir'])