lib/assembly-utils/utils.rb in assembly-utils-1.4.6 vs lib/assembly-utils/utils.rb in assembly-utils-1.5.0

- old
+ new

@@ -1,19 +1,17 @@ require 'net/ssh' require 'csv' -require 'csv-mapper' require 'druid-tools' begin require 'net/ssh/kerberos' rescue LoadError end module Assembly # The Utils class contains methods to help with accessioning and assembly class Utils - WFS = Dor::WorkflowService REPO = 'dor' # Get the staging directory tree given a druid, and optionally prepend a basepath. # Deprecated and should not be needed anymore. # @@ -161,11 +159,11 @@ # # Example: # puts Assembly::Utils.get_workflow_status('druid:aa000aa0001','assemblyWF','jp2-create') # > "completed" def self.get_workflow_status(druid, workflow, step) - Dor::WorkflowService.get_workflow_status('dor', druid, workflow, step) + Dor::Config.workflow.client.get_workflow_status('dor', druid, workflow, step) end # Cleanup a list of objects and associated files given a list of druids. WARNING: VERY DESTRUCTIVE. # This method only works when this gem is used in a project that is configured to connect to DOR # @@ -262,12 +260,12 @@ ssh_session.exec!("rm -fr #{path_to_content}") unless dry_run end if steps.include?(:workflows) puts "-- deleting #{pid} accessionWF and assemblyWF workflows from Fedora #{ENV['ROBOT_ENVIRONMENT']}" unless dry_run - Dor::WorkflowService.delete_workflow('dor', pid, 'accessionWF') - Dor::WorkflowService.delete_workflow('dor', pid, 'assemblyWF') + Dor::Config.workflow.client.delete_workflow('dor', pid, 'accessionWF') + Dor::Config.workflow.client.delete_workflow('dor', pid, 'assemblyWF') end end rescue Exception => e puts "** cleaning up failed for #{pid} with #{e.message}" ensure @@ -426,11 +424,11 @@ # def self.set_workflow_step_to_error(pid, step) wf_name = Assembly::ASSEMBLY_WF msg = 'Integration testing' params = ['dor', pid, wf_name, step, msg] - resp = Dor::WorkflowService.update_workflow_error_status *params + resp = Dor::Config.workflow.client.update_workflow_error_status *params raise 'update_workflow_error_status() returned false.' unless resp == true end # Delete all workflows for the given PID. Destructive and should only be used when deleting an object from DOR. # This method only works when this gem is used in a project that is configured to connect to DOR @@ -438,11 +436,11 @@ # @param [string] pid of druid # @param [String] repo repository dealing with the workflow. Default is 'dor'. Another option is 'sdr' # e.g. # Assembly::Utils.delete_all_workflows('druid:oo000oo0001') def self.delete_all_workflows(pid, repo = 'dor') - Dor::WorkflowService.get_workflows(pid).each {|workflow| Dor::WorkflowService.delete_workflow(repo, pid, workflow)} + Dor::Config.workflow.client.get_workflows(pid).each {|workflow| Dor::Config.workflow.client.delete_workflow(repo, pid, workflow)} end # Reindex the supplied PID in solr. # # @param [string] pid of druid @@ -458,11 +456,11 @@ # This method only works when this gem is used in a project that is configured to connect to DOR def self.clear_stray_workflows repo = 'dor' wf = 'assemblyWF' msg = 'Integration testing' - wfs = Dor::WorkflowService + wfs = Dor::Config.workflow.client steps = Assembly::ASSEMBLY_WF_STEPS.map { |s| s[0] } completed = steps[0] steps.each do |waiting| druids = wfs.get_objects_for_workstep completed, waiting, repo, wf @@ -481,11 +479,11 @@ # @return [boolean] if object is fully ingested # Example: # Assembly::Utils.is_ingested?('druid:oo000oo0001') # > false def self.is_ingested?(pid) - WFS.get_lifecycle(REPO, pid, 'accessioned') ? true : false + Dor::Config.workflow.client.get_lifecycle(REPO, pid, 'accessioned') ? true : false end # Check if the object is currently in accessioning # This method only works when this gem is used in a project that is configured to connect to the workflow service. # @@ -493,11 +491,11 @@ # @return [boolean] if object is currently in accessioning # Example: # Assembly::Utils.in_accessioning?('druid:oo000oo0001') # > false def self.in_accessioning?(pid) - WFS.get_active_lifecycle(REPO, pid, 'submitted') ? true : false + Dor::Config.workflow.client.get_active_lifecycle(REPO, pid, 'submitted') ? true : false end # Check if the object is on ingest hold # This method only works when this gem is used in a project that is configured to connect to the workflow service. # @@ -505,11 +503,11 @@ # @return [boolean] if object is on ingest hold # Example: # Assembly::Utils.ingest_hold?('druid:oo000oo0001') # > false def self.ingest_hold?(pid) - WFS.get_workflow_status(REPO, pid, 'accessionWF', 'sdr-ingest-transfer') == 'hold' + Dor::Config.workflow.client.get_workflow_status(REPO, pid, 'accessionWF', 'sdr-ingest-transfer') == 'hold' end # Check if the object is submitted # This method only works when this gem is used in a project that is configured to connect to the workflow service. # @@ -517,11 +515,11 @@ # @return [boolean] if object is submitted # Example: # Assembly::Utils.is_submitted?('druid:oo000oo0001') # > false def self.is_submitted?(pid) - WFS.get_lifecycle(REPO, pid, 'submitted').nil? + Dor::Config.workflow.client.get_lifecycle(REPO, pid, 'submitted').nil? end # Check if the updates are allowed on the object # This method only works when this gem is used in a project that is configured to connect to the workflow service. # @@ -568,11 +566,11 @@ puts "** #{druid}" begin workflows.each do |workflow, steps| steps.each do |step| puts "Updating #{workflow}:#{step} to #{state}" - Dor::WorkflowService.update_workflow_status 'dor', druid, workflow, step, state + Dor::Config.workflow.client.update_workflow_status 'dor', druid, workflow, step, state end end rescue Exception => e puts "an error occurred trying to update workflows for #{druid} with message #{e.message}" end @@ -585,18 +583,18 @@ # @param [string] filename of CSV that has a column called "druid" # @return [array] array of druids # Example: # Assembly::Utils.read_druids_from_file('download.csv') # ['druid:xxxxx', 'druid:yyyyy'] def self.read_druids_from_file(csv_filename) - rows = CsvMapper.import(csv_filename) do read_attributes_from_file end - druids = [] - rows.each do |row| - druid = row.druid + return to_enum(:read_druids_from_file, csv_filename) unless block_given? + + CSV.foreach(csv_filename, :headers => true) do |row| + druid = row['druid'] druid = "druid:#{druid}" unless druid.include?('druid:') - druids << druid + + yield druid end - druids end # Get a list of druids that have errored out in a particular workflow and step # # @param [string] workflow name @@ -606,10 +604,10 @@ # @return [hash] hash of results, with key has a druid, and value as the error message # e.g. # result=Assembly::Utils.get_errored_objects_for_workstep('accessionWF','content-metadata','Project : Revs') # => {"druid:qd556jq0580"=>"druid:qd556jq0580 - Item error; caused by #<Rubydora::FedoraInvalidRequest: Error modifying datastream contentMetadata for druid:qd556jq0580. See logger for details>"} def self.get_errored_objects_for_workstep(workflow, step, tag = '') - result = Dor::WorkflowService.get_errored_objects_for_workstep workflow, step, 'dor' + result = Dor::Config.workflow.client.get_errored_objects_for_workstep workflow, step, 'dor' return result if tag == '' filtered_result = {} result.each do |druid, error| begin item = Dor::Item.find(druid)