summaryrefslogtreecommitdiffstats
path: root/src/bin/devel-tools/generateSettings.pl
blob: b0e1b447c343bf45b0ff8d2dacecb05c8169eb88 (plain) (blame)
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#! /usr/bin/perl
# -----------------------------------------------------------------------------
# Copyright (c) 2006 - 2009 - OpenSLX GmbH
#
# This program is free software distributed under the GPL version 2.
# See http://openslx.org/COPYING
#
# If you have any feedback please consult http://openslx.org/feedback and
# send your suggestions, praise, or complaints to feedback@openslx.org
#
# General information about OpenSLX can be found at http://openslx.org/
# -----------------------------------------------------------------------------
use strict;
use warnings;
use Data::Dumper;
my $abstract = q[
determineMinimumPackageSet.pl
    This script is a tool for OpenSLX developers that is meant to generate a
    packageset for settings.default. It can be used to generate
    bootstrap-packages for example.
];

use Getopt::Long;
use Pod::Usage;

my (
    $helpReq,
    $verbose,
    $versionReq,
    $inputfile,
    $outputfile,
    $url,
    $errorfile,

    @Pkgs,
    @files,
    @GivenNames,
    @filelisting,
    @errors,
);

$errorfile="/tmp/genSettings.err";
GetOptions(
    'help|?' => \$helpReq,
    'verbose' => \$verbose,
    'version' => \$versionReq,
    'if=s' => \$inputfile,
    'of=s' => \$outputfile,
    'url=s' => \$url,
) or pod2usage(2);
pod2usage(-msg => $abstract, -verbose => 0, -exitval => 1) if $helpReq;
if ($versionReq) {
    system('slxversion');
    exit 1;
}
	
open(INPUTFILE,$inputfile) || die("Can't open input-file $inputfile!");
    while (my $zeile=<INPUTFILE>){
        if ($zeile ne "") {
            push (@GivenNames,$zeile);
        }
    }
close(INPUTFILE);
	
print "getting filelisting:\n" if $verbose;
if (substr($url,0,3) eq "ftp") {
    print "\trecognized mirror as ftp - $url\n" if $verbose;
    @filelisting=_getPackageListingFtp($url);
} elsif (substr($url,0,4) eq "http") {
    print "\trecognized mirror as http - $url\n" if $verbose;
    @filelisting=_getPackageListingHttp($url);
} else {
    die "Unable to get mirror type (ftp or http)";
}
print "\tgot file listing from $url\n" if $verbose;
print "resolving names:\n" if $verbose;
foreach my $name (@GivenNames) {
    $name=~ s/^[\s\t]+//;  #removes whitespaces
    $name=~ s/[\n\t\r]$//; #removes new lines
    my @possiblepackages = grep(/^\Q$name\E*/i,@filelisting);
    my $res;
    if ($possiblepackages[0]) {
        $res = $possiblepackages[0];
    } else {
        push (@errors,$name);
    }
    print "\t$name->$res\n" if $verbose;
    push (@Pkgs,$res) if $res;
}
open (OUTPUTFILE,">>$outputfile") || die("Can't open output-file $outputfile!");
foreach my $package (@Pkgs) {
    print OUTPUTFILE "$package\n";		
}
close (OUTPUTFILE);
open (ERRORFILE,">>$errorfile") || die("Can't open output-file $errorfile!");
    foreach my $error (@errors) {
    print ERRORFILE "$error\n";		
}
close (ERRORFILE);
print "\n";

if ($verbose) {
    print "THE PACKAGE LIST:\n";
    print(('=' x 40)."\n");
    print join("\n", sort @Pkgs)."\n";
}

exit;


sub _getPackageListingFtp {
	my $url = shift;
    use Net::FTP;
    use URI;
    require URI::_generic;
    
    my $urlObject = URI->new($url);
    my $path = shift;
	my $ftp = Net::FTP->new($urlObject->host( ), Timeout => 240)
	  or die _tr("Unable to connect to FTP-Server");
    $ftp->login("anonymous", "mailaddress@");
    $ftp->cwd($urlObject->path( ));
    return $ftp->ls();    
}

sub _getPackageListingHttp {
    my $url = shift;
    use URI;
    use URI::http;
    use URI::_foreign;
    use HTTP::Request;
    use LWP::UserAgent;
    use LWP::Protocol::http;
    
    my @filelisting;
    my $ua = LWP::UserAgent->new;
    $ua->agent("Mozilla/5.0 (Windows; U; Windows NT 5.1; de; rv:1.8) Gecko/20051111 Firefox/1.5");
    my $req;
    $req = HTTP::Request->new(GET => $url);
    $req->header('Accept' => 'text/html');

    # send request
    my $res = $ua->request($req);
    # check the outcome
    if ($res->is_success) {
	print "\tThe given URL is : $url\n"       	if $verbose;
        @filelisting = ($res->decoded_content =~ m/<a href=\"([^\"]*.rpm)/g);
        foreach my $i (@filelisting){
        	print $i."\n"				if $verbose;
        }
	print "\tgot list of files from mirror.\n"	if $verbose;
        return @filelisting;
    }
    die("Error: " . $res->status_line . "\n");
}


__END__

=head1 NAME

generateSettings.pl - OpenSLX script to extract full package names
from a given mirror.

=head1 SYNOPSIS

generateSettings.pl [options]

  Options:
      --if                     inputfile
      --of                     outputfile
      --url                    url of the mirror
      --help                   brief help message
      --verbose                show files as they are being processed
      --version                show version

=head1 OPTIONS

=over 8

=item B<--if>

Select input file with package names in each line.

=item B<--of>

Select output file for complete package names to append.

=item B<--url>

Select a mirror directory for the desired distribution

=item B<--help>

Prints a brief help message and exits.

=item B<--verbose>

Prints information about each installed package as it is being processed.

=item B<--version>

Prints the version and exits.

=back

=cut