Class: BioDSL::Filesys

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/BioDSL/filesys.rb

Overview

Class for handling filesystem manipulations.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ios) ⇒ Filesys

Returns a new instance of Filesys.



111
112
113
# File 'lib/BioDSL/filesys.rb', line 111

def initialize(ios)
  @io = ios
end

Instance Attribute Details

#ioObject (readonly)

Returns the value of attribute io.



109
110
111
# File 'lib/BioDSL/filesys.rb', line 109

def io
  @io
end

Class Method Details

.open(*args) ⇒ Object

Open a file which may be compressed with gzip og bzip2.



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
# File 'lib/BioDSL/filesys.rb', line 67

def self.open(*args)
  file    = args.shift
  mode    = args.shift
  options = args.shift || {}

  if mode == 'w'
    case options[:compress]
    when :gzip
      ios, = Open3.pipeline_w('gzip -f', out: file)
    when :bzip, :bzip2
      ios, = Open3.pipeline_w('bzip2 -c', out: file)
    else
      ios = File.open(file, mode, options)
    end
  else
    type = if file.respond_to? :path
             `file -Lk #{file.path}`
           else
             `file -Lk #{file}`
           end

    ios = case type
          when /gzip/
            IO.popen("gzip -cd #{file}")
          when /bzip/
            IO.popen("bzcat #{file}")
          else
            File.open(file, mode, options)
          end
  end

  if block_given?
    begin
      yield new(ios)
    ensure
      ios.close
    end
  else
    return new(ios)
  end
end

.tmpfile(tmp_dir = ) ⇒ Object

Class method that returns a path to a unique temporary file. If no directory is specified reverts to the systems tmp directory.



58
59
60
61
62
63
64
# File 'lib/BioDSL/filesys.rb', line 58

def self.tmpfile(tmp_dir = ENV['TMPDIR'])
  time = Time.now.to_i
  user = ENV['USER']
  pid  = $PID
  path = tmp_dir + [user, time + pid, pid].join('_') + '.tmp'
  path
end

.which(cmd) ⇒ Object

Cross-platform way of finding an executable in the $PATH.

which('ruby') #=> /usr/bin/ruby


43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/BioDSL/filesys.rb', line 43

def self.which(cmd)
  exts = ENV['PATHEXT'] ? ENV['PATHEXT'].split(';') : ['']

  ENV['PATH'].split(File::PATH_SEPARATOR).each do |path|
    exts.each do |ext|
      exe = File.join(path, "#{cmd}#{ext}")
      return exe if File.executable?(exe) && !File.directory?(exe)
    end
  end

  nil
end

Instance Method Details

#closeObject



131
132
133
# File 'lib/BioDSL/filesys.rb', line 131

def close
  @io.close
end

#eachObject

Iterator method for parsing entries.



140
141
142
143
144
# File 'lib/BioDSL/filesys.rb', line 140

def each
  return to_enum :each unless block_given?

  @io.each { |line| yield line }
end

#eof?Boolean

Returns:

  • (Boolean)


135
136
137
# File 'lib/BioDSL/filesys.rb', line 135

def eof?
  @io.eof?
end

#getsObject



115
116
117
# File 'lib/BioDSL/filesys.rb', line 115

def gets
  @io.gets
end

#puts(*args) ⇒ Object



119
120
121
# File 'lib/BioDSL/filesys.rb', line 119

def puts(*args)
  @io.puts(*args)
end

#readObject



123
124
125
# File 'lib/BioDSL/filesys.rb', line 123

def read
  @io.read
end

#write(arg) ⇒ Object



127
128
129
# File 'lib/BioDSL/filesys.rb', line 127

def write(arg)
  @io.write arg
end