chiark / gitweb /
pwx/last_mutual_commits.csv: Add last mutual before branching out v238-stable
[elogind.git] / pwx / update_po_files.pl
1 #!/usr/bin/perl -w
2
3 # ================================================================
4 # ===        ==> --------     HISTORY      -------- <==        ===
5 # ================================================================
6 #
7 # Version  Date        Maintainer      Changes, Additions, Fixes
8 # 0.0.1    2017-03-12  sed, PrydeWorX  First basic design
9 #
10 # ========================
11 # === Little TODO list ===
12 # ========================
13 #
14 use strict;
15 use warnings;
16 use Cwd qw(getcwd abs_path);
17 use File::Basename;
18 use File::Find;
19 use Readonly;
20
21 # ================================================================
22 # ===        ==> ------ Help Text and Version ----- <==        ===
23 # ================================================================
24 Readonly my $VERSION     => "0.0.1"; ## Please keep this current!
25 Readonly my $VERSMIN     => "-" x length($VERSION);
26 Readonly my $PROGDIR     => dirname($0);
27 Readonly my $PROGNAME    => basename($0);
28 Readonly my $WORKDIR     => getcwd();
29 Readonly my $USAGE_SHORT => "$PROGNAME <--help|path to po directory>";
30 Readonly my $USAGE_LONG  => qq#
31 elogind po file cleaner V$VERSION
32 -------------------------$VERSMIN
33
34     Check all .po files in the given directory and comment out all lines
35     that refer to non-existent files.
36
37 Usage :
38 -------
39   $USAGE_SHORT
40
41 Options :
42 ---------
43     -h|--help           | Print this help text and exit.
44 #;
45
46 # ================================================================
47 # ===        ==> -------- Global variables -------- <==        ===
48 # ================================================================
49
50 my $file_fmt     = ""; ## Format string built from the longest file name in generate_file_list().
51 my $main_result  = 1;  ## Used for parse_args() only, as simple $result is local everywhere.
52 my $po_file_path = ""; ## Path to where the .po files are to be found.
53 my $show_help    = 0;
54 my @source_files = (); ## File list generated by generate_file_list()
55
56
57 # ================================================================
58 # ===        ==> --------  Function list   -------- <==        ===
59 # ================================================================
60
61 sub generate_file_list; ## Find all relevant files and store them in @wanted_files
62 sub parse_args;         ## Parse ARGV for the options we support
63 sub wanted;             ## Callback function for File::Find
64
65
66 # ================================================================
67 # ===        ==> --------    Prechecks     -------- <==        ==
68 # ================================================================
69
70 $main_result = parse_args(@ARGV);
71 ( (!$main_result)                 ## Note: Error or --help given, then exit.
72         or ( $show_help and print "$USAGE_LONG" ) )
73         and exit(!$main_result);
74 generate_file_list or exit 1;
75
76
77 # ================================================================
78 # ===        ==> -------- = MAIN PROGRAM = -------- <==        ===
79 # ================================================================
80
81 for my $pofile (@source_files) {
82         printf("$file_fmt: ", $pofile);
83
84
85         # --- 1) Load the file ---
86         # ------------------------
87         my @lIn = ();
88         if (open(my $fIn, "<", $pofile)) {
89                 @lIn = <$fIn>;
90                 close($fIn);
91         } else {
92                 print "ERROR READING: $!\n";
93                 next;
94         }
95         
96         # --- 2) Copy @lIn to @lOut, commenting out all parts ---
97         # ---    belonging to files that do not exist.        ---
98         # -------------------------------------------------------
99         my $count     = 0;
100         my $in_block  = 0; ## 1 if in commented out block
101         my @lOut      = ();
102         my $was_block = 0; ## save in_block, so we know when to add elogind masks
103         for my $line (@lIn) {
104                 chomp $line;
105                 
106                 # in_block switches are done on file identifications lines, which look like
107                 # this : "#: ../src/import/org.freedesktop.import1.policy.in.h:2"
108                 if ($line =~ m/^#:\s+([^:]+):\d+/) {
109                         # Note: There might be two file references, if the transalted text spans
110                         #       more than one line. The second path is the same as the first, so
111                         #       it is sufficient not to end the regex with '$' here.
112                         my $altfile = substr($1, 0, -2); ## .in files have an extra '.h' attached
113                         $was_block  = $in_block;
114                         $in_block   = (-f $po_file_path . "/" . $1) || (-f $po_file_path . "/" . $altfile) ? 0 : 1;
115                         $in_block and ++$count;
116                 }
117                 
118                 # If the in_block switches, add elogind mask start or end
119                 if ($was_block != $in_block) {
120                         $was_block
121                                 and push(@lOut, "#endif // 0\n")
122                                  or push(@lOut, "#if 0 /// UNNEEDED by elgoind");
123                         $was_block = $in_block;
124                 }
125                 
126                 # If we are in block, comment out the line:
127                 $in_block and $line = "# $line";
128                 
129                 # Now push the line, it is ready.
130                 push(@lOut, $line);
131         } ## End of line copying
132         
133         # Make sure to end the last block
134         $in_block and push(@lOut, "#endif // 0\n");
135         
136         # --- 3) Overwrite the input file with the adapted text. ---
137         # ----------------------------------------------------------
138         if (open(my $fOut, ">", $pofile)) {
139                 for my $line (@lOut) {
140                         print $fOut "$line\n";
141                 }
142                 close($fOut);
143                 print "$count blocks masked\n";
144         } else {
145                 print "ERROR WRITING: $!\n";
146         }
147 } ## End of main loop
148
149
150 # ===========================
151 # === END OF MAIN PROGRAM ===
152 # ===========================
153
154
155 # ================================================================
156 # ===        ==> ---- Function Implementations ---- <==        ===
157 # ================================================================
158
159
160 # -----------------------------------------------------------------------
161 # --- Finds all relevant files and store them in @wanted_files        ---
162 # --- Returns 1 on success, 0 otherwise.                              ---
163 # -----------------------------------------------------------------------
164 sub generate_file_list {
165
166         # Use File::Find to search for .po files:
167         find(\&wanted, "$po_file_path");
168
169         # Just to be sure...
170         scalar @source_files
171                  or print("ERROR: No source files found? Where the hell are we?\n")
172                 and return 0;
173
174         # Get the maximum file length and build $file_fmt
175         my $mlen = 0;
176         for my $f (@source_files) {
177                 length($f) > $mlen and $mlen = length($f);
178         }
179         $file_fmt = sprintf("%%-%d%s", $mlen, "s");
180
181         return 1;
182 }
183
184
185 # -----------------------------------------------------------------------
186 # --- parse the given list for arguments.                             ---
187 # --- returns 1 on success, 0 otherwise.                              ---
188 # --- sets global $show_help to 1 if the long help should be printed. ---
189 # -----------------------------------------------------------------------
190 sub parse_args {
191         my @args      = @_;
192         my $result    = 1;
193
194         for (my $i = 0; $i < @args; ++$i) {
195
196                 # Check for -h|--help option
197                 # -------------------------------------------------------------------------------
198                 if ($args[$i] =~ m/^-(?:h|-help)$/) {
199                         $show_help = 1;
200                 }
201
202                 # Check for unknown options:
203                 # -------------------------------------------------------------------------------
204                 elsif ($args[$i] =~ m/^-/) {
205                         print "ERROR: Unknown option \"$args[$1]\" encountered!\n\nUsage: $USAGE_SHORT\n";
206                         $result = 0;
207                 }
208
209                 # Everything else is considered to the path to the .po files
210                 # -------------------------------------------------------------------------------
211                 else {
212                         # But only if it is not set, yet:
213                         if (length($po_file_path)) {
214                                 print "ERROR: Superfluous po file path \"$args[$i]\" found!\n\nUsage: $USAGE_SHORT\n";
215                                 $result = 0;
216                                 next;
217                         }
218                         if ( ! -d "$args[$i]") {
219                                 print "ERROR: po file path \"$args[$i]\" does not exist!\n\nUsage: $USAGE_SHORT\n";
220                                 $result = 0;
221                                 next;
222                         }
223                         $po_file_path = $args[$i];
224                 }
225         } ## End looping arguments
226
227         # If we have no upstream path now, show short help.
228         if ($result && !$show_help && !length($po_file_path)) {
229                 print "ERROR: Please provide a path to the po files!\n\nUsage: $USAGE_SHORT\n";
230                 $result = 0;
231         }
232
233         return $result;
234 } ## parse_srgs() end
235
236
237 # Callback function for File::Find
238 sub wanted {
239         -f $_ and ($_ =~ m/\.po$/ )
240               and push @source_files, $File::Find::name;
241         return 1;
242 }
243
244