Cross Reference: /ontohub/lib/hets.rb
hets.rb revision 099d0060b8091e71ce78eb3ae1267cfe76a2abdd
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
40c5626383ebd5e8cf11a636f864023a2aafcd6bDaniel Couto Valerequire 'date'
40c5626383ebd5e8cf11a636f864023a2aafcd6bDaniel Couto Vale
58721b9d3a8cd6a624269ddf507f80af4417c9bdhenning muellermodule Hets
58721b9d3a8cd6a624269ddf507f80af4417c9bdhenning mueller
40c5626383ebd5e8cf11a636f864023a2aafcd6bDaniel Couto Vale class HetsError < Exception; end
40c5626383ebd5e8cf11a636f864023a2aafcd6bDaniel Couto Vale class HetsDeploymentError < Exception; end
40c5626383ebd5e8cf11a636f864023a2aafcd6bDaniel Couto Vale class HetsNotFoundError < HetsError; end
40c5626383ebd5e8cf11a636f864023a2aafcd6bDaniel Couto Vale class HetsVersionOutdatedError < HetsError; end
58721b9d3a8cd6a624269ddf507f80af4417c9bdhenning mueller class HetsConfigDateFormatError < HetsError; end
58721b9d3a8cd6a624269ddf507f80af4417c9bdhenning mueller class HetsVersionDateFormatError < HetsError; end
2b056a2f5c950d0b5aec8f3f08d1050c408d73a2henning mueller
58721b9d3a8cd6a624269ddf507f80af4417c9bdhenning mueller class Config
58721b9d3a8cd6a624269ddf507f80af4417c9bdhenning mueller attr_reader :path, :library_path
58721b9d3a8cd6a624269ddf507f80af4417c9bdhenning mueller
58721b9d3a8cd6a624269ddf507f80af4417c9bdhenning mueller def initialize
58721b9d3a8cd6a624269ddf507f80af4417c9bdhenning mueller yaml = YAML.load_file(File.join(Rails.root, 'config', 'hets.yml'))
58721b9d3a8cd6a624269ddf507f80af4417c9bdhenning mueller
40c5626383ebd5e8cf11a636f864023a2aafcd6bDaniel Couto Vale @path = first_which_exists yaml['hets_path']
40c5626383ebd5e8cf11a636f864023a2aafcd6bDaniel Couto Vale
82d525750e23960f3f2cc6a11220e0ef8505918fhenning mueller raise HetsNotFoundError, 'Could not find hets' unless @path
82d525750e23960f3f2cc6a11220e0ef8505918fhenning mueller
82d525750e23960f3f2cc6a11220e0ef8505918fhenning mueller unless is_compatible? yaml['hets_version_minimum_date']
58721b9d3a8cd6a624269ddf507f80af4417c9bdhenning mueller raise HetsVersionOutdatedError, 'The installed version of Hets is too old'
58721b9d3a8cd6a624269ddf507f80af4417c9bdhenning mueller end
40c5626383ebd5e8cf11a636f864023a2aafcd6bDaniel Couto Vale
58721b9d3a8cd6a624269ddf507f80af4417c9bdhenning mueller # Set hets environment variables for when the wrapper script is not used.
58721b9d3a8cd6a624269ddf507f80af4417c9bdhenning mueller yaml.each_pair do |key, value|
58721b9d3a8cd6a624269ddf507f80af4417c9bdhenning mueller if key != 'hets_path' and value.is_a? Array
58721b9d3a8cd6a624269ddf507f80af4417c9bdhenning mueller ENV[key.upcase] = first_which_exists value
58721b9d3a8cd6a624269ddf507f80af4417c9bdhenning mueller end
40c5626383ebd5e8cf11a636f864023a2aafcd6bDaniel Couto Vale end
40c5626383ebd5e8cf11a636f864023a2aafcd6bDaniel Couto Vale
40c5626383ebd5e8cf11a636f864023a2aafcd6bDaniel Couto Vale @library_path = first_which_exists yaml['hets_lib']
82d525750e23960f3f2cc6a11220e0ef8505918fhenning mueller
40c5626383ebd5e8cf11a636f864023a2aafcd6bDaniel Couto Vale raise HetsDeploymentError, 'Hets library not found.' unless @library_path
40c5626383ebd5e8cf11a636f864023a2aafcd6bDaniel Couto Vale end
40c5626383ebd5e8cf11a636f864023a2aafcd6bDaniel Couto Vale
82d525750e23960f3f2cc6a11220e0ef8505918fhenning mueller
40c5626383ebd5e8cf11a636f864023a2aafcd6bDaniel Couto Vale private
82d525750e23960f3f2cc6a11220e0ef8505918fhenning mueller
40c5626383ebd5e8cf11a636f864023a2aafcd6bDaniel Couto Vale # Checks Hets installation compatibility by its version date
40c5626383ebd5e8cf11a636f864023a2aafcd6bDaniel Couto Vale #
40c5626383ebd5e8cf11a636f864023a2aafcd6bDaniel Couto Vale # * *Args* :
71fdc30cc6e637d99cacb455537e7b8fbfe77395henning mueller # * - +minimum_date+ -> Minimum working hets version date
71fdc30cc6e637d99cacb455537e7b8fbfe77395henning mueller # * *Returns* :
71fdc30cc6e637d99cacb455537e7b8fbfe77395henning mueller # * - true if hets version minimum date prior or equal to actual hets version date
71fdc30cc6e637d99cacb455537e7b8fbfe77395henning mueller # * - false otherwise
71fdc30cc6e637d99cacb455537e7b8fbfe77395henning mueller def is_compatible?(minimum_date)
71fdc30cc6e637d99cacb455537e7b8fbfe77395henning mueller # Read Hets version minimum date
40c5626383ebd5e8cf11a636f864023a2aafcd6bDaniel Couto Vale raise HetsConfigDateFormatError, 'Could not read hets version minimum date in YAML' unless minimum_date
40c5626383ebd5e8cf11a636f864023a2aafcd6bDaniel Couto Vale
40c5626383ebd5e8cf11a636f864023a2aafcd6bDaniel Couto Vale # Read Hets version date
40c5626383ebd5e8cf11a636f864023a2aafcd6bDaniel Couto Vale version = `#{@path} -V`
40c5626383ebd5e8cf11a636f864023a2aafcd6bDaniel Couto Vale version_date = begin
40c5626383ebd5e8cf11a636f864023a2aafcd6bDaniel Couto Vale Date.parse version.split.last
58721b9d3a8cd6a624269ddf507f80af4417c9bdhenning mueller rescue ArgumentError
58721b9d3a8cd6a624269ddf507f80af4417c9bdhenning mueller nil
58721b9d3a8cd6a624269ddf507f80af4417c9bdhenning mueller end
58721b9d3a8cd6a624269ddf507f80af4417c9bdhenning mueller
58721b9d3a8cd6a624269ddf507f80af4417c9bdhenning mueller raise HetsVersionDateFormatError, 'Could not read hets version date in output of `hets -V`' unless version_date
58721b9d3a8cd6a624269ddf507f80af4417c9bdhenning mueller
58721b9d3a8cd6a624269ddf507f80af4417c9bdhenning mueller # Return true if minimum date is prior or equal to version date
58721b9d3a8cd6a624269ddf507f80af4417c9bdhenning mueller return minimum_date <= version_date
58721b9d3a8cd6a624269ddf507f80af4417c9bdhenning mueller end
58721b9d3a8cd6a624269ddf507f80af4417c9bdhenning mueller
58721b9d3a8cd6a624269ddf507f80af4417c9bdhenning mueller def first_which_exists(paths)
705933deb08bc4269e8c08d50143af3cb5c1c670henning mueller paths.map { |path| File.expand_path path }.each do |path|
58721b9d3a8cd6a624269ddf507f80af4417c9bdhenning mueller return path if File.exists? path
705933deb08bc4269e8c08d50143af3cb5c1c670henning mueller end
705933deb08bc4269e8c08d50143af3cb5c1c670henning mueller
705933deb08bc4269e8c08d50143af3cb5c1c670henning mueller nil
1cc8cf027c7907544308ffefc3e8071292c907f7henning mueller end
705933deb08bc4269e8c08d50143af3cb5c1c670henning mueller end
705933deb08bc4269e8c08d50143af3cb5c1c670henning mueller
705933deb08bc4269e8c08d50143af3cb5c1c670henning mueller # Runs hets with input_file and returns XML output file path.
574a02d837d442144fe066284dfed20e43a42053Julian Kornberger def self.parse(input_file, url_catalog = [], output_path = nil)
574a02d837d442144fe066284dfed20e43a42053Julian Kornberger @@config ||= Config.new
7ea9649883e1bbe8f2582db1a3c66af8b7206056henning mueller
705933deb08bc4269e8c08d50143af3cb5c1c670henning mueller if output_path
c34ca181e48d016718223de38106fdfbe8ffc65aDaniel Couto Vale FileUtils.mkdir_p output_path
705933deb08bc4269e8c08d50143af3cb5c1c670henning mueller output_path = "-O \"#{output_path}\""
0772cf7b22adf46dbab68e829d6a2cd8e8d1bc2ahenning mueller end
58721b9d3a8cd6a624269ddf507f80af4417c9bdhenning mueller
58721b9d3a8cd6a624269ddf507f80af4417c9bdhenning mueller url_mapping = url_catalog.empty? '' : " -C #{url_catalog.join(',')}"
7ea9649883e1bbe8f2582db1a3c66af8b7206056henning mueller
7ea9649883e1bbe8f2582db1a3c66af8b7206056henning mueller command = "#{@@config.path}#{url_mapping} -o xml --full-signatures -a none -v2 #{output_path} '#{input_file}' 2>&1"
58721b9d3a8cd6a624269ddf507f80af4417c9bdhenning mueller
58721b9d3a8cd6a624269ddf507f80af4417c9bdhenning mueller Rails.logger.debug command
# Executes command with low priority
output = `nice #{command}`
# Exclude usage message if exit status equals 2
if $?.exitstatus == 2 and output.include? 'Usage:'
output = output.split("Usage:").first
end
output = output.split("\n").last
Rails.logger.debug output
# Raise error if exit status different from 0
if $?.exitstatus != 0 or output.starts_with? '*** Error'
raise HetsError.new(output)
end
return output.split(': ').last
end
# Traverses a directory recursively, importing ontology file with supported
# extension.
#
# @param user [User] the user that imports the ontology files
# @param repo [Repository] Repository, the files shall be saved in
# @param dir [String] the path to the ontology library
#
def self.import_ontologies(user, repo, dir)
find_ontologies(dir) { |path| import_ontology(user, repo, dir, path) }
end
# Imports an ontology in demand of a user.
#
# @param user [User] the user that imports the ontology file
# @param repo [Repository] Repository, the files shall be saved in
# @param path [String] the path to the ontology file
#
def self.import_ontology(user, repo, dir, path)
relpath = File.relative_path(dir, path)
puts relpath
repo.save_file(path, relpath, "Added #{relpath}.", user)
end
def self.library_path
(@@config ||= Config.new).library_path
end
private
# Traverses a directory for ontologies with supported extensions recursively,
# yielding their path.
def self.find_ontologies(dir)
Dir.glob("#{dir}/**/*.{#{Ontology::FILE_EXTENSIONS.join(',')}}").each { |path| yield path }
end
end