1- import os
21import json
32import clang .cindex as clang
3+ from pathlib import Path
44
55
66class CompilationDatabase :
7- """
8- Build a compilation database from a given directory
9- """
7+ """Class to get information from a CMake compilation database."""
108
119 def __init__ (self , build_dir ):
1210 self .compilation_database = clang .CompilationDatabase .fromDirectory (
1311 buildDir = build_dir
1412 )
1513
1614 def get_compilation_arguments (self , filename = None ):
17- """
18- Returns the compilation commands extracted from the compilation database
19-
20- Parameters:
21- - filename (optional): To get compilaton commands of a file
15+ """Returns the compilation commands extracted from the compilation database
2216
23- Returns:
24- - compilation_arguments (dict): {filename: compiler arguments}
17+ :param filename: Get compilation arguments of the file, defaults to None: get for all files
18+ :type filename: str, optional
19+ :return: ilenames and their compiler arguments: {filename: compiler arguments}
20+ :rtype: dict
2521 """
2622
2723 if filename :
@@ -40,25 +36,42 @@ def get_compilation_arguments(self, filename=None):
4036
4137
4238class Codemodel :
39+ """Class to get information about the Codemodel generated by the CMake file API."""
40+
4341 def __init__ (self , reply_dir ):
4442 self .reply_dir = reply_dir
4543 self .codemodel = {}
4644 self .codemodel_targets = {}
4745
4846 def set_codemodel (self , codemodel_file = None ):
47+ """Set the codemodel variable.
48+
49+ :param codemodel_file: codemodel file to read from, defaults to None
50+ :type codemodel_file: str, optional
51+ """
4952 if not codemodel_file :
50- for file in os . listdir (self .reply_dir ):
51- if file .startswith ("codemodel" ):
52- codemodel_file = os . path . join (self .reply_dir , file )
53+ for file in Path (self .reply_dir ). iterdir ( ):
54+ if file .name . startswith ("codemodel" ):
55+ codemodel_file = Path (self .reply_dir , file )
5356 with open (codemodel_file ) as f :
5457 self .codemodel = json .load (f )
5558
5659 def get_codemodel (self ):
60+ """Get the codemodel.
61+
62+ :return: JSON codemodel object.
63+ :rtype: dict
64+ """
5765 if not self .codemodel :
5866 self .set_codemodel ()
5967 return self .codemodel
6068
6169 def set_codemodel_targets (self , codemodel = None ):
70+ """Set targets variable.
71+
72+ :param codemodel: Codemodel dict object, defaults to None
73+ :type codemodel: dict, optional
74+ """
6275 if not codemodel :
6376 codemodel = self .get_codemodel ()
6477 configurations = codemodel .get ("configurations" , [])
@@ -68,37 +81,69 @@ def set_codemodel_targets(self, codemodel=None):
6881 self .codemodel_targets .update ({target ["name" ]: target ["jsonFile" ]})
6982
7083 def get_codemodel_targets (self ):
84+ """Get codemodel targets.
85+
86+ :return: dict of targets and the corresponding file
87+ :rtype: dict
88+ """
7189 if not self .codemodel_targets :
7290 self .set_codemodel_targets ()
7391 return self .codemodel_targets
7492
7593
7694class Target :
95+ """Class to get information about targets found from the CMake file API."""
96+
7797 def __init__ (self , reply_dir , target_name ):
7898 self .reply_dir = reply_dir
7999 self .target_file = Codemodel (reply_dir ).get_codemodel_targets ().get (target_name )
80100 self .target = {}
81101
82102 def set_target (self , target_file = None ):
103+ """Set target variable.
104+
105+ :param target_file: File to get target from, defaults to None
106+ :type target_file: str, optional
107+ """
83108 if not target_file :
84109 target_file = self .target_file
85- with open (os . path . join (self .reply_dir , target_file )) as f :
110+ with open (Path (self .reply_dir , target_file )) as f :
86111 self .target = json .load (f )
87112
88113 def get_target (self ):
114+ """Get target.
115+
116+ :return: JSON target
117+ :rtype: dict
118+ """
89119 if not self .target :
90120 self .set_target ()
91121 return self .target
92122
93123 def get_artifacts (self ):
124+ """Get artifacts from the target.
125+
126+ :return: Artifacts' paths
127+ :rtype: list
128+ """
94129 return [
95130 artifact .get ("path" ) for artifact in self .get_target ().get ("artifacts" , [])
96131 ]
97132
98133 def get_commands (self ):
134+ """Get commands from the target.
135+
136+ :return: Commands concerning the build system.
137+ :rtype: list
138+ """
99139 return self .get_target ().get ("backtraceGraph" , {}).get ("commands" )
100140
101141 def get_compile_groups (self ):
142+ """Get compile groups from the target.
143+
144+ :return: list of dictionaries of compile groups
145+ :rtype: list
146+ """
102147 return [
103148 {
104149 "fragments" : [
@@ -119,21 +164,46 @@ def get_compile_groups(self):
119164 ]
120165
121166 def get_dependencies (self ):
167+ """Get dependencies from the target.
168+
169+ :return: Dependencies ids' list
170+ :rtype: list
171+ """
122172 return [
123173 dependency .get ("id" )
124174 for dependency in self .get_target ().get ("dependencies" , [])
125175 ]
126176
127177 def get_files (self ):
178+ """Get files from the target.
179+
180+ :return: Files concerning the build system.
181+ :rtype: list
182+ """
128183 return self .get_target ().get ("backtraceGraph" , {}).get ("files" )
129184
130185 def get_folder (self ):
186+ """Get folder from the target.
187+
188+ :return: Folder of the target.
189+ :rtype: str
190+ """
131191 return self .get_target ().get ("folder" , {}).get ("name" )
132192
133193 def get_id (self ):
194+ """Get id from the target.
195+
196+ :return: ID of the target
197+ :rtype: str
198+ """
134199 return self .get_target ().get ("id" )
135200
136201 def get_install (self ):
202+ """Get install info from the target.
203+
204+ :return: Install info
205+ :rtype: dict
206+ """
137207 install = self .get_target ().get ("install" , {})
138208 return {
139209 "destinations" : [
@@ -144,6 +214,11 @@ def get_install(self):
144214 }
145215
146216 def get_link (self ):
217+ """Get link info from the target.
218+
219+ :return: Link info
220+ :rtype: dict
221+ """
147222 link = self .get_target ().get ("link" , {})
148223 command_fragments = link .get ("commandFragments" , [])
149224 return {
@@ -161,30 +236,64 @@ def get_link(self):
161236 }
162237
163238 def get_name (self ):
239+ """Get name from the target.
240+
241+ :return: Name of the target
242+ :rtype: str
243+ """
164244 return self .get_target ().get ("name" )
165245
166246 def get_name_on_disk (self ):
247+ """Get name on disk from the target.
248+
249+ :return: Name on disk of the target
250+ :rtype: str
251+ """
167252 return self .get_target ().get ("nameOnDisk" )
168253
169254 def get_paths (self ):
255+ """Get paths from the target.
256+
257+ :return: Paths of the target.
258+ :rtype: dict
259+ """
170260 return self .get_target ().get ("paths" )
171261
172262 def get_sources (self ):
263+ """Get sources from the target.
264+
265+ :return: Sources of the target.
266+ :rtype: list
267+ """
173268 return [sources .get ("path" ) for sources in self .get_target ().get ("sources" , [])]
174269
175270 def get_type (self ):
271+ """Get type from the target.
272+
273+ :return: Type of the target.
274+ :rtype: str
275+ """
176276 return self .get_target ().get ("type" )
177277
178278
179279class CMakeFileAPI :
280+ """CMake File API front end."""
281+
180282 def __init__ (self , build_dir ):
181- reply_dir = os . path . join (build_dir , ".cmake" , "api" , "v1" , "reply" )
283+ reply_dir = Path (build_dir , ".cmake" , "api" , "v1" , "reply" )
182284 self .targets = {}
183285 for codemodel_target in Codemodel (reply_dir ).get_codemodel_targets ():
184286 target = Target (reply_dir , codemodel_target )
185287 self .targets [target .get_name ()] = target
186288
187289 def get_dependencies (self , target = None ):
290+ """Get dependencies of the target(s).
291+
292+ :param target: Target to get the dependencies, defaults to None
293+ :type target: str, optional
294+ :return: Dependencies of the target(s).
295+ :rtype: dict
296+ """
188297 targets = [self .targets .get (target )] if target else self .targets .values ()
189298 return {
190299 target .get_name (): list (
@@ -194,6 +303,12 @@ def get_dependencies(self, target=None):
194303 }
195304
196305 def get_sources (self , target = None ):
306+ """Get sources of the target(s).
307+
308+ :param target: Target to get the dependencies, defaults to None
309+ :type target: str, optional
310+ :return: Sources of the target(s).
311+ :rtype: dict
312+ """
197313 targets = [self .targets .get (target )] if target else self .targets .values ()
198314 return {target .get_name (): target .get_sources () for target in targets }
199-
0 commit comments