mizzy (owner)

Forks

Revisions

gist: 59314 Download_button fork
public
Public Clone URL: git://gist.github.com/59314.git
Embed All Files: show embed
cpanflute2_wrapper.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
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
#!/usr/bin/perl
 
use strict;
use warnings;
use Getopt::Long;
use LWP::Simple;
use File::Temp qw(tempdir);
use Archive::Tar;
use YAML;
use IPC::Run3;
 
my @doc_modules = qw( Catalyst::Manual );
 
my %use_module_mapping = (
    'Module::Install' => 'inc::Module::Install',
);
 
GetOptions(
    \my %options, "outdir=s", "tmpdir=s", "email=s", "name|n=s", "create",
    "test|t", "epoch|e=n", "version|v=s", "release|r=s", "patch=s@",
    "noarch", "arch|a=s", "noperlreqs", "usage", "buildall|b", "installdirs=s",
    "descfile=s", "help", "requires=s@", "buildrequires=s@", "sign", "post=s",
    "pre=s", "preun=s", "postun=s", "just-spec|j", 'use-usr-local', 'packager',
);
 
my $module = shift;
 
my $log_level = $options{'log-level'} || 'info';
my $tmpdir = tempdir( CLEANUP => 1, DIR => $options{tmpdir} || '/tmp' );
 
my @modules_to_build = ( { name => $module, version => $options{version} } );
 
delete $options{version};
 
while ( my $mod = shift @modules_to_build ) {
    build_module( $mod->{name}, $mod->{version} );
}
 
sub build_module {
    my ( $module, $version ) = @_;
 
    my $tarball = get_file_from_cpan_web($module, $version);
    return unless $tarball;
 
    my $resolved = resolve_dependencies($tarball);
 
    if ( $resolved ) {
        my $opts = join ' ', map {
            do {
                if ( $options{$_} == 1 ) {
                    "--$_";
                }
                else {
                    "--$_=$options{$_}"
                }
            }
        } keys %options;
 
        print_log( info => "cpanflute2 $opts $tmpdir/$tarball" );
        system "cpanflute2 $opts $tmpdir/$tarball";
        unless ( $? ) {
            my $package = get_package_name($module);
            $package = "perl-$package";
            my @rpms = glob("$package*.rpm");
            for my $rpm ( @rpms ) {
                next if $rpm =~ /src\.rpm/;
                `sudo rpm -Uvh $rpm`;
            }
        }
   }
    else {
        push @modules_to_build,{ name => $module, version => $version };
    }
}
 
sub get_file_from_cpan_web {
    my ( $module, $version ) = @_;
 
    my $tarball_url = get_tarball_url($module, $version);
 
    my ($loc, $file) = $tarball_url =~ m|(.*)/(.*)|;
    print_log( info => "Getting $file ...");
    mirror($tarball_url, "$tmpdir/$file");
 
    return $file;
}
 
sub get_tarball_url {
    my ( $module, $version ) = @_;
 
    my $base = "http://search.cpan.org";
    my $url = "$base/dist/$module";
    $url =~ s/::/-/g;
    $url .= "-$version" if $version;
 
    local $_ = LWP::Simple::get($url) || return;
    m% \<a[^<>]* # Begin Anchor tag
       href\s*=\s* # href parameter
       (['"]?) # Maybe quote
([^<>\s"']*) # Extract link as $2
       \1 # Maybe quote
       [^<>]*\> # End Anchor tag
       \s*Download # of the "Download" link
     %ix; # case insensitive HTML
 
    my $tarball_url = "$base$2" if $2;
 
    unless ( $2 ) {
        my $package = get_package_name($module);
        $tarball_url = get_tarball_url($package);
    }
 
    die "$module not found on CPAN web site!"
        unless $tarball_url;
 
    return $tarball_url;
}
 
sub resolve_dependencies {
    my $tarball = shift;
 
    my @files = Archive::Tar->list_archive("$tmpdir/$tarball");
    my $use_module_build = 1 if grep { /Build\.PL$/ } @files;
 
    my $resolved = 1;
    my %prefixes;
    foreach (@files) {
        my @path_components = split m[/], $_;
        $prefixes{$path_components[0]}++;
        if ($path_components[-1] eq 'META.yml') {
            my $tar = new Archive::Tar;
            $tar->read("$tmpdir/$tarball", 1);
            my $contents = $tar->get_content($_);
            my $yaml = YAML::Load($contents);
 
            while (my ($mod, $ver) = each %{$yaml->{build_requires}}) {
                next if $mod eq 'perl';
                unless ( install_package($mod, $ver) ) {
                    push @modules_to_build, { name => $mod, version => 0 };
                    $resolved = 0;
                }
            }
 
            while (my ($mod, $ver) = each %{$yaml->{requires}}) {
                next if $mod eq 'perl';
                unless ( install_package($mod, $ver) ) {
                    push @modules_to_build, { name => $mod, version => 0 };
                    $resolved = 0;
                }
            }
            while (my ($mod, $ver) = each %{$yaml->{recommends}}) {
                next if $mod eq 'perl';
                unless ( install_package($mod, $ver) ) {
                    push @modules_to_build, { name => $mod, version => 0 };
                    $resolved = 0;
                }
            }
        }
    }
 
    return $resolved;
}
 
sub install_package {
    my ( $module, $version ) = @_;
 
    $module = $use_module_mapping{$module} if $use_module_mapping{$module};
 
    # モジュールが存在するかチェック
    if ( grep { $module eq $_ } @doc_modules ) {
        `perldoc -l $module`;
        return 1 unless $?;
    }
    else {
        print_log( info => "Checking $module $version'" );
        my $check = check_module_and_version( $module, $version );
        # 存在するなら return
        return 1 if $check;
    }
 
    print_log( info => "Required module $module $version not found" );
 
    my $package = get_package_name($module);
    $package = "perl-$package";
 
    # パッケージが存在するかチェック
    `rpm -q $package`;
 
    # 存在する場合は yum update してみる
    unless ( $? ) {
        print_log( info => "Trying to update a required package $package ...");
        run3("sudo yum update -y $package", \my $in, \my $out, \my $err);
        print_log( 'debug' => $out );
 
        if ( $out =~ /no packages marked for update/i ) {
            # update できなかったら 0 で treturn
            print_log( error => "Failed to update $package");
            return 0;
        }
        else {
            # もう一度モジュール存在チェック
            print_log( info => "Succeeded to update $package");
            my $check = check_module_and_version( $module, $version );
            return 1 if $check;
        }
    }
    else {
        # 存在しなければパッケージインストールしてみる
        print_log( info => "Trying to install a required package $package ...");
        run3("sudo yum install -y $package", \my $in, \my $out, \my $err);
        print_log( 'debug' => $out );
        if ( $out =~ /Nothing to do/ ) {
            print_log( error => "Failed to install $package");
            return 0;
        }
        else {
            print_log( info => "Succeeded to install $package");
            return 1;
        }
    }
}
 
sub get_package_name {
    my $module = shift;
 
    my $content = get("http://search.cpan.org/search?query=$module") || return;
    my ( $package ) = ( $content =~ m!href="/~[^/]+/([^/]+)/"! );
    $package =~ s/-[^-]+$//;
    return $package;
}
 
# ログ表示用のルーチン
sub print_log {
    my( $level, $msg ) = @_;
    return unless should_log($level);
    chomp($msg);
    warn "[$level] $msg\n";
}
 
sub should_log {
    my $level = shift;
 
    my %levels = (
        debug => 0,
        warn => 1,
        info => 2,
        error => 3,
    );
 
    $levels{$level} >= $levels{$log_level};
}
 
sub check_module_and_version {
    # code from ExtUtils::MakeMaker
    my ( $module, $version ) = @_;
 
    my $file = "${module}.pm";
    $file =~ s{::}{/}g;
    eval { require $file };
 
    my $pr_version = $module->VERSION || 0;
 
    # convert X.Y_Z alpha version #s to X.YZ for easier comparisons
    $pr_version =~ s/(\d+)\.(\d+)_(\d+)/$1.$2$3/;
 
    if ( $@ or $pr_version < $version ) {
        return 0;
    }
 
    return 1;
}