| 1 | #!/bin/bash |
|---|
| 2 | #----------------------------------------------------------------- |
|---|
| 3 | # This script formats the LMDZ fortran routines with |
|---|
| 4 | # a common and unified format |
|---|
| 5 | # it works in 1 or 2 steps: |
|---|
| 6 | # 1/an optional preprocessing step with findent |
|---|
| 7 | # (more robust for code structuration) |
|---|
| 8 | # 2/a formatting step with fprettify using a .fprettify format file |
|---|
| 9 | # |
|---|
| 10 | # if findent or fprettify displays an error or warning message, |
|---|
| 11 | # the file is not processed |
|---|
| 12 | # v1: November 2025 |
|---|
| 13 | #----------------------------------------------------------------- |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | # List of files to process |
|---|
| 17 | #------------------------------------------------------------------ |
|---|
| 18 | files_to_process=( $(find ../libf/*/* -maxdepth 0 -type f ! -xtype l \( -name "*.f90" -o -name "*.F90" \)) ) |
|---|
| 19 | |
|---|
| 20 | # Check installation of findent and fprettify |
|---|
| 21 | #------------------------------------------------------------------ |
|---|
| 22 | if command -v findent >/dev/null 2>&1; then |
|---|
| 23 | echo "findent has been installed" |
|---|
| 24 | findent --version |
|---|
| 25 | else |
|---|
| 26 | echo "findent has not been installed" |
|---|
| 27 | echo "please install it with sudo apt-get install findent" |
|---|
| 28 | fi |
|---|
| 29 | |
|---|
| 30 | |
|---|
| 31 | if command -v fprettify >/dev/null 2>&1; then |
|---|
| 32 | echo "fprettify has been installed." |
|---|
| 33 | fprettify --version |
|---|
| 34 | else |
|---|
| 35 | echo "fprettify has not been installed." |
|---|
| 36 | echo "please install it with: pip install fprettify" |
|---|
| 37 | fi |
|---|
| 38 | |
|---|
| 39 | # write the .fprettify configuration file |
|---|
| 40 | #------------------------------------------------------------------ |
|---|
| 41 | FILENAME=".fprettify" |
|---|
| 42 | |
|---|
| 43 | # check if the .fprettify exists |
|---|
| 44 | if [ -f "$FILENAME" ]; then |
|---|
| 45 | echo "the file $FILENAME already exists" |
|---|
| 46 | else |
|---|
| 47 | # create the file |
|---|
| 48 | cat << 'EOF' > "$FILENAME" |
|---|
| 49 | # --- Indentation --- |
|---|
| 50 | indent = 4 # 4-space indentation (standard practice) |
|---|
| 51 | indent_cont = 6 # continuation lines more indented for clarity |
|---|
| 52 | tabindent = false # always use spaces, never tabs |
|---|
| 53 | |
|---|
| 54 | # --- Line formatting --- |
|---|
| 55 | max_line_length = 200 # good default for modern screens |
|---|
| 56 | whitespace = true # trim trailing whitespace |
|---|
| 57 | whitespace_statements = true |
|---|
| 58 | whitespace_operators = true # enforce spaces around operators |
|---|
| 59 | compact = false # do not compact everything (more readable) |
|---|
| 60 | |
|---|
| 61 | # --- Keywords --- |
|---|
| 62 | case_keywords = lower # lowercase keywords (if, do, end, module) |
|---|
| 63 | case_intrinsics = lower # lowercase intrinsics (sin, cos, abs) |
|---|
| 64 | case_types = preserve # preserve user-defined type casing |
|---|
| 65 | |
|---|
| 66 | # --- Logical formatting --- |
|---|
| 67 | split_multiline_statements = true # break long statements |
|---|
| 68 | align_assignments = true # align "=" signs vertically |
|---|
| 69 | align_continuation = true |
|---|
| 70 | |
|---|
| 71 | # --- Operators --- |
|---|
| 72 | space_in_percents = false # Fortran % operator (component), no spaces |
|---|
| 73 | space_in_power = false # no spaces around exponent (**) |
|---|
| 74 | space_in_colons = false # array slices remain compact (i:j) |
|---|
| 75 | space_in_parentheses = false |
|---|
| 76 | |
|---|
| 77 | # --- Comments --- |
|---|
| 78 | keep_comments = true |
|---|
| 79 | align_comments = true # align trailing comments |
|---|
| 80 | comment_indent = 2 |
|---|
| 81 | |
|---|
| 82 | # --- Preprocessing --- |
|---|
| 83 | cpp_keywords = true # format #ifdef / #define blocks |
|---|
| 84 | preserve_cpp_indentation = true |
|---|
| 85 | |
|---|
| 86 | # --- Safety --- |
|---|
| 87 | strict_indent = true # fail on inconsistent indentation |
|---|
| 88 | ignore_old_style = false # do not preserve ugly fixed-format legacy stuff |
|---|
| 89 | |
|---|
| 90 | EOF |
|---|
| 91 | fi |
|---|
| 92 | |
|---|
| 93 | # formatting step |
|---|
| 94 | #------------------------------------------------------------------ |
|---|
| 95 | # Array to store files that could not be processed |
|---|
| 96 | failures=() |
|---|
| 97 | |
|---|
| 98 | # Function that applies findent and fprettify to a file |
|---|
| 99 | # with checks on errors and warnings |
|---|
| 100 | |
|---|
| 101 | process_file() { |
|---|
| 102 | local f="$1" |
|---|
| 103 | local tmp |
|---|
| 104 | tmp=$(mktemp) || return 1 |
|---|
| 105 | |
|---|
| 106 | local patterns="warning|error|failed|deprecated" |
|---|
| 107 | local err |
|---|
| 108 | local ret |
|---|
| 109 | |
|---|
| 110 | cp "$f" "$tmp" |
|---|
| 111 | |
|---|
| 112 | # # Step 1: findent |
|---|
| 113 | # rm "$tmp" |
|---|
| 114 | # err=$(findent < "$f" 2>&1) |
|---|
| 115 | # ret=$? |
|---|
| 116 | # if [ $ret -ne 0 ] || echo "$err" | grep -Eqi "$patterns"; then |
|---|
| 117 | # echo "findent detected WARNING/ERROR in $f" |
|---|
| 118 | # rm -f "$tmp" |
|---|
| 119 | # return 1 |
|---|
| 120 | # fi |
|---|
| 121 | # findent < "$f" > "$tmp" 2>/dev/null |
|---|
| 122 | |
|---|
| 123 | |
|---|
| 124 | # Step 2: fprettify |
|---|
| 125 | err=$(fprettify "$tmp" 2>&1) |
|---|
| 126 | ret=$? |
|---|
| 127 | if [ $ret -ne 0 ] || echo "$err" | grep -Eqi "$patterns"; then |
|---|
| 128 | echo "fprettify detected WARNING/ERROR in $f" |
|---|
| 129 | rm -f "$tmp" |
|---|
| 130 | return 1 |
|---|
| 131 | fi |
|---|
| 132 | |
|---|
| 133 | # Step 3: replace original file |
|---|
| 134 | mv "$tmp" "$f" |
|---|
| 135 | return 0 |
|---|
| 136 | } |
|---|
| 137 | |
|---|
| 138 | |
|---|
| 139 | # Loop over all the files that we want to process |
|---|
| 140 | for f in "${files_to_process[@]}"; do |
|---|
| 141 | echo "Processing $f ..." |
|---|
| 142 | |
|---|
| 143 | if process_file "$f"; then |
|---|
| 144 | echo "ok" |
|---|
| 145 | else |
|---|
| 146 | echo "failure" |
|---|
| 147 | failures+=("$f") |
|---|
| 148 | fi |
|---|
| 149 | done |
|---|
| 150 | |
|---|
| 151 | # Summary report |
|---|
| 152 | echo |
|---|
| 153 | echo "========== SUMMARY ==========" |
|---|
| 154 | if [ ${#failures[@]} -eq 0 ]; then |
|---|
| 155 | echo "All files were processed successfully." |
|---|
| 156 | else |
|---|
| 157 | echo "The following files could NOT be processed:" |
|---|
| 158 | for f in "${failures[@]}"; do |
|---|
| 159 | echo " - $f" |
|---|
| 160 | done |
|---|
| 161 | fi |
|---|
| 162 | |
|---|
| 163 | |
|---|