File: fix.pl

package info (click to toggle)
lcov 2.3.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,412 kB
  • sloc: perl: 27,615; xml: 6,982; sh: 6,977; python: 1,147; makefile: 593; cpp: 455; ansic: 167
file content (312 lines) | stat: -rwxr-xr-x 9,554 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
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
#!/usr/bin/env perl
#
# Usage: fix.pl [FILE TYPE] [OPTIONS] FILE(s)
#
# Apply file-type specific fixups to the specified list of FILES. If no
# file type is specified, the type is automatically determined from file
# contents and name.
#
# FILE TYPE
#   --manpage           Specified files are man pages
#   --exec              Specified files are executable tools
#   --text              Specified files are text files
#   --spec              Specified files are RPM spec files
#
# OPTIONS
#   --help              Print this text, then exit
#   --verfile FILENAME  Write version information to FILENAME
#   --version VERSION   Use VERSION as version
#   --release RELEASE   Use RELEASE as release
#   --libdir PATH       Use PATH as library path
#   --bindir PATH       Use PATH as executable path
#   --scriptdir PATH    Use PATH as script path
#   --fixinterp         Replace /usr/bin/env interpreter references with values
#                       specified by LCOV_PERL_PATH and LCOV_PYTHON_PATH
#   --fixdate           Replace dates references with the value specified by
#                       SOURCE_DATA_EPOCH or the latest file modification time
#   --fixver            Replace version references with values specified by
#                       --version and --release
#   --fixlibdir         Replace library path references with value specified
#                       by --libdir
#   --fixbindir         Replace executable path references with value specified
#                       by --bindir
#   --fixscriptdir      Replace script path references with value specified by
#                       --scriptdir

use strict;
use warnings;

use Getopt::Long;

my ($opt_man, $opt_exec, $opt_text, $opt_spec,
    $opt_verfile, $opt_version, $opt_release, $opt_libdir,
    $opt_bindir, $opt_scriptdir, $opt_fixinterp, $opt_fixdate,
    $opt_fixver, $opt_fixlibdir, $opt_fixbindir, $opt_fixscriptdir);
my $verbose = $ENV{"V"};

sub get_file_info($)
{
    my ($filename) = @_;
    my ($sec, $min, $hour, $year, $month, $day);
    my @stat;

    die("Error: cannot stat $filename: $!\n") if (!-e $filename);

    @stat = stat($filename);
    my $epoch = int($ENV{SOURCE_DATE_EPOCH} || $stat[9]);
    $epoch = $stat[9] if $stat[9] < $epoch;
    ($sec, $min, $hour, $day, $month, $year) = gmtime($epoch);
    $year  += 1900;
    $month += 1;

    return (sprintf("%04d-%02d-%02d", $year, $month, $day),
            $epoch, sprintf("%o", $stat[2] & 07777));
}

sub update_man_page($$)
{
    my ($source, $date_string) = @_;

    if ($opt_fixver) {
        die("$0: Missing option --version\n") if (!defined($opt_version));

        $source =~ s/\"LCOV\s+\d+\.\d+\"/\"LCOV $opt_version\"/mg;
    }

    if ($opt_fixdate) {
        $date_string =~ s/-/\\-/g;
        $source      =~ s/\d\d\d\d\\\-\d\d\\\-\d\d/$date_string/mg;
    }

    if ($opt_fixscriptdir) {
        die("$0: Missing option --scriptdir\n") if (!defined($opt_scriptdir));
        $source =~ s/^(.ds\s+scriptdir\s).*$/$1$opt_scriptdir/mg;
    }

    return $source;
}

sub update_perl($)
{
    my ($source) = @_;
    my $path = $ENV{"LCOV_PERL_PATH"};

    if ($opt_fixver) {
        die("$0: Missing option --version\n") if (!defined($opt_version));
        die("$0: Missing option --release\n") if (!defined($opt_release));

        $source =~
            s/^(our\s+\$VERSION\s*=).*$/$1 "$opt_version-$opt_release";/mg;
    }

    if ($opt_fixinterp && defined($path) && $path ne "") {
        $source =~ s/^#!.*perl.*\n/#!$path\n/
            unless $source =~ @^#!/usr/bin/env perl$@;
    }

    if ($opt_fixlibdir) {
        die("$0: Missing option --libdir\n") if (!defined($opt_libdir));

        $source =~ s/^use FindBin;\n//mg;
        $source =~ s/"\$FindBin::RealBin[\/.]+lib"/"$opt_libdir"/mg;
    }

    if ($opt_fixbindir) {
        die("$0: Missing option --bindir\n") if (!defined($opt_bindir));

        $source =~ s/^use FindBin;\n//mg;
        $source =~ s/"\$FindBin::RealBin"/"$opt_bindir"/mg;
    }

    if ($opt_fixscriptdir) {
        die("$0: Missing option --scriptdir\n") if (!defined($opt_scriptdir));

        $source =~ s/^use FindBin;\n//mg;
        $source =~ s/"\$FindBin::RealBin"/"$opt_scriptdir"/mg;
    }

    return $source;
}

sub update_python($)
{
    my ($source) = @_;
    my $path = $ENV{"LCOV_PYTHON_PATH"};

    if ($opt_fixinterp && defined($path) && $path ne "") {
        $source =~ s/^#!.*python.*\n/#!$path\n/
            unless $source =~ @^#!/usr/bin/env python3?$@;
    }

    return $source;
}

sub update_txt_file($$)
{
    my ($source, $date_string) = @_;

    if ($opt_fixdate) {
        $source =~ s/(Last\s+changes:\s+)\d\d\d\d-\d\d-\d\d/$1$date_string/mg;
    }

    return $source;
}

sub update_spec_file($)
{
    my ($source) = @_;

    if ($opt_fixver) {
        die("$0: Missing option --version\n") if (!defined($opt_version));
        die("$0: Missing option --release\n") if (!defined($opt_release));

        $source =~ s/^(Version:\s*)\d+\.\d+.*$/$1$opt_version/mg;
        $source =~ s/^(Release:\s*).*$/$1$opt_release/mg;
    }

    return $source;
}

sub write_version_file($$$)
{
    my ($filename, $version, $release) = @_;
    my $fd;

    die("$0: Missing option --version\n") if (!defined($version));
    die("$0: Missing option --release\n") if (!defined($release));

    open($fd, ">", $filename) or die("Error: cannot write $filename: $!\n");
    print($fd "VERSION=$version\n");
    print($fd "RELEASE=$release\n");
    close($fd);
}

sub guess_filetype($$)
{
    my ($filename, $data) = @_;

    return "exec"
        if ($data =~ /^#!/ ||
            $filename =~ /\.pl$/ ||
            $filename =~ /\.pm$/);
    return "manpage" if ($data =~ /^\.TH/m || $filename =~ /\.\d$/);
    return "spec" if ($data =~ /^%install/m || $filename =~ /\.spec$/);
    return "text" if ($data =~ /^[-=]+/m);

    return "";
}

sub usage()
{
    my ($fd, $do);

    open($fd, "<", $0) || return;
    while (my $line = <$fd>) {
        if (!$do && $line =~ /Usage/) {
            $do = 1;
        }
        if ($do) {
            last if ($line !~ s/^# ?//);
            print($line);
        }
    }
    close($fd);
}

sub main()
{
    my $opt_help;

    if (!GetOptions("help"         => \$opt_help,
                    "manpage"      => \$opt_man,
                    "exec"         => \$opt_exec,
                    "text"         => \$opt_text,
                    "spec"         => \$opt_spec,
                    "verfile=s"    => \$opt_verfile,
                    "version=s"    => \$opt_version,
                    "release=s"    => \$opt_release,
                    "libdir=s"     => \$opt_libdir,
                    "bindir=s"     => \$opt_bindir,
                    "scriptdir=s"  => \$opt_scriptdir,
                    "fixinterp"    => \$opt_fixinterp,
                    "fixdate"      => \$opt_fixdate,
                    "fixver"       => \$opt_fixver,
                    "fixlibdir"    => \$opt_fixlibdir,
                    "fixbindir"    => \$opt_fixbindir,
                    "fixscriptdir" => \$opt_fixscriptdir,
    )) {
        print(STDERR "Use $0 --help to get usage information.\n");
        exit(1);
    }

    if ($opt_help) {
        usage();
        exit(0);
    }

    if (defined($opt_verfile)) {
        print("Updating version file $opt_verfile\n") if ($verbose);
        write_version_file($opt_verfile, $opt_version, $opt_release);
    }

    for my $filename (@ARGV) {
        my ($fd, $source, $original, $guess);
        my @date = get_file_info($filename);

        next if (-d $filename);

        open($fd, "<", $filename) or die("Error: cannot open $filename\n");
        local ($/);
        $source = $original = <$fd>;
        close($fd);

        if (!$opt_man && !$opt_exec && !$opt_text && !$opt_spec) {
            $guess = guess_filetype($filename, $source);
        } else {
            $guess = "";
        }

        if ($opt_man || $guess eq "manpage") {
            print("Updating man page $filename\n") if ($verbose);
            $source = update_man_page($source, $date[0]);
        }
        if ($opt_exec || $guess eq "exec") {
            print("Updating bin tool $filename\n") if ($verbose);
            if ($filename =~ /\.pm$/ || $source =~ /^[^\n]*perl[^\n]*\n/) {
                $source = update_perl($source);
            } elsif ($filename =~ /\.py$/ ||
                     $source =~ /^[^\n]*python[^\n]*\n/) {
                $source = update_python($source);
            }
        }
        if ($opt_text || $guess eq "text") {
            print("Updating text file $filename\n") if ($verbose);
            $source = update_txt_file($source, $date[0]);
        }
        if ($opt_spec || $guess eq "spec") {
            print("Updating spec file $filename\n") if ($verbose);
            $source = update_spec_file($source);
        }

        if ($source ne $original) {
            open($fd, ">", "$filename.new") ||
                die("Error: cannot create $filename.new\n");
            print($fd $source);
            close($fd);

            chmod(oct($date[2]), "$filename.new") or
                die("Error: chmod failed for $filename\n");
            unlink($filename) or
                die("Error: cannot remove $filename\n");
            rename("$filename.new", "$filename") or
                die("Error: cannot rename $filename.new to $filename\n");
        }

        utime($date[1], $date[1], $filename) or
            warn("Warning: cannot update modification time for $filename\n");
    }
}

main();

exit(0);