# 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()
# 
# def readpar(parameter, alt=False, version=0, queuename=None):
#     """ Read 'parameter' from queuedata via SiteInformation class """
# 
#     from SiteInformation import SiteInformation
#     si = SiteInformation()
# 
#     return si.readpar(parameter, alt=alt, version=version, queuename=queuename)
# 
# Path: FileHandling.py
# def getMaxWorkDirSize(path, jobId):
#     """ Return the maximum disk space used by a payload """
# 
#     filename = os.path.join(path, getWorkDirSizeFilename(jobId))
#     maxdirsize = 0
# 
#     if os.path.exists(filename):
#         # Read back the workdir space dictionary
#         dictionary = readJSON(filename)
#         if dictionary != {}:
#             # Get the workdir space list
#             try:
#                 workdir_size_list = dictionary['workdir_size']
#             except Exception, e:
#                 tolog("!!WARNING!!4557!! Could not read back work dir space list: %s" % (e))
#             else:
#                 # Get the maximum value from the list
#                 maxdirsize = max(workdir_size_list)
#         else:
#             tolog("!!WARNING!!4555!! Failed to read back work dir space from file: %s" % (filename))
#     else:
#         tolog("!!WARNING!!4556!! No such file: %s" % (filename))
# 
#     return maxdirsize


import string
import os
import re
import commands
from pUtil import tolog, readpar
from FileHandling import getMaxWorkDirSize

        # Thread(s) per core:    1
        # Core(s) per socket:    1
        # Socket(s):             8

        threads_per_core = 0
        cores_per_socket = 0
        sockets = 0

        threads_pattern = re.compile(r"Thread\(s\)\ per\ core\:.+(\d+)+")
        cores_pattern = re.compile(r"Core\(s\)\ per\ socket\:.+(\d+)+")
        sockets_pattern = re.compile(r"Socket\(s\)\:.+(\d+)+")

        lscpu = os.popen('lscpu')
        if lscpu:
            lines = lscpu.readlines()
            lscpu.close()

            if lines:
                for line in lines:
                    threads_found = re.findall(threads_pattern, line)
                    if len(threads_found) > 0:
                        tolog("Found %s thread(s) per core" % (threads_found[0]))
                        threads_per_core = int(threads_found[0])
                    cores_found = re.findall(cores_pattern, line)
                    if len(cores_found) > 0:
                        tolog("Found %s core(s) per socket" % (cores_found[0]))
                        cores_per_socket = int(cores_found[0])
                    sockets_found = re.findall(sockets_pattern, line)
                    if len(sockets_found) > 0:

# 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()
# 
# def readpar(parameter, alt=False, version=0, queuename=None):
#     """ Read 'parameter' from queuedata via SiteInformation class """
# 
#     from SiteInformation import SiteInformation
#     si = SiteInformation()
# 
#     return si.readpar(parameter, alt=alt, version=version, queuename=queuename)
# 
# Path: FileHandling.py
# def getMaxWorkDirSize(path, jobId):
#     """ Return the maximum disk space used by a payload """
# 
#     filename = os.path.join(path, getWorkDirSizeFilename(jobId))
#     maxdirsize = 0
# 
#     if os.path.exists(filename):
#         # Read back the workdir space dictionary
#         dictionary = readJSON(filename)
#         if dictionary != {}:
#             # Get the workdir space list
#             try:
#                 workdir_size_list = dictionary['workdir_size']
#             except Exception, e:
#                 tolog("!!WARNING!!4557!! Could not read back work dir space list: %s" % (e))
#             else:
#                 # Get the maximum value from the list
#                 maxdirsize = max(workdir_size_list)
#         else:
#             tolog("!!WARNING!!4555!! Failed to read back work dir space from file: %s" % (filename))
#     else:
#         tolog("!!WARNING!!4556!! No such file: %s" % (filename))
# 
#     return maxdirsize


# Path: Node.py
import string
import os
import re
import commands
from pUtil import tolog, readpar
from FileHandling import getMaxWorkDirSize


        # Thread(s) per core:    1
        # Core(s) per socket:    1
        # Socket(s):             8

        threads_per_core = 0
        cores_per_socket = 0
        sockets = 0

        threads_pattern = re.compile(r"Thread\(s\)\ per\ core\:.+(\d+)+")
        cores_pattern = re.compile(r"Core\(s\)\ per\ socket\:.+(\d+)+")
        sockets_pattern = re.compile(r"Socket\(s\)\:.+(\d+)+")

        lscpu = os.popen('lscpu')
        if lscpu:
            lines = lscpu.readlines()
            lscpu.close()

            if lines:
                for line in lines:
                    threads_found = re.findall(threads_pattern, line)
                    if len(threads_found) > 0:
                        tolog("Found %s thread(s) per core" % (threads_found[0]))
                        threads_per_core = int(threads_found[0])
                    cores_found = re.findall(cores_pattern, line)
                    if len(cores_found) > 0:
                        tolog("Found %s core(s) per socket" % (cores_found[0]))
                        cores_per_socket = int(cores_found[0])
                    sockets_found = re.findall(sockets_pattern, line)
                    if len(sockets_found) > 0:

                        tolog("Found %s socket(s)" % (sockets_found[0]))