Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/perl
- use strict;
- use warnings;
- use Getopt::Long;
- my $report;
- my $dir;
- my $help;
- GetOptions("r=s", => \$report,
- "d=s", => \$dir,
- "h" => \$help);
- if($help) {
- print qq["Help:
- -r <path to the Downloaded csv>, input report.
- -d <path to the reports directory>, input directory to search for reports.
- -h , print this help and exit.\n];
- exit;
- }
- die "You must specify either a report or a directory.\n" unless ($report || $dir);
- if($report && $dir) {
- die "Specify the path to a report file or to a reports directory, not to both at the same time. See help.\n";
- }
- if($report) {
- die "Invalid filename: $report. Exiting. $!\n" unless (-f $report);
- }
- my @files;
- if($dir) {
- die "Invalid directory: $dir. Exiting. $!\n" unless (-d $dir);
- opendir(DIR, $dir) or die "Couldn't open directory $dir: $!\n";
- @files = (readdir DIR) or die "Couldn't read directory $dir: $!\n";
- closedir(DIR) or die "Couldn't close directory $dir: $!\n";
- } elsif ($report) {
- @files = $report;
- $dir = '.';
- }
- my $hours;
- my $total_hours = 0;
- my %categories;
- foreach (@files) {
- next if ($_ =~m/^\.+$/);
- next if (-d $_);
- my $line_count = 0;
- if(!(open FILE, '<', "$dir/$_")) {
- print "Couldn't open file $_: $!\nSkipping it.\n";
- next;
- }
- while(!eof(FILE)) {
- defined(my $line = readline FILE) or die "Couldn't open $_. $!\n";
- if (!$line_count) {
- if(!($line =~ m/.*Hours.*\,.*Description.*/)) {
- print "File $_ is not a valid report. Skipping it.\n";
- close FILE;
- last;
- }
- }
- if ($line =~ m/([0-9]+)\:([0-9]+).*\,.*\[(.*)\]/) {
- $hours = $1 + $2 / 60.0;
- $total_hours += $hours;
- my $category = $3;
- if ($categories{$category}) {
- $categories{$category} += $hours;
- } else {
- $categories{$category} = $hours;
- }
- }
- $line_count++;
- }
- close FILE or die $!;
- }
- foreach my $key (keys %categories) {
- print qq[Project $key: $categories{$key}\n];
- }
- print qq[Total hours: $total_hours\n];
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement