diff --git a/environment.yml b/environment.yml index b4bdaee..d9b33d3 100644 --- a/environment.yml +++ b/environment.yml @@ -2,6 +2,9 @@ name: livereduce channels: - conda-forge + - mantid dependencies: + - mantid # framework only + - pyinotify - pre-commit diff --git a/livereduce.spec b/livereduce.spec index 499830d..83ef58c 100644 --- a/livereduce.spec +++ b/livereduce.spec @@ -5,7 +5,7 @@ Summary: %{summary} Name: python-%{srcname} -Version: 1.7 +Version: 1.8 Release: %{release}%{?dist} Source0: %{srcname}-%{version}.tar.gz License: MIT diff --git a/ruff.toml b/ruff.toml index bf1abf9..4006e80 100644 --- a/ruff.toml +++ b/ruff.toml @@ -1,4 +1,4 @@ line-length = 120 # https://beta.ruff.rs/docs/rules/ -select = ["A", "ARG", "BLE", "E", "F", "I", "PT", "UP"] +select = ["A", "ARG", "BLE", "E", "F", "I", "PL", "PT", "RUF", "S", "UP"] ignore = [] diff --git a/scripts/livereduce.py b/scripts/livereduce.py index 03f1e39..7de850c 100644 --- a/scripts/livereduce.py +++ b/scripts/livereduce.py @@ -9,28 +9,41 @@ import mantid # for clearer error message import pyinotify from mantid.simpleapi import StartLiveData +from mantid.utils.logging import log_to_python as mtd_log_to_python +from packging.version import parse as parse_version # ################## # configure logging # ################## LOG_NAME = "livereduce" # constant for logging LOG_FILE = "/var/log/SNS_applications/livereduce.log" -logger = logging.getLogger(__name__) -logger.setLevel(logging.INFO) + +# mantid should let python logging do the work +mtd_log_to_python("information") +logging.getLogger("Mantid").setLevel(logging.INFO) # create a file handler if os.environ["USER"] == "snsdata": - handler = logging.FileHandler(LOG_FILE) + fileHandler = logging.FileHandler(LOG_FILE) else: - handler = logging.FileHandler("livereduce.log") -handler.setLevel(logging.INFO) - -# create a logging format + fileHandler = logging.FileHandler("livereduce.log") +fileHandler.setLevel(logging.INFO) +# set the logging format logformat = "%(asctime)s - %(name)s - %(levelname)s - %(message)s" -handler.setFormatter(logging.Formatter(logformat)) +fileHandler.setFormatter(logging.Formatter(logformat)) + +# create a stream handler +streamHandler = logging.StreamHandler() +streamHandler.setLevel(logging.INFO) +streamHandler.addFilter(logging.Filter("Mantid")) + +# add the handlers to the root logger +logging.getLogger().addHandler(fileHandler) +logging.getLogger().addHandler(streamHandler) -# add the handlers to the logger -logger.addHandler(handler) + +logging.getLogger(LOG_NAME).setLevel(logging.INFO) +logger = logging.getLogger(LOG_NAME) logger.info("logging started by user '" + os.environ["USER"] + "'") logger.info(f"using python interpreter {sys.executable}") @@ -40,12 +53,14 @@ class LiveDataManager: """class for handling ``StartLiveData`` and ``MonitorLiveData``""" - logger = logger or logging.getLogger("LiveDataManager") + logger = logging.getLogger(LOG_NAME + ".LiveDataManager") def __init__(self, config): self.config = config def start(self): + mtd_log_to_python("information") + liveArgs = self.config.toStartLiveArgs() self.logger.info("StartLiveData(" + json.dumps(liveArgs, sort_keys=True, indent=2) + ")") try: @@ -106,7 +121,7 @@ class Config: def __init__(self, filename): r"""Optional argument is the json formatted config file""" - self.logger = logger or logging.getLogger("Config") + self.logger = logging.getLogger(LOG_NAME + ".Config") # read file from json into a dict self.filename = None @@ -127,7 +142,7 @@ def __init__(self, filename): self.logger.info(f'mantid_loc="{os.path.dirname(mantid.__file__)}"') try: - from mantid.kernel import UsageService # noqa + from mantid.kernel import UsageService # to differentiate from other apps UsageService.setApplicationName("livereduce") @@ -254,7 +269,7 @@ def toJson(self, **kwargs): #################### class EventHandler(pyinotify.ProcessEvent): - logger = logger or logging.getLogger("EventHandler") + logger = logging.getLogger(LOG_NAME + ".EventHandler") def __init__(self, config, livemanager): # files that we actually care about @@ -273,7 +288,12 @@ def __init__(self, config, livemanager): def _md5(self, filename): if filename and os.path.exists(filename): - return md5(open(filename, "rb").read()).hexdigest() + # starting in python 3.9 one can point out md5 is not used in security context + if parse_version(sys.version) < parse_version("3.9"): + md5sum = md5(open(filename, "rb").read()) # noqa: S324 + else: + md5sum = md5(open(filename, "rb").read(), usedforsecurity=False) + return md5sum.hexdigest() else: return "" @@ -325,9 +345,10 @@ def process_default(self, event): # for passing into the eventhandler for inotify liveDataMgr = LiveDataManager(config) -handler = EventHandler(config, LiveDataManager(config)) +handler = EventHandler(config, liveDataMgr) wm = pyinotify.WatchManager() notifier = pyinotify.Notifier(wm, handler) + # watched events mask = pyinotify.IN_DELETE | pyinotify.IN_MODIFY | pyinotify.IN_CREATE logger.info(f"WATCHING:{handler.filestowatch()}") diff --git a/setup.py b/setup.py index 5cfa00a..b52d208 100644 --- a/setup.py +++ b/setup.py @@ -2,7 +2,7 @@ setup( name="livereduce", - version="1.7", + version="1.8", description="Need a description", author="Pete Peterson", author_email="petersonpf@ornl.gov", diff --git a/test/README.md b/test/README.md index ddf72e9..a48ad51 100644 --- a/test/README.md +++ b/test/README.md @@ -1,6 +1,7 @@ This should be a fully working example. All that you need is to add a working version of mantid. Please note that the server and client need -to be started separately. +to be started separately and are configured to be executed with the +conda environment activated Start Live Data Server ---------------------- diff --git a/test/fake.conf b/test/fake.conf index 76f259c..89bf3fb 100644 --- a/test/fake.conf +++ b/test/fake.conf @@ -2,6 +2,7 @@ "instrument": "ISIS_Histogram", "script_dir": "test", "update_every": 3, + "CONDA_ENV": "livereduce", "accum_method":"Replace", "periods":[1,3] }