-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathula3_pbs.py
518 lines (392 loc) · 16 KB
/
ula3_pbs.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
#!/usr/bin/env python
import os, pprint, argparse, errno, re, time, fnmatch, datetime
import ConfigParser
import subprocess
import logging.config
ppr = pprint.pprint
# Absolute path for job runner and its log.
#ROOT = '/projects/v10/dev/rae/scale-test/jobs'
ROOT = '/home/547/axi547/ULA3'
EXE = os.path.normpath(os.path.join(ROOT, __file__))
# Set up logging
logging.config.fileConfig(os.path.join(ROOT, 'logging.config'))
ROOT_LOGGER_NAME = os.path.split(__file__)[1].strip('.py')
log = logging.getLogger(ROOT_LOGGER_NAME)
# Globals
JOB_NAME = '_ula3'
CONFIG_DICT = {}
STATUS_FILE_OK = 'STATUS.OK'
STATUS_FILE_ERROR = 'STATUS.ERROR'
# Template for dummy job to check template substitution and submission logic.
TEST_SCRIPT_TEMPLATE = """
#!/bin/bash
#PBS -P v10
#PBS -q normal
#PBS -l walltime=00:05:00
#PBS -wd
module load python/2.7.1
module load geotiff/1.3.0
module load hdf4/4.2.6_2012
module load proj/4.7.0
module load gdal/1.9.0
echo "-------- TEST_SCRIPT_TEMPLATE --------"
echo "__dataset_path = %(__dataset_path)s"
echo "__product_root = %(__product_root)s"
echo "__tmp_root = %(__tmp_root)s"
echo "__status_file_ok = %(__status_file_ok)s"
echo "__status_file_error = %(__status_file_error)s"
echo "__link_job = %(__link_job)s"
/bin/true
test $? -eq 0 && touch %(__status_file_ok)s || touch %(__status_file_error)s
sleep 120
%(__link_job)s
"""
# Template for NBAR production job.
# Set walltime=90min to prevent job links from breaking when things get slow.
# If the system is really and truly hosed, the job will time out anyway...
NBAR_SCRIPT_TEMPLATE = """
#!/bin/bash
#PBS -P v10
#PBS -q normal
#@#PBS -l walltime=00:45:00,vmem=8778MB,ncpus=1,jobfs=16GB
#PBS -l walltime=00:60:00,vmem=8778MB,ncpus=1
#PBS -wd
#@#PBS -m e
#@#PBS -M %(__notification_email)s
module load python/2.7.1
module load geotiff/1.3.0
module load hdf4/4.2.6_2012
module load proj/4.7.0
module load gdal/1.9.0
export ULA_NBAR_ROOT=%(__nbar_root)s
export PATH=$ULA_NBAR_ROOT:$ULA_NBAR_ROOT/bin:${PATH}
PYTHONPATH=$ULA_NBAR_ROOT:$ULA_NBAR_ROOT/common:$PYTHONPATH
PYTHONPATH=/projects/v10/GASoftware/pyephem-3.7.5.1/lib/python2.7/site-packages:$PYTHONPATH
# Only process dataset if path is not an empty string
if [ "%(__dataset_path)s" != "" ]
then
# Use explicit work directory
%(__nbar_exe)s --input=%(__dataset_path)s --output-root=%(__product_root)s --work=%(__tmp_root)s/$PBS_JOBID --repackage
# Use $PBS_JOBFS work directory
#@%(__nbar_exe)s --input=%(__dataset_path)s --output-root=%(__product_root)s --work=$PBS_JOBFS --repackage
# Put status marker file in the log directory
test $? -eq 0 && touch %(__status_file_ok)s || touch %(__status_file_error)s
fi
# Submit the next job
sleep 2
%(__link_job)s
"""
def __subdirs(path):
try:
return sorted(os.listdir(path))
except OSError, e:
if e.errno != errno.ENOENT:
raise
return []
def products():
try:
return __subdirs(CONFIG_DICT['product_root'])
except KeyError:
return []
def logged():
try:
return __subdirs(CONFIG_DICT['log_root'])
except KeyError:
return []
def unprocessed():
try:
paths = CONFIG_DICT['input_paths']
except KeyError:
return []
names = [os.path.split(x)[1] for x in paths]
# TODO assert inputs actually exist
return [paths[names.index(n)] for n in
[x for x in names if x not in logged()]]
def next():
try:
return unprocessed().pop(0)
except IndexError:
return None
def create_dir(path):
log = logging.getLogger(ROOT_LOGGER_NAME + '.create_dir')
try:
os.makedirs(path)
log.debug('path=%s' % path)
return True
except OSError, e:
if e.errno == errno.EEXIST:
return False
else:
log.error(str(e))
raise
def get_inputs(root, fpattern='*_L1?.xml'):
def filter(filelist, include_pattern='.*', exclude_pattern='.*_SYS_.*'):
return [file for file in filelist if re.search(include_pattern, file) and not re.search(exclude_pattern, file)]
log = logging.getLogger(ROOT_LOGGER_NAME + '.get_inputs')
log.debug('walking %s (pattern=%s)...' % (root, fpattern))
return filter([re.sub('/scene01$', '', r) for r,d,f in os.walk(root) if fnmatch.filter(f, fpattern)])
def submit_job(dataset_path=None, config_path=None, link=False, test=False):
log = logging.getLogger(ROOT_LOGGER_NAME + '.submit_job')
log.debug('%s link=%s config_path=%s'
% (dataset_path, link, config_path))
if config_path is None:
log.error('config_path is undefined')
return None
if dataset_path is None:
if link: # If no more datasets to process, then move onto next config file (if defined)
next_config = CONFIG_DICT['next_config']
if next_config:
config_path = next_config # Move onto next config file
dataset_path = '' # Set path to empty string to bypass processing stage
if dataset_path is None:
log.error('dataset_path is undefined')
return None
# Ensure product and log root directories exist.
create_dir(CONFIG_DICT['product_root'])
create_dir(CONFIG_DICT['log_root'])
create_dir(CONFIG_DICT['tmp_root'])
# Create the job log directory.
if dataset_path: # Skip this for empty string
dataset_name = os.path.split(dataset_path)[1]
log_dir = os.path.join(CONFIG_DICT['log_root'], dataset_name)
# Recursively fetch next scene if another job has already started this one
if create_dir(log_dir) is False:
return submit_job(next(), config_path, link, test)
else:
dataset_name = ''
log_dir = CONFIG_DICT['log_root']
# Define locals required to populate the job template.
# TODO Debugging errors in template substitution is ugly!!
# TODO Add a sanity check: template keys vs locals
if link:
# DEBUG ...allow --link to spawn one job only
#__link_job = '%s --jobs=1 --config=%s' % (EXE, config_path)
__link_job = '%s --jobs=1 --config=%s --link' % (EXE, config_path)
else:
__link_job = '# link=False'
__dataset_path = dataset_path
__product_root = CONFIG_DICT['product_root']
__tmp_root = CONFIG_DICT['tmp_root']
__nbar_exe = CONFIG_DICT['nbar_exe']
__notification_email = CONFIG_DICT['notification_email']
__nbar_root = CONFIG_DICT['nbar_root']
__status_file_ok = STATUS_FILE_OK
__status_file_error = STATUS_FILE_ERROR
# Write the job script into the log directory.
scr_file = os.path.join(log_dir, JOB_NAME)
if test:
scr_text = TEST_SCRIPT_TEMPLATE.lstrip('\n') % locals()
else:
scr_text = NBAR_SCRIPT_TEMPLATE.lstrip('\n') % locals()
with open(scr_file, 'w') as _file:
_file.write(scr_text)
log.debug('created job script: %s' % scr_file)
# Check the state of the input dataset (online, offline, ...)
if dataset_path: # Skip this for empty string
p = subprocess.Popen('dmls -l %s' % dataset_path, shell=True,
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
pout, perr = p.communicate()
log.debug('input dataset status\n%s' % (pout + perr))
# Submit the job.
p = subprocess.Popen('qsub %s' % JOB_NAME, shell=True, cwd=log_dir,
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
pout, perr = p.communicate()
job_id = None
if p.returncode == 0 and re.search('^(\d+).(\w+)', pout):
job_id = pout.strip('\n')
log.info('%s %s (link=%s)' % (job_id, dataset_path, link))
else:
log.error('SUBMIT FAILED\n\n%s' % '\n'.join([pout, perr, '\n']))
return job_id
def report(log_root):
log = logging.getLogger(ROOT_LOGGER_NAME + '.report')
if not os.path.exists(log_root):
log.warning('log root not found: %s' % log_root)
return
# Generate a report for the run.
job_status = { 'pass': [], 'fail': [], 'unknown': [] }
job_process = { 'process': [], 'package': [] }
process_etimes = []
package_etimes = []
# Count passed, failed, unknown jobs and gather elapsed times
# for jobs that completed successfully.
for log in sorted(os.listdir(log_root)):
d = os.path.join(log_root, log)
if os.path.isdir(d):
files = sorted(os.listdir(d))
state = 'unknown'
if STATUS_FILE_OK in files:
state = 'pass'
elif STATUS_FILE_ERROR in files:
state = 'fail'
job_status[state].append(d)
if state == 'pass':
out_file = os.path.join(d, fnmatch.filter(files, '%s.o*' % JOB_NAME)[0])
with open(out_file, 'r') as _file:
out_text = _file.read()
if re.search('process completed successfully', out_text):
job_process['process'].append(d)
m = re.search('Elapsed time:(\s+)(\d+):(\d+):(\d+)', out_text)
if m:
_etime = datetime.timedelta(hours=int(m.group(2)),
minutes=int(m.group(3)),
seconds=int(m.group(4)))
process_etimes.append(_etime)
if re.search('Repackaging completed successfully', out_text):
job_process['package'].append(d)
m = re.search('Elapsed time:(\s+)(\d+):(\d+):(\d+)', out_text)
if m:
_etime = datetime.timedelta(hours=int(m.group(2)),
minutes=int(m.group(3)),
seconds=int(m.group(4)))
package_etimes.append(_etime)
# Calculate mean and max elapsed times.
if process_etimes:
process_time_min = min(process_etimes)
process_time_max = max(process_etimes)
process_seconds_sum = 0.0
for t in process_etimes:
process_seconds_sum += t.total_seconds()
process_seconds_mean = process_seconds_sum / len(process_etimes)
else:
process_time_min = datetime.timedelta(seconds=0)
process_time_max = datetime.timedelta(seconds=0)
process_seconds_mean = 0
if package_etimes:
package_time_min = min(package_etimes)
package_time_max = max(package_etimes)
package_seconds_sum = 0.0
for t in package_etimes:
package_seconds_sum += t.total_seconds()
package_seconds_mean = package_seconds_sum / len(package_etimes)
else:
package_time_min = datetime.timedelta(seconds=0)
package_time_max = datetime.timedelta(seconds=0)
package_seconds_mean = 0
# Print report.
_stats_txt = (
'\nSUMMARY [%s]\n'
'\tpass %d\t(%d processed, %d packaged)\n'
'\tfail %d\n'
'\tunknown %d\n'
'\tmin / mean / max elapsed processing times: %.1f / %.1f / %.1f sec\t(%s / %s / %s)\n'
'\tmin / mean / max elapsed packaging times: %.1f / %.1f / %.1f sec\t(%s / %s / %s)\n'
) % (
log_root,
len(job_status['pass']), len(job_process['process']), len(job_process['package']),
len(job_status['fail']),
len(job_status['unknown']),
process_time_min.total_seconds(), process_seconds_mean, process_time_max.total_seconds(),
process_time_min, datetime.timedelta(seconds=process_seconds_mean), process_time_max,
package_time_min.total_seconds(), package_seconds_mean, package_time_max.total_seconds(),
package_time_min, datetime.timedelta(seconds=package_seconds_mean), package_time_max
)
print _stats_txt
def list_incomplete_logs(log_root):
ilog_list = []
for log in sorted(os.listdir(log_root)):
logdir = os.path.join(log_root, log)
logfiles = sorted(os.listdir(logdir))
if (STATUS_FILE_OK not in logfiles and
STATUS_FILE_ERROR not in logfiles and
JOB_NAME in logfiles):
for __r, __d, __f in os.walk(logdir, topdown=False):
ilog_list.append(__r)
print 'INCOMPLETE LOGS'
for x in ilog_list:
print x
def foo():
# DEBUG for testing logging
log = logging.getLogger(ROOT_LOGGER_NAME + '.foo')
log.info('fooey!')
def parse_config(config_path):
log = logging.getLogger(ROOT_LOGGER_NAME + '.parse_config')
log.debug('config file: %s' % config_path)
c = ConfigParser.ConfigParser(allow_no_value=True)
c.optionxform = str
c.readfp(open(config_path))
_ipaths = []
try:
# Read [INPUTS] section if present.
_ipaths = [x[0] for x in c.items('INPUTS')]
log.debug('found [INPUTS] in config file')
except ConfigParser.NoSectionError:
# No [INPUTS], so gather input list...
_iroot = c.get('CONFIGURATION', 'input_root')
_ipaths = get_inputs(_iroot, fpattern='*_L1?.xml')
# Add [INPUTS] section to config instance.
c.add_section('INPUTS')
for i in _ipaths:
c.set('INPUTS', i, '')
# Write modified config file.
with open(config_path, 'w') as _file:
c.write(_file)
log.debug('created [INPUTS] in config file %s' % config_path)
return {
'product_root': c.get('CONFIGURATION', 'product_root'),
'log_root': c.get('CONFIGURATION', 'log_root'),
'tmp_root': c.get('CONFIGURATION', 'tmp_root'),
'nbar_exe': c.get('CONFIGURATION', 'nbar_exe'),
'nbar_root': c.get('CONFIGURATION', 'nbar_root'),
'notification_email': c.get('CONFIGURATION', 'notification_email'),
'next_config': c.get('CONFIGURATION', 'next_config'),
'input_paths': _ipaths,
'input_names': [os.path.split(x)[1] for x in _ipaths],
'config_path': config_path
}
def status():
print
print 'LOGGED (%s)' % CONFIG_DICT['log_root']
ppr(logged())
print
print 'PRODUCTS (%s)' % CONFIG_DICT['product_root']
ppr(products())
print
print 'UNPROCESSED'
ppr(unprocessed())
print
print 'NEXT'
print next()
print
def parse_args():
argp = argparse.ArgumentParser(__file__)
# TODO require --config??
argp.add_argument('-c', '--config', default=None, required=True,
help='Specify configuration file')
argp.add_argument('-s', '--status', action='store_true', default=False,
help='Display NBAR processing status')
argp.add_argument('-j', '--jobs', type=int, default=0,
help='Specify number of jobs to submit')
argp.add_argument('-l', '--link', action='store_true', default=False,
help='Enable linked jobs')
argp.add_argument('-t', '--test', action='store_true', default=False,
help='Submit dummy jobs for testing')
argp.add_argument('-r', '--report', action='store_true', default=False, help='Summarise completed jobs')
argp.add_argument('--incomplete', action='store_true', default=False,
help='List log directories that are incomplete (active job or no result)')
#argp.add_argument('--foo', action='store_true', default=False,
# help='dummy flag (logging tests)')
return argp.parse_args()
def main():
global CONFIG_DICT
log = logging.getLogger(ROOT_LOGGER_NAME + '.main')
args = parse_args()
CONFIG_DICT = parse_config(os.path.join(ROOT, args.config))
if args.status:
status()
elif args.report:
report(CONFIG_DICT['log_root'])
elif args.incomplete:
list_incomplete_logs(CONFIG_DICT['log_root'])
else:
# Submit PBS job(s)
log.debug('jobs=%d link=%s' % (args.jobs, args.link))
for j in xrange(args.jobs):
flag = submit_job(dataset_path=next(), config_path=args.config,
link=args.link, test=args.test)
if flag is None:
log.info('ALL DATASETS PROCESSED')
break
time.sleep(2)
if __name__ == '__main__':
main()