3 # Copyright (C) 2007-2022 CEA/DEN, EDF R&D, OPEN CASCADE
5 # Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
6 # CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
8 # This library is free software; you can redistribute it and/or
9 # modify it under the terms of the GNU Lesser General Public
10 # License as published by the Free Software Foundation; either
11 # version 2.1 of the License, or (at your option) any later version.
13 # This library is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 # Lesser General Public License for more details.
18 # You should have received a copy of the GNU Lesser General Public
19 # License along with this library; if not, write to the Free Software
20 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 # See https://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
25 # File : extension_query.py
26 # Author : Konstantin Leontev, Open Cascade
28 # @package SalomeOnDemandTK
29 # @brief An utility package that will allow you to know the size of an extension
30 # and generate a dependency tree.
33 An utility package that will allow you to know the size of an extension
34 and generate a dependency tree.
39 from traceback import format_exc
41 from .extension_utilities import logger, \
42 SALOME_EXTDIR, DFILE_EXT, EXTDEPENDSON_KEY, EXTDESCR_KEY, EXTAUTHOR_KEY, EXTCOMPONENT_KEY, \
43 isvalid_dirname, find_salomexc, list_files_ext, read_salomexd
46 def size_to_str(size, format_in_bytes=False):
48 Returns a string describing a size.
51 size - the size to represent as a string.
52 format_in_bytes - if True, size is expressed in bytes,
53 otherwise human readable units are used.
56 The size as a string with units.
62 __units__ = ["", "Ki", "Mi", "Gi", "Ti"]
66 prefix_index_max = len(__units__) - 1
67 while (size > kilo and prefix_index < prefix_index_max):
71 unit = __units__[prefix_index]
72 return '%.2f %sB' % (size, unit)
75 def dir_size(directory):
77 Calculate a total size of the given directory.
80 directory - the given directory
83 Size of the directory.
86 logger.debug('Get the size of %s', directory)
89 for root, _, files in os.walk(directory):
91 itempath = os.path.join(root, file)
92 if os.path.islink(itempath):
95 total_size += os.path.getsize(itempath)
97 logger.debug('The size of %s: %s', directory, total_size)
101 def dir_size_str(directory, format_in_bytes=False):
103 Calculate a total size of the given directory and format as a string.
106 directory - the given directory
107 format_in_bytes - if True, size is expressed in bytes,
108 otherwise human readable units are used.
111 Size of the directory as a formatted string.
114 size = dir_size(directory)
115 size_str = size_to_str(size, format_in_bytes)
117 logger.debug('The size of %s: %s', directory, size_str)
121 def size_bylist(root_dir, salomexc):
123 Calcualate the total size of files listed in the given salomexc file.
126 root_dir - a root dir for listed files
127 salomexc - file that contents a list of files.
130 The total size of listed files.
133 logger.debug('Calcualate the total size of files inside %s listed in %s...',
137 with open(salomexc, 'r', encoding='UTF-8') as file:
140 path_to_file = os.path.join(root_dir, line.strip())
141 #logger.debug('Check the file %s...', path_to_file)
143 if os.path.isfile(path_to_file):
144 size = os.path.getsize(path_to_file)
146 logger.debug('%s size: %s', path_to_file, size)
148 elif os.path.islink(path_to_file):
149 logger.debug('%s is link. Skip.', path_to_file)
152 elif os.path.isdir(path_to_file):
153 logger.warning('Directories are not expected to be listed in %s file! '
159 logger.warning('Unexpected path %s '
160 'is not a file, link or directory. Skip.',
166 logger.error(format_exc())
171 def ext_size(install_dir, salomex_name):
173 Calculate a total size of a salome extension from SALOME install root.
176 salome_root - path to SALOME install root directory.
177 salomex_name - a name of the given salome extension.
180 Size of the directory in bytes.
183 # Check if provided dirname is valid
184 if not isvalid_dirname(install_dir):
187 # Check if an extension with this name is installed
188 salomexc = find_salomexc(install_dir, salomex_name)
190 logger.error('Cannot calculate the size of %s extension!',
195 return size_bylist(os.path.join(install_dir, SALOME_EXTDIR), salomexc)
198 def ext_size_str(install_dir, salomex_name, format_in_bytes=False):
200 Calculate a total size of the given extension and format as a string.
203 install_dir - directory where the given extension is installed.
204 salomex_name - the given extension's name.
205 format_in_bytes - if True, size is expressed in bytes,
206 otherwise human readable units are used.
209 Size of the extension as a formatted string.
212 size = ext_size(install_dir, salomex_name)
216 size_str = size_to_str(size, format_in_bytes)
218 logger.debug('The size of %s: %s', salomex_name, size_str)
222 def dependency_tree(directory):
224 Create a dependency tree for all salome extensions
225 installed in the given directory.
228 directory - the given directory
231 A dictionary like that for extensions A, B, C, D and E:
238 { 'A': ['B', 'C', 'D'], 'B': ['C', 'E'], 'C': [], 'D': ['E'], 'E': [] }.
241 logger.debug('Build dependency tree for extensions in %s', directory)
244 salomexd_files = list_files_ext(directory, DFILE_EXT)
245 logger.debug('There are %s extensions in %s', len(salomexd_files), directory)
247 for salomexd_file in salomexd_files:
248 ext_name, _ = os.path.splitext(os.path.basename(salomexd_file))
250 logger.debug('Check dependencies for %s...', salomexd_file)
251 salomexd_content = read_salomexd(salomexd_file)
253 if EXTDEPENDSON_KEY in salomexd_content:
254 depends_on = salomexd_content[EXTDEPENDSON_KEY]
255 logger.debug('List of dependencies: %s', depends_on)
257 tree[ext_name] = depends_on
259 logger.debug('Dependency tree: %s', tree)
263 def ext_info_dict(directory):
265 Get installed salome extensions info.
268 directory - the given ext install directory.
271 A dictionary {name: [descr, author, components, size]}.
274 logger.debug('Build info dictionary for extensions in %s', directory)
277 salomexd_files = list_files_ext(directory, DFILE_EXT)
278 logger.debug('There are %s extensions in %s', len(salomexd_files), directory)
280 for salomexd_file in salomexd_files:
282 ext_name, _ = os.path.splitext(os.path.basename(salomexd_file))
283 size = ext_size_str(directory, ext_name)
285 # Collect salomexd info
286 salomexd_content = read_salomexd(salomexd_file)
289 if EXTDESCR_KEY in salomexd_content:
290 descr = salomexd_content[EXTDESCR_KEY]
291 logger.debug('descr: %s', descr)
294 if EXTAUTHOR_KEY in salomexd_content:
295 author = salomexd_content[EXTAUTHOR_KEY]
296 logger.debug('author: %s', author)
299 if EXTCOMPONENT_KEY in salomexd_content:
300 components = salomexd_content[EXTCOMPONENT_KEY]
301 logger.debug('components: %s', components)
303 ext_info[ext_name] = [descr, author, components, size]
305 logger.debug('Installed extensions info: %s', ext_info)
309 def ext_by_dependants(dep_tree, depends_on=None):
311 Calcualate a list of extensions names sorted by dependencies.
314 dep_tree - a dependecy tree of all considered extensions.
315 depends_on - a set of extensions names to check if the current one depends on them.
318 A list of of extensions from dep_tree sorted by dependencies.
319 For example, dictionary like that for extensions A, B, C, D and E:
326 represented { 'A': ['B', 'C', 'D'], 'B': ['C', 'E'], 'C': [], 'D': ['E'], 'E': [] }
327 returns ['A', 'B', 'D', 'C', 'E'].
330 logger.debug('dep_tree: %s, depends_on: %s', dep_tree, depends_on)
333 logger.debug('Dependency tree is empty! Return')
336 # Check if we've got a default value
340 # Collect all dependencies for the given dependency level in the tree
341 cur_depends_on = set()
342 for ext, dependendants in dep_tree.items():
343 # Select all components with empty dependendants
344 is_selected = not dependendants
346 # Check dependendants intersection if the case
348 dep_set = set(dependendants)
349 intersection = dep_set & depends_on
350 logger.debug('ext: %s, dep_set: %s, intersection: %s', ext, dep_set, intersection)
351 is_selected = intersection == dep_set
354 cur_depends_on.add(ext)
356 # Check collected dependencies
357 logger.debug('cur_depends_on: %s', cur_depends_on)
358 if not cur_depends_on:
360 'Extensions in the tree doesnt rely on any other extensions! Return all of them')
361 return [ext for ext, _ in dep_tree.items()]
363 # Collect all extension for this level
365 for ext in cur_depends_on:
366 res_extensions.append(ext)
369 logger.debug('res_extensions: %s', res_extensions)
371 # Get extensions from the next dependency level of the tree
372 cur_depends_on |= depends_on
373 return res_extensions + ext_by_dependants(dep_tree, cur_depends_on)
376 def ext_by_name(directory):
378 Calcualate a list of extensions installed in the given directory.
381 directory - a directory where extensions are installed.
384 A list of extensions names sorted by name.
387 logger.debug('directory: %s', directory)
390 salomexd_files = list_files_ext(directory, DFILE_EXT)
391 logger.debug('There are %s extensions in %s', len(salomexd_files), directory)
395 for salomexd_file in salomexd_files:
396 ext_name, _ = os.path.splitext(os.path.basename(salomexd_file))
397 res_names.append(ext_name)
401 logger.debug('Installed extensions: %s', res_names)
406 if __name__ == '__main__':
407 if len(sys.argv) == 2:
408 dir_size_str(sys.argv[1])
409 ext_tree = dependency_tree(sys.argv[1])
410 ext_list = ext_by_dependants(ext_tree)
411 logger.info('ext_list: %s', ext_list)
412 ext_by_name(sys.argv[1])
413 ext_info_dict(sys.argv[1])
414 elif len(sys.argv) == 3:
415 arg_1, arg_2 = sys.argv[1:] # pylint: disable=unbalanced-tuple-unpacking
416 ext_size_str(arg_1, arg_2)
418 logger.error('You must provide all the arguments!')
419 logger.info(dir_size_str.__doc__)
420 logger.info(dependency_tree.__doc__)
421 logger.info(ext_size_str.__doc__)