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



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/metamri/core_additions.rb', line 80

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

def each_pfile(min_file_size = MIN_PFILE_SIZE)
  entries.each do |leaf|
    next unless leaf.to_s =~ /^P.{5}\.7(\.bz2)/
    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_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



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/metamri/core_additions.rb', line 63

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.



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

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



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/metamri/core_additions.rb', line 102

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



118
119
120
# File 'lib/metamri/core_additions.rb', line 118

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