Class: Preservation::Transfer::Pure

Inherits:
Ingest
  • Object
show all
Defined in:
lib/preservation/transfer/pure.rb

Overview

Transfer preparation for Pure

Instance Attribute Summary

Attributes inherited from Ingest

#logger

Instance Method Summary collapse

Constructor Details

#initialize(base_url: nil, username: nil, password: nil, basic_auth: nil) ⇒ Pure

Returns a new instance of Pure.

Parameters:

  • base_url (String) (defaults to: nil)
  • username (String) (defaults to: nil)
  • password (String) (defaults to: nil)
  • basic_auth (Boolean) (defaults to: nil)


15
16
17
18
19
20
21
22
23
# File 'lib/preservation/transfer/pure.rb', line 15

def initialize(base_url: nil, username: nil, password: nil, basic_auth: nil)
  super()
  @base_url = base_url
  @basic_auth = basic_auth
  if basic_auth === true
    @username = username
    @password = password
  end
end

Instance Method Details

#prepare_dataset(uuid: nil, dir_scheme: :uuid, delay: 0) ⇒ Object

For given uuid, if necessary, fetch the metadata, prepare a directory in the ingest path and populate it with the files and JSON description file.

Parameters:

  • uuid (String) (defaults to: nil)

    uuid to preserve

  • dir_scheme (Symbol) (defaults to: :uuid)

    how to make directory name

  • delay (Integer) (defaults to: 0)

    days to wait (after modification date) before preserving



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
# File 'lib/preservation/transfer/pure.rb', line 32

def prepare_dataset(uuid: nil,
                    dir_scheme: :uuid,
                    delay: 0)
  if uuid.nil?
    @logger.error 'Missing ' + uuid
    exit
  end
  dir_base_path = Preservation.ingest_path

  dataset = Puree::Dataset.new base_url: @base_url,
                               username: @username,
                               password: @password,
                               basic_auth: @basic_auth

  dataset.find uuid: uuid
  d = dataset.
  if d.empty?
    @logger.error 'No metadata for ' + uuid
    exit
  end
  # configurable to become more human-readable
  dir_name = Preservation::Builder.build_directory_name(d, dir_scheme)

  # continue only if dir_name is not empty (e.g. because there was no DOI)
  # continue only if there is no DB entry
  # continue only if the dataset has a DOI
  # continue only if there are files for this resource
  # continue only if it is time to preserve
  if !dir_name.nil? &&
     !dir_name.empty? &&
     !Preservation::Report::Transfer.in_db?(dir_name) &&
     !d['doi'].empty? &&
     !d['file'].empty? &&
     Preservation::Temporal.time_to_preserve?(d['modified'], delay)

    dir_file_path = dir_base_path + '/' + dir_name
     = dir_file_path + '/metadata/'
     =  + 'metadata.json'

    # calculate total size of data files
    download_storage_required = 0
    d['file'].each { |i| download_storage_required += i['size'].to_i }

    # do we have enough space in filesystem to fetch data files?
    if Preservation::Storage.enough_storage_for_download? download_storage_required
      # @logger.info 'Sufficient disk space for ' + dir_file_path
    else
      @logger.error 'Insufficient disk space to store files fetched from Pure. Skipping ' + dir_file_path
    end

    # has metadata file been created? if so, files and metadata are in place
    # continue only if files not present in ingest location
    if !File.size? 

      @logger.info 'Preparing ' + dir_name + ', Pure UUID ' + d['uuid']

      data = []
      d['file'].each do |f|
        o =  d, f
        data << o
        wget_str = Preservation::Builder.build_wget @username,
                                                    @password,
                                                    f['url']

        Dir.mkdir(dir_file_path) if !Dir.exists?(dir_file_path)

        # fetch the file
        Dir.chdir(dir_file_path) do
          # puts 'Changing dir to ' + Dir.pwd
          # puts 'Size of ' + f['name'] + ' is ' + File.size(f['name']).to_s
          if File.size?(f['name'])
            # puts 'Should be deleting ' + f['name']
            File.delete(f['name'])
          end
          # puts f['name'] + ' missing or empty'
          # puts wget_str
          `#{wget_str}`
        end
      end

      Dir.mkdir() if !Dir.exists?()

      pretty = JSON.pretty_generate( data, :indent => '  ')
      # puts pretty
      File.write(,pretty)
      @logger.info 'Created ' + 
    else
      @logger.info 'Skipping ' + dir_name + ', Pure UUID ' + d['uuid'] +
                   ' because ' +  + ' exists'
    end
  else
    @logger.info 'Skipping ' + dir_name + ', Pure UUID ' + d['uuid']
  end
end