Class: DRbFileClient

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

Instance Method Summary collapse

Constructor Details

#initialize(location = nil, host: nil, port: '61010', debug: false) ⇒ DRbFileClient



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/drb_fileclient.rb', line 15

def initialize(location=nil, host: nil, port: '61010', debug: false)

  @debug = debug

  if location then

    host = location[/(?<=^dfs:\/\/)[^\/:]+/]
    port = location[/(?<=^dfs:\/\/)[^:]+:(\d+)/,1]  || '61010'
    @directory = location[/(?<=^dfs:\/\/)[^\/]+\/(.*)/,1]

  end

  DRb.start_service

end

Instance Method Details

#chdir(raw_path) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/drb_fileclient.rb', line 31

def chdir(raw_path)

  return Dir.chdir raw_path unless @directory or raw_path =~ /^dfs:\/\//

  if raw_path[0] == '/'  then
    directory = raw_path[1..-1]
  elsif raw_path =~ /^dfs:\/\//
    @file, directory = parse_path(raw_path)
  else
    directory = File.join(@directory, raw_path)
  end

  if @file.exists? directory then
    @directory = directory
  else
    'No such file or directory'
  end

end

#chmod(permissions, raw_path) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
# File 'lib/drb_fileclient.rb', line 51

def chmod(permissions, raw_path)

  if raw_path =~ /^dfs:\/\// then

    @file, path = parse_path(raw_path)
    @file.chmod permissions, path

  else
    return FileUtils.chmod(permissions, raw_path)
  end
end

#cp(raw_path, raw_path2) ⇒ Object



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
# File 'lib/drb_fileclient.rb', line 63

def cp(raw_path, raw_path2)

  puts 'inside cp'.info if @debug

  if raw_path =~ /^dfs:\/\// then

    if @debug then
      puts ('raw_path: ' + raw_path.inspect).debug
      puts ('raw_path2: ' + raw_path2.inspect).debug
    end

    if raw_path[/^dfs:\/\/([^\/]+)/] == raw_path2[/^dfs:\/\/([^\/]+)/] then

      _, path = parse_path(raw_path)
      @file, path2 = parse_path(raw_path2)
      @file.cp path, path2

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

      @file, path = parse_path(raw_path)
      file2, path2 = parse_path(raw_path2)
      puts ('path: ' + path.inspect).debug if @debug
      puts ('path2: ' + path.inspect).debug if @debug
      content = @file.read path

      file2.write path2, content

    else

      @file, path = parse_path(raw_path)
      #file2, path2 = parse_path(raw_path2)
      puts ('path: ' + path.inspect).debug if @debug
      puts ('path2: ' + path2.inspect).debug if @debug
      content = @file.read path

      File.write raw_path2, content

    end

  elsif raw_path2 =~ /dfs:\/\// then

    puts 'option2'.info if @debug

    file2, path2 = parse_path(raw_path2)
    puts ('path2: ' + path2.inspect).debug if @debug
    file2.write path2, File.read(raw_path)

  else

    puts 'option3'.info if @debug
    FileUtils.cp raw_path, raw_path2

  end

end

#directory?(filename = @filename) ⇒ Boolean



119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/drb_fileclient.rb', line 119

def directory?(filename=@filename)

  return File.directory? filename unless @directory or filename =~ /^dfs:\/\//

  if filename =~ /^dfs:\/\// then
    @file, filename2 = parse_path(filename)
  else

    filename2 = File.join(@directory, filename)
  end

  @file.directory?(filename2)

end

#exists?(filename = @filename) ⇒ Boolean Also known as: exist?



134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/drb_fileclient.rb', line 134

def exists?(filename=@filename)

  return File.exists? filename unless @directory or filename =~ /^dfs:\/\//

  if filename =~ /^dfs:\/\// then
    @file, filename2 = parse_path(filename)
  else

    filename2 = File.join(@directory, filename)
  end

  @file.exists?(filename2)

end

#glob(s) ⇒ Object



151
152
153
154
155
156
157
158
159
160
161
# File 'lib/drb_fileclient.rb', line 151

def glob(s)

  if s =~ /^dfs:\/\// then
    @file, s2 = parse_path(s)
  else
    s2 = File.join(@directory, s)
  end

  @file.glob s2

end

#ls(raw_path) ⇒ Object



163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/drb_fileclient.rb', line 163

def ls(raw_path)

  return Dir[raw_path] unless @directory or raw_path =~ /^dfs:\/\//

  if raw_path[0] == '/'  then
    path = raw_path[1..-1]
  elsif raw_path =~ /^dfs:\/\//
    @file, path = parse_path(raw_path)
  else
    path = File.join(@directory, raw_path)
  end

  @file.ls path

end

#mkdir(name) ⇒ Object



179
180
181
182
183
184
185
# File 'lib/drb_fileclient.rb', line 179

def mkdir(name)

  return FileUtils.mkdir name unless @directory or name =~ /^dfs:\/\//

  @file, path = parse_path(name)
  @file.mkdir path
end

#mkdir_p(raw_path) ⇒ Object



187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/drb_fileclient.rb', line 187

def mkdir_p(raw_path)

  unless @directory or raw_path =~ /^dfs:\/\// then
    return FileUtils.mkdir_p raw_path
  end

  if raw_path =~ /^dfs:\/\// then
    @file, filepath = parse_path(raw_path)
  else
    filepath = File.join(@directory, raw_path)
  end

  @file.mkdir_p filepath
end

#mv(raw_path, raw_path2) ⇒ Object



202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/drb_fileclient.rb', line 202

def mv(raw_path, raw_path2)

  unless @directory or raw_path =~ /^dfs:\/\// then
    return FileUtils.mv raw_path, raw_path2
  end

  if raw_path =~ /^dfs:\/\// then
    _, path = parse_path(raw_path)
  else
    path = File.join(@directory, raw_path)
  end

  if raw_path2 =~ /^dfs:\/\// then
    _, path2 = parse_path(raw_path2)
  else
    path2 = File.join(@directory, raw_path2)
  end

  @file.mv path, path2
end

#pwdObject



223
224
225
226
227
228
229
# File 'lib/drb_fileclient.rb', line 223

def pwd()

  return Dir.pwd unless @directory

  '/' + @directory if @file

end

#read(filename = @filename) ⇒ Object



231
232
233
234
235
236
237
238
239
240
241
242
# File 'lib/drb_fileclient.rb', line 231

def read(filename=@filename)

  return File.read filename, s unless @directory or filename =~ /^dfs:\/\//

  if filename =~ /^dfs:\/\// then
    @file, path = parse_path(filename)
  else
    path = File.join(@directory, filename)
  end

  @file.read path
end

#rm(path) ⇒ Object



244
245
246
247
248
249
250
251
252
253
254
255
256
# File 'lib/drb_fileclient.rb', line 244

def rm(path)

  return FileUtils.rm path unless @directory or path =~ /^dfs:\/\//

  if path =~ /^dfs:\/\// then
    @file, path2 = parse_path( path)
  else
    path2 = File.join(@directory, path)
  end

  @file.rm  path2

end

#rm_r(path, force: false) ⇒ Object



258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
# File 'lib/drb_fileclient.rb', line 258

def rm_r(path, force: false)

  unless @directory or path =~ /^dfs:\/\// then
    return FileUtils.rm_r(path, force: force)
  end

  if path =~ /^dfs:\/\// then
    @file, path2 = parse_path( path)
  else
    path2 = File.join(@directory, path)
  end

  @file.rm_r(path2, force: force)

end

#ru(path) ⇒ Object



274
275
276
277
278
279
280
281
282
283
284
285
286
287
# File 'lib/drb_fileclient.rb', line 274

def ru(path)

  return DirToXML.new(path, verbose: false).latest unless @directory \
      or path =~ /^dfs:\/\//

  if path =~ /^dfs:\/\// then
    @file, path2 = parse_path( path)
  else
    path2 = File.join(@directory, path)
  end

  @file.ru  path2

end

#ru_r(path) ⇒ Object



289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
# File 'lib/drb_fileclient.rb', line 289

def ru_r(path)

  unless @directory or path =~ /^dfs:\/\// then
    return DirToXML.new(path, recursive: true, verbose: false).latest
  end

  if path =~ /^dfs:\/\// then
    @file, path2 = parse_path( path)
  else
    path2 = File.join(@directory, path)
  end

  @file.ru_r  path2

end

#touch(s, mtime: Time.now) ⇒ Object



305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
# File 'lib/drb_fileclient.rb', line 305

def touch(s, mtime: Time.now)

  unless @directory or s =~ /^dfs:\/\// then
    return FileUtils.touch(s, mtime: mtime)
  end

  if s =~ /^dfs:\/\// then
    @file, s2 = parse_path(s)
  else
    s2 = File.join(@directory, s)
  end

  @file.touch s2, mtime: mtime

end

#write(filename = @filename, s) ⇒ Object



321
322
323
324
325
326
327
328
329
330
331
332
333
# File 'lib/drb_fileclient.rb', line 321

def write(filename=@filename, s)

  return File.write filename, s unless @directory or filename =~ /^dfs:\/\//

  if filename =~ /^dfs:\/\// then
    @file, path = parse_path(filename)
  else
    path = File.join(@directory, filename)
  end

  @file.write path, s

end

#zip(filename_zip, a) ⇒ Object



335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
# File 'lib/drb_fileclient.rb', line 335

def zip(filename_zip, a)

  puts '@directory: ' + @directory.inspect if @debug

  unless @directory or filename_zip =~ /^dfs:\/\// then

    Zip::File.open(zipfile_zip, Zip::File::CREATE) do |x|

      a.each do |filename, buffer|
        x.get_output_stream(filename) {|os| os.write buffer }
      end

    end

  end

  if filename_zip =~ /^dfs:\/\// then
    @file, filepath = parse_path(filename_zip)
  else
    filepath = File.join(@directory, filename_zip)
  end

  @file.zip filepath, a

end