# Path: myDevices/utils/logger.py
# def debug(message):
#     outerFrame = getouterframes(currentframe())[1][0]
#     (args, _, _, values) = getargvalues(outerFrame)
#     argsValue = ''
#     
#     for i in args:
#         if i is 'self':
#             continue
#         argsValue += "(%s=%s)" % (i, str(values[i]))
#     
#     stack = extract_stack()
#     (filename, line, procname, text) = stack[-2]
#     LOGGER.debug(str(filename) + ' ' + str(procname) + str(argsValue) + ':' + str(line) + '> '  + str(message))
# 
# def info(message):
#     # if checkFlood(message):
#     #     return
#     LOGGER.info(message)
# 
# def error(message, *args, **kwargs):
#     if checkFlood(message):
#         return
#     LOGGER.error(message, *args, **kwargs)
# 
# def exception(message):
#     if checkFlood(message):
#         return
#     LOGGER.exception(message)


from subprocess import Popen, PIPE, DEVNULL
from myDevices.utils.logger import debug, info, error, exception
        from resource import getrlimit, setrlimit, RLIMIT_AS
def setMemoryLimits():
    """Set memory limit when launching a process to the default maximum"""
    try:
        soft, hard = getrlimit(RLIMIT_AS)
        setrlimit(RLIMIT_AS, (hard, hard))
    except:
        pass

def executeCommand(command, increaseMemoryLimit=False, disablePipe=False):
    """Execute a specified command, increasing the processes memory limits if specified"""
    debug('executeCommand: ' +  command)
    output = ''
    returncode = 1
    try:
        preexec = None
        pipe = PIPE
        if increaseMemoryLimit:
            preexec = setMemoryLimits
        if disablePipe:
            debug('Disable pipe to prevent child exiting when parent exits')
            pipe = DEVNULL
        process = Popen(command, stdout=pipe, stderr=pipe, shell=True, preexec_fn=preexec)
        (stdout_data, stderr_data) = process.communicate()
        returncode = process.wait()
        returncode = process.returncode
        # debug('executeCommand: stdout_data {}, stderr_data {}'.format(stdout_data, stderr_data))
        if stdout_data:
            output = stdout_data.decode('utf-8')
            stdout_data = None
    except:

# Path: myDevices/utils/logger.py
# def debug(message):
#     outerFrame = getouterframes(currentframe())[1][0]
#     (args, _, _, values) = getargvalues(outerFrame)
#     argsValue = ''
#     
#     for i in args:
#         if i is 'self':
#             continue
#         argsValue += "(%s=%s)" % (i, str(values[i]))
#     
#     stack = extract_stack()
#     (filename, line, procname, text) = stack[-2]
#     LOGGER.debug(str(filename) + ' ' + str(procname) + str(argsValue) + ':' + str(line) + '> '  + str(message))
# 
# def info(message):
#     # if checkFlood(message):
#     #     return
#     LOGGER.info(message)
# 
# def error(message, *args, **kwargs):
#     if checkFlood(message):
#         return
#     LOGGER.error(message, *args, **kwargs)
# 
# def exception(message):
#     if checkFlood(message):
#         return
#     LOGGER.exception(message)


# Path: myDevices/utils/subprocess.py
from subprocess import Popen, PIPE, DEVNULL
from myDevices.utils.logger import debug, info, error, exception
        from resource import getrlimit, setrlimit, RLIMIT_AS

def setMemoryLimits():
    """Set memory limit when launching a process to the default maximum"""
    try:
        soft, hard = getrlimit(RLIMIT_AS)
        setrlimit(RLIMIT_AS, (hard, hard))
    except:
        pass

def executeCommand(command, increaseMemoryLimit=False, disablePipe=False):
    """Execute a specified command, increasing the processes memory limits if specified"""
    debug('executeCommand: ' +  command)
    output = ''
    returncode = 1
    try:
        preexec = None
        pipe = PIPE
        if increaseMemoryLimit:
            preexec = setMemoryLimits
        if disablePipe:
            debug('Disable pipe to prevent child exiting when parent exits')
            pipe = DEVNULL
        process = Popen(command, stdout=pipe, stderr=pipe, shell=True, preexec_fn=preexec)
        (stdout_data, stderr_data) = process.communicate()
        returncode = process.wait()
        returncode = process.returncode
        # debug('executeCommand: stdout_data {}, stderr_data {}'.format(stdout_data, stderr_data))
        if stdout_data:
            output = stdout_data.decode('utf-8')
            stdout_data = None
    except:

        exception('executeCommand failed: ' + command)