]> SALOME platform Git repositories - modules/kernel.git/commitdiff
Salome HOME
[bos #32522][EDF] SALOME on Demand. Added using Unix filename pattern matching to...
authorkosta <kleontev@Debian11.kleontev.virtualbox.org>
Tue, 21 Feb 2023 09:53:09 +0000 (10:53 +0100)
committerKonstantin LEONTEV <konstantin.leontev@opencascade.com>
Wed, 8 Mar 2023 12:46:28 +0000 (13:46 +0100)
bin/SalomeOnDemandTK/extension_builder.py
bin/SalomeOnDemandTK/extension_utilities.py

index 59bf47ae18ad80e2f55302843784b76daee547d7..c2a70b0a85e275115cda64e3ee132483c0c20dd7 100644 (file)
@@ -153,7 +153,7 @@ def create_salomex(salomexb, salomexd, env_py, top_repository):
 
 if __name__ == '__main__':
     if len(sys.argv) == 5:
-        arg_1, arg_2, arg_3, arg_4 = sys.argv[1:]
+        arg_1, arg_2, arg_3, arg_4 = sys.argv[1:5]
         create_salomex(arg_1, arg_2, arg_3, arg_4)
     else:
         logger.error('You must provide all the arguments!')
index 330dc8281ec12c39ed71b5e35f937e243ee1be45..352c281e08b85b9fed0621c440f489c9d289dc3b 100644 (file)
@@ -39,6 +39,8 @@ import json
 from traceback import format_exc
 from pathlib import Path
 import importlib.util
+import fnmatch
+import re
 
 # Usually logging verbosity is set inside bin/runSalomeCommon.py when salome is starting.
 # Here we do just the same for a case if we call this package stand alone.
@@ -248,6 +250,40 @@ def list_files(dir_path):
     return files_list
 
 
+def filter_to_regex(dir_path, filter_patterns):
+    r"""
+    Makes a regex pattern from a given filter.
+
+    Args:
+        dir_path - the path to the directory where you search for files.
+        filter_patterns - list of expressions for matching file names.
+
+    Returns:
+        A regex string translated from the filter.
+        For example:
+        Filter:  ['SMESH/**.cmake', 'SMESH/share/s*.med']
+        Regex:   (?s:SMESH/.*\.cmake)\Z|(?s:SMESH/share/s.*\.med)\Z
+        Matches: SMESH/adm_local/cmake_files/SalomeSMESHConfig.cmake
+                 SMESH/share/salome/resources/smesh/padderdata/ferraill.med
+    """
+
+    logger.debug('Convert given filter to regex...')
+
+    # On Windows, it converts forward slashes to backward slashes.
+    norm_filter = [os.path.normpath(pat) for pat in filter_patterns]
+
+    # Make a regex pattern
+    # Adding '*' at the end of the folders names to match all the files inside.
+    regex_pattern = r'|'.join(
+        [fnmatch.translate(pat + '*' if os.path.isdir(os.path.join(dir_path, pat)) else pat)
+        for pat
+        in norm_filter])
+
+    logger.debug('Regex pattern: %s', regex_pattern)
+
+    return regex_pattern
+
+
 def list_files_filter(dir_path, filter_patterns):
     """
     Returns the recursive list of relative paths to files as strings
@@ -264,19 +300,20 @@ def list_files_filter(dir_path, filter_patterns):
 
     logger.debug('Get list of files to add into archive...')
 
+    regex_pattern = filter_to_regex(dir_path, filter_patterns)
+
     files_abs = []
     files_rel = []
 
     for root, _, files in os.walk(dir_path):
         for file in files:
-            for pattern in filter_patterns:
-                filename_abs = os.path.join(root, file)
-                filename_rel = os.path.relpath(filename_abs, dir_path)
-
-                if filename_rel.startswith(pattern):
-                    logger.debug('File name %s matches pattern %s', filename_rel, pattern)
-                    files_abs.append(filename_abs)
-                    files_rel.append(filename_rel)
+            filename_abs = os.path.join(root, file)
+            filename_rel = os.path.relpath(filename_abs, dir_path)
+
+            if re.match(regex_pattern, filename_rel):
+                logger.debug('File name %s matches pattern', filename_rel)
+                files_abs.append(filename_abs)
+                files_rel.append(filename_rel)
 
     return files_abs, files_rel