Newer
Older
# Copyright ETH 2018 - 2023 Zürich, Scientific IT Services
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
from datetime import datetime
import os
class CommandLog(object):
""" CommandLog can write a log for a command with multiple steps.
The idea is that, when a later step fails, we want to know what happened
before. This can be useful for cases where no automatic recovery can
be done. If the success method is called, the log is removed.
If there is an existing log, the user must first make sure everything
is in order and delete the log before using obis """
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
def __init__(self):
self.folder_path = os.path.join(os.path.expanduser('~'), ".obis", "log")
self.file_paths = []
self.logs = []
self.most_recent_command = None
def any_log_exists(self):
if os.path.exists(self.folder_path) == False:
return False
return len(os.listdir(self.folder_path)) != 0
def log(self, command, message):
# log first message only when second one is done
# error on first step does not need recovery
self.logs.append((command, message))
if len(self.logs) == 1:
return
elif len(self.logs) == 2:
first_command, first_message = self.logs[0]
self._log(first_command, first_message)
self._log(command, message)
def log_error(self, error):
if self.most_recent_command is not None:
self._log(self.most_recent_command, error)
def _log(self, command, message):
self.most_recent_command = command
if os.path.exists(self.folder_path) == False:
os.makedirs(self.folder_path)
file_path = os.path.join(self.folder_path, command + ".log")
self.file_paths.append(file_path)
timestamp = datetime.now().strftime("%H:%M:%S")
with open(file_path, "a") as file:
file.write(timestamp + ": " + message + "\n")
def success(self):
for file_path in self.file_paths:
if os.path.exists(file_path):
os.remove(file_path)
os.rmdir(self.folder_path)