public inbox for gentoo-commits@lists.gentoo.org
 help / color / mirror / Atom feed
From: "Robin H. Johnson" <robbat2@gentoo.org>
To: gentoo-commits@lists.gentoo.org
Subject: [gentoo-commits] proj/elections:master commit in: /
Date: Sat, 18 Jun 2016 17:28:57 +0000 (UTC)	[thread overview]
Message-ID: <1435469615.274977d0840c8a1879946234c4d07575e8bde153.robbat2@gentoo> (raw)

commit:     274977d0840c8a1879946234c4d07575e8bde153
Author:     Robin H. Johnson <robbat2 <AT> gentoo <DOT> org>
AuthorDate: Sun Jun 28 05:32:45 2015 +0000
Commit:     Robin H. Johnson <robbat2 <AT> gentoo <DOT> org>
CommitDate: Sun Jun 28 05:33:35 2015 +0000
URL:        https://gitweb.gentoo.org/proj/elections.git/commit/?id=274977d0

Refactor elections, so we can have common fetching of which elections are open, and then consistently use the epoch timestamp inside the start/stop files.

Signed-off-by: Robin H. Johnson <robbat2 <AT> gentoo.org>

 Votify.pm | 127 +++++++++++++++++++++++++++++++++++++++++++++++++++++++-------
 countify  |  38 ++++++-------------
 listify   |  58 +++++++++-------------------
 votify    |  55 ++++++++-------------------
 4 files changed, 157 insertions(+), 121 deletions(-)

diff --git a/Votify.pm b/Votify.pm
index 1be8de7..289be2c 100644
--- a/Votify.pm
+++ b/Votify.pm
@@ -9,10 +9,14 @@
 package Votify;
 
 use POSIX;
+use Cwd qw(abs_path);
+use File::Spec::Functions;
 use List::Util;
 use strict;
 
-our ($datadir) = '/etc/elections/current';
+our $datefmt = '%Y-%m-%d %H:%M:%S UTC';
+
+our ($basedir) = List::Util::first { -d $_ } ('/etc/elections', '.');
 (our $zero = $0) =~ s,.*/,,;
 our $version = '1.6';
 
@@ -21,6 +25,98 @@ sub import {
     $Votify::mode = $mode;
 }
 
+my @REQUIRED_FILES = qw(ballot officials start stop voters);
+
+sub get_datadir {
+    my $election_name = shift;
+    my $election_dir = abs_path(catfile($Votify::basedir, $election_name));
+    if(!validate_election_dir()) {
+        die "$election_name is not a valid election!"
+    }
+    return $election_dir;
+}
+
+sub validate_election_dir {
+    return 0 unless $_;
+    my $election_dir = $_;
+    my $election_name = $election_dir;
+    $election_name =~ /.*\//;
+    return 0 unless -d $election_dir;
+    return 0 if substr($election_name,0,1) eq ".";
+    my $valid = List::Util::reduce {
+        $a or $b ? 1 : 0;
+    } map {
+        my $file_valid = 0;
+        # Legacy naming:
+        $file_valid = 1 if -f sprintf("%s/%s-%s", $election_name, $_, $election_name);
+        # New naming:
+        $file_valid = 1 if -f sprintf("%s/%s", $election_name, $_);
+        #printf "File %s valid=%d\n", $_, $file_valid;
+        $file_valid;
+    } @REQUIRED_FILES;
+    return $valid;
+}
+
+sub get_elections_list {
+    my @elections;
+    opendir(D, $Votify::basedir) or die;
+    @elections = sort grep {
+        my $valid = validate_election_dir(catfile($Votify::basedir, $_));
+        $valid;
+    } readdir D;
+    closedir D;
+    return @elections;
+}
+
+sub grabfile_int {
+    my $f = shift;
+    #print "Checking $f\n";
+    my $i = 0;
+    open my $fh, '<', $f or return -1;
+    local $/ = undef;
+    $i = <$fh> if defined($fh);
+    close $fh;
+    #print "Raw file: $i\n";
+    chomp $i if $i;
+    return $i;
+}
+
+
+sub get_single_election_hashref {
+    my $election_name = shift;
+    my $election_dir = catfile($Votify::basedir, $election_name);
+    my %election;
+    foreach my $fn (@REQUIRED_FILES){
+        #print "Scan $fn\n";
+        my @filenames = (sprintf("%s/%s", $election_name, $fn), sprintf("%s/%s-%s", $election_name, $fn, $election_name));
+        #print Dumper(@filenames);
+        my $filename = abs_path(List::Util::first { -f $_ } @filenames);
+        $election{"${fn}file"} = $filename;
+    };
+    #print Dumper(%election);
+    $election{starttime} = grabfile_int($election{'startfile'});
+    $election{stoptime} = grabfile_int($election{'stopfile'});
+    return \%election;
+}
+
+sub get_elections_hash {
+    my %elections;
+    %elections = map { $_ => get_single_election_hashref($_) } get_elections_list();
+    return %elections;
+}
+
+sub get_open_elections_hash {
+    my %elections = get_elections_hash();
+    my @open_elections = grep {
+      my $starttime = $elections{$_}{'starttime'};
+      my $stoptime = $elections{$_}{'stoptime'};
+      my $valid = ((not defined $starttime or $starttime < time) and
+            (not defined $stoptime or $stoptime > time));
+      $valid;
+    } keys %elections;
+    return map { $_ => $elections{$_} } @open_elections;
+}
+
 ######################################################################
 # OfficialList
 ######################################################################
@@ -28,14 +124,15 @@ sub import {
 package OfficialList;
 
 sub new {
-    my ($class, $election) = @_;
+    my ($class, $election_name) = @_;
     my ($self) = {
-        election => $election,
+        election => $election_name,
         officials => [],
     };
 
+    my $election = Votify::get_single_election_hashref($self->{'election'});
     # no point in waiting to load
-    open(F, "<$Votify::datadir/officials-$election") 
+    open(F, '<', $election->{'officialsfile'})
         or die("failed to open officials file");
     chomp(@{$self->{'officials'}} = <F>);
     close(F);
@@ -56,18 +153,20 @@ sub officials {
 package VoterList;
 
 sub new {
-    my ($class, $election) = @_;
+    my ($class, $election_name) = @_;
     my (@voterlist, $r);
+    my $datadir = Votify::get_datadir($election_name);
     my ($self) = {
-        election => $election,
-        default_filename => "$Votify::datadir/confs-$election",
+        election => $election_name,
+        default_filename => catfile($datadir, "confs-$election_name"),
         filename => '',
         voters => {},   # confnum => voter
         confs => {},    # voter => confnum
     };
 
     # no point in waiting to load
-    open(F, "<$Votify::datadir/voters-$election") 
+    my $election = Votify::get_single_election_hashref($self->{'election'});
+    open(F, '<', $election->{'votersfile'})
         or die("failed to open voters file");
     chomp(@voterlist = <F>);
     close(F);
@@ -133,10 +232,11 @@ package MasterBallot;
 use Data::Dumper;
 
 sub new {
-    my ($class, $election, $vl) = @_;
+    my ($class, $election_name, $vl) = @_;
+    my $datadir = Votify::get_datadir($election_name);
     my ($self) = {
-        election => $election,
-        default_filename => "$Votify::datadir/master-$election",
+        election => $election_name,
+        default_filename => catfile($datadir, "master-$election_name"),
         filename => '',
         voterlist => $vl,
         ballots => {},          # indexed by conf num
@@ -296,7 +396,7 @@ sub display_table {
     @shortnames = sort values %{$self->{'candidates'}};
     $minlen = length scalar keys %{$self->{'ballots'}};
     $minlen = 5 if $minlen < 5;
-    
+
     # build the format string
     for my $s (@shortnames) {
         if (length($s) > $minlen) {
@@ -491,7 +591,8 @@ sub read {
 
 sub populate {
     my ($self) = @_;
-    $self->read("$Votify::datadir/ballot-$self->{election}");
+    my $election = Votify::get_single_election_hashref($self->{'election'});
+    $self->read($election->{'ballotfile'});
     @{$self->{'choices'}} = List::Util::shuffle(@{$self->{'choices'}});
 }
 

diff --git a/countify b/countify
index 975a7ec..1465da4 100755
--- a/countify
+++ b/countify
@@ -8,7 +8,13 @@
 #
 
 #BEGIN { push @INC, (getpwnam 'fox2mike')[7].'/elections' }
-BEGIN { push @INC, '/etc/elections/current' }
+BEGIN {
+    if(-f '/etc/elections/Votify.pm') {
+        push @INC, '/etc/elections';
+    } else {
+        push @INC, '.' if -f 'Votify.pm';
+    }
+}
 
 use POSIX;
 use Getopt::Long;
@@ -24,33 +30,11 @@ use strict;
 (my $version = $Votify::version) =~ s/.*?(\d.*\d).*/$zero version $1\n/;
 my %opt;
 
-sub grabfile_int {
-    my $f = shift;
-    open FILE, "<", $f;
-    my $i = <FILE>;
-    close FILE;
-    chomp $i;
-    return $i + 0;
-}
-
 # Collect the open elections
-my (@open_elections, $usage_elections);
-opendir(D, "$Votify::datadir/") or die;
-@open_elections = sort grep {
-    s/^start-// and do {
-        my ($startfile) = sprintf "%s/start-%s", $Votify::datadir, $_;
-        my ($stopfile) = sprintf "%s/stop-%s", $Votify::datadir, $_;
-        my ($starttime) = grabfile_int($startfile);
-        my ($stoptime) = grabfile_int($stopfile);
-        $starttime = (stat _)[9] if stat($startfile) and (!defined($starttime) or ($starttime <= 0));
-        $stoptime = (stat _)[9] if stat($stopfile) and (!defined($stoptime) or ($stoptime <= 0));
-        ((not defined $starttime or $starttime < time) and
-            (not defined $stoptime or $stoptime > time))
-    }
-} readdir D;
-closedir D;
-if (@open_elections) {
-    $usage_elections = join("\n        ", @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)";
 }

diff --git a/listify b/listify
index 10b4b82..c8c24eb 100755
--- a/listify
+++ b/listify
@@ -7,8 +7,13 @@
 # votify: generate, verify and submit voting ballots for trustee elections
 #
 
-#BEGIN { push @INC, (getpwnam 'fox2mike')[7].'/elections' }
-BEGIN { push @INC, '/etc/elections/current' }
+BEGIN {
+    if(-f '/etc/elections/Votify.pm') {
+        push @INC, '/etc/elections';
+    } else {
+        push @INC, '.' if -f 'Votify.pm';
+    }
+}
 
 use POSIX;
 use Getopt::Long;
@@ -24,46 +29,17 @@ use strict;
 (my $version = $Votify::version) =~ s/.*?(\d.*\d).*/$zero version $1\n/;
 my (%opt, %elections);
 
-sub grabfile_int {
-    my $f = shift;
-	my $i = 0;
-    open FILE, "<", $f;
-    $i = <FILE> if defined(<FILE>);
-    close FILE;
-    chomp $i if $i;
-    return $i;
-}
-
 # Collect the open elections
-my (@open_elections, $usage_elections);
-opendir(D, "$Votify::datadir/") or die;
-@open_elections = sort grep {
-    s/^start-// and do {
-        my ($name) = $_;
-        my ($startfile) = sprintf "%s/start-%s", $Votify::datadir, $_;
-        my ($stopfile) = sprintf "%s/stop-%s", $Votify::datadir, $_;
-        my ($starttime) = grabfile_int($startfile);
-        my ($stoptime) = grabfile_int($stopfile);
-        $starttime = (stat _)[9] if stat($startfile) and (!defined($starttime) or ($starttime <= 0));
-        $stoptime = (stat _)[9] if stat($stopfile) and (!defined($stoptime) or ($stoptime <= 0));
-        my $valid = ((not defined $starttime or $starttime < time) and
-            (not defined $stoptime or $stoptime > time));
-        if($valid) {
-            $elections{$name} = {};
-            $elections{$name}{starttime} = $starttime;
-            $elections{$name}{stoptime} = $stoptime;
-        }
-        $valid;
-    }
-} readdir D;
-closedir D;
-my $datefmt = '%Y-%m-%d %H:%M UTC';
-if (@open_elections) {
-    $usage_elections = "Presently available elections:\n" . join('', map { 
-            my ($name) = $_; 
-            my ($start) = strftime($datefmt, localtime($elections{$name}{starttime}));
-            my ($stop) = strftime($datefmt, localtime($elections{$name}{stoptime}));
-            sprintf("\t%s: %s to %s\n", $name, $start, $stop) } @open_elections);
+my ($usage_elections);
+
+%elections = Votify::get_open_elections_hash();
+
+if (scalar(keys %elections) > 0) {
+    $usage_elections = "Presently available elections:\n" . join('', map {
+            my ($name) = $_;
+            my ($start) = strftime($Votify::datefmt, gmtime($elections{$name}{starttime}));
+            my ($stop) = strftime($Votify::datefmt, gmtime($elections{$name}{stoptime}));
+            sprintf("\t%s: %s to %s\n", $name, $start, $stop) } keys %elections);
     $usage_elections .= <<EOF
 \nA handy tool called "votify" can be used to vote in the election. You can use
 "votify --help" to get instructions on how to vote, verify, and submit your

diff --git a/votify b/votify
index 40607b7..e52881d 100755
--- a/votify
+++ b/votify
@@ -8,7 +8,13 @@
 #
 
 #BEGIN { push @INC, (getpwnam 'fox2mike')[7].'/elections' }
-BEGIN { push @INC, '/etc/elections/current' }
+BEGIN {
+    if(-f '/etc/elections/Votify.pm') {
+        push @INC, '/etc/elections';
+    } else {
+        push @INC, '.' if -f 'Votify.pm';
+    }
+}
 
 use POSIX;
 use Getopt::Long;
@@ -24,34 +30,11 @@ use strict;
 (my $version = $Votify::version) =~ s/.*?(\d.*\d).*/$zero version $1\n/;
 my (%opt);
 
-sub grabfile_int {
-    my $f = shift;
-	my $i = 0;
-    open FILE, "<", $f;
-    $i = <FILE> if defined(<FILE>);
-    close FILE;
-    chomp $i if $i;
-    return $i;
-}
-
 # Collect the open elections
-my (@open_elections, $usage_elections);
-opendir(D, "$Votify::datadir/") or die;
-@open_elections = sort grep {
-    s/^start-// and do {
-        my ($startfile) = sprintf "%s/start-%s", $Votify::datadir, $_;
-        my ($stopfile) = sprintf "%s/stop-%s", $Votify::datadir, $_;
-        my ($starttime) = grabfile_int($startfile);
-        my ($stoptime) = grabfile_int($stopfile);
-        $starttime = (stat _)[9] if stat($startfile) and (!defined($starttime) or ($starttime <= 0));
-        $stoptime = (stat _)[9] if stat($stopfile) and (!defined($stoptime) or ($stoptime <= 0));
-        ((not defined $starttime or $starttime < time) and
-            (not defined $stoptime or $stoptime > time))
-    }
-} readdir D;
-closedir D;
-if (@open_elections) {
-    $usage_elections = join("\n        ", @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)";
 }
@@ -104,7 +87,7 @@ Instructions:
 (4) Submit your ballot.  This renames your ballot to
     ~/.ballot-<election>-submitted so that it will be tallied when the votes
     are collected.
-    
+
         \$ $zero --submit <election>
 
 EOT
@@ -141,16 +124,8 @@ my ($b) = Ballot->new($election);
 # Check if the election is open.  This should really happen in an
 # Election class in Votify.pm eventually
 my ($starttime, $stoptime);
-my ($startfile) = sprintf "%s/start-%s", $Votify::datadir, $election;
-my ($stopfile) = sprintf "%s/stop-%s", $Votify::datadir, $election;
-if ( -f $startfile ) {
-	$starttime = grabfile_int($startfile);
-	$starttime = (stat _)[9] if stat($startfile) and (!defined($starttime) or ($starttime <= 0));
-}
-if ( -f $stopfile ) {
-	($stoptime) = grabfile_int($stopfile);
-	$stoptime = (stat _)[9] if stat($stopfile) and (!defined($stoptime) or ($stoptime <= 0));
-}
+$starttime = $open_elections{$election}{'starttime'};
+$stoptime = $open_elections{$election}{'stoptime'};
 if ($starttime && $starttime > time) {
     print "\n", "*" x 75, "\n";
     print "WARNING: Specified election doesn't start until ",
@@ -166,7 +141,7 @@ if ($stoptime && $stoptime < time) {
 
 if ($opt{'new'}) {
     # Make sure the user is eligible
-    open(F, "<$Votify::datadir/voters-$election") or die "Failed to open voters file";
+    open(F, '<', $open_elections{$election}{'votersfile'}) or die "Failed to open voters file";
     my (@voters) = <F>;
     chomp(@voters);
     close(F);


             reply	other threads:[~2016-06-18 17:29 UTC|newest]

Thread overview: 44+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-06-18 17:28 Robin H. Johnson [this message]
  -- strict thread matches above, loose matches on Subject: below --
2024-06-02  0:13 [gentoo-commits] proj/elections:master commit in: / Robin H. Johnson
2024-05-25 17:14 Robin H. Johnson
2023-04-19 22:28 Robin H. Johnson
2022-07-01 19:12 Robin H. Johnson
2022-07-01 15:50 Robin H. Johnson
2022-06-30 22:36 Robin H. Johnson
2022-06-25 16:31 Robin H. Johnson
2022-06-25 13:58 Jorge Manuel B. S. Vicetto
2021-06-18  6:57 Robin H. Johnson
2021-06-18  6:57 Robin H. Johnson
2020-07-06  5:10 Robin H. Johnson
2020-06-20  5:17 Robin H. Johnson
2020-06-20  5:17 Robin H. Johnson
2017-06-25  0:04 Robin H. Johnson
2017-06-24 23:54 Robin H. Johnson
2016-07-21 18:07 Robin H. Johnson
2016-07-21 18:07 Robin H. Johnson
2016-07-03 15:01 Robin H. Johnson
2016-07-03 11:04 Jorge Manuel B. S. Vicetto
2016-06-19 12:13 Jorge Manuel B. S. Vicetto
2016-06-19 12:13 Jorge Manuel B. S. Vicetto
2016-06-18 17:28 Robin H. Johnson
2016-06-18 17:28 Robin H. Johnson
2016-06-18 17:28 Robin H. Johnson
2015-06-28  0:12 Jorge Manuel B. S. Vicetto
2015-06-27 16:18 Robin H. Johnson
2013-06-30  6:32 Jorge Manuel B. S. Vicetto
2013-02-06  0:09 Jorge Manuel B. S. Vicetto
2012-07-16 19:01 Christian Ruppert
2012-06-18 21:08 Christian Ruppert
2012-02-15  2:07 Robin H. Johnson
2011-06-20  6:14 Robin H. Johnson
2011-06-20  6:13 Robin H. Johnson
2011-06-20  6:13 Robin H. Johnson
2011-06-20  6:13 Robin H. Johnson
2011-06-20  6:13 Robin H. Johnson
2011-06-20  6:08 Robin H. Johnson
2011-06-09  7:46 Robin H. Johnson
2011-04-09  0:09 Robin H. Johnson
2011-03-12 18:41 Robin H. Johnson
2011-03-12 10:57 Robin H. Johnson
2011-03-12 10:56 Robin H. Johnson
2011-02-13  2:21 Robin H. Johnson

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1435469615.274977d0840c8a1879946234c4d07575e8bde153.robbat2@gentoo \
    --to=robbat2@gentoo.org \
    --cc=gentoo-commits@lists.gentoo.org \
    --cc=gentoo-dev@lists.gentoo.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox