Skip to content

Commit

Permalink
improve dbcompare and add a test
Browse files Browse the repository at this point in the history
  • Loading branch information
gilesknap committed Dec 17, 2024
1 parent 7fd1b50 commit 1218834
Show file tree
Hide file tree
Showing 7 changed files with 217,165 additions and 16 deletions.
11 changes: 9 additions & 2 deletions src/builder2ibek/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,19 @@ def autosave(


@cli.command()
def db_compare(original: Path, new: Path):
def db_compare(
original: Path,
new: Path,
ignore: list[str] = typer.Option(
[], help="List of record name sub strings to ignore"
),
output: Optional[Path] = typer.Option(..., help="Output file"),
):
"""
Compare two DB files and output the differences
"""

compare_dbs(original, new)
compare_dbs(original, new, ignore, output)


if __name__ == "__main__":
Expand Down
11 changes: 9 additions & 2 deletions src/builder2ibek/converters/autosave.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,12 @@ def handler(entity: Entity, entity_type: str, ioc: Generic_IOC):
XML to YAML specialist convertor function for the pvlogging support module
"""

# TODO not supporting this yet - just remove it
entity.delete_me()
if entity_type == "Autosave":
entity.rename("iocName", "P")
entity.P += ":"
# TODO if this is a motor then set entity.postions_req_period = 5
entity.remove("bl")
entity.remove("ip")
entity.remove("name")
entity.remove("path")
entity.remove("server")
45 changes: 33 additions & 12 deletions src/builder2ibek/dbcompare.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
regex_record = re.compile(r'record *\( *([^,]*), *"?([^"]*)"? *\)[\s\S]{')


def compare_dbs(original: Path, new: Path):
def compare_dbs(
original: Path, new: Path, ignore: list[str], output: Path | None = None
):
"""
validate that two DBs have the same set of records
Expand All @@ -23,14 +25,33 @@ def compare_dbs(original: Path, new: Path):

old_only = sorted(old_set - new_set)
new_only = sorted(new_set - old_set)
print("\n*****************************************************************")
print("Records in original but not in new:")
print("\n".join(old_only))
print("\n*****************************************************************")
print("Records in new but not in original:")
print("\n".join(new_only))
print("\n*****************************************************************")
print(" records in original: ", len(old_set))
print(" records in new: ", len(new_set))
print(" records missing in new: ", len(old_only))
print(" records extra in new: ", len(new_only))

old_only_filtered = old_only.copy()
new_only_filtered = new_only.copy()
for record in old_only:
for s in ignore:
if s in record:
old_only_filtered.remove(record)
for record in new_only:
for s in ignore:
if s in record:
new_only_filtered.remove(record)

result = (
"Records in original but not in new:\n"
+ "*******************************************************************\n"
+ "\n".join(old_only_filtered)
+ "\n\n"
+ "Records in new but not in original:\n"
+ "*******************************************************************\n"
+ "\n".join(new_only_filtered)
+ "\n\n"
+ f"\records in original: {len(old_set)}\n"
f" records in new: {len(new_set)}\n"
f" records missing in new: {len(old_only_filtered)}\n"
f" records extra in new: {len(new_only_filtered)}\n"
)
if not output:
print(result)
else:
output.write_text(result)
Loading

0 comments on commit 1218834

Please sign in to comment.