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.
This commit is contained in:
Jacob Hinkle 2022-09-21 21:23:20 -04:00
parent f18368f1e0
commit f55d110e49
4 changed files with 62 additions and 13 deletions

View File

@ -5,6 +5,20 @@ from .. import store
import os 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.command()
@click.argument("directory", default='.') @click.argument("directory", default='.')
def record(directory): def record(directory):
@ -16,14 +30,5 @@ def record(directory):
directory is part of an existing store, it will be updated and versions directory is part of an existing store, it will be updated and versions
of any files changes since the last recording will be incremented. of any files changes since the last recording will be incremented.
""" """
if not os.path.isdir(directory): do_record(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()

View File

@ -192,9 +192,12 @@ class FSEntry:
sha256=bytes.fromhex(sha256), sha256=bytes.fromhex(sha256),
) )
def diff(self, other):
return FSDiff(self, other)
class FSDiff: class FSDiff:
def __init__(A, B): def __init__(self, A, B):
"""Given two hashed directories, efficiently compute difference. """Given two hashed directories, efficiently compute difference.
This assumes the hashes are consistent, so that directories with This assumes the hashes are consistent, so that directories with

View File

@ -42,17 +42,33 @@ class Program:
env.id, #environment INTEGER NOT NULL, env.id, #environment INTEGER NOT NULL,
self.message, #message TEXT, -- user-defined message to help distinguish similar runs 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()) self.set_start_time(datetime.datetime.now())
# track this program in the store
return self return self
def __exit__(self, exc_type, exc_value, exc_traceback): 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 # 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 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: class Store:

View File

@ -1,4 +1,29 @@
import os
from pathlib import Path
import pytest 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 @pytest.fixture
def store(): def store():