HWRF  trunk@4391
exhwrf_para_archive.py
1 #! /usr/bin/env python
2 
3 ##@namespace scripts.exhwrf_para_archive
4 # Generates an archive file from HWRF COM directory outputs. This
5 # archive file can be on disk, or on the HPSS archiving system (via
6 # the htar command).
7 #
8 # How this is done depends on the [config] section archive option, in
9 # the HWRF configuration files:
10 #
11 # * archive=none --- no archiving is done. This script will exit
12 # immediately without doing anything.
13 #
14 # * archive=disk:/path/to/archive.tar.gz --- make a gzipped tar file via
15 # the tar -czf command, and place it on disk in a long-term storage area
16 #
17 # * archive=hpss:/path/to/archive.tar --- use the "htar" command to
18 # place an archive on tape.
19 #
20 # * archive=hpsz:/path/to/archive.tar.gz --- a two-step process. The
21 # first step uses tar -czf to create an on-disk archive in a staging
22 # area. The second step copies that archive to tape using "hsi put"
23 
24 import sys, os, glob
26 import hwrf_expt
27 from produtil.log import postmsg, jlogger
28 from produtil.run import exe, checkrun, run
29 from produtil.cd import NamedDir
30 
31 def main_disk():
32  """!Main program for disk archiving.
33 
34  Creates an on-disk archiving for one of two cases:
35  * disk:/path/to/archive.tar.gz --- generates an on-disk *.tar.gz
36  archive in a long-term storage disk area
37  * hpsz:/path/to/tape-archive.tar.gz --- generates an on-disk
38  *.tar.gz archive in a temporary location so it can be copied to
39  tape in a later step."""
40  postmsg('hwrf_archive disk step starting')
41  conf=hwrf_expt.conf
42  logger=conf.log('archive.disk')
43  if conf.has_section('archive'):
44  makethedir=conf.getbool('archive','mkdir',False)
45  else:
46  makethedir=False
47  archive=conf.getloc('archive','NONE')
48  if archive=='NONE':
49  logger.info('No archive location specified. Exiting.')
50  postmsg('hwrf_archive disk step has nothing to do when archiving is '
51  'disabled.')
52  return
53  with NamedDir(conf.getdir('com')):
54  flist=[ filename+'\n' for filename in glob.glob('*') ]
55  flist.sort()
56  files=''.join(flist)
57  assert(len(files)>0)
58  if archive.lower()=='none':
59  postmsg('Archiving is disabled: archive=none')
60  return
61  elif archive[0:5]=='disk:':
62  path=archive[5:]
63  if makethedir:
64  adir=os.path.dirname(path)
65  if not os.path.exists(adir):
66  produtil.fileop.makedirs(adir,logger=logger)
67  flags='-cvp'
68  if path[-3:]=='.gz' or path[-4:]=='.tgz':
69  flags+='z'
70  cmd=exe(conf.getexe('tar'))[flags+'f',path,'-T','-'] << files
71  elif archive[0:5]=='hpss:':
72  logger.info('HPSS archiving enabled.')
73  logger.info('Nothing to do in the disk archiving step.')
74  logger.info('Returning successfully after doing nothing.')
75  postmsg('hwrf_archive disk step does nothing when using htar '
76  'archives.')
77  return
78  elif archive[0:5]=='hpsz:':
79  path=conf.strinterp('config','{WORKhwrf}/stage-archive.tar.gz')
80  cmd=exe(conf.getexe('tar'))['-cvpf',path,'-T','-'] << files
81  else:
82  jlogger.error('Ignoring invalid archive method %s in %s'
83  %(archive[0:4],archive))
84  return
85  checkrun(cmd,logger=logger)
86  postmsg('hwrf_archive disk step completed')
87 
88 def main_tape():
89  """!Main program for tape archiving.
90 
91  Does one of two things:
92 
93  * hpss:/path/to/archive.tar --- will use the htar command to
94  archive COM directory outputs
95  * hpsz:/path/to/archive.tar.gz --- will copy a tar.gz file from a
96  temporary area, made by the disk archiving step, to a tape
97  destination using the "hsi put" command."""
98  postmsg('hwrf_archive tape step starting')
99  conf=hwrf_expt.conf
100  logger=conf.log('archive.disk')
101  archive=conf.getloc('archive','NONE')
102  if conf.has_section('archive'):
103  makethedir=conf.getbool('archive','mkdir',False)
104  else:
105  makethedir=False
106  if archive=='NONE':
107  logger.info('No archive location specified. Exiting.')
108  postmsg('hwrf_archive disk step has nothing to do when archiving is '
109  'disabled.')
110  return
111  with NamedDir(conf.getdir('com')):
112  flist=[ filename+'\n' for filename in glob.glob('*') ]
113  flist.sort()
114  files=''.join(flist)
115  assert(len(files)>0)
116 
117  if archive.lower()=='none':
118  postmsg('Archiving is disabled: archive=none')
119  return
120  elif archive[0:5]=='disk:':
121  logger.info('Disk archiving enabled.')
122  logger.info('Nothing to do in the HPSS archiving step.')
123  logger.info('Returning successfully after doing nothing.')
124  postmsg('hwrf_archive tape step does nothing when using disk '
125  'archives.')
126  return
127  elif archive[0:5]!='hpss:' and archive[0:5]!='hpsz:':
128  jlogger.error('Ignoring invalid archive method %s in %s'
129  %(archive[0:4],archive))
130  return
131 
132  if makethedir:
133  adir=os.path.dirname(archive[5:])
134  logger.info('%s: make this HPSS directory, even if it exists'%(adir,))
135  mcmd=exe(conf.getexe('hsi'))['-P','mkdir','-p',adir]
136  run(mcmd,logger=logger)
137 
138  if archive[0:5]=='hpss:':
139  path=archive[5:]
140  flags='-cvp'
141  cmd=exe(conf.getexe('htar'))[flags+'f',path,'-L','-'] << files
142  elif archive[0:5]=='hpsz:':
143 
144  topath=archive[5:]
145  frompath=conf.strinterp('config',
146  '{WORKhwrf}/stage-archive.tar.gz')
147  cmd=exe(conf.getexe('hsi'))['put',frompath,':',topath]
148  checkrun(cmd,logger=logger)
149  postmsg('hwrf_archive tape step completed')
150 
151 if __name__=='__main__':
152  try:
153  acase=os.environ.get('ARCHIVE_STEP','BOTH').upper()
156  if acase == 'DISK':
157  main_disk()
158  elif acase == 'TAPE':
159  main_tape()
160  elif acase == 'BOTH':
161  main_disk()
162  main_tape()
163  else:
164  postmsg('INVALID JHWRF_ARCHIVE STEP %s!! ABORTING!'
165  %(repr(acase),))
166  except Exception as e:
167  jlogger.critical('hwrf_archive is aborting: '+str(e),exc_info=True)
168  sys.exit(2)
Change directory, handle temporary directories.
Definition: cd.py:1
Contains setup(), which initializes the produtil package.
Definition: setup.py:1
def init_module
Initializes the HWRF object structure.
Definition: hwrf_expt.py:384
A shell-like syntax for running serial, MPI and OpenMP programs.
Definition: run.py:1
def setup(ignore_hup=False, dbnalert_logger=None, jobname=None, cluster=None, send_dbn=None, thread_logger=False, thread_stack=2 **24, kwargs)
Initializes the produtil package.
Definition: setup.py:15
This subclass of TempDir takes a directory name, instead of generating one automatically.
Definition: cd.py:228
def makedirs
Make a directory tree, working around filesystem bugs.
Definition: fileop.py:224
def main_tape()
Main program for tape archiving.
Configures logging.
Definition: log.py:1
def main_disk()
Main program for disk archiving.