forked from webmin/webmin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextract-module-info-langs.pl
executable file
·78 lines (72 loc) · 1.83 KB
/
extract-module-info-langs.pl
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
#!/usr/local/bin/perl
# For each given module.info file, create new module.info.XX files for each
# desc_XX= line in the original
if ($ARGV[0] eq "--dry-run") {
$dryrun = 1;
shift(@ARGV);
}
@files = @ARGV;
@files || die "usage: $0 [--dry-run] [module.info]+";
foreach my $f (@files) {
my %minfo;
$f =~ /^(\S+)\/([^\/]+)$/ || die "$f is not a full path";
my ($dir, $name) = ($1, $2);
&read_file($f, \%minfo) || die "failed to read $f : $!";
my @keys = keys %minfo;
my %extract;
foreach my $k (@keys) {
if ($k =~ /^(desc|longdesc)_(\S+)/) {
if (!$extract{$2}) {
$extract{$2} ||= { };
&read_file($f.".".$2, $extract{$2});
}
$extract{$2}->{$k} = $minfo{$k};
delete($minfo{$k});
}
}
if (%extract) {
# Write out new and old files
if ($dryrun) {
print STDERR "$f : create separate files for ",
join(" ", sort(keys %extract)),"\n";
}
else {
&write_file($f, \%minfo);
system("cd $dir && git add $name");
foreach my $l (keys %extract) {
&write_file($f.".".$l, $extract{$l});
system("cd $dir && git add $name.$l");
}
}
}
}
# read_file(file, &assoc, [&order], [lowercase])
# Fill an associative array with name=value pairs from a file
sub read_file
{
open(ARFILE, $_[0]) || return 0;
while(<ARFILE>) {
s/\r|\n//g;
if (!/^#/ && /^([^=]+)=(.*)$/) {
$_[1]->{$_[3] ? lc($1) : $1} = $2;
push(@{$_[2]}, $1) if ($_[2]);
}
}
close(ARFILE);
return 1;
}
# write_file(file, array)
# Write out the contents of an associative array as name=value lines
sub write_file
{
local(%old, @order);
&read_file($_[0], \%old, \@order);
open(ARFILE, ">$_[0]");
foreach $k (@order) {
print ARFILE $k,"=",$_[1]->{$k},"\n" if (exists($_[1]->{$k}));
}
foreach $k (keys %{$_[1]}) {
print ARFILE $k,"=",$_[1]->{$k},"\n" if (!exists($old{$k}));
}
close(ARFILE);
}