-
Notifications
You must be signed in to change notification settings - Fork 0
/
new_mr_application.py
executable file
·555 lines (383 loc) · 12.2 KB
/
new_mr_application.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
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
#! /usr/bin/env python
import os
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 2 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
# 02111-1307, USA.
#
# As a special exception, if other files instantiate generics from
# this unit, or you link this unit with other files to produce an
# executable, this unit does not by itself cause the resulting
# executable to be covered by the GNU General Public License. This
# exception does not however invalidate any other reasons why the
# executable file might be covered by the GNU Public License.
# readin workspace
default_workspace = '~/ada_mr'
user_workspace = raw_input('Workspace path [' + default_workspace + ']: ')
if user_workspace == "":
user_workspace = default_workspace
# readin project name
project_path = ""
while(project_path == ""):
project_path = raw_input('Project path (folder where the project files stored): ')
#readin package name
default_package_name = "Demo_Job"
user_package_name = raw_input('Package name (only "Job" is not allowed) [' + default_package_name + ']: ')
if user_package_name == "":
user_package_name = default_package_name
create_example_config_files = ""
while(create_example_config_files != "y" and create_example_config_files != "n"):
create_example_config_files = raw_input('Create example config files, too? [YyNn]: ').lower()
# merge path
path = os.path.expanduser(user_workspace) + "/" + project_path + "/"
# create project folder
os.makedirs(path)
# job.ads
job_ads = ("""\
-- This program is free software; you can redistribute it and/or
-- modify it under the terms of the GNU General Public License as
-- published by the Free Software Foundation; either version 2 of the
-- License, or (at your option) any later version.
-- This program is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
-- General Public License for more details.
-- You should have received a copy of the GNU General Public License
-- along with this program; if not, write to the Free Software
-- Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
-- 02111-1307, USA.
-- As a special exception, if other files instantiate generics from
-- this unit, or you link this unit with other files to produce an
-- executable, this unit does not by itself cause the resulting
-- executable to be covered by the GNU General Public License. This
-- exception does not however invalidate any other reasons why the
-- executable file might be covered by the GNU Public License.
with Ada.Containers.Vectors;
with Ada.Strings.Unbounded;
with Ada_Mr.Helper;
with Ada_Mr.Xml;
with Ada_Mr.Job;
package #job_package# is
-- Package rename
package ASU renames Ada.Strings.Unbounded;
-- Job record definition
type Job is new Ada_Mr.Job.Object with record
null;
end record;
-- xml stuff
overriding function To_Xml(The_Job : Job) return String;
overriding function From_Xml(Xml_Node : Ada_Mr.Xml.Node_Access) return Job;
-- split and get raw data
procedure Split_Raw_Data;
overriding function Get_Next_Raw_Job return Job;
-- print job on stdio (optional)
-- overriding procedure Print_Job(The_Job : Job; State : String; Message : String);
-- compute job (map function)
overriding procedure Compute_Job(The_Job : Job);
-- splitting the job result for serval reducers
function Split_Result_For_Different_Reducer return Ada_Mr.Helper.String_String_Maps.Map;
-- merge job results
procedure Merge_Job_Results(Xml_Node : Ada_Mr.Xml.Node_Access; Stop_System : out Boolean);
-- handle the final reducer results
procedure Finalize;
-- package instance
-- Vector to store all computed jobs
package Job_Vector is new Ada.Containers.Vectors(
Element_Type => Job,
Index_Type => Positive
);
-- Precalculated jobs
Calculated_Jobs : Job_Vector.Vector;
end #job_package#;
""")
job_ads = job_ads.replace("#job_package#", user_package_name.title())
# create job.ads
file_name = user_package_name + ".ads"
out_file = open(path + file_name,"w")
out_file.write(job_ads)
out_file.close()
# job.adb
job_adb = ("""\
with Ada.Text_IO;
with Ada_Mr.Logger;
with Ada_Mr.Xml.Helper;
package body #job_package# is
overriding function To_Xml(The_Job : Job) return String is
Details : Ada_Mr.Helper.String_String_Maps.Map;
begin
Details.Insert("job_id", Ada_Mr.Helper.Trim(The_Job.Job_Id'Img));
-- Example:
-- Details.Insert("key, "value");
return Ada_Mr.Xml.Helper.Hash_To_Xml_String(Details);
end To_Xml;
overriding function From_Xml(Xml_Node : Ada_Mr.Xml.Node_Access) return Job is
J : Job;
begin
J.Job_Id := Integer'Value(Ada_Mr.Xml.Get_Value(Xml_Node, "job_id"));
return J;
end From_Xml;
procedure Split_Raw_Data is
begin
declare
The_Job : Job;
begin
Calculated_Jobs.Append(The_Job);
end;
end Split_Raw_Data;
overriding function Get_Next_Raw_Job return Job is
J : Job := Calculated_Jobs.First_Element;
begin
Calculated_Jobs.Delete_First;
return J;
end Get_Next_Raw_Job;
-- overriding procedure Print_Job(The_Job : Job; State : String; Message : String) is
-- begin
-- Ada_Mr.Helper.Put(The_Job.Job_Id'Img, 10, 1);
-- Ada_Mr.Helper.Put("Please override ""Print_Job"" for more details", 50, 1);
-- Ada_Mr.Helper.Put(State, 20, 1);
-- Ada_Mr.Helper.Put(" " & Message);
-- Ada.Text_IO.New_Line;
-- end Print_Job;
overriding procedure Compute_Job(The_Job : Job) is
begin
null;
end Compute_Job;
function Split_Result_For_Different_Reducer return Ada_Mr.Helper.String_String_Maps.Map is
Mapping : Ada_Mr.Helper.String_String_Maps.Map;
begin
-- Example:
-- Mapping.Insert(
-- "Reducer_1",
-- ""
-- );
-- !! Dont't forget to reset the mapper result after submitting it !!
return Mapping;
end Split_Result_For_Different_Reducer;
procedure Merge_Job_Results(Xml_Node : Ada_Mr.Xml.Node_Access; Stop_System : out Boolean) is
begin
null;
-- Never stop the system at this point
Stop_System := false;
end Merge_Job_Results;
procedure Finalize is
begin
null;
end Finalize;
end #job_package#;
""")
job_adb = job_adb.replace("#job_package#", user_package_name.title())
# create job.adb
file_name = user_package_name + ".adb"
out_file = open(path + file_name,"w")
out_file.write(job_adb)
out_file.close()
# make file
makefile = ("""\
VER=1.0.0
DEL=rm -f
DELDIR= rm -rf
CC=gnatmake
CFLAGS=-largs -L/Developer/SDKs/MacOSX10.5.sdk/usr/lib -mmacosx-version-min=10.5.0 -shared-libgcc -ladacrypt -ladamr -ladacrypt
LIBS=-I/usr/local/lib/libadamr -I/usr/local/lib/libadacrypt
all: clean pure_master pure_mapper pure_reducer clean_compilation_files
pure_master:
$(DEL) master
$(CC) $(LIBS) master.adb $(CFLAGS)
pure_mapper:
$(DEL) mapper
$(CC) $(LIBS) mapper.adb $(CFLAGS)
pure_reducer:
$(DEL) reducer
$(CC) $(LIBS) reducer.adb $(CFLAGS)
clean_compilation_files:
$(DEL) *.o *.ali
master: pure_master clean_compilation_files
mapper: pure_mapper clean_compilation_files
reducer: pure_reducer clean_compilation_files
clean:
$(DEL) *.o *.ali master mapper reducer
""")
# create Makefile
file_name = "Makefile"
out_file = open(path + file_name,"w")
out_file.write(makefile)
out_file.close()
# master.adb
master_adb = ("""\
-- insert specification for the job
with Ada_Mr.Master.Main;
procedure Master is
package Job renames #job_package#;
package Master_MR is new Ada_Mr.Master.Main(
Job.Job,
Job.From_Xml,
Job.To_Xml,
Job.Set_Job_Id,
Job.Get_Job_Id,
Job.Print_Job,
Job.Split_Raw_Data,
Job.Get_Next_Raw_Job
);
begin
declare
M : Master_MR.Master_Task_Access := new Master_MR.Master_Task;
begin
M.Start(M);
end;
end Master;
""")
master_adb = master_adb.replace("#job_package#", user_package_name.title())
master_adb = master_adb.replace("-- insert specification for the job", "with " + user_package_name.title() + ";")
# create master.adb
file_name = "master.adb"
out_file = open(path + file_name,"w")
out_file.write(master_adb)
out_file.close()
#mapper.adb
mapper_adb = ("""\
-- insert specification for the job
with Ada_Mr.Mapper.Main;
procedure Mapper is
package Job renames #job_package#;
package Mapper_MR is new Ada_Mr.Mapper.Main(
Job.Job,
Job.From_Xml,
Job.To_Xml,
Job.Get_Job_Id,
Job.Compute_Job,
Job.Split_Result_For_Different_Reducer
);
begin
declare
C : Mapper_MR.Mapper_Task_Access := new Mapper_MR.Mapper_Task;
begin
C.Start(C);
end;
end Mapper;
""")
mapper_adb = mapper_adb.replace("#job_package#", user_package_name.title())
mapper_adb = mapper_adb.replace("-- insert specification for the job", "with " + user_package_name.title() + ";")
# create mapper.adb
file_name = "mapper.adb"
out_file = open(path + file_name,"w")
out_file.write(mapper_adb)
out_file.close()
reducer_adb = ("""\
-- insert specification for the job
with Ada_Mr.Reducer.Main;
procedure Reducer is
package Job renames #job_package#;
package Reducer_MR is new Ada_Mr.Reducer.Main(
Job.Merge_Job_Results,
Job.Finalize
);
begin
declare
C : Reducer_MR.Reducer_Task_Access := new Reducer_MR.Reducer_Task;
begin
C.Start(C);
end;
end Reducer;
""")
reducer_adb = reducer_adb.replace("#job_package#", user_package_name.title())
reducer_adb = reducer_adb.replace("-- insert specification for the job", "with " + user_package_name.title() + ";")
# create reducer.adb
file_name = "reducer.adb"
out_file = open(path + file_name,"w")
out_file.write(reducer_adb)
out_file.close()
# master_config
master_config = ("""\
<?xml version="1.0" ?>
<adamr-master-config>
<local_server>
<ip></ip>
<port></port>
</local_server>
<crypto>
<hmac></hmac>
</crypto>
<settings>
<max_connection_tries></max_connection_tries>
<timeout_connection_tries></timeout_connection_tries>
<log_level></log_level>
</settings>
<user></user>
</adamr-master-config>
""")
# create master_config.xml
if create_example_config_files == "y":
file_name = "master_config.xml.example"
out_file = open(path + file_name,"w")
out_file.write(master_config)
out_file.close()
# mapper_config
mapper_config = ("""\
<?xml version="1.0" ?>
<adamr-mapper-config>
<identifier></identifier>
<local_server>
<ip></ip>
<port></port>
</local_server>
<master>
<ip></ip>
<port></port>
</master>
<crypto>
<hmac></hmac>
</crypto>
<settings>
<max_connection_tries></max_connection_tries>
<timeout_connection_tries></timeout_connection_tries>
<log_level></log_level>
</settings>
<user></user>
</adamr-mapper-config>
""")
# create mapper_config.xml
if create_example_config_files == "y":
file_name = "mapper_config.xml.example"
out_file = open(path + file_name,"w")
out_file.write(mapper_config)
out_file.close()
# reducer_config
reducer_config = ("""\
<?xml version="1.0" ?>
<adamr-reducer-config>
<identifier></identifier>
<local_server>
<ip></ip>
<port></port>
</local_server>
<master>
<ip></ip>
<port></port>
</master>
<crypto>
<hmac></hmac>
</crypto>
<settings>
<max_connection_tries></max_connection_tries>
<timeout_connection_tries></timeout_connection_tries>
<log_level></log_level>
</settings>
<user></user>
</adamr-reducer-config>
""")
# create reducer_config.xml
if create_example_config_files == "y":
file_name = "reducer_config.xml.example"
out_file = open(path + file_name,"w")
out_file.write(reducer_config)
out_file.close()