Class: Pathname

Inherits:
Object
  • Object
show all
Defined in:
lib/metamri/core_additions.rb

Constant Summary collapse

MIN_PFILE_SIZE =
10_000_000

Instance Method Summary collapse

Instance Method Details

#all_dicomsObject



172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/metamri/core_additions.rb', line 172

def all_dicoms
  local_copies = []
  Dir.mktmpdir do |tempdir|
    # begin
      entries.each do |leaf|
        branch = self + leaf
        if leaf.to_s =~ /^I\.(\.bz2)?$|\.dcm(\.bz2)?$|\.[0-9]+(\.bz2)?$/
          local_copies << branch.local_copy(tempdir)
        end
      end

      yield local_copies

    # ensure
      # No ensure needed since Dir.mktmpdir will implode after a block.
      # local_copies.each { |lc| lc.delete if lc.exist? }
    # end
  end
  
  return
end

#each_pfile(min_file_size = MIN_PFILE_SIZE) ⇒ Object



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
# File 'lib/metamri/core_additions.rb', line 40

def each_pfile(min_file_size = MIN_PFILE_SIZE)
  #puts "in each_pfile"
  #puts "core_additions each_pfile ==> local_copy"
  entries.each do |leaf|
    next unless leaf.to_s =~ /^P.{5}\.7(\.bz2)/
    branch = self + leaf
    next if branch.symlink?
    v_pfile_small_but_summary_flag ="N"
    if branch.size < min_file_size
      leaf_summary_s = (leaf.to_s).gsub(/\.bz2/,"")+".summary"
      branch_summary_s = self.to_s+"/"+leaf_summary_s
      if File.exist?(branch_summary_s)
         v_pfile_small_but_summary_flag ="Y"
      end
    end
    if branch.size >= min_file_size or v_pfile_small_but_summary_flag == "Y"
      # check for P*.7.summary 
      # if there, skip local_copy of P*.7.bz2
     leaf_summary_s = (leaf.to_s).gsub(/\.bz2/,"")+".summary"
     branch_summary_s = self.to_s+"/"+leaf_summary_s
     if File.exist?(branch_summary_s)
          branch_summary_pn = Pathname.new(branch_summary_s)
          lc = branch_summary_pn.local_copy
         # summary_lc.delete
     else
      lc = branch.local_copy
     end
      begin
        yield lc
      rescue StandardError => e
        case $LOG.level
        when Logger::DEBUG
          raise e
        else
          puts "#{e}"
        end
      ensure
        lc.delete
      end
    end
  end
end

#each_pfile_non_bz2(min_file_size = MIN_PFILE_SIZE) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/metamri/core_additions.rb', line 83

def each_pfile_non_bz2(min_file_size = MIN_PFILE_SIZE)
    # not using the P*.7.summary --- should be bzip2'ed
  entries.each do |leaf|
    next unless leaf.to_s =~ /^P.{5}(\.7)$/
    branch = self + leaf
    next if branch.symlink?
    if branch.size >= min_file_size
      lc = branch.local_copy
      begin
        yield lc
      rescue StandardError => e
        case $LOG.level
        when Logger::DEBUG
          raise e
        else
          puts "#{e}"
        end
      ensure
        lc.delete
      end
    end
  end
end

#each_pfile_summary(min_file_size = MIN_PFILE_SIZE) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/metamri/core_additions.rb', line 108

def each_pfile_summary(min_file_size = MIN_PFILE_SIZE)
  entries.each do |leaf|
    next unless leaf.to_s =~ /^P.{5}(\.7\.summary)/
    branch = self + leaf
    next if branch.symlink?
    if branch.size >= min_file_size
      lc = branch.local_copy
      begin
        yield lc
      rescue StandardError => e
        case $LOG.level
        when Logger::DEBUG
          raise e
        else
          puts "#{e}"
        end
      ensure
        lc.delete
      end
    end
  end
end

#each_scanner_archive_summaryObject



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/metamri/core_additions.rb', line 132

def each_scanner_archive_summary
  entries.each do |leaf|
    next unless leaf.to_s =~ /^ScanArchive.*(.h5.json)$/
    branch = self + leaf
    next if branch.symlink?
      lc = branch.local_copy
      begin
        yield lc
      rescue StandardError => e
        case $LOG.level
        when Logger::DEBUG
          raise e
        else
          puts "#{e}"
        end
      ensure
        lc.delete
      end
  end
end

#each_subdirectoryObject



29
30
31
32
33
34
35
36
37
38
# File 'lib/metamri/core_additions.rb', line 29

def each_subdirectory
  each_entry do |leaf|
    next if leaf.to_s =~ /^\./
    branch = self + leaf
    next if not branch.directory?
    next if branch.symlink?
    branch.each_subdirectory { |subbranch| yield subbranch }
    yield branch
  end
end

#first_dicomObject



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/metamri/core_additions.rb', line 155

def first_dicom
  entries.each do |leaf|
    branch = self + leaf
    if leaf.to_s =~ /^I\..*(\.bz2)?$|\.dcm(\.bz2)?$|\.[0-9]{2,}(\.bz2)?$/
      lc = branch.local_copy
      begin
        yield lc
      rescue Exception => e
        puts "#{e}"
      ensure
        lc.delete
      end
      return
    end 
  end
end

#local_copy(tempdir = Dir.mktmpdir, &block) ⇒ Object

Creates a local, unzipped copy of a file for use in scanning. Will return a pathname to the local copy if called directly, or can also be passed a block. If it is passed a block, it will create the local copy and ensure the local copy is deleted.



219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
# File 'lib/metamri/core_additions.rb', line 219

def local_copy(tempdir = Dir.mktmpdir, &block)
  tfbase = self.to_s =~ /\.bz2$/ ? self.basename.to_s.chomp(".bz2") : self.basename.to_s
  tfbase.escape_filename
  tmpfile = File.join(tempdir, tfbase)
  # puts tmpfile
  # puts File.exist?(tmpfile)
  File.delete(tmpfile) if File.exist?(tmpfile)
  if self.to_s =~ /\.bz2$/
    `bunzip2 -k -c '#{self.to_s}' >> '#{tmpfile}'`
  else
    FileUtils.cp(self.to_s, tmpfile)
  end

  lc = Pathname.new(tmpfile)
  
  if block
    begin
      yield lc
    ensure
      lc.delete
    end

  else
    return lc
  end
end

#recursive_local_copy(ignore_patterns = [], &block) ⇒ Object



194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/metamri/core_additions.rb', line 194

def recursive_local_copy(ignore_patterns = [], &block)
  tempdir = Dir.mktmpdir('local_orig')

  entries.each do |leaf|
    puts branch = self + leaf
    next if branch.directory?
    ignore_patterns.collect { |pat| next if leaf.to_s =~ pattern } 
    next if branch.should_be_skipped
    puts "Locally provisioning #{leaf}"
    lc = branch.local_copy(tempdir)
    lc.chmod(0444 | 0200 | 0020 )
  end
  
  return tempdir
end

#should_be_skippedObject



210
211
212
# File 'lib/metamri/core_additions.rb', line 210

def should_be_skipped
  self.to_s =~ /^\./ || self.symlink?
end