Debian Codegraph @

lib/unpack-locdrop

raw
1 # -*-Shell-script-*-
2 #
3 # Copyright (C) 2015-2020 SUSE Software Solutions Germany GmbH
4 #
5 # Author:
6 # Frank Sundermeyer <fsundermeyer at opensuse dot org>
7 #
8 ###########################################################################
9 #
10 # PACKAGING unpack-locdrop
11 #
12 # Subcommand: unpack-locdrop only
13 #
14 # unpack a localization drop packages source.
15 # Files listed in manifest_trans.txt will be extracted from the given tarball
16 # Files listed in manifest_notrans will be linked/copied from the notrans
17 # directory specified with --notrans-dir
18 #
19 ###########################################################################funct
20
21 function unpack-locdrop {
22 local SHORT_OPTS LONG_OPTS SUB_CMD
23 local ALL_TRANS_FILES ALL_NOTRANS_FILES DCFILE_LIST ENTITIES
24 local FILES_IN_TAR FILES_TRANS_IN_MANIFEST FILES_MANIFEST_ONLY
25 local FILES_TRANS_EXTRACT LINK_NOTRANS_FILES MANIFEST_TRANS
26 local MANIFEST_NOTRANS MFT_TRANS_SUFF MFT_NOTRANS_SUFF NO_DCFILE_LIST
27 local NO_EXIFTOOL NO_OPTIPNG XML_FILE_LIST
28 local -a ALL_MANIFEST_NOTRANS_FILES ALL_MANIFEST_TRANS_FILES
29
30 MFT_TRANS_SUFF=manifest_trans.txt
31 MFT_NOTRANS_SUFF=manifest_notrans.txt
32
33 SUB_CMD=$1
34 shift
35
36 SHORT_OPTS="h"
37 LONG_OPTS="help,notrans-dir:,not-validate-tables,optipng,output-dir:,remove-dm,trans-files:"
38
39 parse_args "$SHORT_OPTS" "$LONG_OPTS" "$SUB_CMD" "$@"
40 eval set -- "$P_REMAIN_ARGS"
41
42 #------ Computing the values returned from the parser -----
43 if [[ 1 -eq $P_HELP ]]; then
44 help_scmd_head "$SUB_CMD" "${HELP_SUBCOMMAND[$SUB_CMD]}"
45 help_trans-files
46 help_help
47 help_notrans-dir
48 help_not-validate-tables
49 help_optipng
50 help_output-dir
51 help_remove-dm
52 echo
53 exit 0
54 fi
55
56 if [[ -z "$P_TRANS_FILES" ]]; then
57 exit_on_error "Fatal: Specifying one or more locdrop tarballs with --trans-files is mandatory."
58 fi
59
60 if [[ -z "$P_NOTRANS_DIR" ]]; then
61 ccecho "warn" "No notrans-dir was specified. Assuming DC-file directory"
62 P_NOTRANS_DIR="$MY_BASEDIR"
63 fi
64
65 [[ -z "$P_OUTPUT_DIR" ]] && exit_on_error "Fatal: Specifying an output directory\nwith --output-dir is mandatory."
66
67 #
68 # Extract translated files
69 #
70 for TAR in $P_TRANS_FILES; do
71 if [[ -f $TAR ]]; then
72 [[ 1 -eq $DEBUG ]] && echo "Extracting $TAR"
73 pushd "$P_OUTPUT_DIR" > /dev/null
74 MANIFEST_TRANS=$(tar tfj "$TAR" | grep "$MFT_TRANS_SUFF" 2>/dev/null) || exit_on_error "Could not get manifest of translated files from $TAR"
75 ALL_MANIFEST_TRANS_FILES=( "${ALL_MANIFEST_TRANS_FILES[@]}" "$MANIFEST_TRANS" )
76 MANIFEST_NOTRANS=$(tar tfj "$TAR" | grep "$MFT_NOTRANS_SUFF" 2>/dev/null) || exit_on_error "Could not get manifest of non-translated files from $TAR"
77 ALL_MANIFEST_NOTRANS_FILES=( "${ALL_MANIFEST_NOTRANS_FILES[@]}" "$MANIFEST_NOTRANS" )
78 #
79 # Extract manifest files found above
80 tar xfj "$TAR" "$MANIFEST_TRANS" "$MANIFEST_NOTRANS" 2>/dev/null || exit_on_error "Could not extract manifest files from tarball $TAR"
81
82 #
83 # Sometimes it happens that the manifest files contain files that
84 # are not part of the tar archive (should not happen, but it did,
85 # e.g. with the odg images)
86 # This causes tar to fail on extracting (there is no "continue on
87 # error) with tar. We rather want to print a warning and continue.
88 # Therefore we need to generate appropriate lists
89 #
90 FILES_IN_TAR=$(tar tfj "$TAR" | sort -u)
91
92 FILES_TRANS_IN_MANIFEST=$(cat "$MANIFEST_TRANS" | awk NF | tr ' ' '\n' | sort -u)
93
94 FILES_MANIFEST_ONLY=$(comm -1 -3 <(echo -e "$FILES_IN_TAR") <(echo -e "$FILES_TRANS_IN_MANIFEST"))
95 [[ -n $FILES_MANIFEST_ONLY ]] && ccecho "warn" "Warning: The following files from the manifest are not part of the tarball:\n$FILES_MANIFEST_ONLY"
96 FILES_TRANS_EXTRACT="$(echo $FILES_TRANS_IN_MANIFEST)"
97 for WRONG in $FILES_MANIFEST_ONLY; do
98 FILES_TRANS_EXTRACT="${FILES_TRANS_EXTRACT/$WRONG/}"
99 done
100 [[ 1 -eq $DEBUG ]] && echo "Running: tar xfj \"$TAR\" $FILES_TRANS_EXTRACT"
101 tar xfj "$TAR" $FILES_TRANS_EXTRACT 2>/dev/null || exit_on_error "Could not extract translated files from tarball $TAR"
102 popd >/dev/null
103 else
104 exit_on_error "Fatal: $TAR does not exist"
105 fi
106 done
107
108 pushd "$P_OUTPUT_DIR" > /dev/null
109
110 #
111 # Run optipng on images/src/png if --optipng was specified
112 #
113 which optipng >/dev/null 2>&1 || NO_OPTIPNG="1"
114 which exiftool >/dev/null 2>&1 || NO_EXIFTOOL="1"
115
116 if [[ -z $NO_OPTIPNG && -z $NO_EXIFTOOL ]]; then
117 if [[ -d images/src/png && 1 -eq $OPTIPNG ]]; then
118 local j=0
119 for IMG in images/src/png/*.png; do
120 if [[ -z "$(exiftool -Comment $IMG | grep optipng)" ]]; then
121 let "j += 1"
122 optipng -o2 $IMG >/dev/null 2>&1
123 exiftool -Comment=optipng -overwrite_original -P $IMG >/dev/null
124 fi
125 done
126 if [[ 0 -eq $j ]]; then \
127 ccecho "result" "No PNG images needed optimization"
128 else
129 ccecho "result" "$j PNG images have been optimized."
130 fi
131 fi
132 else
133 [[ -n $NO_OPTIPNG ]] && ccecho "error" "Error: Cannot find optipng!"
134 [[ -n $NO_EXIFTOOL ]] && ccecho "error" "Error: Cannot find exiftool!"
135 fi
136
137 #
138 # Now create a complete list of translated/untranslated files
139 #
140 ALL_TRANS_FILES=$(cat "${ALL_MANIFEST_TRANS_FILES[@]}" | awk NF | tr ' ' '\n' | sort -u)
141 ALL_NOTRANS_FILES=$(cat "${ALL_MANIFEST_NOTRANS_FILES[@]}" | awk NF | tr ' ' '\n' | sort -u)
142
143 # get all files unique to the notrans lists (the ones that have not
144 # been translated)
145 LINK_NOTRANS_FILES=$(comm -1 -3 <(echo -e "$ALL_TRANS_FILES") <(echo -e "$ALL_NOTRANS_FILES") 2>/dev/null) || exit_on_error "Could not get list of files that have not been translated"
146
147 # Process LINK_NOTRANS_FILES to get the file lists we need
148 # the awk statements matches multiple occurrences on a single line
149 #
150 DCFILE_LIST=$(awk '{for(i=1;i<=NF;++i)if($i~/DC-[^ ]*/)print $i}' <(echo "$LINK_NOTRANS_FILES") 2>/dev/null) || exit_on_error "Could not get list of DC files"
151 NO_DCFILE_LIST=$(awk '{for(i=1;i<=NF;++i)if($i !~ /DC-[^ ]*/)print $i}' <(echo "$LINK_NOTRANS_FILES") 2>/dev/null) || exit_on_error "Could not get list of images and XML files that have not been 'translated'"
152
153 # Get a list of all xml files, we need to query them for entities
154 #
155 XML_FILE_LIST=$(awk -v path="${P_NOTRANS_DIR}/" '{for(i=1;i<=NF;++i)if($i~/xml\/.*\.xml/)print path $i}' <(echo "$ALL_TRANS_FILES $ALL_NOTRANS_FILES") 2>/dev/null) || exit_on_error "Could not get list of all XML files"
156
157 # Copy remaining DC-files
158 #
159 echo "Copying DC files"
160 for DC in $DCFILE_LIST; do
161 if [[ -f ${P_NOTRANS_DIR}/$DC ]]; then
162 [[ 1 -eq $DEBUG ]] && echo "Copying ${P_NOTRANS_DIR}/$DATA"
163 cp "${P_NOTRANS_DIR}/$DC" .
164 else
165 ccecho "warn" "DC-file does not exist en-tree: ${P_NOTRANS_DIR}/$DC"
166 fi
167 done
168
169 # Remove the <dm:docmanager> part from the XML files, we cannot read
170 # Chinese bug reports and therefore do not want to encourage users
171 # to send them
172 #
173
174 # check for xmlstarlet binary
175 for _binary in xmlstarlet xml; do
176 XMLSTARLET=$(which $_binary 2>/dev/null)
177 [[ 0 -eq $? ]] && break
178 done
179 [[ -z $XMLSTARLET ]] && exit_on_error "Required package \"xmlstarlet\" is not installed"
180
181 #
182 # Get a list of all translated xml files
183 #
184 XML_TRANS_FILE_LIST=$(awk '{for(i=1;i<=NF;++i)if($i~/xml\/.*\.xml/)print $i}' <(echo "$ALL_TRANS_FILES") 2>/dev/null) || exit_on_error "Could not get list of translated XML files"
185 #
186 # Remove entire docmanager block (with --remove-dm) or at least
187 # dm_editurl, since we do not have editable localised content
188 #
189 for _XML_TRANS_FILE in $XML_TRANS_FILE_LIST; do
190 if [[ 1 -eq $P_REMOVEDM ]]; then
191 # Remove complete docmanager block
192 echo "Removing <dm:docmanager> block"
193 $XMLSTARLET ed -S --inplace -N dm="urn:x-suse:ns:docmanager" -d "//dm:docmanager" $_XML_TRANS_FILE
194 else
195 # Remove editurl part - we only want bugreports for localised
196 # content. This is always done.
197 echo "Removing <dm:editurl> tag"
198 $XMLSTARLET ed -S --inplace -N dm="urn:x-suse:ns:docmanager" -d "//dm:docmanager/dm:editurl"$_XML_TRANS_FILE
199 fi
200 done
201
202 # Link not translated images and XML files
203 #
204 echo "Linking xml files and images"
205 for DATA in $NO_DCFILE_LIST; do
206 if [[ -f ${P_NOTRANS_DIR}/$DATA ]]; then
207 DIR="$(dirname "$DATA")"
208 if [[ "." = "$DIR" ]]; then
209 [[ 1 -eq $DEBUG ]] && echo "Linking ${P_NOTRANS_DIR}/$DATA"
210 ln -sf "${P_NOTRANS_DIR}/$DATA"
211 else
212 [[ -d "$DIR" ]] || mkdir -p "$DIR"
213 # the realpath statement makes sure a realtive path is used
214 # for the link (also see
215 # https://stackoverflow.com/questions/2564634)
216 [[ 1 -eq $DEBUG ]] && echo "Linking ${P_NOTRANS_DIR}/$DATA"
217 (cd "$DIR" && ln -sf "$(realpath --relative-to="." "${P_NOTRANS_DIR}/$DATA")")
218 fi
219 else
220 ccecho "warn" "Cannot create link to \"en\", file does not exist: ${P_NOTRANS_DIR}/$DATA"
221 fi
222 done
223
224
225 # Get entity files from the notrans sources and link to them
226 #
227 echo "Getting entities"
228 ENTITIES="$("${LIBEXEC_DIR}/getentityname.py" ${P_NOTRANS_DIR}/xml/*.xml)"
229 for ENT in $ENTITIES; do
230 if [[ -f $ENT ]]; then
231 [[ 1 -eq $DEBUG ]] && echo "Linking ${P_NOTRANS_DIR}/xml/$ENT"
232 (cd xml && ln -sf "$(realpath --relative-to="." "$ENT")")
233 else
234 ccecho "warn" "Cannot find entity file: $ENT"
235 fi
236 done
237
238 popd >/dev/null
239
240 ccecho "result" "Successfully created files in\n$P_OUTPUT_DIR"
241 exit 0
242 }
243
244 ###########################################################################
245 # HELP
246 #
247
248 function help_output-dir {
249 cat <<EOF
250 --output-dir=DIRECTORY Output directory in which to unpack the files
251 from the localization drop. Mandatory.
252 Default: unset
253 EOF
254 }
255 function help_notrans-dir {
256 cat <<EOF
257 --notrans-dir=DIRECTORY Relative or absolute path to the directory
258 containing the original (untranslated) sources.
259 Links pointing to this directory are created for
260 all files listed in manifest_notrans.txt.
261 Optional. If not specified, the dir name of the
262 DC file is used.
263 Default: DC file directory
264 EOF
265 }
266 function help_remove-dm {
267 cat <<EOF
268 --remove-dm Removes <dm:docmanager> blocks from the
269 translated files.
270 Default: unset
271 EOF
272 }
273 function help_trans-files {
274 cat <<EOF
275 --trans-files="<LIST>" Specify one or more tarballs with translated
276 files (space-separated) for a single
277 language.
278 Default: unset
279 EOF
280 }