Mercurial > forge
annotate admin/find_changes @ 12669:1c92b4b26ced octave-forge
releasePKG.m: exclude .hg* files of export as of bug #45669
author | jpicarbajal |
---|---|
date | Fri, 31 Jul 2015 19:49:28 +0000 |
parents | f82660cf7dee |
children |
rev | line source |
---|---|
594 | 1 #!/usr/bin/perl -w |
2 | |
3 =head1 NAME find_changes.pl - Find patterns in text files and propose changes. | |
4 | |
5 This script passes lines of text through a perl function with the aim of | |
595 | 6 reporting the occurence of patterns and proposing changes to be |
7 done. L<patch> should be used to actually modify the original files. | |
594 | 8 |
9 =head1 SYNOPSIS | |
10 | |
595 | 11 find_changes.pl [options] changes_def file(s).txt ... E<gt> changes_patch |
594 | 12 |
13 Reads and executes the perl F<changes_def> file, which must contain the | |
14 definition of a function called C<change_line()>, and then reads the | |
15 F<file(s).txt> line by line. | |
16 | |
17 For each line of each file, the function F<change_line()> is called with the | |
18 line as single argument, which can be modified inplace. | |
19 | |
595 | 20 If any change is done to the line, it is reported by printing to stdout a |
21 universal diff patch. | |
594 | 22 |
23 =head2 Modifying files | |
24 | |
595 | 25 It is then recommended to inspect and edit the file F<changes_patch>, |
594 | 26 removing lines that should not be changed. Once this is done, the changes |
595 | 27 can be done in the original files by using C<patch -p0 < changes_patch>. |
594 | 28 |
29 =head2 Commenting on lines | |
30 | |
31 If a function C<comment_line()> is present, each line will be passed to it | |
595 | 32 and its output will be shown on standard error. |
594 | 33 |
34 =head1 OPTIONS | |
35 | |
36 =over 4 | |
37 | |
38 =item -p, --patch | |
39 | |
595 | 40 Output a patch (default). |
41 | |
42 =item -d | |
43 | |
44 Use another output format, adapted to some F<do_changes.pl> script. | |
594 | 45 |
46 =item -v, --verbose | |
47 | |
48 Print some info on stderr during execution. | |
49 | |
50 =item -h, --help | |
51 | |
52 Print a help message and exit. | |
53 | |
54 =head1 COPYRIGHT | |
55 | |
56 This file is distributed under the terms of the GNU General Public Licence. | |
57 | |
10050
f82660cf7dee
maint: update e-mail address of Etienne Grossmann to <etienne@egdn.net> as requested by himself
carandraug
parents:
596
diff
changeset
|
58 =head1 AUTHOR Etienne Grossmann E<lt>etienne@egdn.netE<gt> |
594 | 59 |
60 =cut | |
61 | |
62 ### | |
63 sub help {system ("perldoc $0|cat")} | |
64 | |
595 | 65 $patch = 1; |
594 | 66 $verbose = 0; |
67 # Read options | |
68 | |
69 while (@ARGV && ($ARGV[0] =~ /^\-/)) { | |
70 if ($ARGV[0] =~ /^\-(h|\-help)$/){ help(); exit 0;} | |
71 elsif ($ARGV[0] =~ /^\-(v|\-verbose)$/){ $verbose = 1;} | |
72 elsif ($ARGV[0] =~ /^\-(p|\-patch)$/) { | |
73 $patch = 1; | |
596 | 74 } elsif ($ARGV[0] =~ /^\-(d)$/) { |
595 | 75 $patch = 0; |
594 | 76 } else { |
77 die "Unknown option '$ARGV[0]'. Try '$0 --help'"; | |
78 } | |
79 | |
80 shift @ARGV; | |
81 } | |
82 | |
83 die "No definition file specified" unless @ARGV; | |
84 | |
85 $dfile = shift @ARGV; | |
86 | |
87 # Read and compile definition file | |
88 print STDERR "Definition file is $dfile\n" if $verbose; | |
89 | |
90 if (!defined (do ($dfile))) { | |
91 if ($!) { | |
92 print STDERR "Can't find $dfile : '$!'\n"; | |
93 exit 1; | |
94 } elsif ($@) { | |
95 print STDERR "Can't compile $dfile : '$@'\n"; | |
96 exit 1; | |
97 } else { | |
98 print STDERR "Problem with $dfile : do returns undefined\n"; | |
99 exit 1; | |
100 } | |
101 } | |
102 | |
103 if (!defined (&change_line)) { | |
104 print STDERR "File '$dfile' does not define a function 'change_line()'\n"; | |
105 exit 1; | |
106 } | |
107 | |
108 $has_comment = defined (&comment_line); | |
109 | |
110 foreach my $f (@ARGV) { | |
111 | |
112 if (!$patch) { | |
113 unless (open FF, "<$f") { | |
114 print "## Whoa!!! Can't open file '$f'. Skipping"; | |
115 next; | |
116 } | |
117 print STDERR "Scanning file $f\n" if $verbose; | |
118 | |
119 | |
120 local $lnum = 0; | |
121 while (! eof (FF)) { | |
122 | |
123 my $line = <FF>; # Read a line | |
124 my $olin = $line; | |
125 $lnum++; | |
126 | |
127 if ($has_comment) { | |
128 if ($comment = &comment_line ($line)) { | |
129 $comment = "## $comment" if $comment !~ /^\s*\#/; | |
130 $comment .= "\n" if $comment !~ /\n$/; | |
131 print "$f:$.:$comment"; | |
132 } | |
133 } | |
134 | |
135 &change_line ($line); | |
136 if ($olin ne $line) { | |
137 print "$f:$.:$olin"; | |
138 } | |
139 } | |
140 close FF; | |
141 } else { # Do a patch. Using diff, what else? | |
142 my $nf = "$f.new"; | |
143 my $i = 1; | |
144 while (-e $nf) { # Find a name for the temp file. | |
145 $nf = $f . ".new." . $i ; | |
146 } | |
147 unless (open FF, "<$f") { | |
148 print STDERR "## Whoa!!! Can't open file '$f'. Skipping\n"; | |
149 } | |
150 unless (open GG, ">$nf") { | |
151 print STDERR "## Whoa!!! Can't open temp file '$nf'. Skipping\n"; | |
152 next; | |
153 } | |
154 print STDERR "## Scanning file $f\n" if $verbose; | |
155 | |
156 while (! eof (FF)) { # Create (temp) transformed file | |
157 | |
158 my $line = <FF>; # Read a line | |
159 | |
160 &change_line ($line); | |
161 print GG $line; | |
162 } | |
163 close FF; close GG; | |
164 # Call diff and modify slightly the output | |
165 my $tmp = `diff -u $f $nf`; | |
166 $tmp =~ s{^\+\+\+ $nf}{+++ $f}m; | |
167 print $tmp; | |
168 | |
169 unlink($nf) or print STDERR "## Whoa!!! Can't remove temp file '$nf'\n"; | |
170 } | |
171 } | |
172 |