Repository navigation
Expand file tree
/
Copy pathep3-hook.rb
More file actions
executable file
·95 lines (82 loc) · 2.15 KB
/
Copy pathep3-hook.rb
File metadata and controls
executable file
·95 lines (82 loc) · 2.15 KB
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
#!/usr/bin/env ruby
require 'optparse'
require 'fileutils'
def ep3_hook(args)
extensions = []
parser = OptionParser.new
parser.banner = "Usage: ep3 list [options]"
parser.on('--target-dir=DIR')
parser.on('--extensions=EXTS') { |exts|
extensions = exts.split(',')
}
opts = parser.getopts(args)
unless args.empty?
puts parser.help
exit
end
dir = if opts.include? 'target-dir'
opts['target-dir']
else
raise '--target-dir is needed'
end
unless Dir.exist? dir
raise "Directory not found: #{dir}"
end
unless ENV.include? 'EP3_EXT_PATH'
raise 'EP3_EXT_PATH is necessary to apply extensions'
end
extPaths = ENV['EP3_EXT_PATH'].split(':')
extFiles = extensions.map{ |e|
ret = extPaths.find{ |path|
File.exist?(File.join(path, e))
}
if ret.nil?
raise "Extension #{e} not found"
end
File.join(ret, e, 'extension.yml')
}
extArgs = extFiles.map{ |e| "--hook=#{e}" }
ret = apply(File.join(dir, 'workdir', 'root.yml'), extArgs)
if ret
ret = recursiveApply(File.join(dir, 'workdir', 'job.yml'), extArgs)
end
if ret.nil?
1
else
$?.exitstatus
end
end
def apply(network, exts)
orig = "#{network}.orig"
unless File.exist?(orig)
FileUtils.mv(network, orig)
end
ret = system("medal-hook #{orig} #{exts.join(' ')} > #{network}")
code = nil
if ret
code = $?.exitstatus
else
code = if ret.nil? then 1 else $?.exitstatus end
if File.exist?(network) && File.size(network).zero?
FileUtils.mv(orig, network)
end
end
code
end
def recursiveApply(network, exts)
succeeded = apply(network, exts)
unless succeeded.zero?
return succeeded
end
stepdir = File.join(File.dirname(network), 'steps')
if Dir.exist?(stepdir)
Dir.glob("#{stepdir}/*") { |d|
succeeded = recursiveApply(File.join(d, 'job.yml'), exts)
break unless succeeded.zero?
}
end
succeeded
end
if $0 == __FILE__
ep3_hook(ARGV)
end