Debian Codegraph @

po/update-potfiles

raw
1 #!/bin/bash
2 #
3 # Copyright (c) 2014,2015 Samuel Degrande
4 #
5 # This file is part of Freedroid
6 #
7 # Freedroid is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 2 of the License, or
10 # (at your option) any later version.
11 #
12 # Freedroid is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with Freedroid; see the file COPYING. If not, write to the
19 # Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
20 # MA 02111-1307 USA
21 #
22
23 # This script analyzes a set of files to generate the list of the files
24 # containing marked strings (to create a POTFILES.in).
25 #
26 # Inspired by the update-pot script of the gitg project.
27
28 declare -a include
29 declare -a exclude
30
31 while getopts ":i:x:" opt ; do
32 case $opt in
33 "i")
34 include[${#include[@]}]=$OPTARG;;
35 "x")
36 exclude[${#exclude[@]}]=$OPTARG;;
37 ":")
38 echo "arg needed for \"$OPTARG\" option" >&2
39 exit 1;;
40 \?)
41 echo "\"$OPTARG\" option is invalid" >&2
42 exit 1;;
43 esac
44 done
45
46 shift $((OPTIND-1))
47 if [ $# -ne 0 ] ; then
48 echo "$0: too many arguments" >&2
49 exit 1
50 fi
51
52 top=$(git rev-parse --show-toplevel)
53
54 # Get the list of the files to inspect from 'git ls-files'.
55 #
56 # The order of the generated list depends of factors such as the OS or
57 # the locale. A different order means a different POTFILES.in, and so
58 # a re-generation of the PO files.
59 # To avoid unwanted modifications of PO files, we sort the list in a
60 # well known order: files first and then directories ("width first
61 # traversal"), using byte ordering (hence the 'LC_ALL=C')
62 declare -A filesmap # Key: directory name - Value: list of files in that dir
63 dirlist=""
64 for f in $(cd "$top" && git ls-files --no-empty-directory --exclude-standard ${include[@]}) ; do
65 dirn=$(dirname $f)
66 filesmap[$dirn]="${filesmap[$dirn]} $f"
67 dirlist="$dirlist $dirn"
68 done
69 allfiles=""
70 for d in $(echo $dirlist | tr " " "\n" | LC_ALL=C sort -u -f) ; do
71 allfiles="$allfiles $(echo ${filesmap[$d]} | tr " " "\n" | LC_ALL=C sort -f)"
72 done
73 unset filesmap
74
75 # Only keep files containing gettext markers
76
77 echo "# List of source files containing translatable strings, automatically"
78 echo "# generated by update-potfiles. Do not edit by hand."
79
80 for f in $allfiles ; do
81 # Remove exclude files
82 excluded=0
83 for exc in ${exclude[@]} ; do
84 if [[ " $f" == " ${exc}" || " $f" =~ " ${exc}/" ]] ; then
85 excluded=1
86 break
87 fi
88 done
89 [ $excluded -eq 1 ] && continue
90
91 # Markers to check depend on the file type
92 case ${f##*.} in
93 # source files
94 "c" | "h" )
95 if grep -E '_\(\"' "$top/$f" &>/dev/null; then
96 echo $f;
97 fi;;
98 # lua files
99 "lua" )
100 if grep -E '_\"|_\[\[' "$top/$f" &>/dev/null; then
101 echo $f;
102 fi;;
103 # data files
104 "dat" | "droids" )
105 if grep -E '_\"' "$top/$f" &>/dev/null; then
106 echo $f;
107 fi;;
108 esac
109 done