# 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: FileHandling.py
# def getExtension(alternative='pickle'):
#     """ get the file extension (json or whatever 'alternative' is set to, pickle by default) """
# 
#     try:
#         from json import load
#     except:
#         extension = alternative
#     else:
#         extension = "json"
# 
#     return extension


import os
import commands
from pUtil import tolog
from FileHandling import getExtension
                    from json import load
                from pickle import load
                from json import dump
                from pickle import dump
        from glob import glob
                    status = False

                fp.close()
                                                                                                        
        return status

    def rename(self, site, job):
        """
        Rename the job state file. Should only be called for
        holding jobs that have passed the maximum number of recovery attempts.
        """
        status = True

        # # Experiencing problems with that code
        # get the file extension
        # extension = getExtension()
        #
        # fileNameOld = "%s/jobState-%s.%s" % (site.workdir, job.jobId, extension)
        # fileNameNew = "%s/jobState-%s.%s.MAXEDOUT" % (site.workdir, job.jobId, extension)

        fileNameOld = self.getFilename(site.workdir,job.jobId)
        fileNameNew = "%s.MAXEDOUT" % fileNameOld
        if os.path.isfile(fileNameOld):
            # rename the job state file
            try:
                os.system("mv %s %s" % (fileNameOld, fileNameNew))
            except OSError:
                tolog("JOBSTATE FAILURE: Failed to rename job state file: %s" % (fileNameOld))
                status = False
            else:

# 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: FileHandling.py
# def getExtension(alternative='pickle'):
#     """ get the file extension (json or whatever 'alternative' is set to, pickle by default) """
# 
#     try:
#         from json import load
#     except:
#         extension = alternative
#     else:
#         extension = "json"
# 
#     return extension


# Path: JobState.py
import os
import commands
from pUtil import tolog
from FileHandling import getExtension
                    from json import load
                from pickle import load
                from json import dump
                from pickle import dump
        from glob import glob

                    status = False

                fp.close()
                                                                                                        
        return status

    def rename(self, site, job):
        """
        Rename the job state file. Should only be called for
        holding jobs that have passed the maximum number of recovery attempts.
        """
        status = True

        # # Experiencing problems with that code
        # get the file extension
        # extension = getExtension()
        #
        # fileNameOld = "%s/jobState-%s.%s" % (site.workdir, job.jobId, extension)
        # fileNameNew = "%s/jobState-%s.%s.MAXEDOUT" % (site.workdir, job.jobId, extension)

        fileNameOld = self.getFilename(site.workdir,job.jobId)
        fileNameNew = "%s.MAXEDOUT" % fileNameOld
        if os.path.isfile(fileNameOld):
            # rename the job state file
            try:
                os.system("mv %s %s" % (fileNameOld, fileNameNew))
            except OSError:
                tolog("JOBSTATE FAILURE: Failed to rename job state file: %s" % (fileNameOld))
                status = False
            else:

                tolog("Job state file renamed to: %s" % (fileNameNew))