From 7043773cd80b832c1cd3b2757cfe02add94090e5 Mon Sep 17 00:00:00 2001 From: Andreas Kloeckner Date: Mon, 10 Mar 2008 22:42:22 -0400 Subject: [PATCH] Add mode argument to LogManager to be able to give better error msgs. --- bin/logtool | 2 +- src/batchjob.py | 15 ++++++++++++--- src/log.py | 14 +++++++++++++- 3 files changed, 26 insertions(+), 5 deletions(-) diff --git a/bin/logtool b/bin/logtool index e921696..5c8cd37 100755 --- a/bin/logtool +++ b/bin/logtool @@ -150,7 +150,7 @@ following: # not a known command, interpret as file name from os import access, R_OK if access(cmd, R_OK): - logmgr = LogManager(cmd) + logmgr = LogManager(cmd, "r") else: raise IOError, "file '%s' not found" % cmd diff --git a/src/batchjob.py b/src/batchjob.py index 585f5d6..2b568a9 100644 --- a/src/batchjob.py +++ b/src/batchjob.py @@ -14,13 +14,21 @@ def _cp(src, dest): +def get_timestamp(): + from datetime import datetime + return datetime.now().strftime("%Y-%m-%d-%H%M%S") + + + + class BatchJob(object): - def __init__(self, moniker, main_file, aux_files=[]): - from datetime import datetime + def __init__(self, moniker, main_file, aux_files=[], timestamp=None): import os import os.path - timestamp = datetime.now().strftime("%Y-%m-%d-%H%M") + if timestamp is None: + timestamp = get_timestamp() + self.moniker = ( moniker .replace("-$DATE", "") @@ -30,6 +38,7 @@ class BatchJob(object): self.subdir = os.path.join( os.getcwd(), moniker.replace("$DATE", timestamp)) + os.makedirs(self.subdir) with open("%s/run.sh" % self.subdir, "w") as runscript: diff --git a/src/log.py b/src/log.py index 23182b0..3744971 100644 --- a/src/log.py +++ b/src/log.py @@ -159,14 +159,21 @@ class LogManager(object): data in a saved log. """ - def __init__(self, filename, mpi_comm=None): + def __init__(self, filename, mode, mpi_comm=None): """Initialize this log manager instance. @arg filename: If given, the filename to which this log is bound. If this database exists, the current state is loaded from it. + @arg mode: One of "w", "r" for write, read. "w" assumes that the + database is initially empty. @arg mpi_comm: A C{boost.mpi} communicator. If given, logs are periodically synchronized to the head node, which then writes them out to disk. """ + + assert isinstance(mode, basestring), "mode must be a string" + mode = mode[0] + assert mode in ["w", "r"], "invalid mode" + self.quantity_data = {} self.quantity_table = {} self.gather_descriptors = [] @@ -202,8 +209,13 @@ class LogManager(object): self.db_conn = sqlite3.connect(filename, timeout=30) try: self.db_conn.execute("select * from quantities;") + if mode == "w": + raise RuntimeError, "Log database '%s' already exists" % filename self._load() except sqlite3.OperationalError: + if mode == "r": + raise RuntimeError, "Log database '%s' not found" % filename + # initialize new database self.db_conn.execute(""" create table quantities ( -- GitLab