Salome HOME
6b64d31f1cf1cc8fc1e128d9088ae2115486157c
[tools/medcoupling.git] / v8_work / medcoup7to8.py
1 #!/usr/bin/env python
2 #  -*- coding: iso-8859-1 -*-
3 # Copyright (C) 2007-2015  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                 ("MEDCouplingNatureOfFieldEnum", "MEDCouplingNatureOfFieldEnum.hxx"),
38                 ("MEDCouplingAutoRefCountObjectPtr", "MCAuto"),
39                 ("deepCpy", "deepCopy") 
40                 ]   
41
42 __myName = os.path.abspath(__file__)
43
44 def convert(root_dir, ext, quiet): 
45   if not os.path.isdir(root_dir):
46     raise ValueError("%s is not a valid directory!" % root_dir)
47   
48   for root, _, fNames in os.walk(root_dir, followlinks=False):
49     for fName in fNames:
50       fileName = os.path.join(root, fName)
51       # Skip this script!
52       if fileName == __myName:
53         if not quiet:
54           print "!!! Skipping script %s !!!" % __myName
55         continue
56       ok = False
57       for e in ext:
58         if fileName[-len(e):] == e:
59           ok = True
60           break
61       if not ok: continue # skip file
62       if not quiet:  print "Handling %s ..." % fileName
63       for line in fileinput.input(fileName, inplace=1, backup='.bak'):
64         for before, after in REPLACEMENTS:
65           line = re.sub("(\W|^)(%s)(\W|$)" % before, r"\1%s\3" % after, line.rstrip())
66         print(line)  # print in file
67
68 parser = OptionParser()
69 parser.set_usage("Automatic code renaming, corresponding to the API changes between version 7x and 8x of MEDCoupling.\n"
70                  "Original files are backup adding '.bak' to the original name.\n"
71                  "On Linux file systems, links are not followed!\n\n"
72                  "   %prog [options] root_directory")
73 ext_disp = ",".join(DEFAULT_EXT)
74 parser.add_option("-e", "--extension", dest="extension", default="", metavar="EXTENSIONS",
75                   help="Comma separated list of handled extensions (default is %s)" % ext_disp)
76 parser.add_option("-q", "--quiet",
77                   action="store_false", dest="verbose", default=True,
78                   help="Don't print status messages to stdout")
79 (opts, args) = parser.parse_args()
80
81 if len(args) != 1:
82   parser.print_usage()
83   exit(1)
84
85 if opts.extension != "":
86   ext = [s.strip() for s in opts.extension.split(",")]
87 else:
88   ext = DEFAULT_EXT
89   
90 dirName = os.path.abspath(args[0])
91 convert(dirName, ext, not opts.verbose)
92