Advertisement
Guest User

Untitled

a guest
Apr 21st, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.89 KB | None | 0 0
  1. #!/usr/bin/perl
  2.  
  3. use strict;
  4. use warnings;
  5. use Getopt::Long;
  6.  
  7. my $report;
  8. my $dir;
  9. my $help;
  10.  
  11. GetOptions("r=s", => \$report,
  12. "d=s", => \$dir,
  13. "h" => \$help);
  14.  
  15. if($help) {
  16. print qq["Help:
  17. -r <path to the Downloaded csv>, input report.
  18. -d <path to the reports directory>, input directory to search for reports.
  19. -h , print this help and exit.\n];
  20. exit;
  21. }
  22.  
  23. die "You must specify either a report or a directory.\n" unless ($report || $dir);
  24.  
  25. if($report && $dir) {
  26. die "Specify the path to a report file or to a reports directory, not to both at the same time. See help.\n";
  27. }
  28.  
  29. if($report) {
  30. die "Invalid filename: $report. Exiting. $!\n" unless (-f $report);
  31. }
  32.  
  33. my @files;
  34. if($dir) {
  35. die "Invalid directory: $dir. Exiting. $!\n" unless (-d $dir);
  36. opendir(DIR, $dir) or die "Couldn't open directory $dir: $!\n";
  37. @files = (readdir DIR) or die "Couldn't read directory $dir: $!\n";
  38. closedir(DIR) or die "Couldn't close directory $dir: $!\n";
  39. } elsif ($report) {
  40. @files = $report;
  41. $dir = '.';
  42. }
  43.  
  44. my $hours;
  45. my $total_hours = 0;
  46. my %categories;
  47.  
  48. foreach (@files) {
  49. next if ($_ =~m/^\.+$/);
  50. next if (-d $_);
  51. my $line_count = 0;
  52. if(!(open FILE, '<', "$dir/$_")) {
  53. print "Couldn't open file $_: $!\nSkipping it.\n";
  54. next;
  55. }
  56. while(!eof(FILE)) {
  57. defined(my $line = readline FILE) or die "Couldn't open $_. $!\n";
  58. if (!$line_count) {
  59. if(!($line =~ m/.*Hours.*\,.*Description.*/)) {
  60. print "File $_ is not a valid report. Skipping it.\n";
  61. close FILE;
  62. last;
  63. }
  64. }
  65.  
  66. if ($line =~ m/([0-9]+)\:([0-9]+).*\,.*\[(.*)\]/) {
  67. $hours = $1 + $2 / 60.0;
  68. $total_hours += $hours;
  69.  
  70. my $category = $3;
  71. if ($categories{$category}) {
  72. $categories{$category} += $hours;
  73. } else {
  74. $categories{$category} = $hours;
  75. }
  76. }
  77. $line_count++;
  78. }
  79. close FILE or die $!;
  80. }
  81.  
  82. foreach my $key (keys %categories) {
  83. print qq[Project $key: $categories{$key}\n];
  84. }
  85. print qq[Total hours: $total_hours\n];
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement