/[gentoo-src]/votify/countify
Gentoo

Contents of /votify/countify

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.3 - (show annotations) (download)
Mon May 16 18:10:46 2005 UTC (9 years, 4 months ago) by agriffis
Branch: MAIN
CVS Tags: HEAD
Changes since 1.2: +45 -5 lines
ranking works completely now, even if it needs badly to be refactored

1 #!/usr/bin/perl -w
2 # $Id: countify,v 1.2 2005/05/16 04:03:46 agriffis Exp $
3 #
4 # Copyright 2005 Gentoo Foundation
5 # Distributed under the terms of the GNU General Public License v2
6 #
7 # countify: collect, tabulate and announce ballot results
8 #
9
10 BEGIN { push @INC, (getpwnam 'agriffis')[7].'/elections' }
11
12 use POSIX;
13 use Getopt::Long;
14 use List::Util;
15 use Votify 'official';
16 use strict;
17
18 ######################################################################
19 # Global vars
20 ######################################################################
21
22 (my $zero = $0) =~ s,.*/,,;
23 (my $version = '$Revision: 1.2 $') =~ s/.*?(\d.*\d).*/$zero version $1\n/;
24 my %opt;
25 my $usage = <<EOT;
26
27 usage: $zero <command> <election>
28
29 where <command> is one of:
30
31 --collect Collect the submitted ballots from home directories
32 --rank Show ranking based on master ballot
33 --help Show this help message
34 --version Show version information
35
36 and <election> is one of the elections currently in-progress. The following
37 elections are currently open:
38
39 trustees2005
40
41 EOT
42
43 ######################################################################
44 # Main
45 ######################################################################
46
47 package main;
48
49 # Make sure umask is secure before we do anything
50 umask 077;
51
52 # Parse the options on the cmdline. Put the short versions first in
53 # each optionstring so that the hash keys are created using the short
54 # versions. For example, use 'q|qar', not 'qar|q'.
55 my ($result) = GetOptions(
56 \%opt,
57 'collect', # collect the submitted ballots from home directories
58 'rank', # show the ranking based on a master ballot
59 'help', # help message
60 'version', # version information
61 );
62 if ($opt{'help'} or not %opt) { print STDERR $usage; exit 0 }
63 if ($opt{'version'}) { print STDERR $version; exit 0 }
64 die "$zero: only one command allowed; use --help for help\n" if 1 < keys %opt;
65 die "$zero: election required; use --help for help\n" unless @ARGV == 1;
66
67 my ($election) = $ARGV[0];
68 my ($vl) = VoterList->new($election);
69
70 if ($opt{'collect'}) {
71 my ($ol) = OfficialList->new($election);
72 my ($master) = MasterBallot->new($election, $vl);
73 $master->collect($vl->voters);
74 for my $o ($ol->officials) {
75 my ($uid, $home) = (getpwnam $o)[2,7];
76 mkdir "$home/results-$election";
77 $master->write("$home/results-$election/master-$election");
78 $vl->write("$home/results-$election/confs-$election");
79 chown $uid, -1, "$home/results-$election",
80 "$home/results-$election/master-$election",
81 "$home/results-$election/confs-$election";
82 }
83 exit 0;
84 }
85
86 if ($opt{'rank'}) {
87 my ($master) = MasterBallot->new($election, $vl);
88 my (@candidates, @winner, @ranked, @ranks);
89 $master->read("$ENV{HOME}/results-$election/master-$election");
90 $master->generate_candidates();
91 @candidates = sort keys %{$master->{'candidates'}};
92
93 while (1) {
94 $master->tabulate();
95
96 # this is a hack :-(
97 #print "\n";
98 for my $r (@ranked) {
99 #print "$r is already ranked\n";
100 for my $c (@candidates) {
101 $master->{'table'}{"$r+$c"} = -1;
102 }
103 $master->{'table'}{"$r+$r"} = '+++';
104 }
105
106 # now display the table
107 print "\n";
108 $master->display_table();
109
110 @winner = $master->cssd();
111 if (@winner == @candidates) {
112 print "\n*** No additional winners to add to the list ***\n";
113 last;
114 }
115
116 push @ranks, [ @winner ];
117 push @ranked, @winner;
118 if (@ranked == @candidates) {
119 print "\n*** Finished ranking candidates ***\n";
120 last;
121 }
122
123 print "\n*** Running another pass to find the next winners... ***\n";
124 }
125
126 print "\nFinal ranked list:\n";
127 print map "@$_\n", @ranks;
128 }
129
130 __END__
131
132 $Log: countify,v $
133 Revision 1.2 2005/05/16 04:03:46 agriffis
134 add first pass at countify --rank
135
136
137 # vim:sw=4 et

  ViewVC Help
Powered by ViewVC 1.1.20