Class: Cnvrg::Dataset

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(project_home) ⇒ Dataset

Returns a new instance of Dataset.



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

def initialize(project_home)
  begin
    config = YAML.load_file(project_home+"/.cnvrg/config.yml")
    @local_path = project_home
    @title = config[:dataset_name]
    @slug = config[:dataset_slug]
    @owner = config[:owner]
    @working_dir = project_home
  rescue => e

  end

end

Instance Attribute Details

#local_pathObject (readonly)

Returns the value of attribute local_path.



4
5
6
# File 'lib/cnvrg/dataset.rb', line 4

def local_path
  @local_path
end

#ownerObject (readonly)

Returns the value of attribute owner.



4
5
6
# File 'lib/cnvrg/dataset.rb', line 4

def owner
  @owner
end

#slugObject (readonly)

Returns the value of attribute slug.



4
5
6
# File 'lib/cnvrg/dataset.rb', line 4

def slug
  @slug
end

#titleObject (readonly)

Returns the value of attribute title.



4
5
6
# File 'lib/cnvrg/dataset.rb', line 4

def title
  @title
end

#working_dirObject (readonly)

Returns the value of attribute working_dir.



4
5
6
# File 'lib/cnvrg/dataset.rb', line 4

def working_dir
  @working_dir
end

Class Method Details

.clone(owner, dataset_name, dataset_slug) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/cnvrg/dataset.rb', line 123

def self.clone(owner, dataset_name,dataset_slug)

  begin
    list_dirs = [ dataset_name, "#{dataset_name}/.cnvrg"
    ]
    list_files = [
        "#{dataset_name}/.cnvrgignore",
        "#{dataset_name}/.cnvrg/config.yml"
    ]

    config = {dataset_name: dataset_name,
              dataset_slug: dataset_slug,
              owner: owner}


    cnvrgignore = Helpers.cnvrgignore_content
    FileUtils.mkdir_p list_dirs
    FileUtils.touch list_files
    File.open("#{dataset_name}/.cnvrg/config.yml", "w+") { |f| f.write config.to_yaml }
    File.open("#{dataset_name}/.cnvrgignore", "w+") { |f| f.write cnvrgignore } unless File.exist? ".cnvrgignore"
  rescue => e
    puts "Exception in clone request:#{e.message}"
    return false
  end
  return true
end

.init(owner, dataset_name, is_public = false) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/cnvrg/dataset.rb', line 96

def self.init(owner, dataset_name, is_public=false)
    list_dirs = [".cnvrg"
    ]
    list_files = [
        ".cnvrgignore",
        ".cnvrg/config.yml"
    ]

    cnvrgignore = Helpers.cnvrgignore_content
    begin
        response = Cnvrg::API.request("cli/create_dataset", 'POST', {title: dataset_name, owner: owner, is_public: is_public})
        Cnvrg::CLI.is_response_success(response)
      response = JSON.parse response["result"]
      dataset_slug = response["slug"]

      config = {dataset_name: dataset_name,
                dataset_slug: dataset_slug,
                owner: owner}
      FileUtils.mkdir_p list_dirs
      FileUtils.touch list_files
      File.open(".cnvrg/config.yml", "w+") { |f| f.write config.to_yaml }
      File.open(".cnvrgignore", "w+") { |f| f.write cnvrgignore } unless File.exist? ".cnvrgignore"
    rescue => e
      return false
    end
    return true
end

.init_container(owner, dataset_slug, dataset_name) ⇒ Object



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/cnvrg/dataset.rb', line 150

def self.init_container(owner, dataset_slug,dataset_name)

  cnvrgignore = Helpers.cnvrgignore_content
  begin
    list_dirs = [ ".cnvrg"
    ]
    list_files = [
        ".cnvrgignore",
        ".cnvrg/config.yml"
    ]
    FileUtils.mkdir_p list_dirs
    FileUtils.touch list_files

    config = {dataset_name: dataset_name,
              dataset_slug: dataset_slug,
              owner: owner}
    File.open(".cnvrg/config.yml", "w+") { |f| f.write config.to_yaml }

    File.open(".cnvrgignore", "w+") { |f| f.write cnvrgignore } unless File.exist? ".cnvrgignore"
  rescue => e
    return false
  end
  return true
end

Instance Method Details

#compare_commit(commit) ⇒ Object



281
282
283
284
285
286
287
288
289
# File 'lib/cnvrg/dataset.rb', line 281

def compare_commit(commit)
  if commit.nil? or commit.empty?
    commit = last_local_commit
  end
  response = Cnvrg::API.request("users/#{self.owner}/projects/#{self.slug}/commit/compare", 'POST', {current_commit: commit})
  CLI.is_response_success(response,false)
  update_is_new_branch(response["result"]["new_branch"])
  return response["result"]["new_branch"]
end

#compare_commits(commit) ⇒ Object



275
276
277
278
279
# File 'lib/cnvrg/dataset.rb', line 275

def compare_commits(commit)
  response = Cnvrg::API.request("users/#{self.owner}/datasets/#{self.slug}/compare_commits", 'POST', {compare_commit: commit,current_commit:last_local_commit})
  CLI.is_response_success(response,false)
  return response
end

#compare_idx(new_branch, commit = last_local_commit, local_idx = nil) ⇒ Object



236
237
238
239
240
241
242
243
244
245
246
# File 'lib/cnvrg/dataset.rb', line 236

def compare_idx(new_branch, commit=last_local_commit,local_idx=nil)

  if local_idx.nil?
    local_idx =  self.generate_idx
  end
  ignore_list = self.send_ignore_list()

  response = Cnvrg::API.request("users/#{self.owner}/datasets/#{self.slug}/status", 'POST', {idx: local_idx, new_branch: new_branch, current_commit: commit, ignore:ignore_list})
  CLI.is_response_success(response,false)
  return response
end

#create_volumeObject



224
225
226
227
228
# File 'lib/cnvrg/dataset.rb', line 224

def create_volume
  response = Cnvrg::API.request("users/#{self.owner}/datasets/#{self.slug}/volumes/create", 'POST')
  CLI.is_response_success(response)
  return response
end

#downlowd_updated_data(current_commit) ⇒ Object



230
231
232
233
234
235
# File 'lib/cnvrg/dataset.rb', line 230

def downlowd_updated_data(current_commit)

  response = Cnvrg::API.request("users/#{self.owner}/datasets/#{self.slug}/downlowd_updated_data", 'POST', {current_commit: current_commit})
  CLI.is_response_success(response,false)
  return response
end

#generate_idx(list_ignore = nil) ⇒ Object



183
184
185
186
187
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
214
215
216
217
218
219
220
221
222
223
# File 'lib/cnvrg/dataset.rb', line 183

def generate_idx(list_ignore=nil)
  if File.exists? "#{self.local_path}/.cnvrg/idx.yml"
    old_idx = YAML.load_file("#{self.local_path}/.cnvrg/idx.yml")
  else
    old_idx = nil
  end

  tree_idx = Hash.new(0)

  list = Dir.glob("#{self.local_path}/**/**", File::FNM_DOTMATCH).reject { |x| (x =~ /\/\.{1,2}$/) or (x =~ /^#{self.local_path}\/\.cnvrg\/*/) }
if list_ignore.nil?
  list_ignore = self.get_ignore_list()
end

  Parallel.map(list, in_threads: IDXParallelThreads) do |e|
    label = e.gsub(self.local_path + "/", "")
    if File.directory? e
      if list_ignore.include? label
        next
      end
      tree_idx[label+"/"] = nil
    else
      if list_ignore.include? label
        next
      end
      sha1 =  OpenSSL::Digest::SHA1.file(e).hexdigest
      if old_idx.nil? or old_idx.to_h["tree"].nil?
        tree_idx[label] = {sha1: sha1, commit_time: nil}
      elsif old_idx["tree"][label].nil? or old_idx["tree"][label]["sha1"] != sha1
        tree_idx[label] = {sha1: sha1, commit_time: nil}
      else
        tree_idx[label] = old_idx["tree"][label]
      end
    end
  end

  idx = {commit: old_idx.to_h[:commit], tree: tree_idx}

  File.open("#{self.local_path}/.cnvrg/idx.yml", 'w') { |f| f.write idx.to_yaml }
  return YAML.load_file("#{self.local_path}/.cnvrg/idx.yml")
end

#get_idxObject



176
177
178
# File 'lib/cnvrg/dataset.rb', line 176

def get_idx
  YAML.load_file("#{self.local_path}/.cnvrg/idx.yml")
end

#get_ignore_listObject



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/cnvrg/dataset.rb', line 72

def get_ignore_list
  ignore_list = []
  File.open(self.local_path+"/.cnvrgignore", "r").each_line do |line|
    line = line.strip
    if line.start_with? "#" or ignore_list.include? line or line.empty?
      next
    end
    if line.end_with? "/" or File.directory?(line)
      ignore_list << line
      all_sub = Dir.glob("#{line}/**/*", File::FNM_DOTMATCH).flatten

      ignore_list << all_sub.flatten
    elsif line.include? "*"
      regex_list = Dir.glob("**/*#{line}", File::FNM_DOTMATCH).flatten
      ignore_list << regex_list
    else
      ignore_list << line
    end
  end
  return ignore_list.flatten

end

#last_local_commitObject



24
25
26
27
# File 'lib/cnvrg/dataset.rb', line 24

def last_local_commit
  idx = YAML.load_file(@local_path + "/.cnvrg/idx.yml")
  return idx[:commit]
end

#list(owner) ⇒ Object



36
37
38
39
40
41
# File 'lib/cnvrg/dataset.rb', line 36

def list(owner)
  response = Cnvrg::API.request("users/#{owner}/datasets/list", 'GET')
  CLI.is_response_success(response)
  return response

end

#list_commitsObject



42
43
44
45
46
47
# File 'lib/cnvrg/dataset.rb', line 42

def list_commits
  response = Cnvrg::API.request("users/#{self.owner}/datasets/#{self.slug}/list_commits", 'GET')
  CLI.is_response_success(response)
  return response

end

#revert(working_dir) ⇒ Object



316
317
318
319
320
321
322
# File 'lib/cnvrg/dataset.rb', line 316

def revert(working_dir)
  FileUtils.rm_rf working_dir
  # response     = Cnvrg::API.request("users/#{self.owner}/projects/#{self.slug}/revert", 'GET')
  # CLI.is_response_success(response)


end

#send_ignore_listObject



247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
# File 'lib/cnvrg/dataset.rb', line 247

def send_ignore_list()
  begin
    ignore_list = []
    File.open(self.local_path+"/.cnvrgignore", "r").each_line do |line|
      line = line.strip
      if line.start_with? "#" or ignore_list.include? line or line.empty?
        next
      end
      if line.end_with? "/"
        ignore_list << line.gsub("/","")
        ignore_list << line+"."
      elsif line.include? "*"
        line = line.gsub("*",".*")
        ignore_list << line
      else
        ignore_list << line
      end
    end
    return ignore_list.flatten
  rescue
    return []
  end

end

#snapshotObject



29
30
31
32
33
34
35
# File 'lib/cnvrg/dataset.rb', line 29

def snapshot
  commit = last_local_commit
  response = Cnvrg::API.request("users/#{self.owner}/datasets/#{self.slug}/volumes/create", 'POST', {data_commit: commit})
  CLI.is_response_success(response)
  return response

end

#update_idx(idx) ⇒ Object



301
302
303
304
305
# File 'lib/cnvrg/dataset.rb', line 301

def update_idx(idx)
  File.open("#{self.local_path}/.cnvrg/idx.yml", 'w') { |f| f.write idx.to_yaml }

  return true
end

#update_idx_with_commit!(commit) ⇒ Object



308
309
310
311
312
313
314
# File 'lib/cnvrg/dataset.rb', line 308

def update_idx_with_commit!(commit)
  idx_hash = YAML.load_file("#{self.local_path}/.cnvrg/idx.yml")
  idx_hash[:commit] = commit

  File.open("#{self.local_path}/.cnvrg/idx.yml", 'w') { |f| f.write idx_hash.to_yaml }
  return true
end

#update_idx_with_files_commits!(files, commit_time) ⇒ Object



291
292
293
294
295
296
297
298
299
300
# File 'lib/cnvrg/dataset.rb', line 291

def update_idx_with_files_commits!(files, commit_time)

  idx_hash = YAML.load_file("#{self.local_path}/.cnvrg/idx.yml")
  files.each do |path|
    idx_hash[:tree].to_h[path].to_h[:commit_time] = commit_time
  end
  File.open("#{self.local_path}/.cnvrg/idx.yml", 'w') { |f| f.write idx_hash.to_yaml }

  return true
end

#update_ignore_list(new_ignore) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/cnvrg/dataset.rb', line 53

def update_ignore_list(new_ignore)

  if new_ignore.nil? or new_ignore.empty?
    return true
  end
  begin
    File.open(self.local_path+"/.cnvrgignore", "a+") do |f|
      f.puts("\n")

      new_ignore.each do |i|
        f.puts("#{i}\n")
      end
    end
    return true
  rescue
    return false
  end
end

#urlObject



48
49
50
51
# File 'lib/cnvrg/dataset.rb', line 48

def url
  url = Cnvrg::Helpers.remote_url
  "#{url}/#{self.owner}/projects/#{self.slug}"
end