16310
|
1 |
#!/usr/bin/env perl
|
|
2 |
#
|
|
3 |
# $Id$
|
|
4 |
# Author: Florian Haftmann, TUM
|
|
5 |
#
|
|
6 |
# A generic framework script for simple theory file migrations
|
|
7 |
# (under developement)
|
|
8 |
#
|
|
9 |
|
|
10 |
use strict;
|
|
11 |
use File::Find;
|
|
12 |
use File::Basename;
|
|
13 |
use File::Copy;
|
|
14 |
|
|
15 |
# configuration
|
|
16 |
my @suffices = ('\.thy');
|
|
17 |
my $backupext = '.bak';
|
|
18 |
|
|
19 |
# migrator lookup hash
|
|
20 |
my %migrators = (
|
|
21 |
id => sub {
|
|
22 |
my ($file, @content) = @_;
|
|
23 |
},
|
16311
|
24 |
thyheader => sub {
|
16310
|
25 |
my ($file, @content) = @_;
|
16311
|
26 |
$_ = join("", @content);
|
|
27 |
if (m!\G
|
|
28 |
^
|
|
29 |
theory\s+([^ ]+)
|
|
30 |
(?:\s+(?:imports|=)\s+([^:]*?))?
|
|
31 |
(?:\s+(?:files|uses)\s+([^:]*?))?
|
|
32 |
\s*(?:begin|:)
|
|
33 |
!cgomsx) {
|
|
34 |
# STRATEGIE: abschnittsweises matchen, zwischendurch immer wieder whitespace
|
|
35 |
# verarbeiten
|
|
36 |
}
|
16310
|
37 |
}
|
|
38 |
);
|
|
39 |
|
16311
|
40 |
# utility functions
|
|
41 |
sub skip_wscomment {
|
|
42 |
my $commentlevel = 0;
|
|
43 |
my @skipped = ();
|
|
44 |
while () {
|
|
45 |
if (m!\G\{\*!cgoms) {
|
|
46 |
push(@skipped, $&);
|
|
47 |
$commentlevel++;
|
|
48 |
} elsif ($commentlevel > 0) {
|
|
49 |
if (m!\G\*\}!cgoms) {
|
|
50 |
push(@skipped, $&);
|
|
51 |
$commentlevel--;
|
|
52 |
} elsif (m!\G(?:
|
|
53 |
[^{*]|\*[^{}]|\{[^*]
|
|
54 |
)*!cgomsx) {
|
|
55 |
push(@skipped, $&);
|
|
56 |
} else {
|
|
57 |
die ("probably incorrectly nested comment");
|
|
58 |
}
|
|
59 |
} elsif (m!\G\s+!cgoms) {
|
|
60 |
push(@skipped, $&);
|
|
61 |
} else {
|
|
62 |
return join('', @skipped);
|
|
63 |
}
|
|
64 |
}
|
|
65 |
}
|
|
66 |
|
16310
|
67 |
# process dir tree
|
|
68 |
sub process_tree {
|
|
69 |
my ($root, $migrator, $backupext) = @_;
|
|
70 |
find(sub { process($File::Find::name, $migrator, $backupext) }, $root);
|
|
71 |
}
|
|
72 |
|
|
73 |
# process single file
|
|
74 |
sub process {
|
|
75 |
my ($file, $migrator, $backupext) = @_;
|
|
76 |
my ($basename, $dirname, $ext) = fileparse($file, @suffices);
|
|
77 |
#~ print "$file\n";
|
|
78 |
if ($ext) {
|
|
79 |
open ISTREAM, $file or die("error opening $file");
|
|
80 |
my @content = <ISTREAM>;
|
|
81 |
close ISTREAM;
|
|
82 |
if ($backupext) {
|
|
83 |
copy($file, "$file$backupext");
|
|
84 |
}
|
|
85 |
print "Migrating $file...\n";
|
|
86 |
&$migrator($file, @content);
|
|
87 |
}
|
|
88 |
}
|
|
89 |
|
|
90 |
# first argument: migrator name
|
|
91 |
my $migrator = $migrators{shift @ARGV};
|
|
92 |
$migrator or die ("invalid migrator name");
|
|
93 |
# other arguments: files or trees
|
|
94 |
foreach my $fileloc (@ARGV) {
|
|
95 |
-e $fileloc or die ("no file $fileloc");
|
|
96 |
}
|
|
97 |
foreach my $fileloc (@ARGV) {
|
|
98 |
if (-d $fileloc) {
|
|
99 |
process_tree($fileloc, $migrator, $backupext);
|
|
100 |
} else {
|
|
101 |
process($fileloc, $migrator, $backupext);
|
|
102 |
}
|
|
103 |
}
|