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') ⇒ DRbFileClient

Returns a new instance of DRbFileClient.



11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/drb_fileclient.rb', line 11

def initialize(location=nil, host: nil, port: '61010')
  
  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



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/drb_fileclient.rb', line 25

def chdir(raw_path)
  
  return Dir.chdir raw_path unless @directory or raw_path =~ /^dfs:\/\//
  
  directory = if raw_path[0] == '/'  then
    raw_path[1..-1]
  elsif raw_path =~ /^dfs:\/\//
    parse_path(raw_path)
  else      
    File.join(@directory, raw_path)
  end    
  
  if @file.exists? directory then
    @directory = directory
  else
    'No such file or directory'
  end
  
end

#cp(raw_path, raw_path2) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/drb_fileclient.rb', line 45

def cp(raw_path, raw_path2)
  
  unless @directory or raw_path =~ /^dfs:\/\// then
    return FileUtils.cp raw_path, raw_path2 
  end
  
  path, path2 = if raw_path =~ /^dfs:\/\// then
    [parse_path(raw_path), parse_path(raw_path2)]
  else
    [File.join(@directory, raw_path), File.join(@directory, raw_path2)]
  end 
    
  @file.cp path, path2
end

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

Returns:

  • (Boolean)


60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/drb_fileclient.rb', line 60

def exists?(filename=@filename)  
  
  return File.exists? filename unless @directory or filename =~ /^dfs:\/\//
  
  filename2 = if filename =~ /^dfs:\/\// then
    parse_path(filename)
  else

    File.join(@directory, filename)
  end

  @file.exists?(filename2)
  
end

#mkdir(name) ⇒ Object



77
78
79
80
81
82
83
# File 'lib/drb_fileclient.rb', line 77

def mkdir(name)
  
  return FileUtils.mkdir name unless @directory or name =~ /^dfs:\/\//
  
  path = parse_path(name)
  @file.mkdir path
end

#mkdir_p(raw_path) ⇒ Object



85
86
87
88
89
90
91
92
93
# File 'lib/drb_fileclient.rb', line 85

def mkdir_p(raw_path)
  
  unless @directory or raw_path =~ /^dfs:\/\// then
    return FileUtils.mkdir_p raw_path 
  end
  
  path = parse_path(raw_path)
  @file.mkdir_p path
end

#mv(raw_path, raw_path2) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/drb_fileclient.rb', line 95

def mv(raw_path, raw_path2)
  
  unless @directory or raw_path =~ /^dfs:\/\// then
    return FileUtils.mv raw_path, raw_path2  
  end
  
  path, path2 = if raw_path =~ /^dfs:\/\// then
    [parse_path(raw_path), parse_path(raw_path2)]
  else
    [File.join(@directory, raw_path), File.join(@directory, raw_path2)]
  end 
    
  @file.mv path, path2
end

#pwdObject



110
111
112
113
114
115
116
# File 'lib/drb_fileclient.rb', line 110

def pwd()
  
  return Dir.pwd unless @directory
  
  '/' + @directory if @file
  
end

#read(filename = @filename) ⇒ Object



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

def read(filename=@filename)
  
  return File.read filename, s unless @directory or filename =~ /^dfs:\/\//
  
  path = if filename =~ /^dfs:\/\// then
    parse_path(filename)
  else
    File.join(@directory, filename)
  end
  
  @file.read path
end

#rm(path) ⇒ Object



131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/drb_fileclient.rb', line 131

def rm(path)
  
  return FileUtils.rm path unless @directory or path =~ /^dfs:\/\//
  
  path2 = if path =~ /^dfs:\/\// then
    parse_path( path)
  else
    File.join(@directory, path)
  end
    
  @file.rm  path2
  
end

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



145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/drb_fileclient.rb', line 145

def write(filename=@filename, s)
      
  return File.write filename, s unless @directory or filename =~ /^dfs:\/\//
  
  path = if filename =~ /^dfs:\/\// then
    parse_path(filename)
  else
    File.join(@directory, filename)
  end
  
  
  @file.write path, s     
  
end

#zip(filename_zip, a) ⇒ Object



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

def zip(filename_zip, a)
  puts '@directory: ' + @directory.inspect
  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
  
  filepath = if filename_zip =~ /^dfs:\/\// then
    parse_path(filename_zip)
  else
    File.join(@directory, filename_zip)
  end

  @file.zip filepath, a
  
end