1
|
|
|
import re |
2
|
|
|
import hashlib |
3
|
|
|
import unicodedata |
4
|
|
|
import json |
5
|
|
|
|
6
|
|
|
from distutils.util import strtobool |
7
|
|
|
from subprocess import Popen, PIPE |
8
|
|
|
|
9
|
|
|
def jobname_generator(jobname, job_id): |
10
|
|
|
'''Crop the jobname to a maximum of 64 characters. |
11
|
|
|
Parameters |
12
|
|
|
---------- |
13
|
|
|
jobname : str |
14
|
|
|
Initial jobname. |
15
|
|
|
job_id: str |
16
|
|
|
ID of the job in the current batch. |
17
|
|
|
Returns |
18
|
|
|
------- |
19
|
|
|
str |
20
|
|
|
The cropped version of the string. |
21
|
|
|
''' |
22
|
|
|
# 64 - 1 since the total length including -1 should be less than 64 |
23
|
|
|
if len(jobname) + len(job_id) > 63: |
24
|
|
|
croped_string = '{}_{}'.format(jobname[:63 - len(job_id)], job_id) |
25
|
|
|
else: |
26
|
|
|
croped_string = '{}_{}'.format(jobname, job_id) |
27
|
|
|
return croped_string |
28
|
|
|
|
29
|
|
|
def print_boxed(string): |
30
|
|
|
splitted_string = string.split('\n') |
31
|
|
|
max_len = max(map(len, splitted_string)) |
32
|
|
|
box_line = u"\u2500" * (max_len + 2) |
33
|
|
|
|
34
|
|
|
out = u"\u250c" + box_line + u"\u2510\n" |
35
|
|
|
out += '\n'.join([u"\u2502 {} \u2502".format(line.ljust(max_len)) for line in splitted_string]) |
36
|
|
|
out += u"\n\u2514" + box_line + u"\u2518" |
37
|
|
|
print out |
38
|
|
|
|
39
|
|
|
|
40
|
|
|
def yes_no_prompt(query, default=None): |
41
|
|
|
available_prompts = {None: " [y/n] ", 'y': " [Y/n] ", 'n': " [y/N] "} |
42
|
|
|
|
43
|
|
|
if default not in available_prompts: |
44
|
|
|
raise ValueError("Invalid default: '{}'".format(default)) |
45
|
|
|
|
46
|
|
|
while True: |
47
|
|
|
try: |
48
|
|
|
answer = raw_input("{0}{1}".format(query, available_prompts[default])) |
49
|
|
|
return strtobool(answer) |
50
|
|
|
except ValueError: |
51
|
|
|
if answer == '' and default is not None: |
52
|
|
|
return strtobool(default) |
53
|
|
|
|
54
|
|
|
|
55
|
|
|
def chunks(sequence, n): |
56
|
|
|
""" Yield successive n-sized chunks from sequence. """ |
57
|
|
|
for i in xrange(0, len(sequence), n): |
58
|
|
|
yield sequence[i:i + n] |
59
|
|
|
|
60
|
|
|
|
61
|
|
|
def generate_uid_from_string(value): |
62
|
|
|
""" Create unique identifier from a string. """ |
63
|
|
|
return hashlib.sha256(value).hexdigest() |
64
|
|
|
|
65
|
|
|
|
66
|
|
|
def slugify(value): |
67
|
|
|
""" |
68
|
|
|
Converts to lowercase, removes non-word characters (alphanumerics and |
69
|
|
|
underscores) and converts spaces to underscores. Also strips leading and |
70
|
|
|
trailing whitespace. |
71
|
|
|
|
72
|
|
|
Reference |
73
|
|
|
--------- |
74
|
|
|
https://github.com/django/django/blob/1.7c3/django/utils/text.py#L436 |
75
|
|
|
""" |
76
|
|
|
value = unicodedata.normalize('NFKD', unicode(value, "UTF-8")).encode('ascii', 'ignore').decode('ascii') |
77
|
|
|
value = re.sub('[^\w\s-]', '', value).strip().lower() |
78
|
|
|
return str(re.sub('[-\s]+', '_', value)) |
79
|
|
|
|
80
|
|
|
|
81
|
|
|
def encode_escaped_characters(text, escaping_character="\\"): |
82
|
|
|
""" Escape the escaped character using its hex representation """ |
83
|
|
|
def hexify(match): |
84
|
|
|
return "\\x{0}".format(match.group()[-1].encode("hex")) |
85
|
|
|
|
86
|
|
|
return re.sub(r"\\.", hexify, text) |
87
|
|
|
|
88
|
|
|
|
89
|
|
|
def decode_escaped_characters(text): |
90
|
|
|
""" Convert hex representation to the character it represents """ |
91
|
|
|
if len(text) == 0: |
92
|
|
|
return '' |
93
|
|
|
|
94
|
|
|
def unhexify(match): |
95
|
|
|
return match.group()[2:].decode("hex") |
96
|
|
|
|
97
|
|
|
return re.sub(r"\\x..", unhexify, text) |
98
|
|
|
|
99
|
|
|
|
100
|
|
|
def save_dict_to_json_file(path, dictionary): |
101
|
|
|
with open(path, "w") as json_file: |
102
|
|
|
json_file.write(json.dumps(dictionary, indent=4, separators=(',', ': '))) |
103
|
|
|
|
104
|
|
|
|
105
|
|
|
def load_dict_from_json_file(path): |
106
|
|
|
with open(path, "r") as json_file: |
107
|
|
|
return json.loads(json_file.read()) |
108
|
|
|
|
109
|
|
|
|
110
|
|
|
def detect_cluster(): |
111
|
|
|
# Get server status |
112
|
|
|
try: |
113
|
|
|
output = Popen(["qstat", "-B"], stdout=PIPE).communicate()[0] |
114
|
|
|
except OSError: |
115
|
|
|
# If qstat is not available we assume that the cluster is unknown. |
116
|
|
|
return None |
117
|
|
|
# Get server name from status |
118
|
|
|
server_name = output.split('\n')[2].split(' ')[0] |
119
|
|
|
# Cleanup the name and return it |
120
|
|
|
cluster_name = None |
121
|
|
|
if server_name.split('.')[-1] == 'm': |
122
|
|
|
cluster_name = "mammouth" |
123
|
|
|
elif server_name.split('.')[-1] == 'guil': |
124
|
|
|
cluster_name = "guillimin" |
125
|
|
|
elif server_name.split('.')[-1] == 'helios': |
126
|
|
|
cluster_name = "helios" |
127
|
|
|
elif server_name.split('.')[-1] == 'hades': |
128
|
|
|
cluster_name = "hades" |
129
|
|
|
return cluster_name |
130
|
|
|
|
131
|
|
|
|
132
|
|
|
def get_launcher(cluster_name): |
133
|
|
|
if cluster_name == "helios": |
134
|
|
|
return "msub" |
135
|
|
|
else: |
136
|
|
|
return "qsub" |
137
|
|
|
|