aboutsummaryrefslogtreecommitdiffstats
path: root/lib/Iurt/Config.pm
blob: 3b8bbe10466dc8b881eba5e0130a782d5bc4dc4a (plain)
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
package Iurt::Config;

use base qw(Exporter);
use Data::Dumper;
use MDK::Common;
use Iurt::Util qw(plog);
use strict;
use Sys::Hostname;
use File::lockf;

our @EXPORT = qw(
    config_usage
    config_init
    get_date
    get_maint
    get_date
    get_prefix
    get_author_email
    get_package_prefix
    get_mandatory_arch
    get_target_arch
    %arch_comp
);

our %arch_comp = (
    'i586' => { 'i386' => 1, 'i486' => 1 },
    'i686' => { 'i386' => 1, 'i486' => 1, 'i586' => 1 },
    'ppc64' => { 'ppc' => 1 },
    'armv5tejl' => { 'armv5tl' => 1 },
    'armv5tel' => { 'armv5tl' => 1 },
    'armv7l' => { 'armv5tl' => 1, 'armv7hnl' => 1 },
    'armv8l' => { 'armv5tl' => 1, 'armv7hl' => 1, 'armv7hnl' => 1 },
);


=head2 config_usage($config_usage, $config)

Create an instance of a class at runtime.
I<$config_usage> is the configuration help,
I<%config> is the current configuration values
Return true.

=cut

sub config_usage {
    my ($config_usage, $config) = @_;
	print "\nIurt configuration keywords:\n\n";
    $Data::Dumper::Indent = 0;
    $Data::Dumper::Terse = 1;
    foreach my $k (sort keys %$config_usage) {
	print "  $k: $config_usage->{$k}{desc}\n\t\tdefault: ",
	       Data::Dumper->Dump([ $config_usage->{$k}{default} ]),
	       ", current: ", Data::Dumper->Dump([ $config->{$k} ]), "\n";
    }
    print "\n\n";
}

=head2 config_init($config_usage, $config, $run)

Create an instance of a class at runtime.
I<$config_usage> is the configuration help,
I<%config> is the current configuration values
I<%run> is the current running options
Return true.

=cut

sub config_init {
    my ($config_usage, $config, $run) = @_;
    
    foreach my $k (keys %$config_usage) {
	ref $config_usage->{$k}{default} eq 'CODE' and next;
	if (defined($run->{config}{$k})) {
	    $config->{$k} = $run->{config}{$k};
        } elsif (!defined($config->{$k})) {
	    $config->{$k} = $config_usage->{$k}{default};
	}
    }
    # warly 20061107
    # we need to have all the default initialised before calling functions, so this
    # cannot be done in the same loop
    foreach my $k (keys %$config_usage) {
	ref $config_usage->{$k}{default} eq 'CODE' or next;
	my $a = $config_usage->{$k}{default}($config, $run);
	if (defined($run->{config}{$k})) {
	    $config->{$k} = $run->{config}{$k};
        } elsif (!defined($config->{$k})) {
	    $config->{$k} = $a;
	}
    }
}


=head2 get_date($shift)

Create a string based on the current date and time
I<$shift> number of second to shift the date
Return date-time and date

=cut

sub get_date {
    my ($o_shift) = @_;
    my ($sec, $min, $hour, $mday, $mon, $year) = gmtime(time() - $o_shift);
    $year += 1900;
    my $fulldate = sprintf "%4d%02d%02d%02d%02d%02d", $year, $mon+1, $mday, $hour, $min, $sec;
    my $daydate = sprintf "%4d%02d%02d", $year, $mon+1, $mday;
    $fulldate, $daydate;
}

sub get_prefix {
    my ($luser) = @_;
    my $hostname = hostname();
    my ($fulldate) = get_date();
    my ($host) = $hostname =~ /([^.]*)/;
    join('.', $fulldate, $luser, $host, $$) . '_';
}

sub get_package_prefix {
    my ($rpm) = @_;
    my ($prefix1) = $rpm =~ /^(\d{14}\.\w+\.\w+\.\d+)_/;
    my ($prefix2) = $rpm =~ /^(\@\d+:)/;
    "$prefix1$prefix2";
}

sub get_maint {
    my ($run, $srpm) = @_;
    my ($srpm_name) = $srpm =~ /(.*)-[^-]+-[^-]+\.[^.]+$/;
    $srpm_name ||= $srpm;
    if ($run->{maint}{$srpm}) {
	return $run->{maint}{$srpm}, $srpm_name;
    }
    my $maint = `GET 'http://maintdb.mageia.org/$srpm_name'`;
    if ($?) {
	return 'NOT_FOUND';
    }
    chomp $maint;
    $run->{maint}{$srpm} = $maint;
    $maint, $srpm_name;
}

sub get_author_email {
    my ($user) = @_;
    my $authoremail = $user . ' <' . $user . '>';

    return $authoremail;
}

sub get_mandatory_arch {
    my ($config, $target) = @_;
    find { ref($_) eq 'ARRAY' } $config->{mandatory_arch},
	(ref($config->{mandatory_arch}) eq 'HASH' ? ($config->{mandatory_arch}{$target}, $config->{mandatory_arch}{default}) : ()), [];
}

sub get_target_arch {
    my ($config, $target) = @_;
    find { ref($_) eq 'ARRAY' } $config->{arch},
	(ref($config->{arch}) eq 'HASH' ? ($config->{arch}{$target}, $config->{arch}{default}) : ()), [ keys %{$config->{bot}} ];
}

1;