forked from samvera/hyku
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimport_from_purl
executable file
·64 lines (54 loc) · 1.47 KB
/
import_from_purl
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
#!/usr/bin/env ruby
def validate_hostname!(hostname)
return if hostname
usage
$stderr.puts 'Please provide the hostname to import to.'
exit(1)
end
def validate_druid_file!(druid_file)
return if druid_file && File.exist?(druid_file)
usage
$stderr.puts 'Please provide a file of druids you want to import.'
exit(1)
end
def validate_username!(username)
user = User.find_by_user_key(username)
return user if user
usage
$stderr.puts 'username was left blank.'
exit(1)
end
def load_rails(hostname)
puts 'Loading environment...'
require File.expand_path('../../config/environment', __FILE__)
require 'stanford'
AccountElevator.switch!(hostname)
end
def main(hostname, username, druid_file)
validate_hostname!(hostname)
validate_druid_file!(druid_file)
load_rails(hostname)
user = validate_username!(username)
count = druids(druid_file) do |druid|
log = Hyrax::Operation.create!(user: user, operation_type: "Import Purl Metadata")
ImportWorkFromPurlJob.perform_later(user, druid, log)
end
puts "Enqueued #{count} import jobs."
end
def druids(druid_file)
count = 0
File.foreach(druid_file) do |line|
# Split in two parts on the first comma
(element, _) = line.split(/,/, 2)
# remove any namespacing
if md = /(druid:)?(.*)/.match(element)
yield(md[2])
count += 1
end
end
count
end
def usage
$stderr.puts "Usage: #{$PROGRAM_NAME} <hostname> <username> <file of druids>"
end
main(ARGV[0], ARGV[1], ARGV[2])