KMR
kmrwrapper.py
Go to the documentation of this file.
1 #!/usr/bin/python
2 # Copyright (C) 2012-2016 RIKEN AICS
3 
4 ## \file kmrwrapper.py KMR-Shell File Spliter and Job-Script Generator.
5 
6 import sys
7 import os
8 from optparse import OptionParser
9 from kmrfsplit import *
10 from kmrgenscript import *
11 
12 ## Wrapper script of kmrfsplit and kmrgenscript.
13 # It works on Python 2.4 or later.
14 
15 if __name__ == "__main__":
16 
17  usage = "usage: %prog [options] -m mapper -r reducer inputfile"
18  parser = OptionParser(usage)
19 
20  parser.add_option("-n",
21  "--number-of-separation",
22  dest="nums",
23  type="int",
24  help="number of file separate",
25  metavar="number",
26  default=1)
27 
28  parser.add_option("-e",
29  "--number-of-exec-nodes",
30  dest="nodes",
31  type="int",
32  help="number of execute nodes",
33  metavar="number",
34  default=1)
35 
36  parser.add_option("-s",
37  "--separator",
38  dest="sep",
39  type="string",
40  help="separator string",
41  metavar="'string'",
42  default='\n')
43 
44  parser.add_option("-p",
45  "--separate-file-prefix",
46  dest="prefix",
47  type="string",
48  help="separate filename prefix",
49  metavar="'string'",
50  default='part')
51 
52  parser.add_option("-o",
53  "--outputfile",
54  dest="outfile",
55  type="string",
56  help="output filename prefix",
57  metavar="'string'",
58  default='output')
59 
60  parser.add_option("-d",
61  "--workdir",
62  dest="workdir",
63  type="string",
64  help="work directory",
65  metavar="'string'",
66  default='./work')
67 
68  parser.add_option("-O",
69  "--outputdir",
70  dest="outdir",
71  type="string",
72  help="output directory",
73  metavar="'string'",
74  default='./outdir')
75 
76  parser.add_option("-t",
77  "--resource-time",
78  dest="rsctime",
79  type="string",
80  help="resource time",
81  metavar="'string'",
82  default='00:10:00')
83 
84  parser.add_option("-m",
85  "--mapper",
86  dest="mapper",
87  type="string",
88  help="mapper path",
89  metavar="'string'")
90 
91  parser.add_option("-r",
92  "--reducer",
93  dest="reducer",
94  type="string",
95  help="reducer path",
96  metavar="'string'")
97 
98  parser.add_option("-S",
99  "--scheduler",
100  dest="sched",
101  type="string",
102  help="scheduler (default is 'K')",
103  metavar="'string'",
104  default='K')
105 
106  parser.add_option("-w",
107  "--write-scriptfile",
108  dest="scrfile",
109  type="string",
110  help="script filename",
111  metavar="'string'")
112 
113  parser.add_option("-f",
114  "--force",
115  dest="force",
116  action="store_true",
117  help="force option",
118  default=False)
119 
120  (options, args) = parser.parse_args()
121 
122  # check parameters.
123 
124  if len(args) <> 1 :
125  parser.error("missing parameter")
126  sys.exit()
127 
128  inputfile = args[0]
129 
130  if options.nodes > options.nums :
131  print 'Error: number of execute nodes must be less than or equal to number of file separation.'
132  sys.exit()
133 
134  # If number of nodes < number of input files,
135  # set M option (attach multi files to a mapper) to True.
136  if options.nodes < options.nums :
137  multi = True
138  else :
139  multi = False
140 
141  if not os.path.exists(inputfile) :
142  print 'Error: inputfile %s is not exist.' % inputfile
143  sys.exit()
144 
145  if os.path.exists(options.workdir) :
146  if not os.path.isdir(options.workdir) :
147  print 'Error: "%s" is not directory.' % options.workdir
148  sys.exit()
149  else:
150  if options.force :
151  try:
152  os.mkdir(options.workdir)
153  except IOError:
154  print 'Error: could not create "%s".' % options.workdir
155  sys.exit()
156  else:
157  print 'Error: directory "%s" is not exist. create it or use -f option.' % options.workdir
158  sys.exit()
159 
160  splitfile(options.nums, options.sep, options.workdir, options.prefix, inputfile)
161 
162  selectscheduler(options.nodes, options.prefix, options.outfile,
163  options.workdir, options.outdir, options.rsctime,
164  options.mapper, options.reducer, multi,
165  options.sched, options.scrfile)
166 
167 # Copyright (C) 2012-2016 RIKEN AICS
168 # This library is distributed WITHOUT ANY WARRANTY. This library can be
169 # redistributed and/or modified under the terms of the BSD 2-Clause License.