|
| 1 | +#!/usr/bin/env python3 |
| 2 | +import sys |
| 3 | +import os |
| 4 | +import filecmp |
| 5 | + |
| 6 | +def main(): |
| 7 | + num_args = len(sys.argv) |
| 8 | + if num_args != 2 or num_args !=3: |
| 9 | + AssertionError('Invalid number of arguments, found ', num_args) |
| 10 | + if num_args == 3 and str(sys.argv[2]) != "diff": |
| 11 | + AssertionError('Invalid third argument, ', sys.argv[2]) |
| 12 | + |
| 13 | + infile = str(sys.argv[1]) |
| 14 | + outfile = infile+".new" |
| 15 | + |
| 16 | + adjust_indentation(infile, outfile) |
| 17 | + if num_args == 2: |
| 18 | + print('replacing',infile,'with',outfile) |
| 19 | + os.replace(outfile,infile) |
| 20 | + if num_args == 3: |
| 21 | + same = filecmp.cmp(outfile,infile) |
| 22 | + if not same: |
| 23 | + print(infile) |
| 24 | + print('different!') |
| 25 | + # os.remove(outfile) |
| 26 | + |
| 27 | +def adjust_indentation(input_file, output_file): |
| 28 | + startingchar='!$acc' |
| 29 | + startingloop1='!$acc parallel loop' |
| 30 | + startingloop2='!$acc loop' |
| 31 | + max_empty_lines=4 |
| 32 | + |
| 33 | + with open(input_file, 'r') as file_in, open(output_file, 'w') as file_out: |
| 34 | + lines = file_in.readlines() |
| 35 | + |
| 36 | + # this makes sure !$acc lines that have line continuations get indented at proper level |
| 37 | + for kk in range(10): |
| 38 | + # loop through file |
| 39 | + for i in range(len(lines)): |
| 40 | + if lines[i].lstrip().startswith(startingchar) and i + 1 < len(lines): |
| 41 | + j = i + 1 |
| 42 | + empty_lines = 0 |
| 43 | + # look down to see how to indent a line |
| 44 | + while j < len(lines) and empty_lines < max_empty_lines: |
| 45 | + # if the following line starts with [end, else, contains], skip to looking up |
| 46 | + if lines[j].lstrip().startswith('end') or \ |
| 47 | + lines[j].lstrip().startswith('contains') or \ |
| 48 | + lines[j].lstrip().startswith('else'): |
| 49 | + empty_lines = max_empty_lines |
| 50 | + # skip empty lines |
| 51 | + elif lines[j].strip() == '': |
| 52 | + empty_lines += 1 |
| 53 | + # indent acc lines |
| 54 | + elif not lines[j].lstrip().startswith(startingchar): |
| 55 | + indent = len(lines[j]) - len(lines[j].lstrip()) |
| 56 | + lines[i] = ' ' * indent + lines[i].lstrip() |
| 57 | + break |
| 58 | + j += 1 |
| 59 | + # if looking down just finds empty lines, start looking up for indendation level |
| 60 | + if empty_lines == max_empty_lines: |
| 61 | + k = i - 1 |
| 62 | + while k >= 0: |
| 63 | + # if line above is not empty |
| 64 | + if lines[k].strip() != '': |
| 65 | + # if line 2 above ends with line continuation, indent at that level |
| 66 | + if lines[k-1].strip().endswith('&'): |
| 67 | + indent = len(lines[k-1]) - len(lines[k-1].lstrip()) |
| 68 | + # else indent at level of line above |
| 69 | + else: |
| 70 | + indent = len(lines[k]) - len(lines[k].lstrip()) |
| 71 | + lines[i] = ' ' * indent + lines[i].lstrip() |
| 72 | + break |
| 73 | + k -= 1 |
| 74 | + |
| 75 | + # remove empty lines following an acc loop directive |
| 76 | + i = 0 |
| 77 | + while i < len(lines): |
| 78 | + if (lines[i].lstrip().startswith(startingloop1) or \ |
| 79 | + lines[i].lstrip().startswith(startingloop2)) and \ |
| 80 | + i+1 < len(lines) and lines[i+1].strip() == '': |
| 81 | + file_out.write(lines[i]) |
| 82 | + i +=2 |
| 83 | + else: |
| 84 | + file_out.write(lines[i]) |
| 85 | + i += 1 |
| 86 | + |
| 87 | +if __name__ == "__main__": |
| 88 | + main() |
0 commit comments