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_builder.py
26 # Author : Konstantin Leontev, Open Cascade
28 # @package SalomeOnDemandTK
29 # @brief Set of utility functions those help to build SALOME python extensions.
31 """Build salome extension archive in tar.gz format.
38 from traceback import format_exc
40 from .extension_utilities import logger, \
41 BFILE_EXT, DFILE_EXT, PYFILE_EXT, EXTNAME_KEY, ARCFILE_EXT, SALOME_EXTDIR, CFILE_EXT, \
42 isvalid_filename, isvalid_dirname, read_salomexd, read_salomexb, list_files_filter, \
46 def add_files(ext, files_abs, files_rel):
48 Add selected files into salome extension archive.
51 ext - a file oject to pack in.
52 files_abs - a list of the files with abs file names.
53 files_rel - a list of the files with rel file names.
59 logger.debug('Add selected files into archive %s directory...', SALOME_EXTDIR)
61 # Set progress bar, because it can get some time for large archives
63 total_count = len(files_abs)
64 default_terminator = logger.handlers[0].terminator
65 logger.handlers[0].terminator = ''
66 for (file_abs, file_rel) in zip(files_abs, files_rel):
67 ext.add(file_abs, os.path.normpath(SALOME_EXTDIR + '/' + file_rel))
69 # Progress bar's length is 100 symbols.
71 logger.debug('\r|%-100s|', '=' * int(100 * progress_count/(total_count - 1)))
73 # Reset terminator to default value, otherwise all the followed logs will be in one line
75 logger.handlers[0].terminator = default_terminator
78 def create_salomex(salomexb, salomexd, env_py, top_repository):
80 Makes salome extension archive from provided in salomexb file directories.
83 salomexb - a path to the <extension>.salomexb file.
84 salomexd - a path to the <extension>.salomexd file.
85 env_py - a path to the <ext>_env.py file.
86 top_repository - a root directory for all the paths listed inside salomexb file.
92 logger.debug('Starting create an salome extension file')
94 # Check if provided filenames are valid
95 if not isvalid_filename(salomexb, BFILE_EXT) or \
96 not isvalid_filename(salomexd, DFILE_EXT) or \
97 not isvalid_filename(env_py, PYFILE_EXT) or \
98 not isvalid_dirname(top_repository):
101 # Try to get info from salomexd file
103 salomexd_content = read_salomexd(salomexd)
104 if EXTNAME_KEY in salomexd_content and salomexd_content[EXTNAME_KEY]:
105 salome_ext_name = salomexd_content[EXTNAME_KEY]
107 # Now as a last resort we get a name right from salomexd filename
108 # We need to decide if we can handle this case handsomely
109 salome_ext_name = os.path.basename(salomexd)
110 logger.warning('Cannot get a SALOME extension name from salomexd file! \
111 Use salomexd file name as an extension name.')
113 logger.debug('Set an extension name as: %s', salome_ext_name)
116 with tarfile.open(salome_ext_name + '.' + ARCFILE_EXT, "w:gz") as ext:
117 # Write all included files to the extension's dir
119 # Get the file's matching pattern in the first place
120 included_files_patterns = read_salomexb(salomexb)
121 logger.debug('Included files pattern: %s', included_files_patterns)
122 if not included_files_patterns:
123 # We don't have any pattern, so we don't know what we must put inside an archive
124 logger.error('Cannot create salomex file: \
125 a list of included files patterns is empty.')
128 # List of the files those actually written to the archive.
129 # It goes to the salomexc file then.
130 files_abs, files_rel = list_files_filter(top_repository, included_files_patterns)
131 add_files(ext, files_abs, files_rel)
133 # Write the control file - list of the files inside extension's dir
134 logger.debug('Write the %s control file into archive root...', CFILE_EXT)
135 included_files_data = list_tonewline_str(files_rel).encode('utf8')
136 info = tarfile.TarInfo(salome_ext_name + '.' + CFILE_EXT)
137 info.size = len(included_files_data)
138 ext.addfile(info, io.BytesIO(included_files_data))
140 # Write the description file as is
141 logger.debug('Copy the %s file into archive root...', salomexd)
142 ext.add(salomexd, os.path.basename(salomexd))
144 # Write the env_py file as is
145 logger.debug('Copy the %s file into archive root...', env_py)
146 ext.add(env_py, os.path.basename(env_py))
148 logger.debug('SALOME extension %s was created.', salome_ext_name)
151 logger.error(format_exc())
154 if __name__ == '__main__':
155 if len(sys.argv) == 5:
156 arg_1, arg_2, arg_3, arg_4 = sys.argv[1:5]
157 create_salomex(arg_1, arg_2, arg_3, arg_4)
159 logger.error('You must provide all the arguments!')
160 logger.info(create_salomex.__doc__)