DUNE-DAQ
DUNE Trigger and Data Acquisition software
Loading...
Searching...
No Matches
consolidate.py
Go to the documentation of this file.
1from pathlib import Path
2import conffwk
3import sys
4import os
5from logging import getLogger
6from typing import Optional, Tuple
7log = getLogger('daqconf.consolidate')
8
9
10def get_all_includes(db, file):
11 includes = db.get_includes(file)
12 for include in includes:
13 if "data.xml" not in include:
14 continue
15 includes += get_all_includes(db, include)
16
17 return list(set(includes))
18
19def consolidate_db(oksfile: str, output_file: str, session_id: Optional[str] = None)->None:
20 """Consolidates a single session
21
22 :param oksfile: OKS file(s) to consolidate
23 :param output_file: File to output consolidated database to
24 :param session_id: Name of session, defaults to None
25 """
26 log.info(f"Consolidating database into output database '{output_file}'. Input database: '{oksfile}'.")
27
28 sys.setrecursionlimit(10000) # for example
29 db, new_db = create_copy_template(oksfile, output_file)
30
31 if session_id is None:
32 log.debug("Consolidating all dals in %s into %s", oksfile, output_file)
33 consolidate_full(db, new_db)
34 else:
35 log.debug("Consolidating all dals in session %s from %s into %s", session_id, oksfile, output_file)
36
37 consolidate_session(db, new_db, session_id)
38
39
40def create_copy_template(oksfile: str, output_file: str)->Tuple[conffwk.Configuration, conffwk.Configuration]:
41 '''
42 Creates a blank oks .data.xml file stored in output_file with all the schema includes of oksfile
43 :param oksfile: OKS file to copy includes from
44 :param output_file: OKS file to copy includes into
45
46 :returns: Tuple of old_db, copied_db
47 '''
48 log.debug("Reading database")
49 db = conffwk.Configuration("oksconflibs:" + oksfile)
50
51 schemafiles = []
52 includes = get_all_includes(db, None)
53 schemafiles += [i for i in includes if "schema.xml" in i]
54 log.debug(f"Included schemas: {schemafiles}")
55
56 log.debug("Creating new database")
57 new_db = conffwk.Configuration("oksconflibs")
58 new_db.create_db(output_file, schemafiles)
59 new_db.commit()
60
61 return db, new_db
62
63
64def consolidate_full(db: conffwk.Configuration, new_db: conffwk.Configuration)->None:
65 """Consolidates ALL dal objects in db into new_db
66
67 :param db: A conffwk.Configuration containing objects you want to copy over
68 :param new_db: A conffwk.Configuration you want to copy objects into
69 """
70 dal_list = list(db.get_all_dals().values())
71 copy_dals_to_cfg(new_db, dal_list)
72
73def consolidate_session(db: conffwk.Configuration, new_db: conffwk.Configuration, session_id: str)->None:
74 """
75 Consolidates all objects related to the session with id 'session_id' into a single file
76
77 :param db: A conffwk.Configuration containing objects you want to copy over
78 :param new_db: A conffwk.Configuration you want to copy objects into
79 :param session_id: Name of session
80 """
81
82 # Check session exists and load
83 try:
84 dal_session = db.get_dal('Session', session_id)
85 except Exception as e:
86 log.exception(e)
87 raise e
88
89 dal_list = get_relationships(db, dal_session, [])
90 copy_dals_to_cfg(new_db, dal_list)
91
92def get_relationships(db: conffwk.Configuration, current_dal, dal_list):
93 '''
94 Recurssively get all objects related to current_dal
95 '''
96 dal_list.append(current_dal)
97
98 for rel in db.relations(current_dal.className(), all=True):
99 rel_obj = getattr(current_dal, rel, None)
100 if rel_obj is None:
101 continue
102
103 if not isinstance(rel_obj, list):
104 rel_obj = [rel_obj]
105
106 for rel_obj in rel_obj:
107 dal_list = get_relationships(db, rel_obj, dal_list)
108
109 return dal_list
110
111
112def copy_dals_to_cfg(new_db: conffwk.Configuration, dal_list)->None:
113 '''
114 Copy a list of dals into a configuration
115 '''
116 log.debug("Copying %d objects to new db", len(dal_list))
117 for dal in dal_list:
118 new_db.add_dal(dal)
119
120 log.debug("Saving database")
121 new_db.commit()
122
123
124def copy_configuration(dest_dir : Path, input_files: list):
125 if len(input_files) == 0:
126 return []
127
128 log.info(f"Copying configuration represented by databases: \'{input_files}\' to \'{dest_dir}\'")
129 dest_dir = dest_dir.resolve() # Always include by absolute path when copying
130 sys.setrecursionlimit(10000) # for example
131
132 output_dbs = []
133
134 for input_file in input_files:
135 db = conffwk.Configuration("oksconflibs:" + input_file)
136 includes = db.get_includes(None)
137 schemas = [i for i in includes if "schema.xml" in i]
138 dbs = [i for i in includes if "data.xml" in i]
139 newdbs = copy_configuration(dest_dir, dbs)
140
141 output_file = dest_dir / os.path.basename(input_file)
142
143 new_db = conffwk.Configuration("oksconflibs")
144 new_db.create_db(str(output_file), schemas + newdbs)
145 new_db.commit()
146
147 dals = db.get_all_dals()
148
149 for dal in dals:
150 db.get_dal(dals[dal].className(), dals[dal].id)
151 new_db.add_dal(dals[dal])
152
153 new_db.commit()
154 output_dbs.append(str(output_file))
155 log.debug("DONE")
156
157 return output_dbs
158
159
160def consolidate_files(oksfile, *input_files):
161 includes = []
162 dbs = []
163 str_in_files = '\n'.join(input_files)
164 log.info(f"Consolidating {len(input_files)} databases into output database \'{oksfile}\'. Input databases: {str_in_files}")
165 sys.setrecursionlimit(10000) # for example
166
167 for input_file in input_files:
168 dbs.append(conffwk.Configuration("oksconflibs:" + input_file))
169 includes += get_all_includes(dbs[len(dbs) - 1], None)
170
171 includes = list(set(includes))
172 includes = [i for i in includes if i not in input_files]
173 log.debug(f"Included files: {includes}")
174
175 new_db = conffwk.Configuration("oksconflibs")
176 new_db.create_db(oksfile, includes)
177
178 new_db.commit()
179
180 for db in dbs:
181 log.debug(f"Reading dal objects from old db {db}")
182 dals = db.get_all_dals()
183
184 log.debug(f"Copying objects to new db {new_db}")
185 for dal in dals:
186
187 try:
188 new_db.get_dal(dals[dal].className(), dals[dal].id)
189 except:
190 new_db.add_dal(dals[dal])
191 new_db.commit()
192
193 log.debug(f"Saving database {new_db}")
194 new_db.commit()
None copy_dals_to_cfg(conffwk.Configuration new_db, dal_list)
Tuple[conffwk.Configuration, conffwk.Configuration] create_copy_template(str oksfile, str output_file)
get_relationships(conffwk.Configuration db, current_dal, dal_list)
get_all_includes(db, file)
None consolidate_full(conffwk.Configuration db, conffwk.Configuration new_db)
None consolidate_session(conffwk.Configuration db, conffwk.Configuration new_db, str session_id)
copy_configuration(Path dest_dir, list input_files)
None consolidate_db(str oksfile, str output_file, Optional[str] session_id=None)
consolidate_files(oksfile, *input_files)