File: generateUnusedKeys

package info (click to toggle)
rawtherapee 5.8-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 99,380 kB
  • sloc: cpp: 185,302; ansic: 23,851; sh: 1,049; cs: 155; xml: 53; makefile: 16
file content (176 lines) | stat: -rwxr-xr-x 6,201 bytes parent folder | download
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
#!/usr/bin/env bash

# This script is part of RawTherapee.
#
# This Bash4 script checks whether each key in "default" is used in
# a .cc or .h file. Those that are not are printed to screen, and the
# user is asked if they should be deleted from all language files.
#
# Keys in commented-out sections "//" are ignored.
#
# Some concatenated keys are ignored, these need to be added to the list manually:
#   HISTORY_MSG_
#   EXTPROGTARGET_
#   FILEBROWSER_POPUPRANK
#   FILEBROWSER_POPUPCOLORLABEL
#   SAMPLEFORMAT_
#   TP_RAW_[demosaic_method]
#
# The script can also clean up already-translated files by removing keys which
# do not exist in default.
#
# Run the script from the project root:
#   ./tools/generateUnusedKeys
#
# Run ./tools/generateTranslationDiffs before and after running this script.
# Double-check the deletion before committing.
#
# Blame DrSlony

tmp=temp_file
if [[ -w $tmp ]]; then
  rm -v "$tmp"
fi

abort () {
  printf '%s\n' "" "Aborted" "Removing leftover files:"
  [[ -e "$tmp" ]] && rm "$tmp"
  rm -v --interactive=once sed*
  exit 1
}

trap 'abort' HUP INT QUIT ABRT TERM

cd "rtdata/languages" || { printf '%s\n' "You must run this script from the root of the project."; exit 1; }
# Build array of all interface translation files, or use user-specified ones only
unset langFiles
if [[ $# = 0 ]]; then
  while read -r; do
    langFiles+=("$REPLY")
  done < <(find . -not -iname "LICENSE" -not -iname "README" -not -iname "*.sh"  -not -iname ".*" -not -iname "$tmp" | sort)
else
  langFiles=("$@")
  for langFile in "${langFiles[@]}"; do
    if [[ ! -w $langFile ]]; then
      printf '%s\n' "File \"$langFile\" not found or not writable." ""
      exit 1
    fi
  done
fi

dos2unix default 2>/dev/null

# In the code block below, the grep/sed outside the loop lists keys to ignore.
# Exit status 1 (failure) means key not found in code, destined for deletion.
# The piped grep inside the loop checks the initial match for known comment markers "//".
# Sometimes a key is first found in a comment, and then the same key is found
# in active code, therefore -m1 (stop reading after 1st match) cannot be used.
# To remove comment support, remove the piped grep and set first grep flags to
#   -Irl -m1
# Dynamically built keys like HISTORY_MSG_1 can't be grepped in the code,
# so it renames KEY_1-KEY_9 to KEY_ so that they can be grepped and therefore ignored.
t1="$(date +%s)"
printf '%s\n' 'Matching keys in "default" against .cc and .h files' 'Unmatched keys follow:'
unset delLines
while read -r 'defLine'; do
  grep -Ir --include=\*.{cc,h} --exclude-dir="klt" "${defLine%%;*}" ../../* | grep -Ev "//.*${defLine%%;*}" &>/dev/null
  if [[ $? = 1 ]]; then
    printf '  %s\n' "${defLine%%;*}"
    delLines+=("${defLine%%;*}")
  fi
done < <( \
    grep -Ev -e "^(#|$)|HISTORY_MSG_" \
        -e "^(#|$)|TP_RAW_1PASSMEDIUM" \
        -e "^(#|$)|TP_RAW_2PASS" \
        -e "^(#|$)|TP_RAW_3PASSBEST" \
        -e "^(#|$)|TP_RAW_4PASS" \
        -e "^(#|$)|TP_RAW_AMAZEVNG4" \
        -e "^(#|$)|TP_RAW_DCBVNG4" \
        -e "^(#|$)|TP_RAW_MONO" \
        -e "^(#|$)|TP_RAW_NONE" \
        -e "^(#|$)|TP_RAW_RCDVNG4" \
        "default" | \
    sed -e "s/EXTPROGTARGET_[0-9]*/EXTPROGTARGET_/" \
    -e "s/FILEBROWSER_POPUPCOLORLABEL[0-9]*/FILEBROWSER_POPUPCOLORLABEL/" \
    -e "s/FILEBROWSER_POPUPRANK[0-9]*/FILEBROWSER_POPUPRANK/" \
    -e "s/SAMPLEFORMAT_[0-9]*/SAMPLEFORMAT_/" \
    | sort -Vu)
t2="$(date +%s)"
tt=$((t2-t1))
printf '%s\n' "" "Scan took $tt seconds" "" "Double-checking the code for matched keys:"

for delLine in "${delLines[@]}"; do
  printf '%s\n' "$delLine"
  grep -Ir --include=\*.{cc,h} --exclude-dir="klt" "${delLine}" ../../*
done
echo

read -r -p 'Write results to "unmatched"? [y/n] '
if [[ $REPLY = y || $REPLY = Y ]]; then
  printf '%s\n' "${delLines[@]}" > unmatched
fi
printf '%s\n' ""

read -r -p "Delete ${#delLines[@]} keys from all ${#langFiles[@]} interface language files? [y/n] "
if [[ $REPLY = y || $REPLY = Y ]]; then
  printf '%s\n' "Removing keys from:"
  i=1
  ttot1="$(date +%s)"
  for file in "${langFiles[@]}"; do
    printf "%02d - ${file#.*/}" "$i"
    t1="$(date +%s)"
    for key in "${delLines[@]}"; do
      sed -i "/.\?$key/d" "$file"
    done
    t2="$(date +%s)"
    tt=$((t2-t1))
    printf '%s\n' " - took $tt seconds"
    ((i++))
  done
  ttot2="$(date +%s)"
  ttot=$((ttot2-ttot1))
  tsec=$((ttot%60))
  tmin=$((ttot/60))
  printf '%s\n' "Finished updating ${#langFiles[@]} files." "Total time: ${tmin}m ${tsec}s"
fi

printf '%s\n' "" "The above cleaned up \"default\" from keys not found in the code and then it removed the same keys from the translation files. However if the translation files contain keys which were not in \"default\" then they would remain (because it would take too long to scan the source code for each key in each translation). Since at this point \"default\" is good as gold, it's most time efficient then to match each interface translation file against it and remove all keys not found in it." "" | fold -s
read -r -p "Match ${#langFiles[@]} interface translation files against \"default\" and delete unmatched keys? [y/n] "
if [[ $REPLY = y || $REPLY = Y ]]; then
  printf '%s\n' "Removing keys from:"
  i=1
  ttot1="$(date +%s)"
  for file in "${langFiles[@]}"; do
    printf "%02d - ${file#.*/}" "$i"
    t1="$(date +%s)"
    unset delLines
    # Read line by line
    while read -r line; do
      # If line starts with a real key, not a comment or empty line
      if [[ $line =~ ^[A-Z0-9_]+\; ]]; then
        # then get the key part
        key="${line%%;*}"
        # quietly check whether it exists in default
        grep -Irq "$key" default
        # and if match fails, put it on the kill list
        if [[ $? = 1 ]]; then
          delLines+=("${key}")
        fi
      fi
    done < "$file"
    # kill szeva
    for key in "${delLines[@]}"; do
      sed -i "/.\?$key/d" "$file"
    done
    t2="$(date +%s)"
    tt=$((t2-t1))
    printf '%s\n' " - took $tt seconds"
    ((i++))
  done
  ttot2="$(date +%s)"
  ttot=$((ttot2-ttot1))
  tsec=$((ttot%60))
  tmin=$((ttot/60))
  printf '%s\n' "Finished updating ${#langFiles[@]} files." "Total time: ${tmin}m ${tsec}s"
fi