File: initialisation_functions.sh

package info (click to toggle)
misspell-fixer 0.6-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,024 kB
  • sloc: sed: 20,181; sh: 993; makefile: 41; perl: 26
file content (297 lines) | stat: -rw-r--r-- 8,814 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
function warning {
    echo "misspell-fixer: $*" >&2
}
export -f warning

function verbose {
    if [[ $opt_verbose = 1 ]]; then
        warning "$@"
    fi
}
export -f verbose

function initialise_variables {
    set -f

    export LC_CTYPE=C
    export LANG=C

    export opt_debug=0
    export opt_verbose=0
    export opt_show_diff=0
    export opt_real_run=0
    export opt_backup=1
    export opt_dots=0
    export bash_arg

    export opt_whitelist_save=0

    export opt_whitelist_filename=".misspell-fixer.ignore"

    rules_safe0="${rules_directory}/safe.0.sed"
    rules_safe1="${rules_directory}/safe.1.sed"
    rules_safe2="${rules_directory}/safe.2.sed"
    rules_safe3="${rules_directory}/safe.3.sed"
    rules_not_so_safe="${rules_directory}/not-so-safe.sed"
    rules_gb_to_us="${rules_directory}/gb-to-us.sed"
    export enabled_rules="$rules_safe0"

    export cmd_part_ignore_scm="\
        -o -iname .git\
        -o -iname .svn\
        -o -iname .hg\
        -o -iname CVS"
    export cmd_part_ignore_bin="\
        -o -iname *.gif\
        -o -iname *.jpg\
        -o -iname *.jpeg\
        -o -iname *.png\
        -o -iname *.zip\
        -o -iname *.gz\
        -o -iname *.bz2\
        -o -iname *.xz\
        -o -iname *.rar\
        -o -iname *.po\
        -o -iname *.pdf\
        -o -iname *.woff\
        -o -iname *.mov\
        -o -iname *.svg\
        -o -iname *.tiff\
        -o -iname *.mp4\
        -o -iname *.jar\
        -o -iname yarn.lock\
        -o -iname package-lock.json\
        -o -iname composer.lock\
        -o -iname *.mo"
    export cmd_part_ignore

    export cmd_part_parallelism

    export loop_function=apply_rules_on_one_file
    export prefilter_progress_function=prefilter_progress_none

    export opt_name_filter=''
    export cmd_size="-and ( -size -1024k )"  # find will ignore files > 1MB
    export smart_sed

    export directories

    export tmpfile=.misspell-fixer.$$

    echo '/// ///' >$tmpfile.git.ignore
    trap 'rm -f $tmpfile.git.ignore' EXIT

    GREP=$((ugrep --version >/dev/null 2>&1 && echo 'ugrep') \
        || (ggrep --version >/dev/null 2>&1 && echo 'ggrep') \
        || echo 'grep')
    export GREP
}

function process_command_arguments {
    local OPTIND
    while getopts ":dvorfsibnRVDGmughWN:P:w:" opt; do
        case $opt in
            d)
                warning "-d Enable debug mode."
                opt_debug=1
                bash_arg=x
            ;;
            v)
                warning "-v Enable verbose mode."
                opt_verbose=1
            ;;
            o)
                warning "-o Print dots for each file scanned,"\
                    "comma for each file fix iteration/file."
                opt_dots=1
                prefilter_progress_function=prefilter_progress_dots
            ;;
            r)
                warning "-r Enable real run. Overwrite original files!"
                opt_real_run=1
            ;;
            f)
                warning "-f Enable fast mode. (Equivalent with -P4)"
                cmd_part_parallelism="-P 4"
            ;;
            s)
                warning "-s Enable showing of diffs."
                opt_show_diff=1
            ;;
            i)
                warning "-i Disable scm dir ignoring."
                cmd_part_ignore_scm=''
            ;;
            b)
                warning "-i Disable binary ignoring."
                cmd_part_ignore_bin=''
            ;;
            G)
                warning "-G Respect .gitignore."
                git ls-files --others --ignored --exclude-standard|\
                while read -r filename; do
                    printf './%s' "$filename"
                done >$tmpfile.git.ignore
            ;;
            n)
                warning "-n Disable backups."
                opt_backup=0
            ;;
            u)
                warning "-u Enable unsafe rules."
                enabled_rules="$enabled_rules $rules_not_so_safe"
            ;;
            R)
                warning "-R Enable rare rules."
                enabled_rules="$enabled_rules $rules_safe1"
            ;;
            V)
                warning "-V Enable very-rare rules."
                enabled_rules="$enabled_rules $rules_safe2"
            ;;
            D)
                warning "-D Enable rules from lintian.debian.org / spelling."
                enabled_rules="$enabled_rules $rules_safe3"
            ;;
            m)
                warning "-m Disable max-size check. "\
                    "Default is to ignore files > 1MB."
                cmd_size=" "
            ;;
            g)
                warning "-g Enable GB to US rules."
                enabled_rules="$enabled_rules $rules_gb_to_us"
            ;;
            N)
                warning "-N Enable name filter: $OPTARG"
                if [ -n "$opt_name_filter" ]; then
                    opt_name_filter="$opt_name_filter -or -name $OPTARG"
                else
                    opt_name_filter="-name $OPTARG"
                fi
            ;;
            P)
                warning "-P Enable parallelism: $OPTARG"
                cmd_part_parallelism="-P $OPTARG"
            ;;
            h)
                d="dirname ${BASH_SOURCE[0]}"
                if [[ -f "$($d)"/../README.md ]]; then
                    cat "$($d)"/../README.md
                else
                    zcat /usr/share/doc/misspell-fixer/README.md.gz
                fi
                return 10
            ;;
            w)
                warning "-w Use $OPTARG as white list file instead of "\
                    "$opt_whitelist_filename."
                opt_whitelist_filename=$OPTARG
            ;;
            W)
                warning "-W Save found misspelled file entries into "\
                    "$opt_whitelist_filename instead of fixing them."
                opt_whitelist_save=1
            ;;
            \?)
                warning "Invalid option: -$OPTARG"
                return 100
            ;;
            :)
                warning "Option -$OPTARG requires an argument."
                return 101
            ;;
        esac
    done

    if [ -z "$opt_name_filter" ]; then
        opt_name_filter='-true'
    fi

    shift $((OPTIND-1))

    if [[ "$*" = "" ]]; then
        warning "Not enough arguments."\
            "(target directory not found) => Exiting."
        return 102
    fi

    directories=( "$@" )
    cmd_part_ignore="(\
        -iname $tmpfile*\
        -o -iname $opt_whitelist_filename\
        -o -iname *.BAK\
        $cmd_part_ignore_scm $cmd_part_ignore_bin\
        ) -prune -o "
    warning "Target directories: ${directories[*]}"

    if [[ $opt_show_diff = 1 ||\
        $opt_backup = 1 ||\
        $opt_real_run = 0 ||\
        $opt_verbose = 1 ]]
    then
        loop_function=decorate_one_iteration
    fi

    return 0
}

function handle_parameter_conflicts {
    if [[ $opt_whitelist_save = 1 && $opt_real_run = 1 ]]; then
        warning "Whitelist cannot be generated in real run mode. => Exiting."
        return 103
    fi
    if [[ $opt_whitelist_save = 0 && $opt_real_run = 0 ]]; then
        warning "Real run (-r) has not been enabled."\
            "Files will not be changed. Use -r to override this."
    fi
    if [[ -z $cmd_part_parallelism ]]; then
        return 0
    fi
    if [[ $opt_show_diff = 1 ]]; then
        warning "Parallel mode cannot show diffs."\
            "Showing diffs is turned on. => Exiting."
        return 104
    fi
    return 0
}

function handle_whitelist_configfile {
    if [[ -s ".github/$opt_whitelist_filename" && -s "$opt_whitelist_filename" ]]; then
        warning "We found both .github/$opt_whitelist_filename and $opt_whitelist_filename."\
            " We can handle only one at the moment."
        return 105
    fi

    if [[ -s ".github/$opt_whitelist_filename" ]]; then
        opt_whitelist_filename=".github/$opt_whitelist_filename"
    fi
}

function check_grep_version {
    local current_version
    current_version=$($GREP --version|head -1|sed -e 's/.* //g')
    local required_version="2.28"
    if printf '%s\n%s\n' "$required_version" "$current_version" | sort -VC
    then
        verbose "Your grep version is $current_version"\
            "which is at least the optimal: $required_version."
    else
        warning "!! Your grep version is $current_version"\
            "which is less than the optimal: $required_version."\
            "This may degrade misspell fixer's performance"\
            "significantly! (100x) !! Alternatively install ugrep "\
            "which is even quicker."
    fi
}

function check_sed_arguments {
    if sed -b 2>&1 |$GREP -q illegal
    then
        # OS/X
        smart_sed=0
    else
        # Everything else
        smart_sed=1
    fi
}