]> SALOME platform Git repositories - modules/kernel.git/blob - bin/SalomeOnDemandTK/ExtensionBuilder.py
Salome HOME
[bos #32522][EDF] SALOME on Demand. Init version of ExtensionBuilder module.
[modules/kernel.git] / bin / SalomeOnDemandTK / ExtensionBuilder.py
1 #  -*- coding: iso-8859-1 -*-
2 # Copyright (C) 2007-2022  CEA/DEN, EDF R&D, OPEN CASCADE
3 #
4 # Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
5 # CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 #
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.
11 #
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.
16 #
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
20 #
21 # See https://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
22 #
23
24 #  File   : ExtensionBuilder.py
25 #  Author : Konstantin Leontev, Open Cascade
26 #
27 ## @package SalomeOnDemandTK
28 #  @brief Set of utility functions those help to build SALOME python extensions.
29
30
31 import tarfile
32 import os.path
33 import sys
34 import logging
35 import io
36 import pathlib
37
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()
42
43 """Salome Extension Directory inside any extension's archive"""
44 salomeExtDir = '__SALOME_EXT__'
45 salomexcName = 'salomexc'
46
47 def listFilesAbs(dirPath):
48     """
49         Returns the recursive list of absolut paths to files (as pathlib.Path objects)
50         in the dirPath root directory and all subdirectories.
51     """
52
53     files = []
54
55     for path in pathlib.Path(dirPath).iterdir():
56         if path.is_file():
57             files.append(path)
58         else:
59             files += listFilesAbs(path)
60
61     return files
62
63 def listFilesRel(dirPath):
64     """
65         Returns the recursive list of relative paths to files (as pathlib.Path objects)
66         in the dirPath root directory and all subdirectories.
67     """
68
69     filesAbs = listFilesAbs(dirPath)
70     return [file.relative_to(dirPath) for file in filesAbs]
71
72 def listFiles(dirPath, isRel = True):
73     """
74         Returns the recursive list of paths to files (as a list of strings)
75         in the dirPath root directory and all subdirectories.
76     """
77
78     pathList = listFilesRel(dirPath) if isRel else listFilesAbs(dirPath)
79     return [str(path) for path in pathList]
80
81 def fileListToNewLineStr(fileList):
82     """
83         Returns the list of paths as a newline separated string
84     """
85     return '\n'.join(file for file in fileList)
86
87 def salomeExtFromInstallDir(sourceDirPath, extName = ''):
88     """
89         Makes salome extension archive from a classic module install directory
90     """
91
92     logger.debug('salomeExtFromInstallDir() call')
93
94     sourceDirName = os.path.basename(sourceDirPath)
95     if extName == '':
96         extName = sourceDirName
97
98     try:
99         with tarfile.open(extName, "w:gz") as ext:
100             ext.add(sourceDirPath, salomeExtDir)
101
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))
108
109     except Exception:
110         import traceback  
111         logger.error(traceback.format_exc())
112
113 if __name__ == '__main__':
114     salomeExtFromInstallDir(sys.argv[1])