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