summary refs log tree commit diff
path: root/gen-gdf.pl
blob: eafd37921f2f29adfd410776ad5c33477c6ee206 (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
#!/usr/bin/perl -w
use strict;
use feature ':5.10';
use Getopt::Long;
use XML::Simple;
use YAML::Syck;
use IO::All;
use DateTime;

use lib ( 'lib' );
use CPAN::mapcpan;

my $options = GetOptions(
    'out=s'   => \my $output_gdf,
    'dbmap=s' => \my $db_map,
    'type=s'  => \my $type,
    'list=s'  => \my $list,
);

print "preparing gexf ... ";
my $dbmap = CPAN::cpanmap->connect( "dbi:SQLite:dbname=" . $db_map, "", "" );

my $struct_graph;
$struct_graph->{ gexf }            = { version => "1.0" };
$struct_graph->{ gexf }->{ meta }  = { creator => [ 'rtgi' ] };
$struct_graph->{ gexf }->{ graph } = { type    => 'static' }; 
# static si pas de dates
$struct_graph->{ gexf }->{ graph }->{ attributes } = {
    class => 'node',
    type  => 'static',
};
push @{ $struct_graph->{ gexf }->{ graph }->{ attributes }->{ attribute } },
    {
    id    => 0,
    #title => 'dist',
    type  => 'string',
    };
say "done";

print "creating nodes ... ";
$struct_graph->{ gexf }->{ graph }->{ nodes } = {};

my ($search, $packages, $id_nodes);
if ( $type && $type eq 'author' ) {
    if ( $list ) {
        my $author < io $list;
        my @author_list = split /\n/, $author;
        $search = {
            -and => [
                author   => { 'in', \@author_list },
                released => { '>',  '1970-01-01' }
            ]
        };
    } else {
        $search = {
            -and => [
                author   => { '!=', 'null' },
                released => { '>',  '1970-01-01' }
            ]
        };
    }
} else {
    $search = {
        -and => [
            author   => { '!=', 'null' },
            released => { '>',  '1970-01-01' }
        ]
    };
}

$packages = $dbmap->resultset( 'packages' )->search( $search );
my $id_authors;
my $authors;
my $i=0;
my $id_edges = 1;
while ( my $package = $packages->next ) {
    if ( $type eq 'author' ) {
        if (!exists $id_authors->{$package->author}){
            $id_authors->{$package->author} = ++$i;
        }
        if ( !exists $authors->{ $package->author } ) {
            push @{ $struct_graph->{ gexf }->{ graph }->{ nodes }->{ node } },
                {
                id    => $id_authors->{$package->author},
                label => $package->author,
                };
            $authors->{ $package->author }++;
        }
        my @edges = $package->edges;
        foreach my $edge ( @edges ) {
            next if $edge->dist_to->author eq $package->author;
            my $edges
                = $struct_graph->{ gexf }->{ graph }->{ edges }->{ edge };
            my @check = grep {
                       $_->{ source } eq $package->author
                    && $_->{ target } eq $edge->dist_to->author
            } @$edges;
            if ( @check ) {
                map { $_->{ cardinal }++ } @check;
            } else {
                if (!exists $id_authors->{$edge->dist_to->author}){
                    $id_authors->{$edge->dist_to->author} = ++$i;
                    push @{ $struct_graph->{ gexf }->{ graph }->{ nodes }->{ node } },
                        {
                        id    => $id_authors->{$edge->dist_to->author},
                        label => $edge->dist_to->author,
                        };
                    $authors->{ $edge->dist_to->author }++;
                }
                push @{ $struct_graph->{ gexf }->{ graph }->{ edges }
                        ->{ edge } },
                    {
                        id => $id_edges++,
                    cardinal => 1,
                    source   => $id_authors->{$package->author},
                    target   => $id_authors->{$edge->dist_to->author},
                    type     => 'dir',
                    };
            }
        }
    } else {
        my ( $year, $month, $day )
            = $package->released =~ /^(\d{4})-(\d{2})-(\d{2})/;
        push @{ $struct_graph->{ gexf }->{ graph }->{ nodes }->{ node } },
            {
            id       => $package->id,
            label    => $package->dist,
            author   => $package->author,
            version  => $package->version,
            datefrom => join( '-', $year, $month, $day ),
            };
        $id_nodes->{ $package->id }++;
    }
}
say "done";

print "creating edges ... ";
my $id = 0;
my $edges;
if ( !$type ) {
    $edges = $dbmap->resultset( 'edges' )->search;
    while ( my $edge = $edges->next ) {
        next unless exists $id_nodes->{ $edge->dist_from };
        next unless exists $id_nodes->{ $edge->dist_to };
        push @{ $struct_graph->{ gexf }->{ graph }->{ edges }->{ edge } },
            {
            cardinal => 1,
            source   => $edge->dist_from,
            target   => $edge->dist_to,
            type     => 'dir',
            id       => ++$id,
            };
    }
}

say "done";

print "generating gdf ... ";
my $xml = XMLout(
    $struct_graph,
    AttrIndent => 1,
    KeepRoot => 1,
);
$xml > io( $output_gdf );
say "done";