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