From 8ae5e926c7af4338022b084d00c38d15a47925a8 Mon Sep 17 00:00:00 2001
From: George Marchment <georgemarchment@yahoo.fr>
Date: Thu, 29 Aug 2024 16:44:55 +0200
Subject: [PATCH] added recursive functionnality for the external script call

---
 src/process.py | 45 ++++++++++++++++++++++++++++++++++-----------
 1 file changed, 34 insertions(+), 11 deletions(-)

diff --git a/src/process.py b/src/process.py
index d7b0287..6390c1a 100644
--- a/src/process.py
+++ b/src/process.py
@@ -24,6 +24,7 @@ class Process(Nextflow_Building_Blocks):
         self.tools = []
         self.modules = []
         self.commands = []
+        self.external_scripts = [-1]
         self.initialise()
         self.initialised = True
 
@@ -55,21 +56,24 @@ class Process(Nextflow_Building_Blocks):
             return self.tools
     
 
-    def get_external_scripts_call(self):
-        code = self.get_script_code()
+    def get_external_scripts_call(self, code):
         tab = []
-        for match in re.finditer(r"([^\s\\\*\{\}\[\]]+\.(sh|py|R|r|pl|rg))[^\w]", code):
-            tab.append(match.group(1))
+        for match in re.finditer(r"(\s|\/)([\w\_\-\&]+\/)*([\w\_\-\&]+)\.(sh|py|R|r|pl|rg)", code):
+            tab.append(match.group(0).strip())
         return list(set(tab))
     
-    def get_external_scripts_code(self):
-        calls = self.get_external_scripts_call()
+    def initialise_external_scripts_code(self, code, extension = "", previously_called = {}):
+        calls = self.get_external_scripts_call(code+'\n')
+        
         #workflow_directory = self.origin.get_address()
         #print(workflow_directory)
         #import os
         #print(os.getcwd(), self.origin.get_address(), self.get_workflow_address())
         scripts = []
+        extensions = []
+        bash_scripts = []
 
+        tab = []
         for call in calls:
             #Check first if the file is in the bin
             file = glob.glob(f'{self.get_workflow_address()}/bin/**/{call}', recursive=True)
@@ -80,14 +84,32 @@ class Process(Nextflow_Building_Blocks):
                 file = glob.glob(f'{self.get_workflow_address()}/**/{call}', recursive=True)
                 if(len(file)>1):
                     raise BioFlowInsightError(f"More than one script named '{call}' in the workflow source code, don't know which one to use when using the process '{self.get_name()}'", num = 13, origin=self)
-                
+            
+            
             for f in file:
-                with open(f, 'r') as s:
-                    scripts.append(s.read())
+                if(f not in previously_called):
+                    val = ""
+                    with open(f, 'r', encoding='latin-1') as s:
+                    #with open(f, 'r') as s:
+                        val = s.read()
+                        scripts.append(val)
+                    previously_called[f] = ""
+                    #Recursive Call to get the ones which are being called if the current file is a bash script
+                    if(extension not in ["py", "R", "pl", "rg", "r"]):
+                        tab += self.initialise_external_scripts_code(val, extension=f.split(".")[-1], previously_called= previously_called)
+        scripts+=tab
         
-        return scripts
+        return list(set(scripts))
     
 
+    def get_external_scripts_code(self):
+        if(self.external_scripts!=[-1]):
+            None
+        else:
+            self.external_scripts = self.initialise_external_scripts_code(self.get_script_code())
+        return self.external_scripts
+
+
     def get_python_packages_imported_internal_script(self):
         packages = []
         packages+= get_python_packages(self.get_script_code())
@@ -95,7 +117,8 @@ class Process(Nextflow_Building_Blocks):
     
     def get_python_packages_imported_external_scripts(self):
         packages = []
-        for s in self.get_external_scripts_code():
+        external_scripts = self.get_external_scripts_code()
+        for s in external_scripts:
             packages+= get_python_packages(s)
         return packages
 
-- 
GitLab