aboutsummaryrefslogtreecommitdiff
path: root/0.8.0/ruby/demos/version_spec_distributions.rb
blob: 6de4568b7726f5fb5a5e19312b0bfa0fc0deb934 (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
#!/usr/bin/ruby
# vim: set sw=4 sts=4 et tw=80 :

require 'Paludis'
require 'getoptlong'

Paludis::Log.instance.log_level = Paludis::LogLevel::Warning

class Distribution
    def initialize
        @groups = [ "0.1", "0.2", "0.5", "1", "2", "5", "10", "100" ].map do | x |
            Paludis::VersionSpec.new x
        end
        @counts = Hash.new(0)
        @rest_count = 0
    end

    def add_version version
        group = @groups.find { | x | version < x }
        if group
            @counts[group] += 1
        else
            @rest_count += 1
        end
    end

    def add_repository repo
        repo.category_names.each do | cat |
            repo.package_names(cat).each do | pkg |
                versions = repo.version_specs(pkg)
                unless versions.empty?
                    add_version versions.last
                end
            end
        end
    end

    def text_graph
        biggest_group = [ @rest_count, @groups.inject(1) { | x, y | [ x, @counts[y] ].max } ].max
        unit_scale = 40.0 / biggest_group
        @groups.each do | group |
            printf "<%-10.10s %10d %s\n", group, @counts[group], "#" * (@counts[group] * unit_scale)
        end
        printf "%-11.11s %10d %s\n", "rest", @rest_count, "#" * (@rest_count * unit_scale)
    end

    def gruff_graph file_name
        require 'rubygems'
        require 'gruff'

        g = Gruff::Bar.new
        g.title = "Package versions distribution"

        g.data("Number of packages", (@groups.map { | group | @counts[group] }) << @rest_count)
        labels = Hash.new
        @groups.each_with_index do | group, idx |
            labels[idx] = "<" + group.to_s
        end
        labels[@groups.length] = "rest"
        g.labels = labels

        g.minimum_value = 0
        g.write file_name
    end
end

opts = GetoptLong.new(
    [ '--help',         '-h', GetoptLong::NO_ARGUMENT ],
    [ '--version',      '-V', GetoptLong::NO_ARGUMENT ],
    [ '--log-level',          GetoptLong::REQUIRED_ARGUMENT ],
    [ '--repository-dir',     GetoptLong::REQUIRED_ARGUMENT ],
    [ '--image',              GetoptLong::REQUIRED_ARGUMENT ])

output_image = nil
repository_dir = Dir.getwd
opts.each do | opt, arg |
    case opt
    when '--help'
        puts "Usage: " + $0 + " [options]"
        puts
        puts "Options:"
        puts "  --help                Display a help message"
        puts "  --version             Display program version"
        puts
        puts "  --log-level level     Set log level (debug, qa, warning, silent)"
        puts "  --repository-dir dir  Set repository directory (default: cwd)"
        puts
        puts "  --image foo.png       Output as the specified image rather than as text"
        exit 0

    when '--version'
        puts $0.to_s.split(/\//).last + " " + Paludis::Version
        exit 0

    when '--log-level'
        case arg
        when 'debug'
            Paludis::Log.instance.log_level = Paludis::LogLevel::Debug
        when 'qa'
            Paludis::Log.instance.log_level = Paludis::LogLevel::Qa
        when 'warning'
            Paludis::Log.instance.log_level = Paludis::LogLevel::Warning
        when 'silent'
            Paludis::Log.instance.log_level = Paludis::LogLevel::Silent
        else
            puts "Bad --log-level value " + arg
            exit 1
        end

    when '--repository-dir'
        repository_dir = arg

    when '--image'
        output_image = arg

    end
end

distribution = Distribution.new
env = Paludis::NoConfigEnvironment.new repository_dir
env.package_database.repositories.each do | repo |
    distribution.add_repository repo
end
if output_image
    distribution.gruff_graph output_image
else
    distribution.text_graph
end