From 2d5816a5045e4eef126cfec55b868ec5c66b401e Mon Sep 17 00:00:00 2001 From: Jacob Hinkle Date: Tue, 27 Sep 2022 15:57:48 -0400 Subject: [PATCH] Fix diff, at least for simple once-recorded test case --- src/nancy/cli/diff.py | 2 +- src/nancy/cli/record.py | 2 +- src/nancy/fs.py | 3 +-- src/nancy/store.py | 59 +++++++++++++++++++---------------------- 4 files changed, 30 insertions(+), 36 deletions(-) diff --git a/src/nancy/cli/diff.py b/src/nancy/cli/diff.py index 8553574..2d9e43e 100644 --- a/src/nancy/cli/diff.py +++ b/src/nancy/cli/diff.py @@ -65,7 +65,7 @@ def print_diff(ABdiff: fs.FSDiff, indent=2, indent_level=0, use_color=True, _print_row('NEW', d.B, l) elif d.B is None: _print_row('DEL', d.A, l) - else: + elif d.A.sha256 != d.B.sha256: _print_row('MOD', d.B, l) diff --git a/src/nancy/cli/record.py b/src/nancy/cli/record.py index 1e21779..1488b50 100644 --- a/src/nancy/cli/record.py +++ b/src/nancy/cli/record.py @@ -34,7 +34,7 @@ def record(message, store_path=None, show_diff=True, show_hashes=False, use_colo if show_diff: print_diff(fsdiff, show_hashes=show_hashes, use_color=use_color) - logger.info('Recording with message:', message) + logger.info('Recording with message: {}', message) if skip_confirm or confirm("Record the values above into the database?"): s.record(fsdiff, message=message) diff --git a/src/nancy/fs.py b/src/nancy/fs.py index 88fd8a1..014c34c 100644 --- a/src/nancy/fs.py +++ b/src/nancy/fs.py @@ -262,7 +262,6 @@ class FSEntry: """Given id of an entry in filedir, recursively fill this object""" if root_row is None: assert root_id is not None - logger.debug('root_id({})={}', type(root_id), root_id) cursor.execute( 'SELECT id, name, frozen FROM filedir WHERE id=?', (root_id,), @@ -391,7 +390,7 @@ class FSDiff: ]) if B is None: # deleted entry return cls(A, B, [ - cls.compute(c, N) \ + cls.compute(c, None) \ for c in sorted(A.children, key=lambda e: e.filename) ]) diff --git a/src/nancy/store.py b/src/nancy/store.py index 90706d9..2e8cc76 100644 --- a/src/nancy/store.py +++ b/src/nancy/store.py @@ -2,15 +2,12 @@ from loguru import logger -from . import db, environment, fs, machine +from . import db, environment, fs import datetime -import importlib -import json import os from pathlib import Path import sqlite3 -from typing import Callable class Program: @@ -33,17 +30,19 @@ class Program: env = environment.Environment.find_or_insert(cur) cur.execute('INSERT INTO program VALUES (?, ?, ?, ?, ?, ?, ?)', ( - None, #id INTEGER PRIMARY KEY NOT NULL, - self.name, #name TEXT, -- name of the program, usually written lowercase by calling code e.g. cnn_crossval + None, # id INTEGER PRIMARY KEY NOT NULL, + self.name, # name TEXT, + # name of the program, usually written lowercase by calling + # code e.g. cnn_crossval - #-- we use POSIX timestamps for time recording. - #-- e.g. datetime.datetime.now().timestamp() - None, #start_time REAL, - None, #end_time REAL, + # -- we use POSIX timestamps for time recording. + # -- e.g. datetime.datetime.now().timestamp() + None, # start_time REAL, + None, # end_time REAL, - os.getpid(), #process_id INTEGER, -- host PID of python process on host OS - env.id, #environment INTEGER NOT NULL, - self.message, #message TEXT, -- user-defined message to help distinguish similar runs + os.getpid(), # process_id INTEGER, -- host PID of python process on host OS + env.id, # environment INTEGER NOT NULL, + self.message, # message TEXT, -- user-defined message to help distinguish similar runs )) self.id = cur.lastrowid @@ -150,8 +149,11 @@ class Store: """Get the database id for the table entry in this store having name '.'""" if cur is None: cur = self.conn.cursor() + cur.execute('SELECT * FROM filedir') + allfiledir = cur.fetchall() cur.execute('SELECT id FROM filedir WHERE store=1 AND parent is NULL') - return cur.fetchone() + root_id, = cur.fetchone() + return root_id def path_to_fsentry(self, path): """Find a path in the filedir database and return it as an fsentry. @@ -185,18 +187,12 @@ class Store: return fd_id return fs.FSEntry.from_db_index(cur, root_id=fd_id) - def recorded_status(self, filepath): - recorded = self.path_to_fsentry(filepath) - def fs_entries(self, shallow=False): """Return recursive structure containing FSEntry objects from db""" - root_id = self.filedir_root_index() - logger.debug('root_id={}', root_id) + root_id = self.filedir_root_index() if root_id is None: - logger.trace("Empty root") - return fs.FSEntry.empty_root() + return None else: - logger.trace("Non-empty root", root_id) return fs.FSEntry.from_db_index(self.conn.cursor(), root_id=root_id) def program(self, name, message=None): @@ -204,20 +200,14 @@ class Store: def diff(self): """ - Find changes to files and directories compared to their recorded versions + Find changes to files and dirs compared to their recorded versions """ - logger.trace("DIFF") # get info about files currently at the given locations current = fs.FSEntry.from_path(self.path) - logger.debug("CURRENT: \n{}", str(current)) - # then find a listing covering all the expected paths - #recorded = self.recorded_status(self.path) recorded = self.fs_entries(shallow=True) - logger.debug("RECORDED: \n{}", str(recorded)) - return fs.FSDiff.compute(recorded, current) @@ -236,6 +226,7 @@ class Store: source_task, ) ) + return cur.lastrowid def _record_new_file_recursive(self, ob, cur, parent_id, source_task): # Find entries with this name and parent @@ -269,12 +260,16 @@ class Store: def _record_recursive(self, diff, cur, parent_id=None, source_task=None): """Record this level of a diff.""" if diff.A is None: - self._record_new_file_recursive(diff.B, cur, parent_id, source_task=None) + self._record_new_file_recursive(diff.B, cur, parent_id, + source_task=source_task) elif diff.B is None: self._record_deleted_file_recursive(diff.B, cur, parent_id) else: - # modified - pass + # possibly modified, record new version then recurse into children + self._record_new_file_recursive(diff.B, cur, parent_id, + source_task=source_task) + self._record_file_version(cur, diff.B, diff.A.id, + source_task=source_task) # descend into children