File: update_copyright_header

package info (click to toggle)
simgrid 4.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 39,192 kB
  • sloc: cpp: 124,913; ansic: 66,744; python: 8,560; java: 6,773; fortran: 6,079; f90: 5,123; xml: 4,587; sh: 2,194; perl: 1,436; makefile: 111; lisp: 49; javascript: 7; sed: 6
file content (139 lines) | stat: -rwxr-xr-x 3,385 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#!/usr/bin/env bash

# Copyright (c) 2014-2025. The SimGrid Team. All rights reserved.

# This program is free software; you can redistribute it and/or modify it
# under the terms of the license (GNU LGPL) which comes with this package.

if [ "$1" = "-t" ]; then
    template=$2
    shift 2
else
    template=$(git rev-parse --show-toplevel)/COPYRIGHT.template
fi

if [ $# -eq 0 ]; then
    cat >&2 <<EOF
Usage: $0 [-t COPYRIGHT.template] files...
EOF
    exit 1
fi

if [ ! -r "$template" ]; then
    printf 'File not found: %s\n' "$template" >&2
    exit 1
fi

printf 'Using template: %s\n' "$template"

get_dates() {
    local file=$1
    local date
    sed -n '/Copyright.*SimGrid/{
              s/.*(c) \([[:digit:], -]*\).*/\1/
              s/[, ]\+/\n/g
              p
            }' "$file" \
    | while read -r date; do
        case "$date" in
            "")
                ;;
            *-*)
                seq ${date/-/ }
                ;;
            *)
                echo $date
                ;;
        esac
    done
    git log --follow --format=%ad "$file" | cut -d\  -f5 | uniq
}

format_dates() {
    local first
    local last
    local next
    read -r first
    last=$first
    while read -r next; do
        if [ $next -eq $((last + 1)) ]; then
            last=$next
        else
            if [ $first -eq $last ]; then
                printf '%d, ' $first
            else
                printf '%d-%d, ' $first $last
            fi
            first=$next
            last=$first
        fi
    done
    if [ $first -eq $last ]; then
        printf '%d\n' $first
    else
        printf '%d-%d\n' $first $last
    fi
}

tmp_head=$(mktemp)
tmp_copy=$(mktemp)
tmp_foot=$(mktemp)
trap "rm -f \"$tmp_head\" \"$tmp_copy\" \"$tmp_foot\"" EXIT

for file; do
    echo "########## $file ##########"

    if [ ! -f "$file" ]; then
        echo "!!! skip"
        continue
    fi

    if grep -q "Copyright.*SimGrid" $file ; then
        if head -n 1 "$file" | grep -q '^#!'; then
            script=1
        else
            script=0
        fi

        ### 1. create new template
        dates=$(get_dates "$file" | sort -u | format_dates)
        sed "s/(c) [[:digit:], -]*\./(c) $dates./" "$template" > "$tmp_copy"
        printf '\n' >> "$tmp_copy"

        # fix comments for scripts
        if [ $script = 1 ]; then
            sed -i 's!^..!#!;s! *\*/!!' "$tmp_copy"
        fi

        ### 2. copy file body
        if grep -q 'Copyright.*SimGrid' "$file"; then
            sed '/Copyright.*SimGrid/,$d' "$file" > "$tmp_head"
            sed -i '${\!^/\* *$!d}' "$tmp_head"
            sed '1,/the terms of the license/d' "$file" > "$tmp_foot"
        elif [ $script = 1 ]; then
            head -n 1 "$file" > "$tmp_head"
            tail -n +2  "$file" > "$tmp_foot"
            printf '\n' >> "$tmp_head"
        else
            :> "$tmp_head"
            cp "$file" "$tmp_foot"
        fi
        sed -i '1{\!^ *\*/!d};/[^[:space:]]/,$!d' "$tmp_foot"

        ### 3. concatenate new template and file body into $file
#        cat "$tmp_head"
#        cat "$tmp_copy"
#        cat "$tmp_foot"
        cat "$tmp_head" "$tmp_copy" "$tmp_foot" > $file
    else
        echo "Pass: there is no SimGrid Copyright header."
    fi ; #
done

cat <<EOF

All files processed.

*** DO NOT FORGET TO DOUBLE CHECK CHANGES BEFORE DOING ANY COMMIT! ***

EOF