From f55d110e49b9dbfa01cfda54a89e8dc40317816d Mon Sep 17 00:00:00 2001 From: Jacob Hinkle Date: Wed, 21 Sep 2022 21:23:20 -0400 Subject: [PATCH] Programs are now recorded in the store. The record() program and cli is not yet working. For that we need a working `FSDiff` next. Then we'll finally be ready to start recording file information. --- src/nancy/cli/record.py | 25 +++++++++++++++---------- src/nancy/fs.py | 5 ++++- src/nancy/store.py | 20 ++++++++++++++++++-- tests/test_store.py | 25 +++++++++++++++++++++++++ 4 files changed, 62 insertions(+), 13 deletions(-) diff --git a/src/nancy/cli/record.py b/src/nancy/cli/record.py index 6b82d33..30237b7 100644 --- a/src/nancy/cli/record.py +++ b/src/nancy/cli/record.py @@ -5,6 +5,20 @@ from .. import store import os +def do_record(directory): + """Unwrapped record command""" + if not os.path.isdir(directory): + raise ValueError(f"Cannot record non-existent directory {directory}") + + existing_store = store.find_store(directory) + if existing_store is None: # this is a new store + s = store.Store.init(directory) + else: # this is an existing store + s = store.Store(directory) + + s.record() + + @click.command() @click.argument("directory", default='.') def record(directory): @@ -16,14 +30,5 @@ def record(directory): directory is part of an existing store, it will be updated and versions of any files changes since the last recording will be incremented. """ - if not os.path.isdir(directory): - raise ValueError(f"Cannot record non-existent directory {directory}") - - existing_store = store.find_store(directory) - if existing_store is None: # this is a new store - s = store.Store.init(directory) - else: # this is an existing store - s = store.Store(directory) - - s.record() + do_record(directory) diff --git a/src/nancy/fs.py b/src/nancy/fs.py index b4b03ea..a3996d6 100644 --- a/src/nancy/fs.py +++ b/src/nancy/fs.py @@ -192,9 +192,12 @@ class FSEntry: sha256=bytes.fromhex(sha256), ) + def diff(self, other): + return FSDiff(self, other) + class FSDiff: - def __init__(A, B): + def __init__(self, A, B): """Given two hashed directories, efficiently compute difference. This assumes the hashes are consistent, so that directories with diff --git a/src/nancy/store.py b/src/nancy/store.py index 2b50c08..901a18e 100644 --- a/src/nancy/store.py +++ b/src/nancy/store.py @@ -42,17 +42,33 @@ class Program: env.id, #environment INTEGER NOT NULL, self.message, #message TEXT, -- user-defined message to help distinguish similar runs )) + self.id = cur.lastrowid + cur.connection.commit() self.set_start_time(datetime.datetime.now()) - # track this program in the store return self def __exit__(self, exc_type, exc_value, exc_traceback): - elapsed = datetime.datetime.now() - self.start_time + end_time = datetime.datetime.now() # record start and end times in store + cur = self.store.conn.cursor() + cur.execute(''' + UPDATE + program + SET + start_time = ?, + end_time = ? + WHERE + id = ? + ''', + (self.start_time, end_time, self.id), + ) + cur.connection.commit() self._evaluated = True # prevent re-running + elapsed = end_time - self.start_time + print(f"Program [{self.id}] {self.name} (message:{self.message}) ran in {elapsed} seconds.") class Store: diff --git a/tests/test_store.py b/tests/test_store.py index e25c1ee..80dbd76 100644 --- a/tests/test_store.py +++ b/tests/test_store.py @@ -1,4 +1,29 @@ +import os +from pathlib import Path import pytest +import sys +import tempfile + +@pytest.fixture +def bare_dir(): + """Create an emptry temp directory""" + with tempfile.TemporaryDirectory(prefix="nancy_testdir") as d: + yield Path(d) + +@pytest.fixture +def filled_dir(bare_dir): + open(bare_dir / 'a.txt', 'w').write("foo") + os.makedirs(bare_dir / 'stats') + open(bare_dir / 'stats' / 'metrics.csv', 'w').write("bar,baz") + # identical to ./a.txt + open(bare_dir / 'stats' / 'a.txt', 'w').write("foo") + return bare_dir + + +def test_record_untracked_dir(filled_dir): + from nancy.cli.record import do_record + do_record(filled_dir) + @pytest.fixture def store():