# Path: Logger.py
# class Logger:
# 
#     sh1 = None
#     sh2 = None
# 
#     def __init__(self, filename=None):
# 
#         # get logger name
#         print inspect.stack()
#         frm = inspect.stack()[1]
#         mod = inspect.getmodule(frm[2])
#         if mod == None or mod.__name__ == '__main__':
#             modName = 'main'
#         else:
#             modName = '.'.join(mod.__name__.split('.')[-2:])
#         global loggerMap
#         if modName in loggerMap:
#             # use existing logger
#             self.log = loggerMap[modName]
#         else:
#             # make handler
#             fmt = MyFormatter()
#             fmt.converter = time.gmtime # to convert timestamps to UTC
# #            if filename:
# #                sh = logging.FileHandler(filename, mode='a')
# #            else:
# #                sh = logging.StreamHandler(sys.stdout)
#             self.sh1 = logging.FileHandler(filename, mode='a')
#             self.sh2 = logging.StreamHandler(sys.stdout)
#             self.sh1.setLevel(logging.DEBUG)
#             self.sh1.setFormatter(fmt)
#             self.sh2.setLevel(logging.DEBUG)
#             self.sh2.setFormatter(fmt)
# 
#             # make logger
#             self.log = logging.getLogger(modName)
#             self.log.propagate = False
#             self.log.addHandler(self.sh1)
#             self.log.addHandler(self.sh2)
#             loggerMap[modName] = self.log
# 
#     def info(self, msg):
#         self.log.info(msg)
#         #self.sh1.flush()
#         #self.sh2.flush()
# 
#     def debug(self, msg):
#         self.log.debug(msg)
# 
#     def warning(self, msg):
#         self.log.warning(msg)
# 
#     def error(self, msg):
#         self.log.error(msg)
# 
#     def critical(self, msg):
#         self.log.critical(msg)


import commands
import os
import shutil
import sys
import time
import traceback
import json
import pickle
import logging
from Logger import Logger
            from pandayoda.yodacore import Database
    def flushOutputs(self):
        try:
            self.__log.debug("Flush Yoda outputs")
            db = Database.Backend(self.__globalYodaDir)
            db.dumpUpdates(True)
        except:
            self.__log.debug("Failed to flush outputs: %s" % traceback.format_exc())

    def old_postRun(self):
        try:
            self.__log.debug("postRun")
            all_files = os.listdir(self.__globalYodaDir)
            for file in all_files:
                path = os.path.join(self.__globalYodaDir, file)
                if file.startswith("rank_") and os.path.isdir(path):
                    self.__log.debug("Found dir %s" % path)
                    for jobId in self.__jobs:
                        dest_dir = os.path.join(self.__jobs[jobId]['GlobalWorkingDir'], file)
                        if not os.path.exists(dest_dir):
                            os.makedirs(dest_dir)
                        for localFile in os.listdir(path):
                            localPath = os.path.join(path, localFile)
                            self.__log.debug("Copying %s to %s" % (localPath, dest_dir))
                            if os.path.isdir(localPath):
                                try:
                                    shutil.copytree(localPath, dest_dir)
                                except:
                                    self.__log.warning("Failed to copy %s to %s: %s" % (localPath, dest_dir, traceback.format_exc()))
                            else:
                                try:

# Path: Logger.py
# class Logger:
# 
#     sh1 = None
#     sh2 = None
# 
#     def __init__(self, filename=None):
# 
#         # get logger name
#         print inspect.stack()
#         frm = inspect.stack()[1]
#         mod = inspect.getmodule(frm[2])
#         if mod == None or mod.__name__ == '__main__':
#             modName = 'main'
#         else:
#             modName = '.'.join(mod.__name__.split('.')[-2:])
#         global loggerMap
#         if modName in loggerMap:
#             # use existing logger
#             self.log = loggerMap[modName]
#         else:
#             # make handler
#             fmt = MyFormatter()
#             fmt.converter = time.gmtime # to convert timestamps to UTC
# #            if filename:
# #                sh = logging.FileHandler(filename, mode='a')
# #            else:
# #                sh = logging.StreamHandler(sys.stdout)
#             self.sh1 = logging.FileHandler(filename, mode='a')
#             self.sh2 = logging.StreamHandler(sys.stdout)
#             self.sh1.setLevel(logging.DEBUG)
#             self.sh1.setFormatter(fmt)
#             self.sh2.setLevel(logging.DEBUG)
#             self.sh2.setFormatter(fmt)
# 
#             # make logger
#             self.log = logging.getLogger(modName)
#             self.log.propagate = False
#             self.log.addHandler(self.sh1)
#             self.log.addHandler(self.sh2)
#             loggerMap[modName] = self.log
# 
#     def info(self, msg):
#         self.log.info(msg)
#         #self.sh1.flush()
#         #self.sh2.flush()
# 
#     def debug(self, msg):
#         self.log.debug(msg)
# 
#     def warning(self, msg):
#         self.log.warning(msg)
# 
#     def error(self, msg):
#         self.log.error(msg)
# 
#     def critical(self, msg):
#         self.log.critical(msg)


# Path: HPC/HPCManager.py
import commands
import os
import shutil
import sys
import time
import traceback
import json
import pickle
import logging
from Logger import Logger
            from pandayoda.yodacore import Database

    def flushOutputs(self):
        try:
            self.__log.debug("Flush Yoda outputs")
            db = Database.Backend(self.__globalYodaDir)
            db.dumpUpdates(True)
        except:
            self.__log.debug("Failed to flush outputs: %s" % traceback.format_exc())

    def old_postRun(self):
        try:
            self.__log.debug("postRun")
            all_files = os.listdir(self.__globalYodaDir)
            for file in all_files:
                path = os.path.join(self.__globalYodaDir, file)
                if file.startswith("rank_") and os.path.isdir(path):
                    self.__log.debug("Found dir %s" % path)
                    for jobId in self.__jobs:
                        dest_dir = os.path.join(self.__jobs[jobId]['GlobalWorkingDir'], file)
                        if not os.path.exists(dest_dir):
                            os.makedirs(dest_dir)
                        for localFile in os.listdir(path):
                            localPath = os.path.join(path, localFile)
                            self.__log.debug("Copying %s to %s" % (localPath, dest_dir))
                            if os.path.isdir(localPath):
                                try:
                                    shutil.copytree(localPath, dest_dir)
                                except:
                                    self.__log.warning("Failed to copy %s to %s: %s" % (localPath, dest_dir, traceback.format_exc()))
                            else:
                                try:

                                    shutil.copy(localPath, dest_dir)