diff --git a/doc/Documentation.md b/doc/Documentation.md index 837d241..f8991f0 100644 --- a/doc/Documentation.md +++ b/doc/Documentation.md @@ -280,6 +280,8 @@ in compute statement for optimization issue. **--buildModi** Builds the corresponding modi_ file +**--splitModuleRoutineFile** Splits a file into several ones (one file per scope) + ### OpenACC **--addACCData** add !$acc data present and !$acc end data directives diff --git a/src/pyft/applications.py b/src/pyft/applications.py index bec3308..3a0b7fc 100644 --- a/src/pyft/applications.py +++ b/src/pyft/applications.py @@ -61,6 +61,19 @@ class Applications(): Methods for high-to-moderate level transformation """ + @debugDecor + def splitModuleRoutineFile(self): + """ + Split all module and subroutine contain in a fortran file + Return a fortran file for each module and subroutine + """ + for scope in self.getScopes(level=1, excludeContains=False, includeItself=True): + with pyft.pyft.generateEmptyPYFT(scope.path.split(":")[1].lower() + ".F90") as file: + file.extend(scope.findall('./{*}*')) + if file[-1].tail is None: + file[-1].tail = '\n' + file.write() + @debugDecor def buildModi(self): """ diff --git a/src/pyft/pyft.py b/src/pyft/pyft.py index a7817c2..8a72fd3 100755 --- a/src/pyft/pyft.py +++ b/src/pyft/pyft.py @@ -155,6 +155,18 @@ def unlockFile(cls, filename, silence=False): if not silence: raise + def __enter__(self): + """ + Context manager + """ + return self + + def __exit__(self, excType, excVal, excTb): + """ + Context manager + """ + self.close() + def close(self): """ Closes the FORTRAN file diff --git a/src/pyft/scope.py b/src/pyft/scope.py index 7d29e88..fdc89f3 100644 --- a/src/pyft/scope.py +++ b/src/pyft/scope.py @@ -141,14 +141,14 @@ def append(self, *args, **kwargs): https://docs.python.org/3/library/xml.etree.elementtree.html#xml.etree.ElementTree.Element.append """ # Append after the 'END SUBROUTINE' statement - raise self._xml.append(*args, **kwargs) + return self._xml.append(*args, **kwargs) def extend(self, *args, **kwargs): """ https://docs.python.org/3/library/xml.etree.elementtree.html#xml.etree.ElementTree.Element.extend """ # Extend after the 'END SUBROUTINE' statement - raise self._xml.extend(*args, **kwargs) + return self._xml.extend(*args, **kwargs) def _getIndex(self, index): """ diff --git a/src/pyft/scripting.py b/src/pyft/scripting.py index 3c3cdc1..b68fc5b 100644 --- a/src/pyft/scripting.py +++ b/src/pyft/scripting.py @@ -501,6 +501,8 @@ def updateParserApplications(parser): gApplications.add_argument('--deleteRoutineCallsMesoNHGPU', default=False, action='store_true', help='Delete parts of the code not compatible with MesoNH-OpenACC' + 'such as OCND2 blocks') + gApplications.add_argument('--splitModuleRoutineFile', default=False, action='store_true', + help='Split a file') gApplications.add_argument('--deleteNonColumnCallsPHYEX', default=False, action='store_true', help='Delete call to PHYEX routines that needs information on ' + 'horizontal points (multiple column dependency') @@ -825,6 +827,8 @@ def applyTransfoApplications(pft, arg, args, simplify, parserOptions, stopScopes pft.convertTypesInCompute() elif arg == '--buildModi': pft.buildModi() + elif arg == '--splitModuleRoutineFile': + pft.splitModuleRoutineFile() def applyTransfoOpenACC(pft, arg, args, stopScopes): # pylint: disable=unused-argument