File: elxCMakeCaseConversion.py

package info (click to toggle)
elastix 5.2.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 42,480 kB
  • sloc: cpp: 68,403; lisp: 4,118; python: 1,013; xml: 182; sh: 177; makefile: 33
file content (72 lines) | stat: -rw-r--r-- 2,029 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import sys
import os
import os.path
import fnmatch
import shutil
import glob
import subprocess
import re

#-------------------------------------------------------------------------------
# the main function
def main() :

  # The path to the source files relative to this script
  srcdir = os.path.join( "..", "src" );

  # Get a list of all files
  patterns = ( 'CMakeLists.txt', '*.cmake' );
  matches = [];
  for pattern in patterns :
    for root, dirnames, filenames in os.walk( srcdir ):
      for filename in fnmatch.filter( filenames, pattern ) :
        matches.append( os.path.join( root, filename ) );
  #print( matches );

  # Create a list of cmake commands (in lower case)
  # cmake --help-command-list >
  commandlistfilename = "commandlist.txt";
  commandlist = open( commandlistfilename, "w" );
  p = subprocess.Popen(
    "cmake --help-command-list", stdout=commandlist, shell=True );
  (output, err) = p.communicate();
  commandlist.close( );
  # Remove first line would be nice ...

  # Read lower case commands in a list
  commands = open( commandlistfilename ).read().splitlines();

  for match in matches :
    # Print the current file name
    print( match );
    #continue;

    # Read the current file as a string
    inp1 = open( match, 'r' );
    fileAsString = inp1.read();
    inp1.close();

    # Replace all entries from the dictionary
    for i in commands :
      ii = r'\b' + re.escape( i ) + r'\b\s*\(';
      re_i = re.compile( ii, re.IGNORECASE );
      fileAsString = re_i.sub( i + r'(', fileAsString );

    # Replace endif(...) with endif()
    re_endif = re.compile( r'endif\(.*\)' );
    fileAsString = re_endif.sub( r'endif()', fileAsString );

    # Overwrite
    inp2 = open( match, 'w' );
    inp2.write( fileAsString );
    inp2.close();

  # Clean up
  if os.path.exists( commandlistfilename ) : os.remove( commandlistfilename );

  # Exit
  return 0

#-------------------------------------------------------------------------------
if __name__ == '__main__':
    sys.exit(main())