#!/usr/bin/python3 """ FORTRAN FLUKA user routine converter to clean up the names of include files with parentheses All occurences and variants of INCLUDE '(ABDFD)' will be replaced by INCLUDE 'abdfd.inc'. """ __authors__ = "C. Theis, V. Vlachoudis" __copyright__ = "Copyright (C) 2019 CERN" __version__ = "1.2 (110619)" import shutil import os import re import sys ##-------------------------------------------------------------------------------------------------------------------------------- # convert the file def convertFORTRAN( fileName ): compiledRegEx = re.compile(r'^(\s*INCLUDE\s*[\'"])\((\w+)\)([\'"].*)$', re.IGNORECASE) # read the file into a buffer to allow overwriting it backup = fileName + "~" try: os.remove(backup) except: pass os.rename(fileName, backup) try: outFile = open( fileName, 'w' ) except IOError as e: print("Could not open output file (%s)." % e) try: print ("Converting %s..." % fileName) with open( backup, 'r' ) as fin: for line in fin: res = compiledRegEx.match( line ) if res is None: outFile.write(line) else: # outFile.write("%s%s.inc%s\n"%res.groups()) outFile.write("%s%s.inc%s\n"%(res.group(1),res.group(2).lower(),res.group(3))) except IOError as e: print("Couldn't open file (%s)." % e) outFile.close() # retain permission bits shutil.copymode( backup, fileName ) print('finished') ##-------------------------------------------------------------------------------------------------------------------------------- if __name__ == "__main__": #sys.argv.append( "e:\\temp\\test-file.f" ) print ('FORTRAN User routine converter Version %s by %s' % (__version__, __authors__) ) print (__copyright__) if len(sys.argv) == 1 or sys.argv[1] == '-h' or sys.argv[1] == '-H': print ('Syntax: fluka_src_converter ') print ('Please note that wildcards are allowed as well as directories with wildcards') print ('e.g: fluka_src_converter mysrc/*') sys.exit() #retrieve file names fileNames = sys.argv[1:] for curFile in fileNames: convertFORTRAN( curFile )