Class: RXFReadWrite

Inherits:
RXFReader
  • Object
show all
Defined in:
lib/rxfreadwrite.rb

Constant Summary collapse

@@fs =
:local

Class Method Summary collapse

Class Method Details

.chdir(x) ⇒ Object



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

def self.chdir(x)

  # We can use @@fs within chdir() to flag the current file system.
  # Allowing us to use relative paths with FileX operations instead
  # of explicitly stating the path each time. e.g. touch 'foo.txt'
  #

  if x[/^file:\/\//] or File.exists?(File.dirname(x)) then

    @@fs = :local
    FileUtils.chdir x

  elsif x[/^dfs:\/\//]

    host = x[/(?<=dfs:\/\/)[^\/]+/]
    @@fs = 'dfs://' + host
    DfsFile.chdir x

  end

end

.exists?(filename) ⇒ Boolean

Returns:

  • (Boolean)


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

def self.exists?(filename)

  type = self.filetype(filename)

  filex = case type
  when :file
    File
  when :dfs
    DfsFile
  else
    nil
  end

  return nil unless filex

  filex.exists? filename

end

.filetype(x) ⇒ Object



119
120
121
122
123
124
125
# File 'lib/rxfreadwrite.rb', line 119

def self.filetype(x)

  return :string if x.lines.length > 1
  return :dfs if @@fs[0..2] == 'dfs'
  RXFReader.filetype(x)

end

.fsObject

identifies the working file system



74
75
76
# File 'lib/rxfreadwrite.rb', line 74

def self.fs()
  @@fs
end

.glob(s) ⇒ Object



127
128
129
130
131
132
133
134
135
# File 'lib/rxfreadwrite.rb', line 127

def self.glob(s)

  if s[/^dfs:\/\//] then
    DfsFile.glob(s)
  else
    Dir.glob(s)
  end

end

.mkdir(x) ⇒ Object



137
138
139
140
141
142
143
144
145
# File 'lib/rxfreadwrite.rb', line 137

def self.mkdir(x)

  if x[/^file:\/\//] or File.exists?(File.dirname(x)) then
    FileUtils.mkdir x
  elsif x[/^dfs:\/\//]
    DfsFile.mkdir x
  end

end

.mkdir_p(x) ⇒ Object



147
148
149
150
151
152
153
154
155
# File 'lib/rxfreadwrite.rb', line 147

def self.mkdir_p(x)

  if x[/^dfs:\/\//] then
    DfsFile.mkdir_p x
  else
    FileUtils.mkdir_p x
  end

end

.pwdObject



157
158
159
# File 'lib/rxfreadwrite.rb', line 157

def self.pwd()
  DfsFile.pwd
end

.rm(filename) ⇒ Object



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/rxfreadwrite.rb', line 161

def self.rm(filename)

  case filename[/^\w+(?=:\/\/)/]
  when 'dfs'
    DfsFile.rm filename
  else

    if File.basename(filename) =~ /\*/ then

      Dir.glob(filename).each do |file|

        begin
          FileUtils.rm file
        rescue
          puts ('RXFReadWrite#rm: ' + file + ' is a Directory').warning
        end

      end

    else
      FileUtils.rm filename
    end

  end

end

.rm_r(filename, force: false) ⇒ Object



188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'lib/rxfreadwrite.rb', line 188

def self.rm_r(filename, force: false)

  case filename[/^\w+(?=:\/\/)/]
  when 'dfs'
    DfsFile.rm_r filename, force: force
  else

    if File.basename(filename) =~ /\*/ then

      Dir.glob(filename).each do |file|

        begin
          FileUtils.rm_r file, force: force
        rescue
          puts ('RXFReadWrite#rm_r: ' + file + ' is a Directory').warning
        end

      end

    else
      FileUtils.rm_r filename, force: force
    end

  end

end

.rm_rf(filename) ⇒ Object



215
216
217
# File 'lib/rxfreadwrite.rb', line 215

def self.rm_rf(filename)
  rm_r(filename, force: true)
end

.touch(filename, mtime: Time.now) ⇒ Object



219
220
221
222
223
224
225
226
227
228
# File 'lib/rxfreadwrite.rb', line 219

def self.touch(filename, mtime: Time.now)

  case filename[/^\w+(?=:\/\/)/]
  when 'dfs'
    DfsFile.touch filename, mtime: mtime
  else
    FileUtils.touch filename, mtime: mtime
  end

end

.write(location, s = nil) ⇒ Object



230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
# File 'lib/rxfreadwrite.rb', line 230

def self.write(location, s=nil)

  case location
  when /^dfs:\/\//

    DfsFile.write location, s

  else

    if DfsFile.exists?(File.dirname(location)) then
      DfsFile.write location, s
    else
      File.write(location, s)
    end

  end

end