1 # -*- coding: iso-8859-1 -*-
2 # Copyright (C) 2007-2022 CEA/DEN, EDF R&D, OPEN CASCADE
4 # Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
5 # CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
7 # This library is free software; you can redistribute it and/or
8 # modify it under the terms of the GNU Lesser General Public
9 # License as published by the Free Software Foundation; either
10 # version 2.1 of the License, or (at your option) any later version.
12 # This library is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 # Lesser General Public License for more details.
17 # You should have received a copy of the GNU Lesser General Public
18 # License along with this library; if not, write to the Free Software
19 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 # See https://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
24 # File : ExtensionBuilder.py
25 # Author : Konstantin Leontev, Open Cascade
27 ## @package SalomeOnDemandTK
28 # @brief Set of utility functions those help to build SALOME python extensions.
38 # Setup logger's output
39 FORMAT = '%(funcName)s():%(lineno)s: %(message)s'
40 logging.basicConfig(format=FORMAT, level=logging.DEBUG)
41 logger = logging.getLogger()
43 """Salome Extension Directory inside any extension's archive"""
44 salomeExtDir = '__SALOME_EXT__'
45 salomexcName = 'salomexc'
47 def listFilesAbs(dirPath):
49 Returns the recursive list of absolut paths to files (as pathlib.Path objects)
50 in the dirPath root directory and all subdirectories.
55 for path in pathlib.Path(dirPath).iterdir():
59 files += listFilesAbs(path)
63 def listFilesRel(dirPath):
65 Returns the recursive list of relative paths to files (as pathlib.Path objects)
66 in the dirPath root directory and all subdirectories.
69 filesAbs = listFilesAbs(dirPath)
70 return [file.relative_to(dirPath) for file in filesAbs]
72 def listFiles(dirPath, isRel = True):
74 Returns the recursive list of paths to files (as a list of strings)
75 in the dirPath root directory and all subdirectories.
78 pathList = listFilesRel(dirPath) if isRel else listFilesAbs(dirPath)
79 return [str(path) for path in pathList]
81 def fileListToNewLineStr(fileList):
83 Returns the list of paths as a newline separated string
85 return '\n'.join(file for file in fileList)
87 def salomeExtFromInstallDir(sourceDirPath, extName = ''):
89 Makes salome extension archive from a classic module install directory
92 logger.debug('salomeExtFromInstallDir() call')
94 sourceDirName = os.path.basename(sourceDirPath)
96 extName = sourceDirName
99 with tarfile.open(extName, "w:gz") as ext:
100 ext.add(sourceDirPath, salomeExtDir)
102 # Write control file - list of files inside extension's dir
103 files = listFiles(sourceDirPath)
104 extDirListData = fileListToNewLineStr(files).encode('utf8')
105 info = tarfile.TarInfo(salomexcName)
106 info.size=len(extDirListData)
107 ext.addfile(info, io.BytesIO(extDirListData))
111 logger.error(traceback.format_exc())
113 if __name__ == '__main__':
114 salomeExtFromInstallDir(sys.argv[1])