1 """!Provides information about the batch system.
3 This module is intended to be used to communicate with the batch
4 system. At present, it just knows how to guess the job name and id,
5 as well as a "longname" that combines the two."""
9 __all__=[
'jobname',
'jobid',
'joblongname',
'NONAME']
13 """!A special class for constants."""
17 UNSPECIFIED=FakeClass()
40 """!Set default for all job names.
42 Sets a default value to use for the job name and long name if it
43 cannot be guessed from the environment. This is used by
44 produtil.setup.setup's jobname= argument. This will override the
45 fallback= arguments of both jobname() and joblongname()
46 @param default_name the name"""
48 _set_default=default_name
51 """!Sets the value that jobname() should return.
52 @param jobname the name"""
54 _set_jobname=str(jobname)
57 """!Sets the value that jobid() should return.
58 @param jobid the id"""
63 """!Sets the value that joblongname() should return.
64 @param joblongname the new long name"""
65 global _set_joblongname
66 _set_joblongname=str(joblongname)
69 """!Get an environment variable, with various fallback options
71 Tries the list of environment variable names, returning the first
72 one that exists and is non-blank. If none are found, returns the
74 @param names the list of environment variables
75 @param fallback the fallback option if none are set """
77 if name
not in os.environ:
continue
79 if not isinstance(val,basestring):
continue
80 if len(val)<1:
continue
85 """!Get the batch job name
87 Returns the human-readable job name, if one exists. If
88 set_jobname was called, returns its value. Otherwise, attempts to
89 get it from the NCO $outid or $job environment variables first,
90 then tries the batch system variables. If none are found, and
91 fallback is specified, then the fallback is returned. Otherwise,
92 the module-level NONAME variable is returned (which defaults to
94 @param fallback return value if no job name is known"""
95 if _set_jobname:
return _set_jobname
96 ret=
getenvs([
'outid',
'job',
'LSB_JOBNAME',
'PBS_JOBNAME',
'MOAB_JOBNAME',
97 'LOADL_STEP_NAME',
'LOADL_JOB_NAME'])
98 if ret
is not None:
return ret
99 if _set_default
is not None:
return _set_default
100 if fallback
is not UNSPECIFIED:
return fallback
104 """!Get the batch job ID
106 Returns the batch system job id for the batch job that is running
107 this program, if known. If set_jobid was called, returns its
108 value. Otherwise, tries the NCO $pid first, then the various
109 batch system environment variables. If none are found, and the
110 fallback is specified, returns the fallback. Otherwise, returns
111 "o$PID" where $PID is the process ID.
112 @param fallback the fallback if no id is known"""
113 ret=
getenvs([
'pid',
'LSB_JOBID',
'PBS_JOBID',
'MOAB_JOBID',
'LOADL_STEP_ID'])
114 if ret
is not None:
return ret
115 if _set_default
is not None:
return _set_default
116 if fallback
is not UNSPECIFIED:
return fallback
117 return 'o%s'%(str(os.getpid()),)
120 jobname_fallback=UNSPECIFIED):
121 """!Get the job longname
123 Returns a human-readable job name that includes both the batch
124 system job name and id. If set_joblongname was called, returns
125 its value. Next, returns the NCO $jobid variable if available,
126 otherwise returns LL{jobid()}.o{jobname()} where jobid and jobname
127 are the results of those two functions. The jobid_fallback and
128 jobname_fallback are passed as the fallback parameters to the
129 calls to jobid and jobname.
130 @param jobid_fallback the fallback if no id is known
131 @param jobname_fallback the fallback if no name is known"""
132 if _set_joblongname:
return _set_joblongname
134 if ret
is not None:
return ret
135 return 'LL%s.%s'%(
jobid(jobid_fallback),
jobname(jobname_fallback))
def getenvs
Get an environment variable, with various fallback options.
def joblongname
Get the job longname.
def set_jobid(jobid)
Sets the value that jobid() should return.
A special class for constants.
def set_jobname(jobname)
Sets the value that jobname() should return.
def jobid
Get the batch job ID.
def set_default_name(default_name)
Set default for all job names.
def set_joblongname(joblongname)
Sets the value that joblongname() should return.
def jobname
Get the batch job name.