|
1 | 1 | # utility functions for the SLURM executor plugin |
2 | 2 |
|
| 3 | +import math |
3 | 4 | import os |
4 | 5 | import re |
5 | 6 | from pathlib import Path |
| 7 | +from typing import Union |
6 | 8 |
|
7 | 9 | from snakemake_interface_executor_plugins.jobs import ( |
8 | 10 | JobExecutorInterface, |
9 | 11 | ) |
10 | 12 | from snakemake_interface_common.exceptions import WorkflowError |
11 | 13 |
|
12 | 14 |
|
| 15 | +def round_half_up(n): |
| 16 | + return int(math.floor(n + 0.5)) |
| 17 | + |
| 18 | + |
| 19 | +def parse_time_to_minutes(time_value: Union[str, int, float]) -> int: |
| 20 | + """ |
| 21 | + Convert a time specification to minutes (integer). This function |
| 22 | + is intended to handle the partition definitions for the max_runtime |
| 23 | + value in a partition config file. |
| 24 | +
|
| 25 | + Supports: |
| 26 | + - Numeric values (assumed to be in minutes): 120, 120.5 |
| 27 | + - Snakemake-style time strings: "6d", "12h", "30m", "90s", "2d12h30m" |
| 28 | + - SLURM time formats: |
| 29 | + - "minutes" (e.g., "60") |
| 30 | + - "minutes:seconds" (interpreted as hours:minutes, e.g., "60:30") |
| 31 | + - "hours:minutes:seconds" (e.g., "1:30:45") |
| 32 | + - "days-hours" (e.g., "2-12") |
| 33 | + - "days-hours:minutes" (e.g., "2-12:30") |
| 34 | + - "days-hours:minutes:seconds" (e.g., "2-12:30:45") |
| 35 | +
|
| 36 | + Args: |
| 37 | + time_value: Time specification as string, int, or float |
| 38 | +
|
| 39 | + Returns: |
| 40 | + Time in minutes as integer (fractional minutes are rounded) |
| 41 | +
|
| 42 | + Raises: |
| 43 | + WorkflowError: If the time format is invalid |
| 44 | + """ |
| 45 | + # If already numeric, return as integer minutes (rounded) |
| 46 | + if isinstance(time_value, (int, float)): |
| 47 | + return round_half_up(time_value) # implicit conversion to int |
| 48 | + |
| 49 | + # Convert to string and strip whitespace |
| 50 | + time_str = str(time_value).strip() |
| 51 | + |
| 52 | + # Try to parse as plain number first |
| 53 | + try: |
| 54 | + return round_half_up(float(time_str)) # implicit conversion to int |
| 55 | + except ValueError: |
| 56 | + pass |
| 57 | + |
| 58 | + # Try SLURM time formats first (with colons and dashes) |
| 59 | + # Format: days-hours:minutes:seconds or variations |
| 60 | + if "-" in time_str or ":" in time_str: |
| 61 | + try: |
| 62 | + days = 0 |
| 63 | + hours = 0 |
| 64 | + minutes = 0 |
| 65 | + seconds = 0 |
| 66 | + |
| 67 | + # Split by dash first (days separator) |
| 68 | + if "-" in time_str: |
| 69 | + parts = time_str.split("-") |
| 70 | + if len(parts) != 2: |
| 71 | + raise ValueError("Invalid format with dash") |
| 72 | + days = int(parts[0]) |
| 73 | + time_str = parts[1] |
| 74 | + |
| 75 | + # Split by colon (time separator) |
| 76 | + time_parts = time_str.split(":") |
| 77 | + |
| 78 | + if len(time_parts) == 1: |
| 79 | + # Just hours (after dash) or just minutes |
| 80 | + if days > 0: |
| 81 | + hours = int(time_parts[0]) |
| 82 | + else: |
| 83 | + minutes = int(time_parts[0]) |
| 84 | + elif len(time_parts) == 2: |
| 85 | + # was: days-hours:minutes |
| 86 | + hours = int(time_parts[0]) |
| 87 | + minutes = int(time_parts[1]) |
| 88 | + elif len(time_parts) == 3: |
| 89 | + # was: hours:minutes:seconds |
| 90 | + hours = int(time_parts[0]) |
| 91 | + minutes = int(time_parts[1]) |
| 92 | + seconds = int(time_parts[2]) |
| 93 | + else: |
| 94 | + raise ValueError("Too many colons in time format") |
| 95 | + |
| 96 | + # Convert everything to minutes |
| 97 | + total_minutes = days * 24 * 60 + hours * 60 + minutes + seconds / 60.0 |
| 98 | + return round_half_up(total_minutes) # implicit conversion to int |
| 99 | + |
| 100 | + except (ValueError, IndexError): |
| 101 | + # If SLURM format parsing fails, try Snakemake style below |
| 102 | + pass |
| 103 | + |
| 104 | + # Parse Snakemake-style time strings (e.g., "6d", "12h", "30m", "90s", "2d12h30m") |
| 105 | + # Pattern matches: optional number followed by unit (d, h, m, s) |
| 106 | + pattern = r"(\d+(?:\.\d+)?)\s*([dhms])" |
| 107 | + matches = re.findall(pattern, time_str.lower()) |
| 108 | + |
| 109 | + if not matches: |
| 110 | + raise WorkflowError( |
| 111 | + f"Invalid time format: '{time_value}'. " |
| 112 | + f"Expected formats:\n" |
| 113 | + f" - Numeric value in minutes: 120\n" |
| 114 | + f" - Snakemake style: '6d', '12h', '30m', '90s', '2d12h30m'\n" |
| 115 | + f" - SLURM style: 'minutes', 'minutes:seconds', 'hours:minutes:seconds',\n" |
| 116 | + f" 'days-hours', 'days-hours:minutes', 'days-hours:minutes:seconds'" |
| 117 | + ) |
| 118 | + |
| 119 | + total_minutes = 0.0 |
| 120 | + for value, unit in matches: |
| 121 | + num = float(value) |
| 122 | + if unit == "d": |
| 123 | + total_minutes += num * 24 * 60 |
| 124 | + elif unit == "h": |
| 125 | + total_minutes += num * 60 |
| 126 | + elif unit == "m": |
| 127 | + total_minutes += num |
| 128 | + elif unit == "s": |
| 129 | + total_minutes += num / 60 |
| 130 | + |
| 131 | + return round_half_up(total_minutes) |
| 132 | + |
| 133 | + |
13 | 134 | def delete_slurm_environment(): |
14 | 135 | """ |
15 | 136 | Function to delete all environment variables |
|
0 commit comments