-
Notifications
You must be signed in to change notification settings - Fork 0
/
Classes.rb
executable file
·655 lines (601 loc) · 17.1 KB
/
Classes.rb
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
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
#!/usr/bin/env ruby
require 'lib.rb'
#A class to represent each of the different phases in the program
class Guess
def initialize(answer,credit)
@answer = answer
@credit = credit
end
def get_answer
return @answer
end
def get_credit
return @credit
end
end
class Tempvars
@@path = String.new
@@extracted_dir = String.new
def Tempvars.set_path(path)
@@path = path
end
def Tempvars.set_extracted_dir(path)
@@extracted_dir = "#{Sysvars.get_source_dir}/#{path}"
end
def Tempvars.get_path
return @@path
end
def Tempvars.get_extracted_dir
return @@extracted_dir
end
end
class Phase
@@phase_queue = Array.new
def initialize(name,steps,enabled=1,status="created")
@name=name
@steps=steps
@enabled=enabled
@status=status
end
# def Phase.phase_empty(name)
# @name=name
# end
def Phase.phase_new_after(name,steps,name_prev,enabled=0)
temp_phase=new(name,steps,enabled)
temp_phase.set_status("ready")
puts @@phase_queue.size
pos=0
@@phase_queue.each_with_index do |phase_item,index|
if phase_item.get_name==name_prev
@@phase_queue.insert(index+1,temp_phase)
pos=index
break
end
end
if enabled==1
for i in pos...@@phase_queue.size
if @@phase_queue[i].get_enabled==1 && @@phase_queue[i].get_status=="completed"
@@phase_queue[i].set_status("ready")
end
end
end
end
def Phase.phase_new_before(name,steps,name_next,enabled=0)
temp_phase=new(name,steps,enabled)
pos=0
@@phase_queue.each_with_index do |phase_item,index|
if phase_item.get_name==name_next
pos=index
end
end
if enabled==1
for i in pos-1...@@phase_queue.size
if @@phase_queue[i].get_enabled==1 && @@phase_queue[i].get_status=="completed"
@@phase_queue[i].set_status("ready")
end
end
end
temp_phase.set_status("ready")
@@phase_queue.insert(pos,temp_phase)
end
def Phase.run_phase_queue
@@phase_queue.each do |phase_item|
if phase_item.get_enabled==1 && phase_item.get_status=="ready"
puts phase_item.get_steps
system("cd #{Sysvars.get_extracted_dir};#{phase_item.get_steps}")
phase_item.set_status("completed")
end
end
end
def Phase.add_phase(name,steps,pos,rel_phase)
if pos=="before"
Phase.phase_new_before(name,steps,rel_phase,1)
elsif pos=="after"
Phase.phase_new_after(name,steps,rel_phase,1)
elsif pos=="first"
temp_phase = Phase.new(name,steps,rel_phase)
Phase.phase_addto_front(temp_phase)
elsif pos=="last"
temp_phase = Phase.new(name,steps,rel_phase)
Phase.phase_push(temp_phase)
end
end
def Phase.add_patch_source(name,steps,pos,rel_phase)
step_list=steps.split("\n")
if pos=="after"
pos="before"
@@phase_queue.each_with_index do |phase_item,index|
if phase_item.get_name==rel_phase
rel_phase=@@phase_queue[index+1].get_name
end
end
end
if pos=="first"
pos="before"
rel_phase=@@phase_queue[0].get_name
end
if pos=="before"
step_list.each do |step_item|
if step_item.split("@")[1]=="Patch"
Pkgvars.patch_list_push(step_item.split("@")[3])
temp_phase = generate_getfile_phase(step_item.split("@")[3])
Phase.phase_new_before(temp_phase.get_name,temp_phase.get_steps,rel_phase,1)
temp_phase = generate_unpack_phase(Tempvars.get_path)
Phase.phase_new_before(temp_phase.get_name,temp_phase.get_steps,rel_phase,1)
Phase.run_phase_queue
temp_phase = generate_patch_phase(Tempvars.get_extracted_dir,step_item.split("@")[2])
Phase.phase_new_before(temp_phase.get_name,temp_phase.get_steps,rel_phase,1)
elsif step_item.split("@")[1]=="Source"
Pkgvars.src_list_push(step_item.split("@")[3])
temp_phase = generate_getfile_phase(step_item.split("@")[3])
Phase.phase_new_before(temp_phase.get_name,temp_phase.get_steps,rel_phase,1)
temp_phase = generate_unpack_phase(Tempvars.get_path)
Phase.phase_new_before(temp_phase.get_name,temp_phase.get_steps,rel_phase,1)
Phase.run_phase_queue
temp_phase = generate_merge_source_phase()
Phase.phase_new_before(temp_phase.get_name,temp_phase.get_steps,rel_phase,1)
end
end
end
if pos=="last"
step_list.each do |step_item|
if step_item.split("@")[1]=="Patch"
Pkgvars.patch_list_push(step_item.split("@")[3])
Phase.push(generate_getfile_phase(step_item.split("@")[3]))
@@phase_queue.last.enable
@@phase_queue.last.set_status("ready")
Phase.push(generate_unpack_phase(Tempvars.get_path))
@@phase_queue.last.enable
@@phase_queue.last.set_status("ready")
Phase.run_phase_queue
Phase.push(generate_patch_phase(Tempvars.get_extracted_dir,step_item.split("@")[2]))
@@phase_queue.last.enable
@@phase_queue.last.set_status("ready")
elsif step_item.split("@")[0]=="Source"
Pkgvars.src_list_push(step_item.split("@")[3])
Phase.push(generate_getfile_phase(step_item.split("@")[3]))
@@phase_queue.last.enable
@@phase_queue.last.set_status("ready")
Phase.push(generate_unpack_phase(Tempvars.get_path))
@@phase_queue.last.enable
@@phase_queue.last.set_status("ready")
Phase.run_phase_queue
Phase.push(generate_merge_source_phase())
@@phase_queue.last.enable
@@phase_queue.last.set_status("ready")
end
end
end
end
def Phase.phase_queue_concat(ext_queue)
@@phase_queue.concat(ext_queue)
end
def Phase.get_phase_queue()
return @@phase_queue
end
def Phase.phase_push(phase)
@@phase_queue.push(phase)
end
def Phase.phase_set(phase)
@@phase_queue.each do |phase_item|
if phase_item.get_name==phase.get_name
phase_item.set_steps(phase.get_steps)
phase_item.set_status("ready")
phase_item.enable
return
end
end
phase.set_status("ready")
phase.enable
Phase.phase_push(phase)
end
def Phase.phase_addto_front(phase)
@@phase_queue.insert(0,phase)
end
def enable
@enabled=1
end
def disable
@enabled=0
end
def get_steps()
return @steps
end
def get_status
return @status
end
def get_enabled
return @enabled
end
def get_name
return @name
end
def set_status(status)
@status = status
end
def set_steps(steps)
@steps = steps
end
def move_to_before(name_next)
temp_phase=self
@@phase_queue.delete_if{|item| item.name==self.name}
@@phase_queue.each_with_index do |phase_item,index|
if phase_item.name==name_next
pos=index-1
end
end
@@phase_queue.insert(pos,temp_phase)
end
def move_to_after(name_prev)
temp_phase=self
@@phase_queue.delete_if{|item| item.name==self.name}
@@phase_queue.each_with_index do |phase_item,index|
if phase_item.name==name_prev
@@phase_queue.insert(index,temp_phase)
end
end
end
end
#A class to represent each of the responses for any question
class Option
def initialize(option_string,credit=0)
@response = option_string
@credit = credit
end
def get_response
return @response
end
def add_credit(new_credit)
@credit+=new_credit
end
end
#A Class to represent something like if optionX for questionA is true, optionY for questionB should gain N credits
#Currently not being used!!
class Association
def initialize(local_response,remote_question,remote_response,credit_modifier)
@local_response = local_response
@remote_question = remote_question
@remote_response = remote_response
@credit_modifier = credit_modifier
end
end
#Question class with a query string which will be displayed as the question at the prompt and an optionset which will be the choice of answers
#The name field is to be used as an id and it is suggested to use the required variable's name as the name
class Question
@@question_queue = Array.new
@@asked_list = Array.new
@@credit_threshold = 0
#use this to create an objective question
def initialize(name,query_string,options,credits=[])
@name = name
@query_string = query_string
@optionset = Array.new
options.each_with_index do |option_item,i|
if options.size==credits.size
@optionset.push(Option.new(option_item,credits.at(i)))
else
@optionset.push(Option.new(option_item))
end
end
@choice_selected = ""
@status = "created"
@hide = 0
@@question_queue.push(self)
end
#A wrapper to have uniform constructor names
def Question.create_multiple_choice(name,query_string,options,credits=[])
new(name,query_string,options,credits)
end
#use this to create an yes/no type question
def Question.create_yes_no(name,query_string)
options=["yes","no"]
new(name,query_string,options)
end
def Question.create_text_question(name,query_string)
options = []
new(name,query_string,options)
end
def Question.get_question_queue
return @@question_queue
end
def Question.get_question_byname(name)
@@question_queue.each do |question_item|
if question_item.get_name==name
return question_item
end
end
return nil
end
def Question.get_asked_list
return @@asked_list
end
def Question.commit_all_asked
@@asked_list.each do |asked_item|
Question.get_question_byname(asked_item.get_name).commit(asked_item.get_answer)
Pkgvars.set_var(asked_item.get_name,asked_item.get_answer)
end
end
def Question.clear_asked_list
@@asked_list.clear
end
def Question.set_expertise(expertise)
case expertise
when "Newbie"
@@credit_threshold = 30
when "Average"
@@credit_threshold = 60
when "Good"
@@credit_threshold = 100
end
end
def modify_credit(option_string,new_credit)
@optionset.each do |option_item|
if option_item.get_response == option_string
option_item.add_credit(new_credit)
end
end
end
def setask(answer=nil)
if answer!=nil && @status!="committed"
@choice_selected=answer
if Pkgvars.send("get_#{@name}")!=""
@choice_selected=Pkgvars.send("get_#{@name}")
end
elsif @status!="committed"
@choice_selected=$hash_guess[@name].call.get_answer
modify_credit(@choice_selected,$hash_guess[@name].call.get_credit)
end
@status = "ask"
if $hash_guess[@name].call.get_credit>@@credit_threshold
@hide=1
end
end
def get_hide
return @hide
end
def answered(option_selected)
@status = "answered"
@choice_selected = option_selected
end
def commit(option_selected)
@status = "committed"
@choice_selected = option_selected
end
def unask
@status = "created"
end
def get_optionset
return @optionset
end
def get_query_string
return @query_string
end
def get_name
return @name
end
def get_status
return @status
end
def get_answer
return @choice_selected
end
#Add a new Association to the association_list of the Question
def add_association(local_response,remote_question,remote_response,credit_modifier)
@association_list.push(Association.new(local_response,remote_question,remote_response,credit_modifier))
end
#Returns option with maximum credit
=begin def option_max_credit
credit_array = Array.new
@optionset.each do |option_item|
credit_array.push(option_item.credit)
end
end
=end #check with the association class for dependencies and update credits for the dependent options
def update_dependents_credit
@association_list.each do |association_item|
if association_item.local_response == self.choice_selected
question_queue.each do |question_item|
if question_item.query_string==association_item.remote_question && question_item.status=="answered"
question_item.optionset.each do |option_item|
if option_item.response==association_list.remote_response
option_item.credit = option_item+association_list.credit_modifier
end
end
end
end
end
end
end
end
class Pkgvars
@@pkg_name = String.new
@@src_path = String.new
@@patch_path = String.new
@@version = String.new
@@release = String.new
@@arch = String.new
@@license = String.new
@@distro = String.new
@@group = String.new
@@section = String.new
@@buildroot = String.new
@@configure_args = String.new
@@cflags = String.new
@@cxxflags = String.new
@@extra_configure_args = String.new
@@make_args = String.new
@@install_args = String.new
@@maintainer = String.new
@@summary = String.new
@@description = String.new
@@packager_name = String.new
@@packager_email = String.new
@@changes = String.new
@@scriptlet = String.new
@@patch_list = Array.new
@@src_list = Array.new
def Pkgvars.set_var(var_name,value)
case var_name
when "pkg_name"
@@pkg_name = value
when "src_path"
@@src_path = value
if(@@pkg_name == "")
@@pkg_name = File.basename(@@src_path).split("-")[0]
end
# @@src_list.unshift(@src_path)
when "patch_path"
@@patch_path = value
@@patch_list.unshift(@@patch_path)
when "version"
@@version = value
when "release"
@@release = value
when "arch"
@@arch = value
when "license"
@@license = value
when "distro"
@@distro = value
when "group"
@@group = value
when "section"
@@section = value
when "buildroot"
@@buildroot = value
when "configure_args"
@@configure_args = value
when "cflags"
@@cflags = value
when "cxxflags"
@@cxxflags = value
when "extra_configure_args"
@@extra_configure_args = value
when "make_args"
@@make_args = value
when "install_args"
@@install_args = value
when "maintainer"
@@maintainer = value
when "summary"
@@summary = value
when "description"
@@description = value
when "packager_name"
@@packager_name = value
when "packager_email"
@@packager_email = value
when "changes"
@@changes = value
when "scriptlet"
@@scriptlet = value
end
end
def Pkgvars.get_pkg_name
return @@pkg_name
end
def Pkgvars.get_src_path
return @@src_path
end
def Pkgvars.get_patch_path
return @@patch_path
end
def Pkgvars.get_version
return @@version
end
def Pkgvars.get_release
return @@release
end
def Pkgvars.get_arch
return @@arch
end
def Pkgvars.get_license
return @@license
end
def Pkgvars.get_distro
return @@distro
end
def Pkgvars.get_group
return @@group
end
def Pkgvars.get_section
return @@section
end
def Pkgvars.get_buildroot
return @@buildroot
end
def Pkgvars.get_configure_args
return @@configure_args
end
def Pkgvars.get_cflags
return @@cflags
end
def Pkgvars.get_cxxflags
return @@cxxflags
end
def Pkgvars.get_extra_configure_args
return @@extra_configure_args
end
def Pkgvars.get_make_args
return @@make_args
end
def Pkgvars.get_install_args
return @@install_args
end
def Pkgvars.get_maintainer
return @@maintainer
end
def Pkgvars.get_summary
return @@summary
end
def Pkgvars.get_description
return @@description
end
def Pkgvars.get_packager_name
return @@packager_name
end
def Pkgvars.get_packager_email
return @@packager_email
end
def Pkgvars.get_changes
return @@changes
end
def Pkgvars.get_scriptlet
return @@scriptlet
end
def Pkgvars.get_patch_list
return @@patch_list
end
def Pkgvars.get_src_list
return @@src_list
end
def Pkgvars.patch_list_push(value)
@@patch_list.push(value)
end
def Pkgvars.src_list_push(value)
@@src_list.push(value)
end
end
class Sysvars
@@rpm_build_root="/tmp/tmproot/"
@@source_dir="#{`echo $HOME`.chomp}/.apbd/SOURCES"
@@extracted_dir=String.new
def Sysvars.set_source_dir(dir)
@@source_dir = dir
end
def Sysvars.set_rpm_build_root(dir)
@@rpm_build_root = dir
end
def Sysvars.set_extracted_dir(dir)
@@extracted_dir = "#{Sysvars.get_source_dir}/#{dir}"
end
def Sysvars.get_source_dir
return @@source_dir
end
def Sysvars.get_rpm_build_root
return @@rpm_build_root
end
def Sysvars.get_extracted_dir
return @@extracted_dir
end
end