Salome HOME
Update copyrights
[tools/medcoupling.git] / v8_work / medcoup7to8.py
1 #!/usr/bin/env python
2 #  -*- coding: iso-8859-1 -*-
3 # Copyright (C) 2007-2019  CEA/DEN, EDF R&D
4 #
5 # This library is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU Lesser General Public
7 # License as published by the Free Software Foundation; either
8 # version 2.1 of the License, or (at your option) any later version.
9 #
10 # This library is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 # Lesser General Public License for more details.
14 #
15 # You should have received a copy of the GNU Lesser General Public
16 # License along with this library; if not, write to the Free Software
17 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
18 #
19 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #
21 # Author : Adrien BRUNETON (CEA/DEN)
22
23 import fileinput
24 import re, os
25 import argparse
26
27 from optparse import OptionParser
28 import os
29
30 DEFAULT_EXT = ['.hxx', '.cxx', '.txx', '.py', '.i', '.dox', '.rst', '.h', '.hh', '.hpp', '.c', '.cpp']
31
32 ## The API changes:
33 REPLACEMENTS = [("RevIntegral",  "IntensiveConservation"), 
34                 ("ConservativeVolumic", "IntensiveMaximum"),
35                 ("IntegralGlobConstraint", "ExtensiveConservation"),
36                 ("Integral", "ExtensiveMaximum"),
37                 ("MEDCouplingAutoRefCountObjectPtr", "MCAuto"),
38                 ("deepCpy", "deepCopy"),
39                 ("performCpy", "performCopyOrIncrRef"),
40                 ("MEDCouplingExtrudedMesh", "MEDCouplingMappedExtrudedMesh"),
41                 ("getBarycenterAndOwner", "computeCellCenterOfMass"),
42                 ("checkCoherency", "checkConsistencyLight"),
43                 ("checkCoherency1", "checkConsistency"),
44                 ("mergeNodes2", "mergeNodesCenter"),
45                 ("renumberNodes2", "renumberNodesCenter"),
46                 ("buildPartOfMySelf2", "buildPartOfMySelfSlice"),
47                 ("buildPartOfMySelfKeepCoords2", "buildPartOfMySelfKeepCoordsSlice"),
48                 ("deepCpyConnectivityOnly", "deepCopyConnectivityOnly"),
49                 ("checkCoherencyOfConnectivity", "checkConsistencyOfConnectivity"),
50                 ("getMeshLength", "getNodalConnectivityArrayLen"),
51                 ("AreCellsEqual0", "AreCellsEqualPolicy0"),
52                 ("AreCellsEqual1", "AreCellsEqualPolicy1"),
53                 ("AreCellsEqual2", "AreCellsEqualPolicy2"),
54                 ("AreCellsEqual7", "AreCellsEqualPolicy7"),
55                 ("AreCellsEqual3", "AreCellsEqualPolicy2NoType"),
56                 ("areCellsIncludedIn2", "areCellsIncludedInPolicy7"),
57                 ("setPartOfMySelf2", "setPartOfMySelfSlice"),
58                 ("ExtractFromIndexedArrays2", "ExtractFromIndexedArraysSlice"),
59                 ("SetPartOfIndexedArrays2", "SetPartOfIndexedArraysSlice"),
60                 ("SetPartOfIndexedArraysSameIdx2", "SetPartOfIndexedArraysSameIdxSlice"),
61                 ("deepCpyConnectivityOnly", "deepCopyConnectivityOnly"),
62                 ("setContigPartOfSelectedValues2", "setContigPartOfSelectedValuesSlice"),
63                 ("selectByTupleId2", "selectByTupleIdSafeSlice"),
64                 ("GetAxTypeRepr", "GetAxisTypeRepr"),
65                 ("cpyFrom", "deepCopyFrom"),
66                 ("selectByTupleId2", "selectByTupleIdSlice"),
67                 ("BuildOld2NewArrayFromSurjectiveFormat2", "ConvertIndexArrayToO2N"),  
68                 ("getIdsEqual", "findIdsEqual"),
69                 ("getIdsNotEqual", "findIdsNotEqual"),
70                 ("getIdsEqualList", "findIdsEqualList"),
71                 ("getIdsNotEqualList", "findIdsNotEqualList"),
72                 ("getIdsEqualTuple", "findIdsEqualTuple"),
73                 ("getIdsInRange", "findIdsInRange"),
74                 ("getIdsNotInRange", "findIdsNotInRange"),
75                 ("getIdsStrictlyNegative", "findIdsStricltyNegative"),
76                 ("searchRangesInListOfIds", "findIdsRangesInListOfIds"),
77                 ("computeOffsets2", "computeOffsetsFull"),
78                 ("applyFunc2", "applyFuncCompo"),
79                 ("applyFunc3", "applyFuncNamedCompo"),
80                 ("getIdsInRange", "findIdsInRange"),
81                 ("fillFromAnalytic2", "fillFromAnalyticCompo"),
82                 ("fillFromAnalytic3", "fillFromAnalyticNamedCompo"),
83                 ("applyFunc2", "applyFuncCompo"),
84                 ("applyFunc3", "applyFuncNamedCompo"),
85                 ("mergeNodes2", "mergeNodesCenter"),
86                 ("setAxType", "setAxisType"),
87                 ("getAxType", "getAxisType"),
88                 ("isIdentity2", "isIota"),
89                 ("SWIGTYPE_p_MEDCoupling__MEDCouplingExtrudedMesh", "SWIGTYPE_p_MEDCoupling__MEDCouplingMappedExtrudedMesh"),
90                 ("MEDCouplingExtrudedMesh____new___", "MEDCouplingMappedExtrudedMesh____new___"),
91                 ("locateValue", "findIdFirstEqual"),
92                 ("locateTuple", "findIdFirstEqualTuple"),   
93                 ("MEDCoupling_DataArrayByte_locateTuple", "MEDCoupling_DataArrayByte_findIdFirstEqualTuple"),
94                 ("MEDCoupling_DataArrayAsciiChar_locateTuple", "MEDCoupling_DataArrayAsciiChar_findIdFirstEqualTuple"),
95                 #("substr", "subArray"),                # conflicts with regular C++ substr, to be handled manually
96                 #("search", "findIdSequence"),          # idem
97                 ]   
98
99 __myName = os.path.abspath(__file__)
100
101 def convert(root_dir, ext, quiet): 
102   if not os.path.isdir(root_dir):
103     raise ValueError("%s is not a valid directory!" % root_dir)
104   
105   for root, _, fNames in os.walk(root_dir, followlinks=False):
106     for fName in fNames:
107       fileName = os.path.join(root, fName)
108       # Skip this script!
109       if fileName == __myName:
110         if not quiet:
111           print("!!! Skipping script %s !!!" % __myName)
112         continue
113       ok = False
114       if fileName[-28:] != "MEDCouplingNatureOfFieldEnum":
115         for e in ext:
116           if fileName[-len(e):] == e:
117             ok = True
118             break
119       else:
120         ok = True
121       if not ok: continue # skip file
122       if not quiet:  print("Handling %s ..." % fileName)
123       for line in fileinput.input(fileName, inplace=1, backup='.bak'):
124         for before, after in REPLACEMENTS:
125           line = re.sub("(\W|^)(%s)(\W|$)" % before, r"\1%s\3" % after, line.rstrip('\r\n'))
126         print(line)  # print in file
127
128 parser = OptionParser()
129 parser.set_usage("Automatic code renaming, corresponding to the API changes between version 7x and 8x of MEDCoupling.\n"
130                  "Original files are backup adding '.bak' to the original name.\n"
131                  "On Linux file systems, links are not followed!\n\n"
132                  "   %prog [options] root_directory")
133 ext_disp = ",".join(DEFAULT_EXT)
134 parser.add_option("-e", "--extension", dest="extension", default="", metavar="EXTENSIONS",
135                   help="Comma separated list of handled extensions (default is %s)" % ext_disp)
136 parser.add_option("-q", "--quiet",
137                   action="store_false", dest="verbose", default=True,
138                   help="Don't print status messages to stdout")
139 (opts, args) = parser.parse_args()
140
141 if len(args) != 1:
142   parser.print_usage()
143   exit(1)
144
145 if opts.extension != "":
146   ext = [s.strip() for s in opts.extension.split(",")]
147 else:
148   ext = DEFAULT_EXT
149   
150 dirName = os.path.abspath(args[0])
151 convert(dirName, ext, not opts.verbose)
152