Cross Reference: /ontohub/lib/hets.rb
hets.rb revision 82d525750e23960f3f2cc6a11220e0ef8505918f
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
40c5626383ebd5e8cf11a636f864023a2aafcd6bDaniel Couto Valerequire 'date'
40c5626383ebd5e8cf11a636f864023a2aafcd6bDaniel Couto Vale
58721b9d3a8cd6a624269ddf507f80af4417c9bdhenning muellermodule Hets
083f87068ffcf24df8b0154bfbca4ca5027f8ecehenning mueller class HetsError < Exception; end
58721b9d3a8cd6a624269ddf507f80af4417c9bdhenning mueller class HetsNotFoundError < HetsError; end
ec416eab0158cfe34b77cea4a11f8b84bc194a7aDaniel Couto Vale class HetsVersionOutdatedError < HetsError; end
40c5626383ebd5e8cf11a636f864023a2aafcd6bDaniel Couto Vale class HetsConfigDateFormatError < HetsError; end
40c5626383ebd5e8cf11a636f864023a2aafcd6bDaniel Couto Vale class HetsVersionDateFormatError < HetsError; end
40c5626383ebd5e8cf11a636f864023a2aafcd6bDaniel Couto Vale
40c5626383ebd5e8cf11a636f864023a2aafcd6bDaniel Couto Vale class Config
58721b9d3a8cd6a624269ddf507f80af4417c9bdhenning mueller attr_reader :path
58721b9d3a8cd6a624269ddf507f80af4417c9bdhenning mueller
a837d007b255d7a6cca7994e1e555aba95ce41cchenning mueller def initialize
58721b9d3a8cd6a624269ddf507f80af4417c9bdhenning mueller yaml = YAML.load_file(File.join(Rails.root, 'config', 'hets.yml'))
58721b9d3a8cd6a624269ddf507f80af4417c9bdhenning mueller
58721b9d3a8cd6a624269ddf507f80af4417c9bdhenning mueller @path = first_which_exists yaml['hets_path']
58721b9d3a8cd6a624269ddf507f80af4417c9bdhenning mueller
58721b9d3a8cd6a624269ddf507f80af4417c9bdhenning mueller raise HetsNotFoundError, 'Could not find hets' unless @path
58721b9d3a8cd6a624269ddf507f80af4417c9bdhenning mueller
40c5626383ebd5e8cf11a636f864023a2aafcd6bDaniel Couto Vale unless is_compatible? yaml['hets_version_minimum_date']
40c5626383ebd5e8cf11a636f864023a2aafcd6bDaniel Couto Vale raise HetsVersionOutdatedError, 'The installed version of Hets is too old'
82d525750e23960f3f2cc6a11220e0ef8505918fhenning mueller end
82d525750e23960f3f2cc6a11220e0ef8505918fhenning mueller
82d525750e23960f3f2cc6a11220e0ef8505918fhenning mueller yaml.each_pair do |key, value|
58721b9d3a8cd6a624269ddf507f80af4417c9bdhenning mueller ENV[key.upcase] = first_which_exists value if (key != 'hets_path' and value.is_a? Array)
a837d007b255d7a6cca7994e1e555aba95ce41cchenning mueller end
58721b9d3a8cd6a624269ddf507f80af4417c9bdhenning mueller end
a837d007b255d7a6cca7994e1e555aba95ce41cchenning mueller
a837d007b255d7a6cca7994e1e555aba95ce41cchenning mueller private
a837d007b255d7a6cca7994e1e555aba95ce41cchenning mueller
58721b9d3a8cd6a624269ddf507f80af4417c9bdhenning mueller # Checks Hets installation compatibility by its version date
a837d007b255d7a6cca7994e1e555aba95ce41cchenning mueller #
a837d007b255d7a6cca7994e1e555aba95ce41cchenning mueller # * *Args* :
a837d007b255d7a6cca7994e1e555aba95ce41cchenning mueller # * - +minimum_version+ -> Minimum working hets version
a837d007b255d7a6cca7994e1e555aba95ce41cchenning mueller # * *Returns* :
58721b9d3a8cd6a624269ddf507f80af4417c9bdhenning mueller # * - true if hets version minimum date prior or equal to actual hets version date
58721b9d3a8cd6a624269ddf507f80af4417c9bdhenning mueller # * - false otherwise
083f87068ffcf24df8b0154bfbca4ca5027f8ecehenning mueller def is_compatible?(minimum_version)
083f87068ffcf24df8b0154bfbca4ca5027f8ecehenning mueller # Read Hets version minimum date
58721b9d3a8cd6a624269ddf507f80af4417c9bdhenning mueller raise HetsConfigDateFormatError, 'Could not read hets version minimum date in YAML' unless minimum_version
40c5626383ebd5e8cf11a636f864023a2aafcd6bDaniel Couto Vale
40c5626383ebd5e8cf11a636f864023a2aafcd6bDaniel Couto Vale # Read Hets version date
40c5626383ebd5e8cf11a636f864023a2aafcd6bDaniel Couto Vale version = `#{@path} -V`
b0c849916b1471424344717ada19c745833e4c5bhenning mueller version_date = begin
40c5626383ebd5e8cf11a636f864023a2aafcd6bDaniel Couto Vale Date.parse(version.split.last)
40c5626383ebd5e8cf11a636f864023a2aafcd6bDaniel Couto Vale rescue ArgumentError
40c5626383ebd5e8cf11a636f864023a2aafcd6bDaniel Couto Vale nil
b0c849916b1471424344717ada19c745833e4c5bhenning mueller end
40c5626383ebd5e8cf11a636f864023a2aafcd6bDaniel Couto Vale
b0c849916b1471424344717ada19c745833e4c5bhenning mueller raise HetsVersionDateFormatError, 'Could not read hets version date in output of `hets -V`' unless version_date
40c5626383ebd5e8cf11a636f864023a2aafcd6bDaniel Couto Vale
40c5626383ebd5e8cf11a636f864023a2aafcd6bDaniel Couto Vale # Return true if minimum date is prior or equal to version date
40c5626383ebd5e8cf11a636f864023a2aafcd6bDaniel Couto Vale return minimum_date <= version_date
71fdc30cc6e637d99cacb455537e7b8fbfe77395henning mueller end
b0c849916b1471424344717ada19c745833e4c5bhenning mueller
71fdc30cc6e637d99cacb455537e7b8fbfe77395henning mueller def first_which_exists(array)
71fdc30cc6e637d99cacb455537e7b8fbfe77395henning mueller array.each do |path|
71fdc30cc6e637d99cacb455537e7b8fbfe77395henning mueller path = File.expand_path path
71fdc30cc6e637d99cacb455537e7b8fbfe77395henning mueller return path if File.exists? path
40c5626383ebd5e8cf11a636f864023a2aafcd6bDaniel Couto Vale end
40c5626383ebd5e8cf11a636f864023a2aafcd6bDaniel Couto Vale
40c5626383ebd5e8cf11a636f864023a2aafcd6bDaniel Couto Vale nil
40c5626383ebd5e8cf11a636f864023a2aafcd6bDaniel Couto Vale end
40c5626383ebd5e8cf11a636f864023a2aafcd6bDaniel Couto Vale end
40c5626383ebd5e8cf11a636f864023a2aafcd6bDaniel Couto Vale
a837d007b255d7a6cca7994e1e555aba95ce41cchenning mueller # Runs hets with input_file and returns XML output file path.
a837d007b255d7a6cca7994e1e555aba95ce41cchenning mueller def self.parse(input_file, output_path = '')
58721b9d3a8cd6a624269ddf507f80af4417c9bdhenning mueller config = Config.new
58721b9d3a8cd6a624269ddf507f80af4417c9bdhenning mueller
58721b9d3a8cd6a624269ddf507f80af4417c9bdhenning mueller output_path = "-O \"#{output_path}\"" unless output_path.blank?
58721b9d3a8cd6a624269ddf507f80af4417c9bdhenning mueller
58721b9d3a8cd6a624269ddf507f80af4417c9bdhenning mueller command = "#{config.path} -o xml --full-signatures -v2 #{output_path} '#{input_file}' 2>&1"
58721b9d3a8cd6a624269ddf507f80af4417c9bdhenning mueller
58721b9d3a8cd6a624269ddf507f80af4417c9bdhenning mueller Rails.logger.debug command
58721b9d3a8cd6a624269ddf507f80af4417c9bdhenning mueller
099d0060b8091e71ce78eb3ae1267cfe76a2abddEugen Kuksa # nice runs the process with lower scheduling priority
a837d007b255d7a6cca7994e1e555aba95ce41cchenning mueller status = `nice #{command}`
705933deb08bc4269e8c08d50143af3cb5c1c670henning mueller status = status.split("\n").last
ac2169141f0b549fc8917a4b1d778f4ba3cab0bfJulian Kornberger
ac2169141f0b549fc8917a4b1d778f4ba3cab0bfJulian Kornberger Rails.logger.debug status
ac2169141f0b549fc8917a4b1d778f4ba3cab0bfJulian Kornberger
ac2169141f0b549fc8917a4b1d778f4ba3cab0bfJulian Kornberger if $?.exitstatus != 0 or status.starts_with? '*** Error'
705933deb08bc4269e8c08d50143af3cb5c1c670henning mueller raise HetsError.new(status)
099d0060b8091e71ce78eb3ae1267cfe76a2abddEugen Kuksa end
705933deb08bc4269e8c08d50143af3cb5c1c670henning mueller
099d0060b8091e71ce78eb3ae1267cfe76a2abddEugen Kuksa status.split(': ').last
705933deb08bc4269e8c08d50143af3cb5c1c670henning mueller end
705933deb08bc4269e8c08d50143af3cb5c1c670henning muellerend
705933deb08bc4269e8c08d50143af3cb5c1c670henning mueller