Документ взят из кэша поисковой машины. Адрес оригинального документа : http://www.stsci.edu/spst/UnixTransition/doc/check_batch.py
Дата изменения: Fri Apr 8 12:46:10 2016
Дата индексирования: Sun Apr 10 23:48:09 2016
Кодировка:

Поисковые слова: star trail
#
#MODULE check_batch
#
#***********************************************************************
"""

**PURPOSE** --
Check our collection of recurring batch jobs.

**DEVELOPER** --
Don Chance

**MODIFICATION HISTORY** --
Initial implementation 3/8/01

Move to the initial working directory before trying to restart the
job. This avoids trying to write any temporary files to the
BATCH_PICKLE_DATA directory. drc 5/17/01

Modified to support either upper or lower case for options. drc 8/1/01

Modified to work with both Unix and VMS. drc 11/13/01

Modified to handle mixed case input; to provide job status information
app 04/24/03
Mods from python review app 05/05/03
Updated for Python 3 compatibility. drc 10/14/14
"""
#***********************************************************************
__version__ = "14.10.10"
import spss_sys_util, spst_getopt, batch_util
import os, pickle, sys

# For Python 2 and 3 compatiblity
try:
input = raw_input
except NameError:
pass


def run(*args):
"""Check our collection of recurring batch jobs.

Usage:
do check_batch [-[no]restart|-query] [-list] [-status]

The -restart option will cause the program to restart any batch jobs
found not to be running. The default is to restart. If the -query
option is specified, the user will be prompted for each non-running
batch job as to whether it should be restarted or not. With the
-norestart option, no batch jobs will be restarted, but a warning
will be output.

The -list option causes the information stored about each batch job to
be sent to standard output.

The -status option provides the job_id, priority, name, user, state,
submission time and queue name.
"""
if not args:
# Spew out the usage and quit when no parameters
# are provided.
print (run.__doc__)
return spss_sys_util.SUCCESS

low_args = []
for a in args:
low_args.append(a.lower())
# Parse the arguments...
optlist = ['restart', 'list', 'norestart', 'query', 'status']

options, parms = spst_getopt.spst_getopt(low_args, optlist)

if (('-restart' in options)
+ ('-query' in options)
+ ('-norestart' in options)) >= 2:
raise ValueError('-restart, -query, and -norestart are all mutually exclusive.')

# Get a list of all the pickle files in BATCH_PICKLE_DATA
ppaths = spss_sys_util.get_environ_variable('BATCH_PICKLE_DATA')
for ppath in ppaths:
cwd = os.getcwd()
os.chdir(ppath)
files = spss_sys_util.glob('*_pickled.dat')
for file in files:
job = pickle.Unpickler(open(file, 'r')).load()
if job.check():
print("\nBatch job %s is running" % job.jobname)
else:
print( "\n>>>> Batch job %s has stopped." % job.jobname)
os.chdir(cwd)
if '-query' in options:
yn = input("Do you wish to restart it? (y/n) ").strip().lower()
if yn[0] == 'y':
print( "Restarting job %s on queue: %s" % (job.jobname, job.queue))
job.start()
elif '-restart' in options:
print( "Restarting job %s on queue: %s" % (job.jobname, job.queue))
job.start()
os.chdir(ppath)

if '-list' in options:
job.pprint()
if '-status' in options:
status = job.status()
for x in status.keys():
print ("%15s : %s" % (x,status[x]))

return spss_sys_util.SUCCESS

if __name__ == "__main__":
if len(sys.argv) > 1:
run(*tuple(sys.argv[1:]))
else:
run()