File: FileMod.pm

package info (click to toggle)
systemconfigurator 2.0.10-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 724 kB
  • ctags: 313
  • sloc: perl: 7,423; makefile: 48
file content (181 lines) | stat: -rw-r--r-- 4,921 bytes parent folder | download | duplicates (3)
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
package Util::FileMod;

#   $Id: FileMod.pm,v 1.4 2003/01/19 23:26:21 sdague Exp $

#   Copyright (c) 2001 International Business Machines

#   This program is free software; you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation; either version 2 of the License, or
#   (at your option) any later version.
 
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
 
#   You should have received a copy of the GNU General Public License
#   along with this program; if not, write to the Free Software
#   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

#   Sean Dague <sean@dague.net>

use strict;
use Carp;
use vars qw($VERSION);

$VERSION = sprintf("%d.%02d", q$Revision: 1.4 $ =~ /(\d+)\.(\d+)/);

=head1 NAME

Util::FileMod - A flexible interface for modifying simple flat text config files.

=head1 SYNOPSIS

# for space seperated, unquoted key/value files, like modules.conf

my $rc = new Util::FileMod(Seperator => ' ', Quotes => '', Comment => '#');

$rc->add_vars(
              "alias eth0" => "eepro100",
              "alias eth1" => "pcnet32",
              "alias tr0" => "olympic",
                );

$rc->update_file("/mnt/etc/modules.conf");

# for '=' seperated, double quoted key/value files, like the suse rc.config file
# and most rc files

my $rc = new Util::FileMod(Seperator => '=', Quotes => '"', Comment => '#');

$rc->add_vars(
              IPADDR_0 => "192.168.64.101",
              NETDEV_0 => "eth0",
              IFCONFIG_0 => "192.168.64.101  broadcast 192.168.64.255 netmask 255.255.255.0",
              NETCONFIG => "_0",
             );

$rc->update_file("/mnt/etc/rc.config");

# Note:  Defaults to Seperator => '=', Quotes => '"', Comment => '#'

=cut

sub new {
    my $class = shift;
    my $this = {  
                Seperator => '=',
                Quotes => '"',
                Comment => '#',
                @_,
                filesmod => [],
               };

    bless $this, $class;
}

sub files {
    my $this = shift;
    return @{$this->{filesmod}};
}

sub append_vars {
    my $this = shift;
    my %vars = @_;
    foreach my $key (keys %vars) {
        $this->{VARS}->{$key}->{VALUE} .= $vars{$key};
    }
    return 1;
}

sub add_vars {
    my $this = shift;
    my %vars = @_;
    foreach my $key (keys %vars) {
        $this->{VARS}->{$key}->{VALUE} = $vars{$key};
    }
    return 1;
}

sub update_file {
    my $this = shift;
    my $file = shift;
    
    if(-e $file) {
        system("cp $file $file.scbak") and croak("Couldn't backup $file to $file.scbak");
        open(IN,"<$file.scbak") or croak("Could not open $file.luibak for reading");
        open(OUT,">$file") or croak("Could not open $file for writing");
    
        while(<IN>) {
            my $line = $this->new_line($_);
            print OUT $line;
        }
        close(IN);
    } else {
        open(OUT,">$file") or croak("Could not open $file for writing");
    }

    if (my $extra = $this->extra_lines()) {
        print OUT $extra;
    }
    close(OUT);

    push @{$this->{filesmod}},$file,"$file.scbak";

    return 1;
}

sub extra_lines {
    my $this = shift;
    my $lines;

    my $quotes=$this->{Quotes};
    my $sep = $this->{Seperator};
    my $rem = $this->{Comment}; #hehe, "rem" brings back memories!

    foreach my $key (sort keys %{$this->{VARS}}) {
        if (!$this->{VARS}->{$key}->{DONE}) {
            my $value = $this->{VARS}->{$key}->{VALUE};
            $lines .= "$key$sep$quotes$value$quotes\n";
            $this->{VARS}->{$key}->{DONE} = 1;
        }
    }

    if (defined $lines) {
        $lines = "$rem BEGIN: Lines added by System Configurator\n" . $lines;
        $lines .= "$rem END: Lines added by System Configurator\n";
    }

    return $lines;
}

sub new_line {
    my $this = shift;
    my $line = shift;

    my $quotes=$this->{Quotes};
    my $sep = $this->{Seperator};
    my $rem = $this->{Comment}; #hehe, "rem" brings back memories!

    foreach my $key (sort keys %{$this->{VARS}}) {
        my $value = $this->{VARS}->{$key}->{VALUE};
        $value =~ s/\s+$//;
        my $message = "$rem This line commented out by System Configurator\n";
        my $newline = "$key$sep$quotes$value$quotes\n";
        if($line =~ /^($key($sep)+.*)/) {
            my $oldine = " " . $1 . "\n";
            if($1 eq $newline) {
                $this->{VARS}->{$key}->{DONE} = 1;
                return $line;
            } else {
                $this->{VARS}->{$key}->{DONE} = 1;
                $line = $message . $rem . $oldine . $newline;
                return $line;
            }
        }
    }
    return $line;
}

1;