Skip to content

Commit

Permalink
Feature/logging (#85)
Browse files Browse the repository at this point in the history
* added log method in class Generator for storing information about command line arguments, date, time, user and hostname in generatemc.log

* added loging current working directory

* fixed pep8 issues without changing some lines

* merged save_logs and log methods and minor esthetical changes

* changed logging using python logging feature

* some fixes in logging

* changed imports order in generator.py according to imports formating guidelines

* changed logger naming

* changed logger naming

* added some formatting for logging batch system output
  • Loading branch information
PanSzczerba authored and grzanka committed Aug 29, 2017
1 parent e02ebcd commit 4c8bc3c
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 18 deletions.
18 changes: 15 additions & 3 deletions mcpartools/generator.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
import os
import getpass
import logging
import os
import shutil
import socket
import sys
import time

from mcpartools.mcengine.common import EngineDiscover
from mcpartools.scheduler.common import SchedulerDiscover

logger = logging.getLogger(__name__)

file_logger = logging.getLogger('file_logger')
file_logger.setLevel(logging.INFO)
file_logger.propagate = False


class Options:

Expand Down Expand Up @@ -139,7 +146,7 @@ def run(self):
# make symlinks to external files found
self.symlink_external_files()

# save logs
# store information about command line arguments, date, time, user and hostname into generatemc.log
self.save_logs()

return 0
Expand All @@ -159,6 +166,8 @@ def generate_main_dir(self):
os.mkdir(dir_path)
self.main_dir = dir_path

file_logger.addHandler(logging.FileHandler(os.path.join(dir_path, "generatemc.log"), mode='w+'))

def generate_workspace(self):
wspdir_name = 'workspace'
wspdir_path = os.path.join(self.main_dir, wspdir_name)
Expand Down Expand Up @@ -225,4 +234,7 @@ def symlink_external_files(self):
os.symlink(abs_path, os.path.join(jobdir_path, os.path.split(abs_path)[-1]))

def save_logs(self):
pass
file_logger.info('Executed command: ' + ' '.join(sys.argv))
file_logger.info('Date and time: ' + time.strftime("%Y-%m-%d %H:%M:%S"))
file_logger.info('username@hostname: ' + getpass.getuser() + '@' + socket.gethostname())
file_logger.info('Current working directory: ' + os.getcwd())
31 changes: 16 additions & 15 deletions mcpartools/scheduler/common.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import logging
import os
from subprocess import check_call, CalledProcessError
from subprocess import check_output, CalledProcessError

from mcpartools.scheduler.slurm import Slurm
from mcpartools.scheduler.torque import Torque
Expand All @@ -16,17 +15,19 @@ def __init__(self):

@classmethod
def get_scheduler(cls, scheduler_options, log_location):
with open(os.path.join(log_location, "generatemc.log"), 'w+') as LOG_FILE:
try:
check_call(['srun --version'], stdout=LOG_FILE, stderr=LOG_FILE, shell=True)
logger.debug("Discovered job scheduler SLURM")
return Slurm(scheduler_options)
except CalledProcessError as e:
logger.debug("Slurm not found: %s", e)
try:
check_call(['qsub --version'], stdout=LOG_FILE, stderr=LOG_FILE, shell=True)
logger.debug("Discovered job scheduler Torque")
return Torque(scheduler_options)
except CalledProcessError as e:
logger.debug("Torque not found: %s", e)
file_logger = logging.getLogger('file_logger')
try:
srun_output = check_output(['srun --version'], shell=True)
file_logger.info("srun output: {}".format(srun_output[:-1]))
logger.debug("Discovered job scheduler SLURM")
return Slurm(scheduler_options)
except CalledProcessError as e:
logger.debug("Slurm not found: %s", e)
try:
qsub_output = check_output(['qsub --version'], shell=True)
file_logger.info("qsub output: {}".format(qsub_output[:-1]))
logger.debug("Discovered job scheduler Torque")
return Torque(scheduler_options)
except CalledProcessError as e:
logger.debug("Torque not found: %s", e)
raise SystemError("No known batch system found!")

0 comments on commit 4c8bc3c

Please sign in to comment.