ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/OSUT3Analysis/DBTools/scripts/crabclean
Revision: 1.3
Committed: Wed Feb 6 00:33:04 2013 UTC (12 years, 3 months ago) by ahart
Branch: MAIN
CVS Tags: V00-00-06
Changes since 1.2: +2 -0 lines
Log Message:
Get rid of a superfluous newline and add to the help message.

File Contents

# User Rev Content
1 ahart 1.1 #!/usr/bin/env perl
2    
3     use strict;
4     use Getopt::Long;
5    
6 ahart 1.2 sub printHelp;
7     sub parseCrabDir;
8    
9 ahart 1.1 my %opt;
10     Getopt::Long::Configure ("bundling");
11 ahart 1.2 GetOptions (\%opt, "dumb|d", "force|f", "help|h");
12 ahart 1.1
13 ahart 1.2 printHelp () if $opt{"help"} || !$ARGV[0] || (!$opt{"dumb"} && !$ARGV[1]);
14     my $crabDir = $ARGV[0];
15     my $outputDir = $ARGV[0];
16     $outputDir = $ARGV[1] if !$opt{"dumb"};
17     if (!(-e $outputDir) || !(-d $outputDir))
18     {
19     print "Output directory $outputDir does not exist!\n";
20     exit;
21     }
22     opendir (CRAB_OUTPUT, "$outputDir");
23 ahart 1.1 my @crabOutput = readdir (CRAB_OUTPUT);
24     closedir (CRAB_OUTPUT);
25     my %filesToKeep;
26 ahart 1.2 if (!$opt{"dumb"})
27     {
28     if (!(-e $crabDir) || !(-d $crabDir))
29     {
30     print "CRAB directory $crabDir does not exist!\n";
31     exit;
32     }
33     parseCrabDir ($crabDir, \%filesToKeep);
34     }
35     else
36 ahart 1.1 {
37 ahart 1.2 foreach my $file (@crabOutput)
38 ahart 1.1 {
39 ahart 1.2 next if ($file eq "." || $file eq "..");
40     next if !($file =~ m/^.*_[^_]*_[^_]*_[^_]*\.root$/);
41     my $jobNumber = $file;
42     my $submissionNumber = $file;
43     $jobNumber =~ s/^.*_([^_]*)_[^_]*_[^_]*\.root$/$1/;
44     $submissionNumber =~ s/^.*_[^_]*_([^_]*)_[^_]*\.root$/$1/;
45     if (!(defined $filesToKeep{$jobNumber})
46     || $filesToKeep{$jobNumber} < $submissionNumber)
47     {
48     $filesToKeep{$jobNumber} = $submissionNumber;
49     }
50 ahart 1.1 }
51     }
52     foreach my $file (@crabOutput)
53     {
54     next if ($file eq "." || $file eq "..");
55     next if !($file =~ m/^.*_[^_]*_[^_]*_[^_]*\.root$/);
56     my $jobNumber = $file;
57     my $submissionNumber = $file;
58     $jobNumber =~ s/^.*_([^_]*)_[^_]*_[^_]*\.root$/$1/;
59     $submissionNumber =~ s/^.*_[^_]*_([^_]*)_[^_]*\.root$/$1/;
60 ahart 1.2 if (!(defined $filesToKeep{$jobNumber}))
61     {
62     print "No information found for job $jobNumber!\n";
63     }
64     elsif (($opt{"dumb"} && $filesToKeep{$jobNumber} != $submissionNumber)
65     || (!$opt{"dumb"} && $filesToKeep{$jobNumber} ne $file))
66 ahart 1.1 {
67 ahart 1.2 system ("rm -f $outputDir/$file") if $opt{"force"};
68     system ("rm -i $outputDir/$file") if !$opt{"force"};
69 ahart 1.1 }
70     }
71    
72     sub
73     printHelp
74     {
75     my $exeName = $0;
76     $exeName =~ s/^.*\/([^\/]*)$/$1/;
77    
78 ahart 1.2 print "Usage: $exeName [OPTION]... CRAB_DIR OUTPUT_DIR\n";
79 ahart 1.3 print " or: $exeName -d [OPTION]... OUTPUT_DIR\n";
80 ahart 1.2 print "Deletes excess EDM output caused by multiple CRAB submissions using the results\n";
81     print "contained in the CRAB working directory provided.\n";
82 ahart 1.1 print "\n";
83     print "Mandatory arguments to long options are mandatory for short options too.\n";
84 ahart 1.2 printf "%-29s%s\n", " -d, --dumb", "tries to use the filenames of the output instead of";
85     printf "%-29s%s\n", " ", "the results from the CRAB working directory; useful";
86     printf "%-29s%s\n", " ", "if the working directory has been lost";
87 ahart 1.1 printf "%-29s%s\n", " -f, --force", "never prompt (default is to prompt before any";
88     printf "%-29s%s\n", " ", "removal)";
89     printf "%-29s%s\n", " -h, --help", "print this help message";
90    
91     exit;
92     }
93 ahart 1.2
94     sub
95     parseCrabDir
96     {
97     my $crabDir = shift;
98     my $filesToKeep = shift;
99    
100     opendir (CRAB_RESULTS, "$crabDir/res");
101     my @crabResults = readdir (CRAB_RESULTS);
102     closedir (CRAB_RESULTS);
103     for my $file (@crabResults)
104     {
105     next if !($file =~ m/^crab_fjr_[^\.]*\.xml$/);
106     my $jobNumber = $file;
107     $jobNumber =~ s/^crab_fjr_([^\.]*)\.xml$/$1/;
108     my @status = `grep 'ExitStatus' $crabDir/res/$file`;
109     my $wrapperStatus = $status[0];
110     $wrapperStatus =~ s/^.*ExitStatus="([^"]*)".*$/$1/;
111 ahart 1.3 $wrapperStatus =~ s/\n//g;
112 ahart 1.2 if ($wrapperStatus != 0)
113     {
114     print "Job $jobNumber failed (return value $wrapperStatus). Skipping.\n";
115     next;
116     }
117     my @surlForGrid = `grep -A 1 '<SurlForGrid>' $crabDir/res/$file`;
118     my $fileName = $surlForGrid[1];
119     $fileName =~ s/^.*\/([^\/]*)\n/$1/;
120     $filesToKeep->{$jobNumber} = $fileName;
121     }
122     }