Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/logging #85

Merged
merged 11 commits into from
Aug 29, 2017
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!")