1717"""
1818import pwd
1919import os
20+ import re
2021
2122import xml .etree .ElementTree as ET
2223
@@ -46,7 +47,7 @@ def format_template(template, *args, **kwargs):
4647 """
4748 if isinstance (template , Template ):
4849 return template .render (* args , ** kwargs )
49- elif '{{' in template or '{%' in template :
50+ elif '{{' in template or '{%' in template :
5051 return Template (template ).render (* args , ** kwargs )
5152 return template .format (* args , ** kwargs )
5253
@@ -334,7 +335,7 @@ def start(self):
334335 if self .user and self .user .server and self .user .server .port :
335336 self .port = self .user .server .port
336337 self .db .commit ()
337- elif (jupyterhub .version_info < (0 ,7 ) and not self .user .server .port ) or \
338+ elif (jupyterhub .version_info < (0 ,7 ) and not self .user .server .port ) or \
338339 (jupyterhub .version_info >= (0 ,7 ) and not self .port ):
339340 self .port = random_port ()
340341 self .db .commit ()
@@ -356,8 +357,8 @@ def start(self):
356357 else :
357358 self .log .warn ('Job ' + self .job_id + ' neither pending nor running.\n ' +
358359 self .job_status )
359- raise RuntimeError ('The Jupyter batch job has disappeared '
360- ' while pending in the queue or died immediately '
360+ raise RuntimeError ('The Jupyter batch job has disappeared'
361+ ' while pending in the queue or died immediately'
361362 ' after starting.' )
362363 yield gen .sleep (self .startup_poll_interval )
363364
@@ -394,7 +395,6 @@ def stop(self, now=False):
394395 self .job_id , self .current_ip , self .port )
395396 )
396397
397- import re
398398
399399class BatchSpawnerRegexStates (BatchSpawnerBase ):
400400 """Subclass of BatchSpawnerBase that uses config-supplied regular expressions
@@ -429,15 +429,11 @@ class BatchSpawnerRegexStates(BatchSpawnerBase):
429429
430430 def state_ispending (self ):
431431 assert self .state_pending_re , "Misconfigured: define state_running_re"
432- if self .job_status and re .search (self .state_pending_re , self .job_status ):
433- return True
434- else : return False
432+ return bool (self .job_status and re .search (self .state_pending_re , self .job_status ))
435433
436434 def state_isrunning (self ):
437435 assert self .state_running_re , "Misconfigured: define state_running_re"
438- if self .job_status and re .search (self .state_running_re , self .job_status ):
439- return True
440- else : return False
436+ return bool (self .job_status and re .search (self .state_running_re , self .job_status ))
441437
442438 def state_gethost (self ):
443439 assert self .state_exechost_re , "Misconfigured: define state_exechost_re"
@@ -450,6 +446,7 @@ def state_gethost(self):
450446 else :
451447 return match .expand (self .state_exechost_exp )
452448
449+
453450class TorqueSpawner (BatchSpawnerRegexStates ):
454451 batch_script = Unicode ("""#!/bin/sh
455452#PBS -q {queue}@{host}
@@ -473,6 +470,7 @@ class TorqueSpawner(BatchSpawnerRegexStates):
473470 state_running_re = Unicode (r'<job_state>R</job_state>' ).tag (config = True )
474471 state_exechost_re = Unicode (r'<exec_host>((?:[\w_-]+\.?)+)/\d+' ).tag (config = True )
475472
473+
476474class MoabSpawner (TorqueSpawner ):
477475 # outputs job id string
478476 batch_submit_cmd = Unicode ('msub' ).tag (config = True )
@@ -483,6 +481,7 @@ class MoabSpawner(TorqueSpawner):
483481 state_running_re = Unicode (r'State="Running"' ).tag (config = True )
484482 state_exechost_re = Unicode (r'AllocNodeList="([^\r\n\t\f :"]*)' ).tag (config = True )
485483
484+
486485class UserEnvMixin :
487486 """Mixin class that computes values for USER, SHELL and HOME in the environment passed to
488487 the job submission subprocess in case the batch system needs these for the batch script."""
@@ -504,6 +503,7 @@ def get_env(self):
504503 env = self .user_env (env )
505504 return env
506505
506+
507507class SlurmSpawner (UserEnvMixin ,BatchSpawnerRegexStates ):
508508 """A Spawner that just uses Popen to start local processes."""
509509
@@ -561,6 +561,7 @@ def parse_job_id(self, output):
561561 raise e
562562 return id
563563
564+
564565class MultiSlurmSpawner (SlurmSpawner ):
565566 '''When slurm has been compiled with --enable-multiple-slurmd, the
566567 administrator sets the name of the slurmd instance via the slurmd -N
@@ -573,6 +574,7 @@ def state_gethost(self):
573574 host = SlurmSpawner .state_gethost (self )
574575 return self .daemon_resolver .get (host , host )
575576
577+
576578class GridengineSpawner (BatchSpawnerBase ):
577579 batch_script = Unicode ("""#!/bin/bash
578580#$ -j yes
@@ -620,6 +622,7 @@ def state_gethost(self):
620622 self .log .error ("Spawner unable to match host addr in job {0} with status {1}" .format (self .job_id , self .job_status ))
621623 return
622624
625+
623626class CondorSpawner (UserEnvMixin ,BatchSpawnerRegexStates ):
624627 batch_script = Unicode ("""
625628Executable = /bin/sh
@@ -657,6 +660,7 @@ def parse_job_id(self, output):
657660 def cmd_formatted_for_batch (self ):
658661 return super (CondorSpawner ,self ).cmd_formatted_for_batch ().replace ('"' ,'""' ).replace ("'" ,"''" )
659662
663+
660664class LsfSpawner (BatchSpawnerBase ):
661665 '''A Spawner that uses IBM's Platform Load Sharing Facility (LSF) to launch notebooks.'''
662666
@@ -701,7 +705,6 @@ def state_isrunning(self):
701705 if self .job_status :
702706 return self .job_status .split (' ' )[0 ].upper () == 'RUN'
703707
704-
705708 def state_gethost (self ):
706709 if self .job_status :
707710 return self .job_status .split (' ' )[1 ].strip ()
0 commit comments