<commit_before>#!/usr/bin/env python

# Based on previous work by
# <NAME> (see: http://stackoverflow.com/questions/10217067/implementing-a-full-python-unix-style-daemon-process)
# and <NAME> (see: http://www.jejik.com/articles/2007/02/a_simple_unix_linux_daemon_in_python/)

# Adapted by M.Hendrix [2015]

# daemon11.py measures the CPU temperature.
# uses moving averages

import syslog, traceback
import os, sys, time, math, ConfigParser
from libdaemon import Daemon
import liblnx


class MyDaemon(Daemon):
  def run(self):
    iniconf = ConfigParser.ConfigParser()
    inisection = "11"
    home = os.path.expanduser('~')
    s = iniconf.read(home + '/' + lblnx.LEAF + '/config.ini')
    syslog_trace("Config file   : {0}".format(s), False, DEBUG)
    syslog_trace("Options       : {0}".format(iniconf.items(inisection)), False, DEBUG)
    reportTime = iniconf.getint(inisection, "reporttime")
    cycles = iniconf.getint(inisection, "cycles")
    samplesperCycle = iniconf.getint(inisection, "samplespercycle")
    flock = iniconf.get(inisection, "lockfile")
    fdata = iniconf.get(inisection, "resultfile")

    samples = samplesperCycle * cycles              # total number of samples averaged
    sampleTime = reportTime/samplesperCycle         # time [s] between samples
    cycleTime = samples * sampleTime                # time [s] per cycle

    data = []                                       # array for holding sampledata

    try:
      hwdevice = iniconf.get(inisection, liblnx.NODE+".hwdevice")
    except ConfigParser.NoOptionError as e:  #no hwdevice
      syslog_trace("** {0}".format(e.message), False, DEBUG)
      sys.exit(0)
    if not os.path.isfile(hwdevice):
      syslog_trace("** Device not found: {0}".format(hwdevice), syslog.LOG_INFO, DEBUG)
      sys.exit(1)

    while True:
      try:
        startTime = time.time()

        result = do_work(hwdevice)
        syslog_trace("Result   : {0}".format(result), False, DEBUG)

        data.append(float(result))
        if (len(data) > samples):
          data.pop(0)
        syslog_trace("Data     : {0}".format(data),   False, DEBUG)

        # report sample average
        if (startTime % reportTime < sampleTime):
          averages = sum(data[:]) / len(data)
          syslog_trace("Averages : {0}".format(averages),  False, DEBUG)
          do_report(averages, flock, fdata)

        waitTime = sampleTime - (time.time() - startTime) - (startTime%sampleTime)
        if (waitTime > 0):
          syslog_trace("Waiting  : {0}s".format(waitTime), False, DEBUG)
          syslog_trace("................................", False, DEBUG)
          time.sleep(waitTime)
      except Exception as e:
        syslog_trace("Unexpected error in run()", syslog.LOG_ALERT, DEBUG)
        syslog_trace("e.message : {0}".format(e.message), syslog.LOG_ALERT, DEBUG)
        syslog_trace("e.__doc__ : {0}".format(e.__doc__), syslog.LOG_ALERT, DEBUG)
        syslog_trace(traceback.format_exc(), syslog.LOG_ALERT, DEBUG)
        raise

def do_work(fdev):
  Tcpu = "NaN"
  # Read the CPU temperature
  with open(fdev,'r') as f:
    Tcpu = float(f.read().strip('\n'))/1000
  if Tcpu > 75.000:
    # can't believe my sensors. Probably a glitch. Wait a while then measure again
    time.sleep(7)
    with open(fdev,'r') as f:
      Tcpu = float(f.read().strip('\n'))/1000
      Tcpu = float(Tcpu) + 0.1

  return Tcpu

def do_report(result, flock, fdata):
  # Get the time and date in human-readable form and UN*X-epoch...
  outDate = time.strftime('%Y-%m-%dT%H:%M:%S')
  outEpoch = int(time.strftime('%s'))
  # round to current minute to ease database JOINs
  outEpoch = outEpoch - (outEpoch % 60)
  lock(flock)
  with open(fdata, 'a') as f:
    f.write('{0}, {1}\n'.format(outDate, outEpoch, float(result)) )
  unlock(flock)

if __name__ == "__main__":

  #if not os.path.isfile('/sys/class/hwmon/hwmon0/device/temp1_input'):
  #  print "Hardware missing!"
  #  syslog.syslog(syslog.LOG_INFO,"Hardware missing!")
  #  sys.exit(2)

  daemon = MyDaemon('/tmp/' + liblnx.LEAF + '/11.pid')
  if len(sys.argv) == 2:
    if 'start' == sys.argv[1]:
      daemon.start()
    elif 'stop' == sys.argv[1]:
      daemon.stop()
    elif 'restart' == sys.argv[1]:
      daemon.restart()
    elif 'foreground' == sys.argv[1]:
      # assist with debugging.
      print "Debug-mode started. Use <Ctrl>+C to stop."
      DEBUG = True
      syslog_trace("Daemon logging is ON", syslog.LOG_DEBUG, DEBUG)
      daemon.run()
    else:
      print "Unknown command"
      sys.exit(2)
    sys.exit(0)
  else:
    print "usage: {0!s} start|stop|restart|foreground".format(sys.argv[0])
    sys.exit(2)<commit_msg>liblnx.syslog_trace<commit_after>#!/usr/bin/env python

# Based on previous work by
# <NAME> (see: http://stackoverflow.com/questions/10217067/implementing-a-full-python-unix-style-daemon-process)
# and <NAME> (see: http://www.jejik.com/articles/2007/02/a_simple_unix_linux_daemon_in_python/)

# Adapted by M.Hendrix [2015]

# daemon11.py measures the CPU temperature.
# uses moving averages

import syslog, traceback
import os, sys, time, math, ConfigParser
from libdaemon import Daemon
import liblnx


class MyDaemon(Daemon):
  def run(self):
    iniconf = ConfigParser.ConfigParser()
    inisection = "11"
    home = os.path.expanduser('~')
    s = iniconf.read(home + '/' + lblnx.LEAF + '/config.ini')
    liblnx.syslog_trace("Config file   : {0}".format(s), False, DEBUG)
    liblnx.syslog_trace("Options       : {0}".format(iniconf.items(inisection)), False, DEBUG)
    reportTime = iniconf.getint(inisection, "reporttime")
    cycles = iniconf.getint(inisection, "cycles")
    samplesperCycle = iniconf.getint(inisection, "samplespercycle")
    flock = iniconf.get(inisection, "lockfile")
    fdata = iniconf.get(inisection, "resultfile")

    samples = samplesperCycle * cycles              # total number of samples averaged
    sampleTime = reportTime/samplesperCycle         # time [s] between samples
    cycleTime = samples * sampleTime                # time [s] per cycle

    data = []                                       # array for holding sampledata

    try:
      hwdevice = iniconf.get(inisection, liblnx.NODE+".hwdevice")
    except ConfigParser.NoOptionError as e:  #no hwdevice
      liblnx.syslog_trace("** {0}".format(e.message), False, DEBUG)
      sys.exit(0)
    if not os.path.isfile(hwdevice):
      liblnx.syslog_trace("** Device not found: {0}".format(hwdevice), syslog.LOG_INFO, DEBUG)
      sys.exit(1)

    while True:
      try:
        startTime = time.time()

        result = do_work(hwdevice)
        liblnx.syslog_trace("Result   : {0}".format(result), False, DEBUG)

        data.append(float(result))
        if (len(data) > samples):
          data.pop(0)
        liblnx.syslog_trace("Data     : {0}".format(data),   False, DEBUG)

        # report sample average
        if (startTime % reportTime < sampleTime):
          averages = sum(data[:]) / len(data)
          liblnx.syslog_trace("Averages : {0}".format(averages),  False, DEBUG)
          do_report(averages, flock, fdata)

        waitTime = sampleTime - (time.time() - startTime) - (startTime%sampleTime)
        if (waitTime > 0):
          liblnx.syslog_trace("Waiting  : {0}s".format(waitTime), False, DEBUG)
          liblnx.syslog_trace("................................", False, DEBUG)
          time.sleep(waitTime)
      except Exception as e:
        liblnx.syslog_trace("Unexpected error in run()", syslog.LOG_ALERT, DEBUG)
        liblnx.syslog_trace("e.message : {0}".format(e.message), syslog.LOG_ALERT, DEBUG)
        liblnx.syslog_trace("e.__doc__ : {0}".format(e.__doc__), syslog.LOG_ALERT, DEBUG)
        liblnx.syslog_trace(traceback.format_exc(), syslog.LOG_ALERT, DEBUG)
        raise

def do_work(fdev):
  Tcpu = "NaN"
  # Read the CPU temperature
  with open(fdev,'r') as f:
    Tcpu = float(f.read().strip('\n'))/1000
  if Tcpu > 75.000:
    # can't believe my sensors. Probably a glitch. Wait a while then measure again
    time.sleep(7)
    with open(fdev,'r') as f:
      Tcpu = float(f.read().strip('\n'))/1000
      Tcpu = float(Tcpu) + 0.1

  return Tcpu

def do_report(result, flock, fdata):
  # Get the time and date in human-readable form and UN*X-epoch...
  outDate = time.strftime('%Y-%m-%dT%H:%M:%S')
  outEpoch = int(time.strftime('%s'))
  # round to current minute to ease database JOINs
  outEpoch = outEpoch - (outEpoch % 60)
  lock(flock)
  with open(fdata, 'a') as f:
    f.write('{0}, {1}\n'.format(outDate, outEpoch, float(result)) )
  unlock(flock)

if __name__ == "__main__":

  #if not os.path.isfile('/sys/class/hwmon/hwmon0/device/temp1_input'):
  #  print "Hardware missing!"
  #  syslog.syslog(syslog.LOG_INFO,"Hardware missing!")
  #  sys.exit(2)

  daemon = MyDaemon('/tmp/' + liblnx.LEAF + '/11.pid')
  if len(sys.argv) == 2:
    if 'start' == sys.argv[1]:
      daemon.start()
    elif 'stop' == sys.argv[1]:
      daemon.stop()
    elif 'restart' == sys.argv[1]:
      daemon.restart()
    elif 'foreground' == sys.argv[1]:
      # assist with debugging.
      print "Debug-mode started. Use <Ctrl>+C to stop."
      DEBUG = True
      liblnx.syslog_trace("Daemon logging is ON", syslog.LOG_DEBUG, DEBUG)
      daemon.run()
    else:
      print "Unknown command"
      sys.exit(2)
    sys.exit(0)
  else:
    print "usage: {0!s} start|stop|restart|foreground".format(sys.argv[0])
    sys.exit(2)
{'fraction_non_alphanumeric': 0.15003185389679338, 'fraction_numerical': 0.015077511148863878, 'mean_word_length': 2.9018227009113504, 'pattern_counts': {'":': 2, '<': 11, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2168938', 'n_tokens_mistral': 3374, 'n_tokens_neox': 3260, 'n_words': 707}