source: trunk/LATMOS-Accounts/lib/LATMOS/Accounts/Utils.pm @ 984

Last change on this file since 984 was 959, checked in by nanardon, 12 years ago
  • merge all work around forms
  • Property svn:keywords set to Id Rev
File size: 4.6 KB
Line 
1package LATMOS::Accounts::Utils;
2use 5.010000;
3use strict;
4use warnings;
5use Exporter ();
6use vars qw(@ISA @EXPORT_OK @EXPORT);
7use utf8;
8use LATMOS::Accounts::Log;
9use File::Temp qw(tempfile);
10
11our $VERSION = (q$Rev$ =~ /^Rev: (\d+) /)[0];
12
13@ISA = qw(Exporter);
14@EXPORT = qw(to_ascii exec_command switch_user run_via_sudo);
15@EXPORT_OK = qw(to_ascii exec_command switch_user run_via_sudo);
16
17sub to_ascii {
18    my ($text) = @_;
19    return $text unless(defined($text));
20    utf8::decode($text) unless(utf8::is_utf8($text));
21    $text =~ s/œ/oe/g;
22    $text =~ s/Ê/ae/g;
23    $text =~ tr {uàâÀÂÄÀçéÚêëÉÈÊËïîÏÎÞöÎÖÔÌûÛÜć}
24                {uaaaAAAceeeeEEEEiiIIoooOOuuUUc};
25    $text =~ s/([^[:ascii:]])/_/g;
26    $text
27} 
28
29sub exec_command {
30    my ($command, $env) = @_;
31    my $rout = undef;
32    $rout = \$_[2] if(@_ > 2);
33
34    my @exec = ref $command
35        ? (@$command)
36        : ($command);
37    la_log(LA_DEBUG, 'running command `%s\'', join(' ', @exec));
38
39    pipe(my $rh, my $wh);
40    my $pid = fork;
41    if (!defined($pid)) {
42        la_log(LA_ERR, "Can't launch script: cannot fork");
43    } elsif ($pid) {
44        # Father
45        close($wh);
46        my $header;
47        while (<$rh>) {
48            if ($rout) {
49                $$rout .= $_;
50            } else {
51                chomp;
52                if (!$header) {
53                    $header = 1;
54                    la_log(LA_NOTICE, "exec `%s'", join(' ', @exec));
55                }
56                la_log(LA_NOTICE, "output: %s", $_);
57            }
58        }
59        waitpid($pid, 0);
60        if (my $exitstatus = $?) {
61            la_log(LA_ERR, 'command %s exit with status %d',
62                join(' ', @exec), $exitstatus);
63            return;
64        } else {
65            return 1;
66        }
67    } else {
68        # Child
69        close($rh);
70        ( $ENV{LA_MODULE} ) = caller();
71        foreach (keys %{ $env || {} }) {
72            $ENV{"LA_$_"} = $env->{$_};
73        }
74        open(STDOUT, ">&=" . fileno($wh));
75        open(STDERR, ">&=" . fileno($wh));
76        exec(@exec);
77        exit($!);
78    }
79    1
80}
81
82sub parse_obj_file {
83    my ($handle) = @_;
84
85    my %attributes;
86    while (my $line = <$handle>) {
87        $line =~ /^#/ and next;
88        chomp($line);
89        my ($attr, $value) = $line =~ /^\s*(\S+):\s*(.*)\s*$/ or
90            die "Malformed input file\n";
91        $value =~ s/\s*$//;
92        $value =~ s/\\n/\n/g;
93        if ($attributes{$attr}) {
94            if (ref $attributes{$attr}) {
95                push(@{ $attributes{$attr} }, $value);
96            } else {
97                my $temp = $attributes{$attr};
98                $attributes{$attr} = [ $temp, $value ];
99            }
100        } else {
101            $attributes{$attr} = $value eq '' ? undef : $value;
102            # Don't remember why this is here
103            #$attr eq 'exported' && !defined $attributes{$attr} and $attributes{$attr} = 1;
104        }
105    }
106    %attributes
107}
108
109sub dump_read_temp_file {
110    my ($writecb, $readcb) = @_;
111
112    my ($fh, $filename) = tempfile(CLEANUP => 0);
113    $writecb->($fh) or return;
114    $fh = undef; # closing file
115    my $res;
116   
117    my @stat = stat($filename);
118    while (1) {
119        my $cmd = ($ENV{EDITOR} || 'vi') . " $filename";
120        warn "Running $cmd\n";
121        if (system($cmd) == -1 ) {
122            warn "Cannot run editor $!\n";
123            last;
124        }
125        if ((stat($filename))[9] == $stat[9]) {
126            warn "No change existing\n";
127            last;
128        }
129
130        open($fh, '<', $filename) or return;
131        $res = $readcb->($fh);
132        $fh = undef; # closing again
133        $res < 2 and last;
134    }
135    unlink($filename);
136    $res;
137}
138
139sub check_oid_validity {
140    my ($name) = @_;
141    return "leadind space" if ($name =~ /^\s/);
142    return "trailing space" if ($name =~ /\s$/);
143    return "containing space" if ($name =~ /\s/);
144
145    return;
146}
147
148sub check_ug_validity {
149    my ($name) = @_;
150    return "Empty name is not a valid name !"
151        if (!$name);
152    return "first caractere must be a-z"
153        if ($name !~ /^[a-z]/);
154    return "must contain only a-z,0-9"
155        if ($name !~ /^[a-z,0-9,_,-]+$/);
156
157    return check_oid_validity($name);
158}
159
160sub switch_user {
161    my ($runas) = @_;
162
163    if ($< == 0 || $> == 0) {
164        my @info = getpwnam($runas) or do {
165            warn "Can find user $runas";
166            return;
167        };
168        $> = $info[3];
169        return;
170    } else {
171        warn "we are not root";
172    }
173}
174
175sub run_via_sudo {
176    my ($runas) = @_;
177
178    my @info = getpwnam($runas) or do {
179        warn "Can find user $runas";
180        return;
181    }; 
182    if ($< != $info[3]) {
183        exec('sudo', '-u', $runas, $0, @ARGV) or "Can run $!";
184    }
185}
186
1871;
Note: See TracBrowser for help on using the repository browser.