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: Sun,  3 Jul 2016 15:01:46 +0000 (UTC)	[thread overview]
Message-ID: <1467558093.6a7d8fab58c70a80175dc107178ee5dcac733adc.robbat2@gentoo> (raw)

commit:     6a7d8fab58c70a80175dc107178ee5dcac733adc
Author:     Robin H. Johnson <robbat2 <AT> gentoo <DOT> org>
AuthorDate: Sun Jul  3 15:01:33 2016 +0000
Commit:     Robin H. Johnson <robbat2 <AT> gentoo <DOT> org>
CommitDate: Sun Jul  3 15:01:33 2016 +0000
URL:        https://gitweb.gentoo.org/proj/elections.git/commit/?id=6a7d8fab

Votify: improve election base validation, and code documentation.

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

 Votify.pm | 81 +++++++++++++++++++++++++++++++++++++++++++--------------------
 1 file changed, 55 insertions(+), 26 deletions(-)

diff --git a/Votify.pm b/Votify.pm
index ed5b519..8e0fe1a 100644
--- a/Votify.pm
+++ b/Votify.pm
@@ -13,7 +13,10 @@ use Cwd qw(abs_path);
 use File::Basename;
 use File::Spec::Functions;
 use List::Util;
+use Data::Dumper;
+use Carp::Always;
 use strict;
+use warnings;
 
 our $datefmt = '%Y-%m-%d %H:%M:%S UTC';
 our ($basedir) = List::Util::first { -d $_ } ('/etc/elections', dirname(__FILE__));
@@ -27,42 +30,61 @@ sub import {
 
 my @REQUIRED_FILES = qw(ballot officials start stop voters);
 
-sub get_datadir {
-    my $election_name = shift;
+# Takes the name of an election and ensure it's validate under the basedir,
+# returning the full path to the election if valid, and undef if not valid.
+# Where valid is:
+# A directory containing ALL of the files in @REQUIRED_FILES, either either
+# their direct names, or the name of the election on the end of the files.
+# Eg 'ballot' or 'ballot-election1234'.
+sub validate_election_dir {
+    my $election_rawdir = shift;
+    return 0 unless defined $election_rawdir;
+    return 0 if substr($election_rawdir,0,1) eq ".";
+
+    my $election_name = $election_rawdir;
+    $election_name =~ s/.*\///;
     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 "$basedir/$election_dir";
+    # Fail if it's not a directory in the basedir
+    return 0 unless -d $election_dir;
+
+    # Do not try to validate hidden directories.
     return 0 if substr($election_name,0,1) eq ".";
-    my $valid = List::Util::reduce {
-        $a or $b ? 1 : 0;
-    } map {
+
+    # Validate that the required files exist in the dir
+    # Part 1, convert the array to a map
+    my %REQUIRED_FILES_valid = map {
         my $file_valid = 0;
         # Legacy naming:
-        $file_valid = 1 if -f sprintf("%s/%s-%s", "$basedir/$election_name", $_, $election_name);
+        $file_valid = 1 if -f sprintf("%s/%s-%s", $election_dir, $_, $election_name);
         # New naming:
-        $file_valid = 1 if -f sprintf("%s/%s", "$basedir/$election_name", $_);
+        $file_valid = 1 if -f sprintf("%s/%s", $election_dir, $_);
         #printf "File %s valid=%d\n", $_, $file_valid;
-        $file_valid;
+        ($_, $file_valid);
     } @REQUIRED_FILES;
-    return $valid;
+
+    # Part 2, ensure all of the map is true
+    my $valid = List::Util::reduce {
+        $a or $b ? 1 : 0;
+    } values(%REQUIRED_FILES_valid);
+
+    # Now return.
+    return $election_dir if $valid;
+    return undef;
 }
 
 sub get_elections_list {
     my @elections;
     opendir(D, $Votify::basedir) or die;
     @elections = sort grep {
-        my $valid = validate_election_dir(catfile($Votify::basedir, $_));
-        $valid;
+       -d $_ and
+       $_ ne "." and
+       $_ ne ".." and
+       $_ ne "" and
+       substr($_, 0, 1) ne ".";
+    } grep {
+        my $valid_election_dir = validate_election_dir($_);
+        defined $valid_election_dir;
     } readdir D;
     closedir D;
     return @elections;
@@ -84,7 +106,8 @@ sub grabfile_int {
 
 sub get_single_election_hashref {
     my $election_name = shift;
-    my $election_dir = catfile($Votify::basedir, $election_name);
+    my $election_dir = validate_election_dir($election_name);
+    return undef unless defined $election_dir;
     my %election;
     foreach my $fn (@REQUIRED_FILES){
         #print "Scan $fn\n";
@@ -101,7 +124,9 @@ sub get_single_election_hashref {
 
 sub get_elections_hash {
     my %elections;
-    %elections = map { $_ => get_single_election_hashref($_) } get_elections_list();
+    my @elections_list = get_elections_list();
+    #print Dumper(\@elections_list);
+    %elections = map { $_ => get_single_election_hashref($_) } @elections_list;
     return %elections;
 }
 
@@ -151,11 +176,13 @@ sub officials {
 ######################################################################
 
 package VoterList;
+use File::Spec::Functions;
 
 sub new {
     my ($class, $election_name) = @_;
     my (@voterlist, $r);
-    my $datadir = Votify::get_datadir($election_name);
+    my $datadir = Votify::validate_election_dir($election_name);
+    die "Unable to get election dir for name $election_name" unless defined $datadir;
     my ($self) = {
         election => $election_name,
         default_filename => catfile($datadir, "confs-$election_name"),
@@ -230,10 +257,12 @@ sub write {
 package MasterBallot;
 
 use Data::Dumper;
+use File::Spec::Functions;
 
 sub new {
     my ($class, $election_name, $vl) = @_;
-    my $datadir = Votify::get_datadir($election_name);
+    my $datadir = Votify::validate_election_dir($election_name);
+    die "Unable to get election dir for name $election_name" unless defined $datadir;
     my ($self) = {
         election => $election_name,
         default_filename => catfile($datadir, "master-$election_name"),


             reply	other threads:[~2016-07-03 15:01 UTC|newest]

Thread overview: 44+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-07-03 15:01 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 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
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=1467558093.6a7d8fab58c70a80175dc107178ee5dcac733adc.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