From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from lists.gentoo.org (pigeon.gentoo.org [208.92.234.80]) by finch.gentoo.org (Postfix) with ESMTP id 971E51382BD for ; Sat, 18 Jun 2016 17:29:04 +0000 (UTC) Received: from pigeon.gentoo.org (localhost [127.0.0.1]) by pigeon.gentoo.org (Postfix) with SMTP id C78ABE0A65; Sat, 18 Jun 2016 17:29:02 +0000 (UTC) Received: from smtp.gentoo.org (smtp.gentoo.org [140.211.166.183]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by pigeon.gentoo.org (Postfix) with ESMTPS id 27FE5E0A65 for ; Sat, 18 Jun 2016 17:29:01 +0000 (UTC) Received: from oystercatcher.gentoo.org (oystercatcher.gentoo.org [148.251.78.52]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.gentoo.org (Postfix) with ESMTPS id 6144134067D for ; Sat, 18 Jun 2016 17:29:00 +0000 (UTC) Received: from localhost.localdomain (localhost [127.0.0.1]) by oystercatcher.gentoo.org (Postfix) with ESMTP id D25292417 for ; Sat, 18 Jun 2016 17:28:57 +0000 (UTC) From: "Robin H. Johnson" To: gentoo-commits@lists.gentoo.org Content-Transfer-Encoding: 8bit Content-type: text/plain; charset=UTF-8 Reply-To: gentoo-dev@lists.gentoo.org, "Robin H. Johnson" Message-ID: <1435469615.4aa542e6634b127ae3c3a9f28719bc070006a39c.robbat2@gentoo> Subject: [gentoo-commits] proj/elections:master commit in: / X-VCS-Repository: proj/elections X-VCS-Files: election-stats-count statify X-VCS-Directories: / X-VCS-Committer: robbat2 X-VCS-Committer-Name: Robin H. Johnson X-VCS-Revision: 4aa542e6634b127ae3c3a9f28719bc070006a39c X-VCS-Branch: master Date: Sat, 18 Jun 2016 17:28:57 +0000 (UTC) Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-Id: Gentoo Linux mail X-BeenThere: gentoo-commits@lists.gentoo.org X-Archives-Salt: b2f1c63e-7cd4-438e-abfb-c5e0684f0e08 X-Archives-Hash: bbcf810e6f069037d2c5eb0d83dc1e9e commit: 4aa542e6634b127ae3c3a9f28719bc070006a39c Author: Robin H. Johnson gentoo org> AuthorDate: Sun Jun 28 05:33:17 2015 +0000 Commit: Robin H. Johnson gentoo org> CommitDate: Sun Jun 28 05:33:35 2015 +0000 URL: https://gitweb.gentoo.org/proj/elections.git/commit/?id=4aa542e6 Rewrite old bash election-stats-count in perl, using the new election code. Signed-off-by: Robin H. Johnson gentoo.org> election-stats-count | 26 +---------- statify | 121 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 122 insertions(+), 25 deletions(-) diff --git a/election-stats-count b/election-stats-count deleted file mode 100755 index 7c39015..0000000 --- a/election-stats-count +++ /dev/null @@ -1,25 +0,0 @@ -#!/bin/sh -# TODO: -# - this should NOT produce any output when the election is closed. -# - should work with concurrent elections -HOMEDIR=/etc/elections/ -NAME=$(awk '/^name/{print $2}' $HOMEDIR/current/election-details) -OFFICIALS=$(cat ${HOMEDIR}/current/officials-$NAME) -COUNT_VOTERS=$(wc -l <${HOMEDIR}/current/voters-$NAME) - -COUNT_SUBMIT=$(ls /home/*/.ballot-${NAME}-submitted 2>/dev/null | wc -l) -COUNT_PENDING=$(ls /home/*/.ballot-${NAME} 2>/dev/null | wc -l) - - -umask 057 -for u in ${OFFICIALS} ; do -OUTFILE="/home/$u/voter-turnout-${NAME}" -#cat <<-EOF -cat >${OUTFILE} <<-EOF -Eligable voters: $COUNT_VOTERS -Submitted votes: ${COUNT_SUBMIT} -Pending votes: ${COUNT_PENDING} -Turnout: $(echo "scale=10; ${COUNT_SUBMIT}/${COUNT_VOTERS}*100" | bc | xargs printf '%02.3f' )% -EOF -chown ${u}:root ${OUTFILE} -done diff --git a/election-stats-count b/election-stats-count new file mode 120000 index 0000000..fad4dda --- /dev/null +++ b/election-stats-count @@ -0,0 +1 @@ +statify \ No newline at end of file diff --git a/statify b/statify new file mode 100755 index 0000000..c424696 --- /dev/null +++ b/statify @@ -0,0 +1,121 @@ +#!/usr/bin/perl -w +# $Id$ +# +# Copyright 2015 Gentoo Foundation +# Distributed under the terms of the GNU General Public License v2 +# Author: Robin H Johnson +# +# statify: Produce early election statistics +# + +BEGIN { + if(-f '/etc/elections/Votify.pm') { + push @INC, '/etc/elections'; + } else { + push @INC, '.' if -f 'Votify.pm'; + } +} + +use POSIX; +use Getopt::Long; +use List::Util; +use Cwd qw(abs_path); +use File::Spec::Functions; +use Votify 'official'; +use strict; + +###################################################################### +# Global vars +###################################################################### + +(my $zero = $0) =~ s,.*/,,; +(my $version = $Votify::version) =~ s/.*?(\d.*\d).*/$zero version $1\n/; +my %opt; + +# Collect the open elections +my (%open_elections, $usage_elections); +%open_elections = Votify::get_open_elections_hash(); +if (scalar keys %open_elections) { + $usage_elections = join("\n ", keys %open_elections); +} else { + $usage_elections = "(no elections currently open)"; +} + +# rest of usage +my $usage = < + $zero [OPTS] --all + +where options are: + + --all Run on all open elections + --write-to-officials Write statistics to officials homedirs + +and is one of the elections currently in-progress. The following +elections are currently open: + + $usage_elections + +EOT + +###################################################################### +# Main +###################################################################### +for my $election_name (keys %open_elections) { + my (@officials, @voters); + open(F, '<', $open_elections{$election_name}{'officialsfile'}) + or die("failed to open officials file"); + chomp(@officials = ); + close(F); + + $open_elections{$election_name}{'officials'} = @officials; + + open(F, '<', $open_elections{$election_name}{'votersfile'}) + or die("failed to open voters file"); + chomp(@voters = ); + close(F); + + $open_elections{$election_name}{'voters'} = @voters; + + my ($count_voters, $count_submit, $count_pending) = (0, 0,0); + for my $votername (@voters) { + $count_voters++; + if(-f catfile('/home', $votername, ".ballot-${election_name}-submitted")) { + $count_submit++; + } elsif (-f catfile('/home', $votername, ".ballot-${election_name}")) { + $count_pending++; + } + } + die "Mismatched voter count" unless $count_voters == scalar(@voters); + $open_elections{$election_name}{'count'} = { + voters => $count_voters, + submitted => $count_submit, + pending => $count_pending, + }; + my $results_buffer = '' + .sprintf("Election: %s\n", $election_name) + .sprintf("Timestamp: %s\n", POSIX::strftime($Votify::datefmt, gmtime)) + .sprintf("Eligable voters: %d\n", $count_voters) + .sprintf("Submitted votes: %d\n", $count_submit) + .sprintf("Pending votes: %d\n", $count_pending) + .sprintf("Turnout: %02.3f%%\n", $count_submit*100.0/$count_voters) + .sprintf("Pending Turnout: %02.3f%%\n", ($count_submit+$count_pending)*100.0/$count_voters); + print "$results_buffer\n"; + + umask 057; + for my $officialname (@officials) { + my $fn = catfile('/home', $officialname, 'voter-turnout-'.$election_name); + open(F, '>', $fn) or + die("failed to open voter turnout file ($fn): $!"); + print F $results_buffer; + close F; + chown ((getpwnam($officialname))[2]), 0, ($fn) or + die("failed to chown voter turnout file ($fn): $!"); + } +} + + +__END__ +# vim:sw=4 et