DUNE-DAQ
DUNE Trigger and Data Acquisition software
Loading...
Searching...
No Matches
oks_format.py
Go to the documentation of this file.
1import conffwk
2import oks
3import shutil
4import tempfile
5import difflib
6import os
7from typing import Tuple
8
9
10def filtered_diff(file1: str, file2: str) -> Tuple[bool, str]:
11 """
12 Execute a diff between two files with optional regex-based filtering.
13
14 Args:
15 file1: Path to the first file
16 file2: Path to the second file
17 filter_patterns: List of regex patterns to filter out from diff output.
18 Lines matching any pattern will be excluded from the diff.
19
20 Returns:
21 Tuple of (has_diff, diff_output) where:
22 has_diff: True if there are differences after filtering, False otherwise
23 diff_output: String containing the unified diff output
24 """
25 # Read the files
26 with open(file1, 'r', encoding='utf-8') as f:
27 file1_lines = f.readlines()
28
29 with open(file2, 'r', encoding='utf-8') as f:
30 file2_lines = f.readlines()
31
32 # Generate unified diff
33 diff_lines = list(difflib.unified_diff(
34 file1_lines,
35 file2_lines,
36 fromfile=file1,
37 tofile=file2,
38 lineterm=''
39 ))
40
41 if not diff_lines:
42 return False, ""
43
44 # Filter diff lines
45 filtered_lines = []
46 has_meaningful_diff = False
47
48 for line in diff_lines:
49 filtered_lines.append(line)
50
51 if line.startswith('---') or line.startswith('+++') or line.startswith('@@'):
52 continue
53
54 if line.startswith('+<info') or line.startswith('-<info'):
55 continue
56
57 # Mark that we have meaningful differences (not just context or headers)
58 if line.startswith('+') or line.startswith('-'):
59 has_meaningful_diff = True
60
61 diff_output = '\n'.join(filtered_lines).replace("\n\n", "\n")
62 return has_meaningful_diff, diff_output
63
64def oks_format(input_file: str, fix: bool = False) -> None:
65 tmpfile = tempfile.NamedTemporaryFile(delete=False)
66 shutil.copy2(input_file, tmpfile.name)
67
68 diff=False
69 err=False
70
71 try:
72 if ".data.xml" in input_file:
73 print(f"Formatting database file {input_file}")
74 dal = conffwk.dal.module("generated", "schema/confmodel/dunedaq.schema.xml")
75 conffwk.Configuration(f"oksconflibs:{input_file}")
76 oks_kernel = conffwk.Configuration(f"oksconflibs:{tmpfile.name}")
77
78 testobj = dal.Service("Reformat-test-obj")
79 oks_kernel.update_dal(testobj)
80 oks_kernel.destroy_dal(testobj)
81
82 oks_kernel.commit()
83 elif ".schema.xml" in input_file:
84 print(f"Formatting schema file {input_file}")
85
86 oks_kernel = oks.OksKernel(silence_mode=True)
87 schema = oks_kernel.load_schema(str(input_file))
88 #oks_kernel.save_all_schema()
89 oks_kernel.save_as_schema(str(tmpfile.name), schema)
90
91 else:
92 print(f"Don't know how to handle file {input_file}")
93 err = True
94 except Exception as e:
95 print(f"Error occurred while formatting {input_file}: {e}")
96 err = True
97
98 diff, diff_output = filtered_diff(input_file, tmpfile.name)
99
100 if diff:
101 print(f"File {input_file} has differences after formatting:")
102 print(diff_output)
103
104 if err:
105 print(f"Leaving temporary file {tmpfile.name} for inspection due to errors. (e.g. check oks_dump -f {tmpfile.name})")
106 return 2
107
108 if diff and fix:
109 shutil.copy2(tmpfile.name, input_file)
110 os.remove(tmpfile.name)
111 return 0
112
113 os.remove(tmpfile.name)
114 if diff:
115 return 1
116 return 0
module(name, schema, other_dals=[], backend='oksconflibs', db=None)
Definition dal.py:695
Tuple[bool, str] filtered_diff(str file1, str file2)
Definition oks_format.py:10