Coverage for melissa/scheduler/slurm_parser.py: 13%
39 statements
« prev ^ index » next coverage.py v7.2.7, created at 2023-09-22 10:36 +0000
« prev ^ index » next coverage.py v7.2.7, created at 2023-09-22 10:36 +0000
1#!/usr/bin/python3
3# Copyright (c) 2020-2022, Institut National de Recherche en Informatique et en Automatique (Inria)
4# All rights reserved.
5#
6# Redistribution and use in source and binary forms, with or without
7# modification, are permitted provided that the following conditions are met:
8#
9# * Redistributions of source code must retain the above copyright notice, this
10# list of conditions and the following disclaimer.
11#
12# * Redistributions in binary form must reproduce the above copyright notice,
13# this list of conditions and the following disclaimer in the documentation
14# and/or other materials provided with the distribution.
15#
16# * Neither the name of the copyright holder nor the names of its
17# contributors may be used to endorse or promote products derived from
18# this software without specific prior written permission.
19#
20# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31from typing import List
34def break_comma2str(node_list: str, node_list_str: List[str]) -> List[str]:
35 """
36 parses list of comma separated strings
37 """
38 return node_list_str + [arg for arg in node_list.split(",")]
41def break_hyphen2str(node_list: str, node_list_str: List[str]) -> List[str]:
42 """
43 extracts list of numbers separated by hyphen characters
44 """
45 return node_list_str + [
46 str(arg) for arg in range(int(node_list.split("-")[0]), int(node_list.split("-")[-1]) + 1)
47 ]
50def break_bracket2list(bracketed_text: str) -> List[str]:
51 """
52 extracts list of numbers from numbers between brackets and possibly
53 comma or hyphen separated
54 """
55 list_of_numbers_str: List[str] = []
56 # treat comma separated arguments possibly containing hyphens
57 if "," in bracketed_text:
58 raw_list_of_numbers = break_comma2str(bracketed_text, [])
59 for arg in raw_list_of_numbers:
60 if "-" in arg:
61 list_of_numbers_str = break_hyphen2str(arg, list_of_numbers_str)
62 else:
63 list_of_numbers_str.append(arg)
64 # treat hyphen separated arguments
65 else:
66 list_of_numbers_str = break_hyphen2str(bracketed_text, list_of_numbers_str)
67 return list_of_numbers_str
70def break2str(node_list: str) -> List[str]:
71 """
72 turns SLURM list of nodes into a Python consistent list of strings
73 - node_list [input] string obtained from SLURM_NODELIST
74 - node_list_str [output] list of strings containing all node names
75 """
76 node_list_str: List[str] = []
77 while True:
78 # if arguments with brackets
79 if "[" in node_list:
80 idx_start_br = node_list.find("[")
81 idx_end_br = node_list.find("]")
82 # extract node numbers between brackets
83 br_str = node_list[idx_start_br + 1 : idx_end_br]
84 # process string before brackets
85 pre_node_list = node_list[:idx_start_br]
86 if "," in pre_node_list:
87 # latest preceding comma
88 last_comma_idx = len(pre_node_list) - 1 - pre_node_list[::-1].index(",")
89 # get listed node name
90 node_sublist_name = node_list[last_comma_idx + 1 : idx_start_br]
91 # all nodes before comma can be treated with break_comma2str
92 node_list_str = break_comma2str(pre_node_list[:last_comma_idx], node_list_str)
93 else:
94 # get the bracket listed node name
95 node_sublist_name = node_list[:idx_start_br]
96 # treat brackets arguments
97 node_list_str += [node_sublist_name + arg for arg in break_bracket2list(br_str)]
98 # remove treated part from node list
99 try:
100 node_list = (
101 node_list[idx_end_br + 1 :]
102 if node_list[idx_end_br + 1] != ","
103 else node_list[idx_end_br + 2 :]
104 )
105 # empty str if remaining list is empty
106 except IndexError:
107 node_list = ""
108 # if only comma separated arguments
109 else:
110 break
111 if "," in node_list:
112 node_list_str = break_comma2str(node_list, node_list_str)
113 elif node_list != "":
114 node_list_str.append(node_list)
115 return node_list_str