Skip to content

Commit 2bce845

Browse files
committed
Changed print to be Python 3 compliant, convert Popen's output from bytes to str and removed function file in argparse
1 parent 845a840 commit 2bce845

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

scripts/smart_dispatch.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#!/usr/bin/env python
22
# -*- coding: utf-8 -*-
3+
from __future__ import print_function
34

45
import os
56
import sys
@@ -118,8 +119,8 @@ def main():
118119
pbs_filenames = job_generator.write_pbs_files(path_job_commands)
119120

120121
# Launch the jobs
121-
print "## {nb_commands} command(s) will be executed in {nb_jobs} job(s) ##".format(nb_commands=nb_commands, nb_jobs=len(pbs_filenames))
122-
print "Batch UID:\n{batch_uid}".format(batch_uid=jobname)
122+
print("## {nb_commands} command(s) will be executed in {nb_jobs} job(s) ##".format(nb_commands=nb_commands, nb_jobs=len(pbs_filenames)))
123+
print("Batch UID:\n{batch_uid}".format(batch_uid=jobname))
123124
if not args.doNotLaunch:
124125
jobs_id = []
125126
for pbs_filename in pbs_filenames:
@@ -129,8 +130,8 @@ def main():
129130
with utils.open_with_lock(pjoin(path_job, "jobs_id.txt"), 'a') as jobs_id_file:
130131
jobs_id_file.writelines(t.strftime("## %Y-%m-%d %H:%M:%S ##\n"))
131132
jobs_id_file.writelines("\n".join(jobs_id) + "\n")
132-
print "\nJobs id:\n{jobs_id}".format(jobs_id=" ".join(jobs_id))
133-
print "\nLogs, command, and jobs id related to this batch will be in:\n {smartdispatch_folder}".format(smartdispatch_folder=path_job)
133+
print("\nJobs id:\n{jobs_id}".format(jobs_id=" ".join(jobs_id)))
134+
print("\nLogs, command, and jobs id related to this batch will be in:\n {smartdispatch_folder}".format(smartdispatch_folder=path_job))
134135

135136

136137
def parse_arguments():
@@ -145,7 +146,7 @@ def parse_arguments():
145146
parser.add_argument('-c', '--coresPerCommand', type=int, required=False, help='How many cores a command needs.', default=1)
146147
parser.add_argument('-g', '--gpusPerCommand', type=int, required=False, help='How many gpus a command needs.', default=1)
147148
# parser.add_argument('-m', '--memPerCommand', type=float, required=False, help='How much memory a command needs (in Gb).')
148-
parser.add_argument('-f', '--commandsFile', type=file, required=False, help='File containing commands to launch. Each command must be on a seperate line. (Replaces commandAndOptions)')
149+
parser.add_argument('-f', '--commandsFile', type=str, required=False, help='File containing commands to launch. Each command must be on a seperate line. (Replaces commandAndOptions)')
149150

150151
parser.add_argument('-l', '--modules', type=str, required=False, help='List of additional modules to load.', nargs='+')
151152
parser.add_argument('-x', '--doNotLaunch', action='store_true', help='Creates the QSUB files without launching them.')
@@ -169,6 +170,9 @@ def parse_arguments():
169170
if args.queueName not in AVAILABLE_QUEUES and ((args.coresPerNode is None and args.gpusPerNode is None) or args.walltime is None):
170171
parser.error("Unknown queue, --coresPerNode/--gpusPerNode and --walltime must be set.")
171172

173+
if args.commandsFile is not None and not os.path.isfile(args.commandsFile):
174+
parser.error("Command file does not exist: '{0}'.".format(args.commandsFile))
175+
172176
return args
173177

174178

smartdispatch/utils.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import print_function
2+
13
import re
24
import fcntl
35
import logging
@@ -18,7 +20,7 @@ def print_boxed(string):
1820
out = u"\u250c" + box_line + u"\u2510\n"
1921
out += '\n'.join([u"\u2502 {} \u2502".format(line.ljust(max_len)) for line in splitted_string])
2022
out += u"\n\u2514" + box_line + u"\u2518"
21-
print out
23+
print(out)
2224

2325

2426
def yes_no_prompt(query, default=None):
@@ -109,6 +111,8 @@ def detect_cluster():
109111
# Get server status
110112
try:
111113
output = Popen(["qstat", "-B"], stdout=PIPE).communicate()[0]
114+
if isinstance(output, bytes):
115+
output = output.decode("utf-8")
112116
except OSError:
113117
# If qstat is not available we assume that the cluster is unknown.
114118
return None

0 commit comments

Comments
 (0)