Fix diff, at least for simple once-recorded test case

This commit is contained in:
Jacob Hinkle 2022-09-27 15:57:48 -04:00
parent 45c2046fd6
commit 2d5816a504
4 changed files with 30 additions and 36 deletions

View File

@ -65,7 +65,7 @@ def print_diff(ABdiff: fs.FSDiff, indent=2, indent_level=0, use_color=True,
_print_row('NEW', d.B, l) _print_row('NEW', d.B, l)
elif d.B is None: elif d.B is None:
_print_row('DEL', d.A, l) _print_row('DEL', d.A, l)
else: elif d.A.sha256 != d.B.sha256:
_print_row('MOD', d.B, l) _print_row('MOD', d.B, l)

View File

@ -34,7 +34,7 @@ def record(message, store_path=None, show_diff=True, show_hashes=False, use_colo
if show_diff: if show_diff:
print_diff(fsdiff, show_hashes=show_hashes, use_color=use_color) 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?"): if skip_confirm or confirm("Record the values above into the database?"):
s.record(fsdiff, message=message) s.record(fsdiff, message=message)

View File

@ -262,7 +262,6 @@ class FSEntry:
"""Given id of an entry in filedir, recursively fill this object""" """Given id of an entry in filedir, recursively fill this object"""
if root_row is None: if root_row is None:
assert root_id is not None assert root_id is not None
logger.debug('root_id({})={}', type(root_id), root_id)
cursor.execute( cursor.execute(
'SELECT id, name, frozen FROM filedir WHERE id=?', 'SELECT id, name, frozen FROM filedir WHERE id=?',
(root_id,), (root_id,),
@ -391,7 +390,7 @@ class FSDiff:
]) ])
if B is None: # deleted entry if B is None: # deleted entry
return cls(A, B, [ return cls(A, B, [
cls.compute(c, N) \ cls.compute(c, None) \
for c in sorted(A.children, key=lambda e: e.filename) for c in sorted(A.children, key=lambda e: e.filename)
]) ])

View File

@ -2,15 +2,12 @@
from loguru import logger from loguru import logger
from . import db, environment, fs, machine from . import db, environment, fs
import datetime import datetime
import importlib
import json
import os import os
from pathlib import Path from pathlib import Path
import sqlite3 import sqlite3
from typing import Callable
class Program: class Program:
@ -33,17 +30,19 @@ class Program:
env = environment.Environment.find_or_insert(cur) env = environment.Environment.find_or_insert(cur)
cur.execute('INSERT INTO program VALUES (?, ?, ?, ?, ?, ?, ?)', ( cur.execute('INSERT INTO program VALUES (?, ?, ?, ?, ?, ?, ?)', (
None, #id INTEGER PRIMARY KEY NOT NULL, 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 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. # -- we use POSIX timestamps for time recording.
#-- e.g. datetime.datetime.now().timestamp() # -- e.g. datetime.datetime.now().timestamp()
None, #start_time REAL, None, # start_time REAL,
None, #end_time REAL, None, # end_time REAL,
os.getpid(), #process_id INTEGER, -- host PID of python process on host OS os.getpid(), # process_id INTEGER, -- host PID of python process on host OS
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 self.id = cur.lastrowid
@ -150,8 +149,11 @@ class Store:
"""Get the database id for the table entry in this store having name '.'""" """Get the database id for the table entry in this store having name '.'"""
if cur is None: if cur is None:
cur = self.conn.cursor() 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') 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): def path_to_fsentry(self, path):
"""Find a path in the filedir database and return it as an fsentry. """Find a path in the filedir database and return it as an fsentry.
@ -185,18 +187,12 @@ class Store:
return fd_id return fd_id
return fs.FSEntry.from_db_index(cur, root_id=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): def fs_entries(self, shallow=False):
"""Return recursive structure containing FSEntry objects from db""" """Return recursive structure containing FSEntry objects from db"""
root_id = self.filedir_root_index() root_id = self.filedir_root_index()
logger.debug('root_id={}', root_id)
if root_id is None: if root_id is None:
logger.trace("Empty root") return None
return fs.FSEntry.empty_root()
else: else:
logger.trace("Non-empty root", root_id)
return fs.FSEntry.from_db_index(self.conn.cursor(), root_id=root_id) return fs.FSEntry.from_db_index(self.conn.cursor(), root_id=root_id)
def program(self, name, message=None): def program(self, name, message=None):
@ -204,20 +200,14 @@ class Store:
def diff(self): 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 # get info about files currently at the given locations
current = fs.FSEntry.from_path(self.path) current = fs.FSEntry.from_path(self.path)
logger.debug("CURRENT: \n{}", str(current))
# then find a listing covering all the expected paths # then find a listing covering all the expected paths
#recorded = self.recorded_status(self.path)
recorded = self.fs_entries(shallow=True) recorded = self.fs_entries(shallow=True)
logger.debug("RECORDED: \n{}", str(recorded))
return fs.FSDiff.compute(recorded, current) return fs.FSDiff.compute(recorded, current)
@ -236,6 +226,7 @@ class Store:
source_task, source_task,
) )
) )
return cur.lastrowid
def _record_new_file_recursive(self, ob, cur, parent_id, source_task): def _record_new_file_recursive(self, ob, cur, parent_id, source_task):
# Find entries with this name and parent # 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): def _record_recursive(self, diff, cur, parent_id=None, source_task=None):
"""Record this level of a diff.""" """Record this level of a diff."""
if diff.A is None: 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: elif diff.B is None:
self._record_deleted_file_recursive(diff.B, cur, parent_id) self._record_deleted_file_recursive(diff.B, cur, parent_id)
else: else:
# modified # possibly modified, record new version then recurse into children
pass 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 # descend into children