source: LMDZ6/trunk/tools/format_code.sh @ 6010

Last change on this file since 6010 was 6010, checked in by evignon, 11 days ago

ajout d'un script format_code dans tools pour formatter les codes fortran de lmdz.
en utilisant fprettify. premier version beta

  • Property svn:executable set to *
File size: 4.6 KB
Line 
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#------------------------------------------------------------------
18files_to_process=( $(find ../libf/*/* -maxdepth 0 -type f ! -xtype l \( -name "*.f90" -o -name "*.F90" \)) )
19
20# Check installation of findent and fprettify
21#------------------------------------------------------------------
22if command -v findent >/dev/null 2>&1; then
23    echo "findent has been installed"
24    findent --version
25else
26    echo "findent has not been installed"
27    echo "please install it with sudo apt-get install findent"
28fi
29
30
31if command -v fprettify >/dev/null 2>&1; then
32    echo "fprettify has been installed."
33    fprettify --version
34else
35    echo "fprettify has not been installed."
36    echo "please install it with: pip install fprettify"
37fi
38
39# write the .fprettify configuration file
40#------------------------------------------------------------------
41FILENAME=".fprettify"
42
43# check if the .fprettify exists
44if [ -f "$FILENAME" ]; then
45    echo "the file $FILENAME already exists"
46else
47# create the file
48cat << 'EOF' > "$FILENAME"
49# --- Indentation ---
50indent = 4                  # 4-space indentation (standard practice)
51indent_cont = 6             # continuation lines more indented for clarity
52tabindent = false           # always use spaces, never tabs
53
54# --- Line formatting ---
55max_line_length = 200       # good default for modern screens
56whitespace = true           # trim trailing whitespace
57whitespace_statements = true
58whitespace_operators = true # enforce spaces around operators
59compact = false             # do not compact everything (more readable)
60
61# --- Keywords ---
62case_keywords = lower       # lowercase keywords (if, do, end, module)
63case_intrinsics = lower     # lowercase intrinsics (sin, cos, abs)
64case_types = preserve       # preserve user-defined type casing
65
66# --- Logical formatting ---
67split_multiline_statements = true     # break long statements
68align_assignments = true              # align "=" signs vertically
69align_continuation = true
70
71# --- Operators ---
72space_in_percents = false   # Fortran % operator (component), no spaces
73space_in_power = false      # no spaces around exponent (**)
74space_in_colons = false     # array slices remain compact (i:j)
75space_in_parentheses = false
76
77# --- Comments ---
78keep_comments = true
79align_comments = true       # align trailing comments
80comment_indent = 2
81
82# --- Preprocessing ---
83cpp_keywords = true         # format #ifdef / #define blocks
84preserve_cpp_indentation = true
85
86# --- Safety ---
87strict_indent = true         # fail on inconsistent indentation
88ignore_old_style = false     # do not preserve ugly fixed-format legacy stuff
89
90EOF
91fi
92
93# formatting step
94#------------------------------------------------------------------
95# Array to store files that could not be processed
96failures=()
97
98# Function that applies findent and fprettify to a file
99# with checks on errors and warnings
100
101process_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
140for 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
149done
150
151# Summary report
152echo
153echo "========== SUMMARY =========="
154if [ ${#failures[@]} -eq 0 ]; then
155    echo "All files were processed successfully."
156else
157    echo "The following files could NOT be processed:"
158    for f in "${failures[@]}"; do
159        echo " - $f"
160    done
161fi
162
163
Note: See TracBrowser for help on using the repository browser.