summaryrefslogtreecommitdiff
blob: 1e7e92a853a306d60b356b8e4df2fb909effb7c8 (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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#!/usr/bin/env perl

eval 'echo "Called with something not perl"' && exit 1    # Non-Perl protection.
  if 0;

use 5.12.2;
use strict;
use warnings;

use FindBin;
use lib "$FindBin::Bin/lib";
use env::gentoo::perl_experimental;
use optparse;
use utf8;
use Data::Dump qw( pp );
use Gentoo::Overlay;

# FILENAME: aggregate_tree.pl
# CREATED: 29/02/12 07:37:54 by Kent Fredric (kentnl) <kentfredric@gmail.com>
# ABSTRACT: Connect all the cpan id's from the metadata.xml

use XML::Smart;

my ( $env, $packages, $cat );
main();

sub main {
  $env = env::gentoo::perl_experimental->new();
  my $opts = optparse->new(
    argv => \@ARGV,
    help => sub { print <DATA>; return },
  );
  my $tree;

  if ( $opts->long_opts->{'from-ini'} ) {
    require Gentoo::Overlay::Group::INI;
    $tree = Gentoo::Overlay::Group::INI->load_named('aggregate_tree')->overlay_group;
  }
  else {
    require Gentoo::Overlay::Group;
    $tree = Gentoo::Overlay::Group->new();
    $tree->add_overlay( set_root( $opts->long_opts->{root} ));
  }

  $packages = {};

  my $dest = open_output( $opts->long_opts->{output} );

  $|++;
  $tree->iterate(
    'packages' => \&handle_package
  );

  $dest->print( make_format( $opts->long_opts->{format} ) );

}

sub set_root {
  my ($root) = @_;
  return $env->root unless defined $root;
  require Path::Class::Dir;
  return Path::Class::Dir->new($root);
}

sub open_output {
  my ($output) = @_;
  return \*STDOUT if not defined $output;
  return \*STDOUT if $output eq '-';
  require Path::Class::File;
  my $file = Path::Class::File->new($output)->absolute();
  return $file->openw( iomode => ':utf8' );
}

sub make_format {
  my ($format) = @_;
  $format ||= 'JSON';
  if ( $format eq 'JSON' ) {
    goto &make_format_json;
  }
  if ( $format eq 'distlist' ) {
    goto &make_format_distlist;
  }
  die "Unknown format type " . $format;
}

sub make_format_json {
  require JSON;
  my $encoder = JSON->new()->pretty->utf8->canonical;
  return $encoder->encode($packages);
}

sub make_format_distlist {
  return join qq{\n}, keys %{$packages};
}

sub handle_package {
  my ( $self, $c ) = @_;
  my $CP      = $c->{category_name} . '/' . $c->{package_name};
  my $xmlfile = $c->{package}->path->file('metadata.xml');
  if ( not -e $xmlfile ) {
    warn "\e[31mNo metadata.xml for $CP\e[0m\n";
    return;
  }
  if ( not $cat or $c->{category_name} ne $cat ) {
    *STDERR->print( "\nProcessing " . $c->{category_name} . " :" );
    $cat = $c->{category_name};
  }
  *STDERR->print(".");
  my $XML = XML::Smart->new( $xmlfile->absolute()->stringify() );
  if ( not exists $XML->{pkgmetadata} ) {
    warn "\e[31m<pkgmetadata> missing in $xmlfile\e[0m\n";
    return;
  }
  if ( not exists $XML->{pkgmetadata}->{upstream} ) {

    # warn "<pkgmetadata>/<upstream> missing in $xmlfile\n";
    return;
  }
  if ( not exists $XML->{pkgmetadata}->{upstream}->{'remote-id'} ) {

    # warn "<pkgmetadata>/<upstream>/<remote-id> missing in $xmlfile\n";
    return;
  }
  for my $remote ( @{ $XML->{pkgmetadata}->{upstream}->{'remote-id'} } ) {

    next if not exists $remote->{type};
    next unless $remote->{type} eq 'cpan';

    my $upstream = $remote->content();

    if ( not defined $packages->{$upstream} ) {
      $packages->{$upstream} = [];
    }
    my $versions = [];
    my $record   = {
      category        => $c->{category_name},
      package         => $c->{package_name},
      repository      => $c->{overlay_name},
      versions_gentoo => $versions,
    };
    $c->{package}->iterate(
      ebuilds => sub {
        my ( $self, $d ) = @_;
        my $version = $d->{ebuild_name};
        my $p       = $c->{package_name};
        $version =~ s/\.ebuild$//;
        $version =~ s/^\Q${p}\E-//;
        push @{$versions}, $version;
      }
    );
    push @{ $packages->{$upstream} }, $record;

    *STDERR->print("\e[32m $CP -> $upstream\e[0m ");
  }

}
0;

__DATA__

This script scrapes the perl repository and finds all the metadata.xml files
  and makes a mapping file connecting categories to upstream dists.

Usage:

  aggregate_tree.pl

  By default uses the perl-experimental overlay as a working dir, and emits JSON to stdout

  aggregate_tree.pl 
  
      --root="/path/to/some/root"

      Specifiy another root to scan ( ie: /usr/portage )
  
      --format=JSON     # Emit JSON  ( Default )
      --format=distlist # Emit a list of CPAN Dist Names

      --output=-                # Write to standard output ( Default ) 
      --output="/path/to/file"  # Write to the specified file