blib/lib/NBI/Opts.pm

filter: Statementstmt 80.0% 217 / 271 Branchbran 62.3% 131 / 210 Conditioncond 33.3% 10 / 30 MC/DCmcdc 23.5% 4 / 17 Subroutinesub 91.8% 34 / 37 Podpod 100% 24 / 24 totaltotal 71.3% 420 / 589 CC 118 SCAR 37.1
CC
118
Cov
70%
_parse_start_date
128.5
new
79.8
_mem_parse_mb
15.8
CRAP 41.0
« bin/lsjobs ↑ Summary bin/session »
1 package NBI::Opts;
2 #ABSTRACT: A class for representing a the SLURM options for NBI::Slurm
3 #
4 # NBI::Opts - Stores and validates SLURM resource options for a job.
5 #
6 # DESCRIPTION:
7 # Holds every resource knob that ends up as a #SBATCH directive in the
8 # job script. Key responsibilities:
9 # - new() : accepts -queue, -threads, -memory, -time, -tmpdir,
10 # -email_address, -email_type, -opts (extra directives),
11 # -files (enables array-job mode), -placeholder
12 # - header() : generates the full #!/bin/bash + #SBATCH header block
13 # consumed by NBI::Job->script()
14 # - timestring() : converts internal hours to SLURM "D-HH:MM:SS" format
15 # - is_array() : returns true when a -files list is present
16 # - add_option() : appends a raw #SBATCH option string
17 # - view() : returns a human-readable summary string
18 # Internal helpers:
19 # - _mem_parse_mb() : normalises memory values (KB/MB/GB/TB) to MB
20 # - _time_to_hour() : normalises time strings (e.g. "2h30m", "1d") to hours
21 # - _parse_start_time(): validates/normalises HH:MM[:SS] → "HH:MM:SS"
22 # - _parse_start_date(): validates/normalises DD/MM[/YYYY] → "YYYY-MM-DD",
23 # inferring year when omitted
24 # - _compute_begin() : combines start_time + start_date with date-inference
25 # logic and returns the SLURM "--begin" value string
26 #
27 # RELATIONSHIPS:
28 # - Composed into NBI::Job via the -opts constructor argument or set_opts().
29 # - NBI::Job calls $self->opts->header() and reads tmpdir, placeholder,
30 # files, and is_array() to build the sbatch script.
31 # - $NBI::Opts::VERSION is set from $NBI::Slurm::VERSION (loaded by caller).
32 #
33  
34 29 ▶use 5.012;
Subroutine BEGIN: called CC=1 SCAR=0.0
35 29 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
36 29 ▶use Carp qw(confess);
Subroutine BEGIN: called CC=1 SCAR=0.0
37 29 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
38 29 $Data::Dumper::Sortkeys = 1;
39 29 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
40 29 ▶use POSIX qw(mktime strftime);
Subroutine BEGIN: called CC=1 SCAR=0.0
41  
42 29 $NBI::Opts::VERSION = $NBI::Slurm::VERSION;
43  
44 29 ▶my $SYSTEM_TEMPDIR = $ENV{'TMPDIR'} || $ENV{'TEMP'} || "/tmp";
Condition40%2/5
$ENV{'TMPDIR'} || $ENV{'TEMP'} || '/tmp'
l!l
029
$ENV{"TMPDIR"} or $ENV{"TEMP"}
l!l&&r!l&&!r
0029
MC/DC0%0/2
$ENV{'TMPDIR'} || $ENV{'TEMP'} || '/tmp'
$ENV{"TMPDIR"} $ENV{"TEMP"}
Truth table33%1/3
A or B
A= $ENV{"TMPDIR"}B= $ENV{"TEMP"}
ABresult
1X1
011
000
45 29 require Exporter;
46 29 our @ISA = qw(Exporter);
47  
48 0 ▶sub _yell {
Subroutine _yell: not called CC=2 SCAR=10.9
49 29 ▶ use Term::ANSIColor;
Subroutine BEGIN: called CC=1 SCAR=0.0
50 0 my $msg = shift @_;
51 0 ▶ my $col = shift @_ || "bold green";
Condition0%0/2
shift @_ || 'bold green'
l!l
00
MC/DC0%0/1
shift @_ || 'bold green'
shift @_
Truth table0%0/2
A
A= shift @_
Aresult
11
00
52 0 say STDERR color($col), "[NBI::Opts]", color("reset"), " $msg";
53 }
54 124 ▶sub new {
Subroutine new: called CC=54 SCAR=43.8
Pod documented
55 124 my $class = shift @_;
56 124 my ($queue, $memory, $threads, $opts_array, $tmpdir, $hours, $email_address, $email_when, $files, $placeholder, $start_time, $start_date, $params_array, $params_rows, $array_offset, $array_tasks) = (undef) x 16;
57
58 # Descriptive instantiation with parameters -param => value
59 124 ▶ if (substr($_[0], 0, 1) eq '-') {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($_[0], 0, 1) eq "-") 121 3 124
60
61 121 my %data = @_;
62  
63 # Try parsing
64 121 for my $i (keys %data) {
65
66 # QUEUE
67 941 ▶ if ($i =~ /^-queue/) {
Branch: 16 branches
BranchTrueFalseTotal
if ($i =~ /^-queue/u) { } 120 821 941
elsif ($i =~ /^-threads/u) { } 118 703 821
elsif ($i =~ /^-memory/u) { } 118 585 703
elsif ($i =~ /^-tmpdir/u) { } 118 467 585
elsif ($i =~ /^-(mail|email_address)/u) { } 105 362 467
elsif ($i =~ /^-(when|email_type)/u) { } 105 257 362
elsif ($i =~ /^-opts/u) { } 105 152 257
elsif ($i =~ /^-time/u) { } 118 34 152
elsif ($i =~ /^-placeholder/u) { } 6 28 34
elsif ($i =~ /^-files/u) { } 5 23 28
elsif ($i =~ /^-params_array/u) { } 6 17 23
elsif ($i =~ /^-params_rows/u) { } 6 11 17
elsif ($i =~ /^-array_offset/u) { } 5 6 11
elsif ($i =~ /^-array_tasks/u) { } 5 1 6
elsif ($i =~ /^-start_time/u) { } 0 1 1
elsif ($i =~ /^-start_date/u) { } 0 1 1
68 120 ▶ next unless (defined $data{$i});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data{$i} 0 120 120
69 120 $queue = $data{$i};
70
71  
72 # THREADS
73 } elsif ($i =~ /^-threads/) {
74 118 ▶ next unless (defined $data{$i});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data{$i} 0 118 118
75 # Check it's an integer
76 118 ▶ if ($data{$i} =~ /^\d+$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data{$i} =~ /^\d+$/u) { } 118 0 118
77 118 $threads = $data{$i};
78 } else {
79 0 confess "ERROR NBI::Seq: -threads expects an integer\n";
80 }
81
82
83 # MEMORY
84 } elsif ($i =~ /^-memory/) {
85 118 ▶ next unless (defined $data{$i});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data{$i} 0 118 118
86 118 $memory = _mem_parse_mb($data{$i});
87
88  
89 # TMPDIR
90 } elsif ($i =~ /^-tmpdir/) {
91 118 ▶ next unless (defined $data{$i});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data{$i} 0 118 118
92 118 $tmpdir = $data{$i};
93
94 # MAIL ADDRESS
95 } elsif ($i =~ /^-(mail|email_address)/) {
96 105 ▶ next unless (defined $data{$i});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data{$i} 4 101 105
97 101 $email_address = $data{$i};
98
99 # WHEN MAIL
100 } elsif ($i =~ /^-(when|email_type)/) {
101 105 ▶ next unless (defined $data{$i});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data{$i} 0 105 105
102 105 $email_when = $data{$i};
103
104  
105 # OPTS ARRAY
106 } elsif ($i =~ /^-opts/) {
107 105 ▶ next unless (defined $data{$i});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data{$i} 0 105 105
108 # in this case we expect an array
109 105 ▶ if (ref($data{$i}) ne "ARRAY") {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $data{$i} ne "ARRAY") 0 105 105
110 0 confess "ERROR NBI::Seq: -opts expects an array\n";
111 }
112 105 $opts_array = $data{$i};
113
114  
115 # TIME
116 } elsif ($i =~ /^-time/) {
117 118 $hours = _time_to_hour($data{$i});
118
119 # PLACEHOLDER
120 } elsif ($i =~ /^-placeholder/) {
121 # check if placeholder contains special regex characters
122 6 ▶ if (not defined $data{$i}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $data{$i}) 0 6 6
123 0 confess "ERROR NBI::Seq: Placeholder cannot be empty\n";
124 }
125 6 ▶ if ($data{$i} =~ /[\*\+\?]/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data{$i} =~ /[\*\+\?]/u) 0 6 6
126 0 confess "ERROR NBI::Seq: Placeholder cannot contain special regex characters\n";
127 }
128 6 $placeholder = $data{$i};
129
130 # ARRAY
131 } elsif ($i =~ /^-files/) {
132 # expects ref to array
133 5 ▶ if (ref($data{$i}) ne "ARRAY") {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $data{$i} ne 'ARRAY') { } 0 5 5
134 0 confess "ERROR NBI::Seq: -files expects an array\n";
135 } else {
136 5 $files = $data{$i};
137 }
138  
139 # PARAMS ARRAY
140 } elsif ($i =~ /^-params_array/) {
141 6 ▶ next unless defined $data{$i};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data{$i} 0 6 6
142 6 $params_array = $data{$i};
143  
144 # PARAMS ARRAY ROW COUNT
145 } elsif ($i =~ /^-params_rows/) {
146 6 ▶ next unless defined $data{$i};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data{$i} 0 6 6
147 6 ▶ if ($data{$i} =~ /^\d+$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data{$i} =~ /^\d+$/u) { } 6 0 6
148 6 $params_rows = $data{$i};
149 } else {
150 0 confess "ERROR NBI::Seq: -params_rows expects an integer\n";
151 }
152  
153 # ARRAY OFFSET
154 } elsif ($i =~ /^-array_offset/) {
155 5 ▶ next unless defined $data{$i};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data{$i} 0 5 5
156 5 ▶ if ($data{$i} =~ /^\d+$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data{$i} =~ /^\d+$/u) { } 5 0 5
157 5 $array_offset = $data{$i};
158 } else {
159 0 confess "ERROR NBI::Seq: -array_offset expects an integer\n";
160 }
161  
162 # ARRAY TASK COUNT
163 } elsif ($i =~ /^-array_tasks/) {
164 5 ▶ next unless defined $data{$i};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data{$i} 0 5 5
165 5 ▶ if ($data{$i} =~ /^\d+$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data{$i} =~ /^\d+$/u) { } 5 0 5
166 5 $array_tasks = $data{$i};
167 } else {
168 0 confess "ERROR NBI::Seq: -array_tasks expects an integer\n";
169 }
170
171 # START TIME
172 } elsif ($i =~ /^-start_time/) {
173 0 ▶ next unless defined $data{$i};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data{$i} 0 0 0
174 0 $start_time = _parse_start_time($data{$i});
175  
176 # START DATE
177 } elsif ($i =~ /^-start_date/) {
178 0 ▶ next unless defined $data{$i};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data{$i} 0 0 0
179 0 $start_date = _parse_start_date($data{$i});
180  
181 } else {
182 1 confess "ERROR NBI::Seq: Unknown parameter $i\n";
183 }
184 }
185 }
186
187 123 my $self = bless {}, $class;
188
189 # Set attributes
190 123 ▶ $self->queue = defined $queue ? $queue : "nbi-short";
Branch: 1 branch
BranchTrueFalseTotal
defined $queue ? : 120 3 123
191 123 ▶ $self->threads = defined $threads ? $threads : 1;
Branch: 1 branch
BranchTrueFalseTotal
defined $threads ? : 118 5 123
192 123 ▶ $self->memory = defined $memory ? $memory : 100;
Branch: 1 branch
BranchTrueFalseTotal
defined $memory ? : 118 5 123
193 123 ▶ $self->hours = defined $hours ? $hours : 1;
Branch: 1 branch
BranchTrueFalseTotal
defined $hours ? : 118 5 123
194 123 ▶ $self->tmpdir = defined $tmpdir ? $tmpdir : $SYSTEM_TEMPDIR;
Branch: 1 branch
BranchTrueFalseTotal
defined $tmpdir ? : 118 5 123
195 123 ▶ $self->email_address = defined $email_address ? $email_address : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $email_address ? : 101 22 123
196 123 ▶ $self->email_type = defined $email_when ? $email_when : "none";
Branch: 1 branch
BranchTrueFalseTotal
defined $email_when ? : 105 18 123
197 123 ▶ $self->files = defined $files ? $files : [];
Branch: 1 branch
BranchTrueFalseTotal
defined $files ? : 5 118 123
198 123 ▶ $self->placeholder = defined $placeholder ? $placeholder : "#FILE#";
Branch: 1 branch
BranchTrueFalseTotal
defined $placeholder ? : 6 117 123
199 123 ▶ $self->params_array = defined $params_array ? $params_array : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $params_array ? : 6 117 123
200 123 ▶ $self->params_rows = defined $params_rows ? $params_rows : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $params_rows ? : 6 117 123
201 123 ▶ $self->{array_offset} = defined $array_offset ? $array_offset : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $array_offset ? : 5 118 123
202 123 ▶ $self->{array_tasks} = defined $array_tasks ? $array_tasks : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $array_tasks ? : 5 118 123
203 # Set options
204 123 ▶ $self->opts = defined $$opts_array[0] ? $opts_array : [];
Branch: 1 branch
BranchTrueFalseTotal
defined $opts_array->[0] ? : 101 22 123
205 # Begin time (optional)
206 123 $self->{start_time} = $start_time;
207 123 $self->{start_date} = $start_date;
208
209
210
211
212  
213 123 return $self;
214
215 }
216  
217  
218 132 ▶sub queue : lvalue {
Subroutine queue: called CC=2 SCAR=7.1
Pod documented
219 # Update queue
220 132 my ($self, $new_val) = @_;
221 132 ▶ $self->{queue} = $new_val if (defined $new_val);
Branch: 1 branch
BranchTrueFalseTotal
if defined $new_val 0 132 132
222 132 return $self->{queue};
223 }
224  
225 123 ▶sub threads : lvalue {
Subroutine threads: called CC=2 SCAR=7.1
Pod documented
226 # Update threads
227 123 my ($self, $new_val) = @_;
228 123 ▶ $self->{threads} = $new_val if (defined $new_val);
Branch: 1 branch
BranchTrueFalseTotal
if defined $new_val 0 123 123
229 123 return $self->{threads};
230 }
231  
232 123 ▶sub memory : lvalue {
Subroutine memory: called CC=2 SCAR=7.1
Pod documented
233 # Update memory
234 123 my ($self, $new_val) = @_;
235 123 ▶ $self->{memory} = _mem_parse_mb($new_val) if (defined $new_val);
Branch: 1 branch
BranchTrueFalseTotal
if defined $new_val 0 123 123
236 123 return $self->{memory};
237 }
238  
239 123 ▶sub email_address : lvalue {
Subroutine email_address: called CC=2 SCAR=7.1
Pod documented
240 # Update memory
241 123 my ($self, $new_val) = @_;
242 123 ▶ $self->{email_address} = $new_val if (defined $new_val);
Branch: 1 branch
BranchTrueFalseTotal
if defined $new_val 0 123 123
243 123 return $self->{email_address};
244 }
245  
246 123 ▶sub email_type : lvalue {
Subroutine email_type: called CC=2 SCAR=7.1
Pod documented
247 # Update memory
248 123 my ($self, $new_val) = @_;
249 123 ▶ $self->{email_type} = $new_val if (defined $new_val);
Branch: 1 branch
BranchTrueFalseTotal
if defined $new_val 0 123 123
250 123 return $self->{email_type};
251 }
252  
253 # property files (list of files)
254 124 ▶sub files : lvalue {
Subroutine files: called CC=2 SCAR=7.1
Pod documented
255 # Update files
256 124 my ($self, $new_val) = @_;
257 124 ▶ $self->{files} = $new_val if (defined $new_val);
Branch: 1 branch
BranchTrueFalseTotal
if defined $new_val 0 124 124
258 124 return $self->{files};
259 }
260  
261 124 ▶sub placeholder : lvalue {
Subroutine placeholder: called CC=2 SCAR=7.1
Pod documented
262 # Update placeholder
263 124 my ($self, $new_val) = @_;
264 124 ▶ $self->{placeholder} = $new_val if (defined $new_val);
Branch: 1 branch
BranchTrueFalseTotal
if defined $new_val 0 124 124
265 124 return $self->{placeholder};
266 }
267 130 ▶sub params_array : lvalue {
Subroutine params_array: called CC=2 SCAR=7.1
Pod documented
268 130 my ($self, $new_val) = @_;
269 130 ▶ $self->{params_array} = $new_val if (defined $new_val);
Branch: 1 branch
BranchTrueFalseTotal
if defined $new_val 0 130 130
270 130 return $self->{params_array};
271 }
272 123 ▶sub params_rows : lvalue {
Subroutine params_rows: called CC=2 SCAR=7.1
Pod documented
273 123 my ($self, $new_val) = @_;
274 123 ▶ $self->{params_rows} = $new_val if (defined $new_val);
Branch: 1 branch
BranchTrueFalseTotal
if defined $new_val 0 123 123
275 123 return $self->{params_rows};
276 }
277 0 ▶sub start_time : lvalue {
Subroutine start_time: not called CC=2 SCAR=17.9
Pod documented
278 0 my ($self, $new_val) = @_;
279 0 ▶ $self->{start_time} = _parse_start_time($new_val) if defined $new_val;
Branch: 1 branch
BranchTrueFalseTotal
if defined $new_val 0 0 0
280 0 return $self->{start_time};
281 }
282  
283 0 ▶sub start_date : lvalue {
Subroutine start_date: not called CC=2 SCAR=17.9
Pod documented
284 0 my ($self, $new_val) = @_;
285 0 ▶ $self->{start_date} = _parse_start_date($new_val) if defined $new_val;
Branch: 1 branch
BranchTrueFalseTotal
if defined $new_val 0 0 0
286 0 return $self->{start_date};
287 }
288  
289 773 ▶sub is_array {
Subroutine is_array: called CC=2 SCAR=6.9
Pod documented
290 # Check if the job is an array
291 773 my $self = shift @_;
292 773 ▶ return $self->is_files_array() || $self->is_params_array();
Condition100%3/3
$self->is_files_array || $self->is_params_array
l!l&&r!l&&!r
433736
MC/DC100%2/2
$self->is_files_array || $self->is_params_array
$self->is_files_array $self->is_params_array
Truth table100%3/3
A || B
A= $self->is_files_arrayB= $self->is_params_array
ABresult
1X1
011
000
293 }
294 863 ▶sub is_files_array {
Subroutine is_files_array: called CC=1 SCAR=0.0
Pod documented
295 863 my $self = shift @_;
296 863 return scalar @{$self->{files}} > 0;
297 }
298 855 ▶sub is_params_array {
Subroutine is_params_array: called CC=2 SCAR=7.1
Pod documented
299 855 my $self = shift @_;
300 855 ▶ return defined $self->{params_array} && $self->{params_rows} > 0;
Condition66%2/3
defined $self->{'params_array'} && $self->{'params_rows'} > 0
!ll&&!rl&&r
7410114
MC/DC50%1/2
defined $self->{'params_array'} && $self->{'params_rows'} > 0
defined $self->{'params_array'} $self->{'params_rows'} > 0
Truth table66%2/3
A && B
A= defined $self->{'params_array'}B= $self->{'params_rows'} > 0
ABresult
0X0
100
111
301 }
302 3 ▶sub array_size {
Subroutine array_size: called CC=3 SCAR=11.3
Pod documented
303 3 my $self = shift @_;
304 3 ▶ return scalar @{$self->{files}} if $self->is_files_array();
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_files_array 1 2 3
305 2 ▶ return $self->{params_rows} if $self->is_params_array();
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_params_array 2 0 2
306 0 return 0;
307 }
308 124 ▶sub hours : lvalue {
Subroutine hours: called CC=2 SCAR=7.1
Pod documented
309 # Update memory
310 124 my ($self, $new_val) = @_;
311 124 ▶ $self->{hours} = _time_to_hour($new_val) if (defined $new_val);
Branch: 1 branch
BranchTrueFalseTotal
if defined $new_val 0 124 124
312 124 return $self->{hours};
313 }
314  
315 362 ▶sub tmpdir : lvalue {
Subroutine tmpdir: called CC=2 SCAR=7.1
Pod documented
316 # Update tmpdir
317 362 my ($self, $new_val) = @_;
318 362 ▶ $self->{tmpdir} = $new_val if (defined $new_val);
Branch: 1 branch
BranchTrueFalseTotal
if defined $new_val 0 362 362
319 362 return $self->{tmpdir};
320 }
321  
322 123 ▶sub opts : lvalue {
Subroutine opts: called CC=4 SCAR=20.4
Pod documented
323 # Update opts
324 123 my ($self, $new_val) = @_;
325 123 ▶ if (not defined $self->{opts}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"opts"}) 123 0 123
326 123 $self->{opts} = [];
327 123 return $self->{opts};
328 }
329 # check newval is an array
330 0 ▶ confess "ERROR NBI::Opts: opts must be an array, got $new_val\n" if (ref($new_val) ne "ARRAY");
Branch: 1 branch
BranchTrueFalseTotal
if ref $new_val ne "ARRAY" 0 0 0
331 0 ▶ $self->{opts} = $new_val if (defined $new_val);
Branch: 1 branch
BranchTrueFalseTotal
if defined $new_val 0 0 0
332 0 return $self->{opts};
333 }
334 2 ▶sub add_option {
Subroutine add_option: called CC=1 SCAR=0.0
Pod documented
335 # Add an option
336 2 my ($self, $new_val) = @_;
337 2 push @{$self->{opts}}, $new_val;
338 2 return $self->{opts};
339 }
340  
341 1 ▶sub opts_count {
Subroutine opts_count: called CC=2 SCAR=7.1
Pod documented
342 # Return the number of options
343 1 my $self = shift @_;
344 1 ▶ return defined $self->{opts} ? scalar @{$self->{opts}} : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'opts'} ? : 1 0 1
345 }
346  
347 203 ▶sub view {
Subroutine view: called CC=4 SCAR=13.9
Pod documented
348 # Return a string representation of the object
349 203 my $self = shift @_;
350 203 my $str = " --- NBI::Opts object ---\n";
351 203 $str .= " queue:\t" . $self->{queue} . "\n";
352 203 $str .= " threads:\t" . $self->{threads} . "\n";
353 203 $str .= " memory MB:\t" . $self->{memory} . "\n";
354 203 $str .= " time (h):\t" . $self->{hours} . "\n";
355 203 $str .= " tmpdir:\t" . $self->{tmpdir} . "\n";
356 203 my $begin = $self->_compute_begin();
357 203 ▶ $str .= " begin:\t" . $begin . "\n" if defined $begin;
Branch: 1 branch
BranchTrueFalseTotal
if defined $begin 0 203 203
358 203 $str .= " ---------------------------\n";
359 203 for my $o (@{$self->{opts}}) {
360 203 ▶ $str .= "#SBATCH $o\n" if defined $o;
Branch: 1 branch
BranchTrueFalseTotal
if defined $o 203 0 203
361 }
362 203 return $str;
363 }
364  
365 215 ▶sub header {
Subroutine header: called CC=7 SCAR=19.5
Pod documented
366 # Return a header for the script based on the options
367 215 my $self = shift @_;
368 215 my $str = "#!/bin/bash\n";
369 # Queue
370 215 $str .= "#SBATCH -p " . $self->{queue} . "\n";
371 # Nodes: 1
372 215 $str .= "#SBATCH -N 1\n";
373 # Time
374 215 $str .= "#SBATCH -t " . $self->timestring() . "\n";
375 # Memory
376 215 $str .= "#SBATCH --mem=" . $self->{memory} . "\n";
377 # Threads
378 215 $str .= "#SBATCH -c " . $self->{threads} . "\n";
379 # Mail
380 215 ▶ if (defined $self->{email_address}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"email_address"}) 202 13 215
381 202 $str .= "#SBATCH --mail-user=" . $self->{email_address} . "\n";
382 202 $str .= "#SBATCH --mail-type=" . $self->{email_type} . "\n";
383 }
384  
385 # Custom options
386 215 for my $o (@{$self->{opts}}) {
387 203 ▶ next if not defined $o;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $o 0 203 203
388 203 $str .= "#SBATCH $o\n";
389 }
390  
391 # Job array
392 215 ▶ if ($self->is_array()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_array) 8 207 215
393 8 ▶ my $tasks = $self->{array_tasks} > 0 ? $self->{array_tasks} : $self->array_size();
Branch: 1 branch
BranchTrueFalseTotal
$self->{'array_tasks'} > 0 ? : 5 3 8
394 8 my $len = $tasks - 1;
395 8 $str .= "#SBATCH --array=0-$len\n";
396 }
397 # Begin time
398 215 my $begin = $self->_compute_begin();
399 215 ▶ $str .= "#SBATCH --begin=$begin\n" if defined $begin;
Branch: 1 branch
BranchTrueFalseTotal
if defined $begin 1 214 215
400 215 return $str;
401 }
402  
403 215 ▶sub timestring {
Subroutine timestring: called CC=1 SCAR=0.0
Pod documented
404 215 my $self = shift @_;
405 215 my $hours = $self->{hours};
406 215 my $days = 0+ int($hours / 24);
407 215 $hours = $hours % 24;
408 # Format hours to be 2 digits
409 215 $hours = sprintf("%02d", $hours);
410 215 return "${days}-${hours}:00:00";
411 }
412  
413 118 ▶sub _mem_parse_mb {
Subroutine _mem_parse_mb: called CC=7 SCAR=27.6
414 118 my $mem = shift @_;
415 118 ▶ if ($mem=~/^(\d+)$/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($mem =~ /^(\d+)$/u) { } 108 10 118
elsif ($mem =~ /^(\d+)\.?(MB?|GB?|TB?|KB?)$/iu) { } 10 0 10
416 # bare number: interpret as MB
417 108 return $mem;
418 } elsif ($mem=~/^(\d+)\.?(MB?|GB?|TB?|KB?)$/i) {
419 10 ▶ if (substr(uc($2), 0, 1) eq "G") {
Branch: 4 branches
BranchTrueFalseTotal
if (substr(uc $2, 0, 1) eq 'G') { } 10 0 10
elsif (substr(uc $2, 0, 1) eq 'T') { } 0 0 0
elsif (substr(uc $2, 0, 1) eq 'M') { } 0 0 0
elsif (substr(uc $2, 0, 1) eq 'K') { } 0 0 0
420 10 $mem = $1 * 1024;
421 } elsif (substr(uc($2), 0, 1) eq "T") {
422 0 $mem = $1 * 1024 * 1024;
423 } elsif (substr(uc($2), 0, 1) eq "M") {
424 0 $mem = $1;
425 } elsif (substr(uc($2), 0, 1) eq "K") {
426 0 $mem = int($1/1024);
427 } else {
428 # Consider MB
429 0 $mem = $1;
430 }
431 } else {
432 0 confess "ERROR NBI::Opts: Cannot parse memory value $mem\n";
433 }
434 10 return $mem;
435 }
436  
437 125 ▶sub _time_to_hour {
Subroutine _time_to_hour: called CC=7 SCAR=19.5
438 # Get an integer (hours) or a string in the format \d+D \d+H \d+M \d+S
439 125 my $time = shift @_;
440 125 $time = uc($time);
441
442 125 ▶ if ($time =~ /^(\d+)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($time =~ /^(\d+)$/u) { } 1 124 125
443 # Got an integer
444 1 return $1;
445 } else {
446 124 my $hours = 0;
447 124 ▶ while ($time =~ /(\d+)([DHMS])/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($time =~ /(\d+)([DHMS])/gu) 426 124 550
448 426 my $val = $1;
449 426 my $unit = $2;
450 426 ▶ if ($unit eq "D") {
Branch: 4 branches
BranchTrueFalseTotal
if ($unit eq 'D') { } 102 324 426
elsif ($unit eq 'H') { } 121 203 324
elsif ($unit eq 'M') { } 102 101 203
elsif ($unit eq 'S') { } 101 0 101
451 102 $hours += $val * 24;
452 } elsif ($unit eq "H") {
453 121 $hours += $val;
454 } elsif ($unit eq "M") {
455 102 $hours += $val / 60;
456 } elsif ($unit eq "S") {
457 101 $hours += $val / 3600;
458 } else {
459 0 die "ERROR NBI::Opts: Cannot parse time value $time\n";
460 }
461 }
462 124 return $hours;
463 }
464 }
465  
466  
467 1 ▶sub _parse_start_time {
Subroutine _parse_start_time: called CC=6 SCAR=19.9
468 # Accept H:MM, HH:MM, H:MM:SS, HH:MM:SS (24h only). Returns "HH:MM:SS".
469 1 my $time = shift;
470 1 ▶ unless ($time =~ /^(\d{1,2}):(\d{2})(?::(\d{2}))?$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($time =~ /^(\d{1,2}):(\d{2})(?::(\d{2}))?$/u) 0 1 1
471 0 confess "ERROR NBI::Opts: Cannot parse start time '$time'. Use HH:MM or HH:MM:SS (24h format)\n";
472 }
473 1 ▶ my ($h, $m, $s) = ($1, $2, $3 // 0);
Condition50%1/2
$3 // 0
l!l
10
MC/DC0%0/1
$3 // 0
$3
Truth table50%1/2
A
A= $3
Aresult
11
00
474 1 ▶ confess "ERROR NBI::Opts: Invalid hour $h in '$time' (must be 0-23)\n" if $h > 23;
Branch: 1 branch
BranchTrueFalseTotal
if $h > 23 0 1 1
475 1 ▶ confess "ERROR NBI::Opts: Invalid minute $m in '$time' (must be 0-59)\n" if $m > 59;
Branch: 1 branch
BranchTrueFalseTotal
if $m > 59 0 1 1
476 1 ▶ confess "ERROR NBI::Opts: Invalid second $s in '$time' (must be 0-59)\n" if $s > 59;
Branch: 1 branch
BranchTrueFalseTotal
if $s > 59 0 1 1
477 1 return sprintf("%02d:%02d:%02d", $h, $m, $s);
478 }
479  
480 1 ▶sub _parse_start_date {
Subroutine _parse_start_date: called CC=12 SCAR=48.6
481 # Accept DD/MM or DD/MM/YYYY. Returns "YYYY-MM-DD".
482 # When year is omitted, infers current year; if that date is already past, uses next year.
483 # Also accepts the already-normalised YYYY-MM-DD form (idempotent).
484 1 my $date = shift;
485  
486 # Already normalised by a previous call - pass through unchanged.
487 1 ▶ return $date if $date =~ /^\d{4}-\d{2}-\d{2}$/;
Branch: 1 branch
BranchTrueFalseTotal
if $date =~ /^\d{4}-\d{2}-\d{2}$/u 1 0 1
488  
489 0 ▶ unless ($date =~ m{^(\d{1,2})/(\d{1,2})(?:/(\d{4}))?$}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($date =~ m[^(\d{1,2})/(\d{1,2})(?:/(\d{4}))?$]u) 0 0 0
490 0 confess "ERROR NBI::Opts: Cannot parse start date '$date'. Use DD/MM or DD/MM/YYYY\n";
491 }
492 0 my ($day, $mon, $year) = ($1, $2, $3);
493 0 ▶ confess "ERROR NBI::Opts: Invalid month $mon in '$date' (must be 1-12)\n" if $mon < 1 || $mon > 12;
Branch: 1 branch
BranchTrueFalseTotal
if $mon < 1 or $mon > 12 0 0 0
Condition0%0/3
$mon < 1 or $mon > 12
l!l&&r!l&&!r
000
MC/DC0%0/2
$mon < 1 or $mon > 12
$mon < 1 $mon > 12
Truth table0%0/3
A or B
A= $mon < 1B= $mon > 12
ABresult
1X1
011
000
494 0 ▶ confess "ERROR NBI::Opts: Invalid day $day in '$date' (must be 1-31)\n" if $day < 1 || $day > 31;
Branch: 1 branch
BranchTrueFalseTotal
if $day < 1 or $day > 31 0 0 0
Condition0%0/3
$day < 1 or $day > 31
l!l&&r!l&&!r
000
MC/DC0%0/2
$day < 1 or $day > 31
$day < 1 $day > 31
Truth table0%0/3
A or B
A= $day < 1B= $day > 31
ABresult
1X1
011
000
495  
496 0 ▶ if (!defined $year) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $year) 0 0 0
497 0 my @now = localtime(time);
498 0 my $curr_year = $now[5] + 1900;
499 # midnight today for comparison
500 0 my $today_midnight = mktime(0, 0, 0, $now[3], $now[4], $now[5]);
501 0 my $candidate = mktime(0, 0, 0, $day, $mon - 1, $curr_year - 1900);
502 0 ▶ $year = ($candidate >= $today_midnight) ? $curr_year : $curr_year + 1;
Branch: 1 branch
BranchTrueFalseTotal
$candidate >= $today_midnight ? : 0 0 0
503 }
504  
505 # Validate via mktime round-trip (catches e.g. 31/02)
506 0 my $epoch = mktime(0, 0, 12, $day, $mon - 1, $year - 1900);
507 0 my @check = localtime($epoch);
508 0 ▶ if ($check[3] != $day || $check[4] + 1 != $mon || $check[5] + 1900 != $year) {
Branch: 1 branch
BranchTrueFalseTotal
if ($check[3] != $day or $check[4] + 1 != $mon or $check[5] + 1900 != $year) 0 0 0
Condition0%0/6
$check[3] != $day or $check[4] + 1 != $mon or $check[5] + 1900 != $year
l!l&&r!l&&!r
000
$check[3] != $day or $check[4] + 1 != $mon
l!l&&r!l&&!r
000
MC/DC0%0/3
$check[3] != $day or $check[4] + 1 != $mon or $check[5] + 1900 != $year
$check[3] != $day $check[4] + 1 != $mon $check[5] + 1900 != $year
Truth table0%0/4
A or B or C
A= $check[3] != $dayB= $check[4] + 1 != $monC= $check[5] + 1900 != $year
ABCresult
1XX1
01X1
0011
0000
509 0 confess "ERROR NBI::Opts: Invalid date '$date' (day out of range for that month)\n";
510 }
511  
512 0 return sprintf("%04d-%02d-%02d", $year, $mon, $day);
513 }
514  
515 418 ▶sub _compute_begin {
Subroutine _compute_begin: called CC=6 SCAR=22.0
516 # Combine start_date and start_time into a SLURM --begin string.
517 # Returns undef when neither is set.
518 # Always normalises through the parsers so lvalue assignment of raw strings works.
519 418 my $self = shift;
520 418 ▶ return undef unless defined $self->{start_time} || defined $self->{start_date};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"start_time"} or defined $self->{"start_date"} 417 1 418
Condition66%2/3
defined $self->{"start_time"} or defined $self->{"start_date"}
l!l&&r!l&&!r
10417
MC/DC50%1/2
defined $self->{"start_time"} or defined $self->{"start_date"}
defined $self->{"start_time"} defined $self->{"start_date"}
Truth table66%2/3
A or B
A= defined $self->{"start_time"}B= defined $self->{"start_date"}
ABresult
1X1
011
000
521  
522 my $time_str = defined $self->{start_time}
523 ? _parse_start_time($self->{start_time})
524 1 ▶ : "00:00:00";
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'start_time'} ? : 1 0 1
525 1 my ($h, $m, $s) = split /:/, $time_str;
526  
527 1 my $date_str;
528 1 ▶ if (defined $self->{start_date}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'start_date'}) { } 1 0 1
529 1 $date_str = _parse_start_date($self->{start_date});
530 } else {
531 # Only time given: use today if that time is still in the future, else tomorrow.
532 0 my @now = localtime(time);
533 0 my $today_begin = mktime($s, $m, $h, $now[3], $now[4], $now[5]);
534 0 ▶ if ($today_begin > time()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($today_begin > time) { } 0 0 0
535 0 $date_str = strftime("%Y-%m-%d", @now);
536 } else {
537 # mktime normalises day overflow correctly (e.g. 31 -> 1st of next month)
538 0 my @tomorrow = localtime(mktime(0, 0, 0, $now[3] + 1, $now[4], $now[5]));
539 0 $date_str = strftime("%Y-%m-%d", @tomorrow);
540 }
541 }
542  
543 1 return "${date_str}T${time_str}";
544 }
545  
546 29 1;
547  
548 __END__
« bin/lsjobs ↑ Summary bin/session »