HWRF  trunk@4391
batchsystem.py
1 """!Provides information about the batch system.
2 
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."""
6 
7 ##@var __all__
8 # List of symbols exported by "from produtil.batchsystem import *"
9 __all__=['jobname','jobid','joblongname','NONAME']
10 import os
11 
12 class FakeClass:
13  """!A special class for constants."""
14 
15 ##@var UNSPECIFIED
16 # Constant for unspecified arguments.
17 UNSPECIFIED=FakeClass()
18 
19 ##@var NONAME
20 # Name for jobs that have no name.
21 NONAME="NONAME"
22 
23 ##@var _set_jobname
24 # Manually-specified jobname, from set_jobname()
25 _set_jobname=None
26 
27 ##@var _set_jobid
28 # Manually specified job id from set_jobid()
29 _set_jobid=None
30 
31 ##@var _set_joblongname
32 # Manually specified job longname from set_joblongname()
33 _set_joblongname=None
34 
35 ##@var _set_default
36 # Manually specified job "everything name" from set_default_name()
37 _set_default=None
38 
39 def set_default_name(default_name):
40  """!Set default for all job names.
41 
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"""
47  global _set_default
48  _set_default=default_name
49 
50 def set_jobname(jobname):
51  """!Sets the value that jobname() should return.
52  @param jobname the name"""
53  global _set_jobname
54  _set_jobname=str(jobname)
55 
56 def set_jobid(jobid):
57  """!Sets the value that jobid() should return.
58  @param jobid the id"""
59  global _set_jobid
60  _set_jobid=str(jobid)
61 
62 def set_joblongname(joblongname):
63  """!Sets the value that joblongname() should return.
64  @param joblongname the new long name"""
65  global _set_joblongname
66  _set_joblongname=str(joblongname)
67 
68 def getenvs(names,fallback=None):
69  """!Get an environment variable, with various fallback options
70 
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
73  fallback.
74  @param names the list of environment variables
75  @param fallback the fallback option if none are set """
76  for name in names:
77  if name not in os.environ: continue
78  val=os.environ[name]
79  if not isinstance(val,basestring): continue
80  if len(val)<1: continue
81  return val
82  return fallback
83 
84 def jobname(fallback=UNSPECIFIED):
85  """!Get the batch job name
86 
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
93  "NONAME").
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
101  return NONAME
102 
103 def jobid(fallback=UNSPECIFIED):
104  """!Get the batch job ID
105 
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()),)
118 
119 def joblongname(jobid_fallback=UNSPECIFIED,
120  jobname_fallback=UNSPECIFIED):
121  """!Get the job longname
122 
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
133  ret=getenvs(['jobid'])
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.
Definition: batchsystem.py:68
def joblongname
Get the job longname.
Definition: batchsystem.py:120
def set_jobid(jobid)
Sets the value that jobid() should return.
Definition: batchsystem.py:56
A special class for constants.
Definition: batchsystem.py:12
def set_jobname(jobname)
Sets the value that jobname() should return.
Definition: batchsystem.py:50
def jobid
Get the batch job ID.
Definition: batchsystem.py:103
def set_default_name(default_name)
Set default for all job names.
Definition: batchsystem.py:39
def set_joblongname(joblongname)
Sets the value that joblongname() should return.
Definition: batchsystem.py:62
def jobname
Get the batch job name.
Definition: batchsystem.py:84