diff --git a/tools/toolchain/install_cp2k_toolchain.sh b/tools/toolchain/install_cp2k_toolchain.sh index 51ec00fb78..92a5904326 100755 --- a/tools/toolchain/install_cp2k_toolchain.sh +++ b/tools/toolchain/install_cp2k_toolchain.sh @@ -1084,7 +1084,7 @@ EOF local __TMPL=$(cat $__filename) eval "printf \"${__TMPL}\n\"" > $__filename # pass this to parsers to replace all of the IF_XYZ statements - python ${SCRIPTDIR}/parse_if.py $__filename $__flags + "${SCRIPTDIR}/parse_if.py" -i -f "${__filename}" $__flags echo "Wrote ${INSTALLDIR}/arch/$__filename" } diff --git a/tools/toolchain/scripts/parse_if.py b/tools/toolchain/scripts/parse_if.py old mode 100644 new mode 100755 index e02def8eb0..668d86084c --- a/tools/toolchain/scripts/parse_if.py +++ b/tools/toolchain/scripts/parse_if.py @@ -1,21 +1,30 @@ +#!/usr/bin/env python +# -*- coding: utf8 -*- + + import sys +import argparse + class Parser: "Parser for files with IF_XYZ(A|B) constructs" def __init__(self, switches): self.mSwitches = switches + def Switches(self): "outputs the switches used by parser" return self.mSwitches + def SetSwitch(self, key, val): "Set or add a (key,val) switch" self.mSwitches[key] = val + def ParseSingleIf(self, string, switch): """ -switch should be a tuple (key, val) -ParseSingleIf(string, switch) will replace in string the -first occurance of IF_key(A|B) with A if val = True; -B if val = False + switch should be a tuple (key, val) + ParseSingleIf(string, switch) will replace in string the + first occurance of IF_key(A|B) with A if val = True; + B if val = False """ init = string.find('IF_' + switch[0]) start = init @@ -44,6 +53,7 @@ B if val = False string[mark + 1 : end] + \ string[end + 1 : len(string)] return result + def ParseIf(self, string, switch): """ ParseIf(string, switch) will replace in string recursively @@ -54,6 +64,7 @@ B if val = False while result.find('IF_' + switch[0]) > -1: result = self.ParseSingleIf(result, switch) return result + def ParseString(self, string): """ ParseString(string) will parse in string recursively @@ -64,47 +75,62 @@ in dictionary self.mSwitches for switch in self.mSwitches.items(): result = self.ParseIf(result, switch) return result - def ParseDocument(self, filename): + + def ParseDocument(self, input_file, output_file): """ -ParseDocument(filename) will replace in a file recursively -all of IF_key(A|B) statements for all (key, val) pairs -in dictionary self.mSwitches + ParseDocument(input_file, output_fiel) will replace recursively + all of IF_key(A|B) statements for all (key, val) pairs + in dictionary self.mSwitches in the input_file stream and output + to the output_file stream. """ - input_file = open(filename, "r") output=[] for line in input_file: output.append(self.ParseString(line)) - input_file.close() + + if input_file == output_file: + output_file.seek(0) + output_file.truncate() + # write to the same file - output_file = open(filename, "w") for line in output: output_file.write(line) - output_file.close() # ------------------------------------------------------------------------ # main program # ------------------------------------------------------------------------ +if __name__ == '__main__': + argparser = argparse.ArgumentParser(description="Resolve IF_*() macros based on given tags") + argparser.add_argument('-f', '--file', metavar="FILENAME", type=str, help="read from given file instead of stdin") + argparser.add_argument('-i', '--inplace', action="store_true", help="do in-place replacement for the given file instead of echo to stdout") + argparser.add_argument('flags', metavar="FLAG", type=str, nargs='*', help="specified flags are set to true") -if (len(sys.argv) < 2) or \ - ("--help" in sys.argv) or \ - ("-h" in sys.argv): - sys.stderr.write("usage: parse_if MPI OMP CUDA ...\n") - sys.exit() + args = argparser.parse_args() -# default list of switches used by the parser -switches = {"MPI" : False, \ - "OMP" : False, \ - "CUDA" : False, \ - "WARNALL" : False, \ - "DEBUG" : False, \ - "VALGRIND" : False, \ - "COVERAGE" : False} + # default list of switches used by the parser + switches = { + "MPI": False, + "OMP": False, + "CUDA": False, + "WARNALL": False, + "DEBUG": False, + "VALGRIND": False, + "COVERAGE": False, + } -parser=Parser(switches) + parser = Parser(switches) -# set the list of switches given on the command line to True -for ii in sys.argv[2:]: - parser.SetSwitch(ii, True) + # set the list of switches given on the command line to True + for flag in args.flags: + parser.SetSwitch(flag, True) -# do parsing -parser.ParseDocument(sys.argv[1]) + # do parsing + + if not args.file: + parser.ParseDocument(sys.stdin, sys.stdout) + else: + if args.inplace: + with open(args.file, mode='r+') as fhandle: + parser.ParseDocument(fhandle, fhandle) + else: + with open(args.file, mode='r') as fhandle: + parser.ParseDocument(fhandle, sys.stdout)