1 |
commit: 77d2781c0c39aefb6411714dbd374a0640b60191 |
2 |
Author: Kent Fredric <kentfredric <AT> gmail <DOT> com> |
3 |
AuthorDate: Tue Feb 28 21:48:32 2012 +0000 |
4 |
Commit: Kent Fredric <kentfredric <AT> gmail <DOT> com> |
5 |
CommitDate: Tue Feb 28 21:48:32 2012 +0000 |
6 |
URL: http://git.overlays.gentoo.org/gitweb/?p=proj/perl-overlay.git;a=commit;h=77d2781c |
7 |
|
8 |
[scripts:new] aggregate_tree.pl, harvest <remote-id type=cpan> data into a big JSON file/list of dists |
9 |
|
10 |
--- |
11 |
scripts/aggregate_tree.pl | 138 +++++++++++++++++++++++++++++++++++++++++++++ |
12 |
1 files changed, 138 insertions(+), 0 deletions(-) |
13 |
|
14 |
diff --git a/scripts/aggregate_tree.pl b/scripts/aggregate_tree.pl |
15 |
new file mode 100755 |
16 |
index 0000000..d4c5bfa |
17 |
--- /dev/null |
18 |
+++ b/scripts/aggregate_tree.pl |
19 |
@@ -0,0 +1,138 @@ |
20 |
+#!/usr/bin/env perl |
21 |
+ |
22 |
+eval 'echo "Called with something not perl"' && exit 1 # Non-Perl protection. |
23 |
+ if 0; |
24 |
+ |
25 |
+use 5.14.2; |
26 |
+use strict; |
27 |
+use warnings; |
28 |
+ |
29 |
+use FindBin; |
30 |
+use lib "$FindBin::Bin/lib"; |
31 |
+use env::gentoo::perl_experimental; |
32 |
+use optparse; |
33 |
+use utf8; |
34 |
+use Data::Dump qw( pp ); |
35 |
+use Gentoo::Overlay; |
36 |
+ |
37 |
+# FILENAME: aggregate_tree.pl |
38 |
+# CREATED: 29/02/12 07:37:54 by Kent Fredric (kentnl) <kentfredric@×××××.com> |
39 |
+# ABSTRACT: Connect all the cpan id's from the metadata.xml |
40 |
+ |
41 |
+use XML::Smart; |
42 |
+ |
43 |
+my $env = env::gentoo::perl_experimental->new(); |
44 |
+my $opts = optparse->new( |
45 |
+ argv => \@ARGV, |
46 |
+ help => sub { print <DATA>; return }, |
47 |
+); |
48 |
+my $root = $env->root; |
49 |
+use Path::Class::Dir; |
50 |
+ |
51 |
+if ( defined $opts->long_opts->{root} ) { |
52 |
+ $root = Path::Class::Dir->new( $opts->long_opts->{root} ); |
53 |
+} |
54 |
+my $overlay = Gentoo::Overlay->new( path => $root ); |
55 |
+ |
56 |
+use JSON; |
57 |
+ |
58 |
+my $data; |
59 |
+ |
60 |
+my $packages = $data->{ $overlay->name } = {}; |
61 |
+ |
62 |
+my $encoder = JSON->new()->pretty->utf8->canonical; |
63 |
+ |
64 |
+my $dest = \*STDOUT; |
65 |
+if ( not $opts->long_opts->{output} or $opts->long_opts->{output} eq '-' ) { |
66 |
+ $dest = \*STDOUT; |
67 |
+} |
68 |
+else { |
69 |
+ use Path::Class::File; |
70 |
+ my $file = Path::Class::File->new( $opts->long_opts->{output} )->absolute(); |
71 |
+ $dest = $file->openw( iomode => ':utf8' ); |
72 |
+} |
73 |
+ |
74 |
+$overlay->iterate( |
75 |
+ 'packages' => sub { |
76 |
+ my ( $self, $c ) = @_; |
77 |
+ my $CP = $c->{category_name} . '/' . $c->{package_name}; |
78 |
+ my $xmlfile = $root->subdir( $c->{category_name}, $c->{package_name} )->file('metadata.xml'); |
79 |
+ if ( not -e $xmlfile ) { |
80 |
+ warn "No metadata.xml for $CP\n"; |
81 |
+ return; |
82 |
+ } |
83 |
+ |
84 |
+ # warn "Processing $xmlfile\n"; |
85 |
+ my $XML = XML::Smart->new( $xmlfile->absolute()->stringify() ); |
86 |
+ if ( not exists $XML->{pkgmetadata} ) { |
87 |
+ |
88 |
+ # warn "<pkgmetadata> missing in $xmlfile\n"; |
89 |
+ return; |
90 |
+ } |
91 |
+ if ( not exists $XML->{pkgmetadata}->{upstream} ) { |
92 |
+ |
93 |
+ # warn "<pkgmetadata>/<upstream> missing in $xmlfile\n"; |
94 |
+ return; |
95 |
+ } |
96 |
+ if ( not exists $XML->{pkgmetadata}->{upstream}->{'remote-id'} ) { |
97 |
+ |
98 |
+ # warn "<pkgmetadata>/<upstream>/<remote-id> missing in $xmlfile\n"; |
99 |
+ return; |
100 |
+ } |
101 |
+ if ( not exists $XML->{pkgmetadata}->{upstream}->{'remote-id'}->{type} ) { |
102 |
+ |
103 |
+ # warn "remote type not specified for $CP"; |
104 |
+ return; |
105 |
+ } |
106 |
+ if ( not $XML->{pkgmetadata}->{upstream}->{'remote-id'}->{type} eq 'cpan' ) { |
107 |
+ |
108 |
+ # warn "$CP: Not a CPAN remote: " . $XML->{pkgmetadata}->{upstream}->{'remote-id'}->{type} ; |
109 |
+ return; |
110 |
+ } |
111 |
+ my $upstream = $XML->{pkgmetadata}->{upstream}->{'remote-id'}->content(); |
112 |
+ $packages->{$upstream} = $CP; |
113 |
+ } |
114 |
+); |
115 |
+ |
116 |
+my $out; |
117 |
+if ( not $opts->long_opts->{format} ) { |
118 |
+ $opts->long_opts->{format} = "JSON"; |
119 |
+} |
120 |
+if ( $opts->long_opts->{format} eq "JSON" ) { |
121 |
+ $out = $encoder->encode($data); |
122 |
+} |
123 |
+elsif ( $opts->long_opts->{format} eq 'distlist' ) { |
124 |
+ $out = join "\n", keys %{$packages}; |
125 |
+} |
126 |
+else { |
127 |
+ die "Unknown format type " . $opts->long_opts->{format}; |
128 |
+} |
129 |
+ |
130 |
+$dest->print($out); |
131 |
+ |
132 |
+0; |
133 |
+ |
134 |
+__DATA__ |
135 |
+ |
136 |
+This script scrapes the perl repository and finds all the metadata.xml files |
137 |
+ and makes a mapping file connecting categories to upstream dists. |
138 |
+ |
139 |
+Usage: |
140 |
+ |
141 |
+ aggregate_tree.pl |
142 |
+ |
143 |
+ By default uses the perl-experimental overlay as a working dir, and emits JSON to stdout |
144 |
+ |
145 |
+ aggregate_tree.pl |
146 |
+ |
147 |
+ --root="/path/to/some/root" |
148 |
+ |
149 |
+ Specifiy another root to scan ( ie: /usr/portage ) |
150 |
+ |
151 |
+ --format=JSON # Emit JSON ( Default ) |
152 |
+ --format=distlist # Emit a list of CPAN Dist Names |
153 |
+ |
154 |
+ --output=- # Write to standard output ( Default ) |
155 |
+ --output="/path/to/file" # Write to the specified file |
156 |
+ |
157 |
+ |