# Path: pUtil.py
# def tolog(msg, tofile=True, label='INFO', essential=False):
#     """ Write date+msg to pilot log and to stdout """
# 
#     try:
#         import inspect
# 
#         MAXLENGTH = 12
#         # getting the name of the module that is invoking tolog() and adjust the length
#         try:
#             module_name = os.path.basename(inspect.stack()[1][1])
#         except Exception, e:
#             module_name = "unknown"
#             #print "Exception caught by tolog(): ", e,
#         module_name_cut = module_name[0:MAXLENGTH].ljust(MAXLENGTH)
#         msg = "%i|%s| %s" % (os.getpid(),module_name_cut, msg)
# 
#         t = timeStampUTC(format='%Y-%m-%d %H:%M:%S')
#         if tofile:
#             appendToLog("%s|%s\n" % (t, msg))
# 
#         # remove backquotes from the msg since they cause problems with batch submission of pilot
#         # (might be present in error messages from the OS)
#         msg = msg.replace("`","'")
#         msg = msg.replace('"','\\"')
#         print "%s| %s" % (t, msg)
# 
#         # write any FAILED messages to stderr
#         if "!!FAILED!!" in msg:
#             try:
#                 print >> sys.stderr, "%s| %s" % (t, msg)
#             except:
#                 print "Failed to print to sys.stderr: %s" % (t, msg)
#     except:
#         print "!!WARNING!!4000!! %s" % traceback.format_exc()


import os
import commands
from pUtil import tolog
            from pickle import load
        from pickle import dump

    E.g. a file with state = "created", "not_registered" should first be transferred and then registered in the LFC.
    The file state dictionary should be created with "not_created" states as soon as the output files are known (pilot).
    The "created" states should be set after the payload has run and if the file in question were actually created.
    "transferred" should be set by the mover once the file in question has been transferred.
    "registered" should be added to the file state once the file has been registered.
    "copy_to_scratch" is to set for all input files by default. In case remote IO / FileStager instructions are found in copysetup[in]
    the state will be changed to "remote_io" / "file_stager". Brokerage can also decide that remote IO is to be used. In that case,
    "remote_io" will be set for the relevant input files (e.g. DBRelease and lib files are excluded, i.e. they will have "copy_to_scratch"
    transfer mode).
    """

    def __init__(self, workDir, jobId="0", mode="", ftype="output", fileName=""):
        """ Default init """

        self.fileStateDictionary = {} # file dictionary holding all objects
        self.mode = mode              # test mode

        # use default filename unless specified by initiator
        if fileName == "" and ftype != "": # assume output files
            self.filename = os.path.join(workDir, "fileState-%s-%s.pickle" % (ftype, jobId))
        else:
            self.filename = os.path.join(workDir, fileName)

        # add mode variable if needed (e.g. mode="test")
        if self.mode != "":
            self.filename = self.filename.replace(".pickle", "-%s.pickle" % (self.mode))

        # load the dictionary from file if it exists
        if os.path.exists(self.filename):

# Path: pUtil.py
# def tolog(msg, tofile=True, label='INFO', essential=False):
#     """ Write date+msg to pilot log and to stdout """
# 
#     try:
#         import inspect
# 
#         MAXLENGTH = 12
#         # getting the name of the module that is invoking tolog() and adjust the length
#         try:
#             module_name = os.path.basename(inspect.stack()[1][1])
#         except Exception, e:
#             module_name = "unknown"
#             #print "Exception caught by tolog(): ", e,
#         module_name_cut = module_name[0:MAXLENGTH].ljust(MAXLENGTH)
#         msg = "%i|%s| %s" % (os.getpid(),module_name_cut, msg)
# 
#         t = timeStampUTC(format='%Y-%m-%d %H:%M:%S')
#         if tofile:
#             appendToLog("%s|%s\n" % (t, msg))
# 
#         # remove backquotes from the msg since they cause problems with batch submission of pilot
#         # (might be present in error messages from the OS)
#         msg = msg.replace("`","'")
#         msg = msg.replace('"','\\"')
#         print "%s| %s" % (t, msg)
# 
#         # write any FAILED messages to stderr
#         if "!!FAILED!!" in msg:
#             try:
#                 print >> sys.stderr, "%s| %s" % (t, msg)
#             except:
#                 print "Failed to print to sys.stderr: %s" % (t, msg)
#     except:
#         print "!!WARNING!!4000!! %s" % traceback.format_exc()


# Path: FileState.py
import os
import commands
from pUtil import tolog
            from pickle import load
        from pickle import dump


    E.g. a file with state = "created", "not_registered" should first be transferred and then registered in the LFC.
    The file state dictionary should be created with "not_created" states as soon as the output files are known (pilot).
    The "created" states should be set after the payload has run and if the file in question were actually created.
    "transferred" should be set by the mover once the file in question has been transferred.
    "registered" should be added to the file state once the file has been registered.
    "copy_to_scratch" is to set for all input files by default. In case remote IO / FileStager instructions are found in copysetup[in]
    the state will be changed to "remote_io" / "file_stager". Brokerage can also decide that remote IO is to be used. In that case,
    "remote_io" will be set for the relevant input files (e.g. DBRelease and lib files are excluded, i.e. they will have "copy_to_scratch"
    transfer mode).
    """

    def __init__(self, workDir, jobId="0", mode="", ftype="output", fileName=""):
        """ Default init """

        self.fileStateDictionary = {} # file dictionary holding all objects
        self.mode = mode              # test mode

        # use default filename unless specified by initiator
        if fileName == "" and ftype != "": # assume output files
            self.filename = os.path.join(workDir, "fileState-%s-%s.pickle" % (ftype, jobId))
        else:
            self.filename = os.path.join(workDir, fileName)

        # add mode variable if needed (e.g. mode="test")
        if self.mode != "":
            self.filename = self.filename.replace(".pickle", "-%s.pickle" % (self.mode))

        # load the dictionary from file if it exists
        if os.path.exists(self.filename):

            tolog("Using file state dictionary: %s" % (self.filename))