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, \
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.error('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 if __name__ == '__main__':
264 if len(sys.argv) == 2:
265 dir_size_str(sys.argv[1])
266 dependency_tree(sys.argv[1])
267 elif len(sys.argv) == 3:
268 arg_1, arg_2 = sys.argv[1:] # pylint: disable=unbalanced-tuple-unpacking
269 ext_size_str(arg_1, arg_2)
271 logger.error('You must provide all the arguments!')
272 logger.info(dir_size.__doc__)