Class: Cnvrg::Project

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(project_home) ⇒ Project

Returns a new instance of Project.



8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/cnvrg/project.rb', line 8

def initialize(project_home)
  begin
    config = YAML.load_file(project_home+"/.cnvrg/config.yml")
    @local_path = project_home
    @title = config[:project_name]
    @slug = config[:project_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/project.rb', line 4

def local_path
  @local_path
end

#ownerObject (readonly)

Returns the value of attribute owner.



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

def owner
  @owner
end

#slugObject (readonly)

Returns the value of attribute slug.



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

def slug
  @slug
end

#titleObject (readonly)

Returns the value of attribute title.



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

def title
  @title
end

#working_dirObject (readonly)

Returns the value of attribute working_dir.



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

def working_dir
  @working_dir
end

Class Method Details

.clone_dir(project_slug, project_owner, project_name) ⇒ Object



210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
# File 'lib/cnvrg/project.rb', line 210

def self.clone_dir(project_slug, project_owner, project_name)
  list_dirs = [project_name,
               project_name + "/.cnvrg"
  ]


  list_files = [
      project_name + "/.cnvrg/config.yml",
      # project_name+"/.cnvrgignore",
  ]
  begin
    config = {project_name: project_name,
              project_slug: project_slug,
              owner: project_owner}
    FileUtils.mkdir_p list_dirs
    FileUtils.touch list_files
    # cnvrgignore = Helpers.cnvrgignore_content


    File.open(project_name + "/.cnvrg/config.yml", "w+") { |f| f.write config.to_yaml }
    # File.open(project_name+"/.cnvrgignore", "w+") { |f| f.write cnvrgignore }

  rescue
    return false
  end
  return true
end

.clone_dir_remote(project_slug, project_owner, project_name) ⇒ Object



265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
# File 'lib/cnvrg/project.rb', line 265

def self.clone_dir_remote(project_slug, project_owner, project_name)
  list_dirs = [
      ".cnvrg"
  ]


  list_files = [
      ".cnvrg/config.yml",
      # ".cnvrgignore",

  ]
  begin
    config = {project_name: project_name,
              project_slug: project_slug,
              owner: project_owner}
    FileUtils.mkdir_p list_dirs
    FileUtils.touch list_files
    # cnvrgignore = Helpers.cnvrgignore_content


    File.open(".cnvrg/config.yml", "w+") { |f| f.write config.to_yaml }
    # File.open(".cnvrgignore", "w+") { |f| f.write cnvrgignore }

  rescue
    return false
  end
  return true
end

.create(project_name, clean, with_docker = false) ⇒ Object

Create project



120
121
122
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/cnvrg/project.rb', line 120

def self.create(project_name, clean, with_docker=false)
  if clean
    list_dirs = [project_name, project_name + "/.cnvrg"]
  else

    list_dirs = [project_name,
                 project_name + "/models",
                 project_name + "/notebooks",
                 project_name + "/src",
                 project_name + "/src/visualizations",
                 project_name + "/src/features",
                 project_name + "/.cnvrg"
    ]
  end


  list_files = [
      project_name + "/README.md",
      project_name + "/.cnvrgignore",
      project_name + "/.cnvrg/config.yml"
  ]
  cnvrgreadme = Helpers.readme_content
  cnvrgignore = Helpers.cnvrgignore_content
  cnvrghyper  = Helpers.hyper_content

  begin

    owner = Cnvrg::CLI.get_owner()
    response = Cnvrg::API.request("cli/create_project", 'POST', {title: project_name, owner: owner, is_docker: with_docker})
    Cnvrg::CLI.is_response_success(response)
    response = JSON.parse response["result"]
    project_slug = response["slug"]

    config = {project_name: project_name,
              project_slug: project_slug,
              owner: owner,
              docker: with_docker}
    FileUtils.mkdir_p list_dirs
    FileUtils.touch list_files

    File.open(project_name + "/.cnvrg/config.yml", "w+") { |f| f.write config.to_yaml }
    File.open(project_name + "/.cnvrgignore", "w+") { |f| f.write cnvrgignore }
    File.open(project_name + "/README.md", "w+") { |f| f.write cnvrgreadme }
    File.open(project_name + "/src/hyper.yaml", "w+") { |f| f.write cnvrghyper }
    
  rescue
    return false
  end
  return true
end


171
172
173
174
175
176
177
178
179
180
181
182
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
# File 'lib/cnvrg/project.rb', line 171

def self.link(owner, project_name, path=nil, docker=false)
  ignore_exits = File.exist? ".cnvrgignore"
  list_dirs = [".cnvrg"
  ]
  list_files = [
      ".cnvrg/config.yml"
  ]
  if !ignore_exits
    list_files <<
        ".cnvrgignore"
  end

  cnvrgreadme = Helpers.readme_content
  cnvrgignore = Helpers.cnvrgignore_content
  begin
    response = Cnvrg::API.request("cli/create_project", 'POST', {title: project_name, owner: owner, is_docker: docker})
    Cnvrg::CLI.is_response_success(response)
    response = JSON.parse response["result"]
    project_slug = response["slug"]

    config = {project_name: project_name,
              project_slug: project_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 ignore_exits
    if !File.exist? "README" and !File.exist? "README.md"
      FileUtils.touch [ "README.md" ]
      File.open("README.md", "w+") { |f| f.write cnvrgreadme }
    end

  rescue => e
    puts e
    return false
  end
  return true
end

.verify_cnvrgignore_exist(project_name, remote) ⇒ Object



237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
# File 'lib/cnvrg/project.rb', line 237

def self.verify_cnvrgignore_exist(project_name,remote)
  if remote
    path = ".cnvrgignore"
  else
    path = "#{project_name}/.cnvrgignore"
  end
  ignore_exits = File.exist? path
  if !ignore_exits
    begin
      list_files = [
          path
    ]
    FileUtils.touch list_files
    cnvrgignore = Helpers.cnvrgignore_content
    File.open(path, "w+") { |f| f.write cnvrgignore }
    rescue => e
      return false
    end





  end


end

Instance Method Details

#clone(remote = 0, commit) ⇒ Object



354
355
356
357
# File 'lib/cnvrg/project.rb', line 354

def clone(remote=0, commit)
  response = Cnvrg::API.request("users/#{self.owner}/projects/#{self.slug}/clone", 'POST', {project_slug: self.slug, remote: remote, commit: commit})
  return response
end

#compare_commit(commit) ⇒ Object



392
393
394
395
396
397
398
399
400
# File 'lib/cnvrg/project.rb', line 392

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_idx(new_branch, commit: last_local_commit, force: false) ⇒ Object



359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
# File 'lib/cnvrg/project.rb', line 359

def compare_idx(new_branch, commit:last_local_commit,force:false)
  local_idx = self.generate_idx
  ignore_list = self.send_ignore_list()
  if force
    added = []
    if local_idx[:tree]
      added << local_idx[:tree].keys
      added.flatten!
    end

    response ={"result"=> {"commit"=>nil,"tree"=> {"added"=> added,
               "updated_on_server"=> [],
               "updated_on_local"=> [],
               "deleted"=> [],
               "conflicts"=> []} } }
    return response

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

#deploy(file_to_run, function, input_params, commit_to_run, instance_type, image_slug, scheduling_query, local_timestamp, workers, file_input) ⇒ Object



413
414
415
416
417
418
419
420
421
422
# File 'lib/cnvrg/project.rb', line 413

def deploy(file_to_run, function, input_params, commit_to_run, instance_type, image_slug, scheduling_query, local_timestamp,workers, file_input)
  response = Cnvrg::API.request("users/#{@owner}/projects/#{@slug}/deploy", 'POST', {file_to_run: file_to_run, function: function,
                                                                                     image_slug: image_slug, input_params: input_params,
                                                                                     commit_sha1: commit_to_run,
                                                                                     instance_type: instance_type,
                                                                                     scheduling_query: scheduling_query,
                                                                                     local_timestamp: local_timestamp,
                                                                                      workers:workers,file_input:file_input})
  return response
end

#generate_idxObject



314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
# File 'lib/cnvrg/project.rb', line 314

def generate_idx
  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\/*/) or (x =~/^#{self.local_path}\/\.cnvrgignore.conflict*/) and not (x =~/^#{self.local_path}\/\.cnvrgignore/) }
  list_ignore = self.get_ignore_list()
  list.each do |e|
    label = e.gsub(self.local_path + "/", "")
    ignore_label = label.gsub("/","//")
    if list_ignore.include? ignore_label
      next
    end
    if File.directory? e

      tree_idx[label+"/"] = nil
    else
      sha1 = 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_experiment(slug) ⇒ Object



437
438
439
440
441
# File 'lib/cnvrg/project.rb', line 437

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

#get_experimentsObject



430
431
432
433
434
# File 'lib/cnvrg/project.rb', line 430

def get_experiments
  response = Cnvrg::API.request("users/#{self.owner}/projects/#{self.slug}/experiments/list", 'GET')
  CLI.is_response_success(response)
  return response
end

#get_idxObject



350
351
352
# File 'lib/cnvrg/project.rb', line 350

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

#get_ignore_listObject



51
52
53
54
55
56
57
58
59
60
61
62
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
# File 'lib/cnvrg/project.rb', line 51

def get_ignore_list
  ignore_list = []
  if !File.exist? self.local_path+"/.cnvrgignore"
    return ignore_list
  end
  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.ends_with? "*"
      list_regex = Dir.glob("**/#{line}",File::FNM_DOTMATCH).flatten
      list_regex.each do |l|
        ignore_list << l
        if File.directory?(l)
          all_sub = Dir.glob("#{line}/**/*", File::FNM_DOTMATCH).flatten

          ignore_list << all_sub.flatten
        end

      end
    elsif line.ends_with? "/*"
      line = line.gsub("/*","")
      regex_list = Dir.glob("**/#{line}/**/*", File::FNM_DOTMATCH).flatten
      ignore_list << regex_list
    elsif line.include? "*"
         regex_list = Dir.glob("**/#{line}").flatten
         ignore_list << regex_list
    elsif line.end_with? "/" or File.directory?(line)
        ignore_list << line
        all_sub = Dir.glob("#{line}/**/*", File::FNM_DOTMATCH).flatten

      ignore_list << all_sub.flatten

    else
      ignore_list << line
    end
  end
  return ignore_list.flatten

end

#get_new_branchObject



298
299
300
301
302
303
304
305
306
# File 'lib/cnvrg/project.rb', line 298

def get_new_branch
  begin
  config = YAML.load_file(@working_dir+"/.cnvrg/config.yml")
  return config[:new_branch]
  rescue =>e
    return false

  end
end

#jump_idx(new_branch, commit = last_local_commit) ⇒ Object



382
383
384
385
386
387
388
389
390
# File 'lib/cnvrg/project.rb', line 382

def jump_idx(new_branch, commit=last_local_commit)

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

#last_local_commitObject



22
23
24
25
# File 'lib/cnvrg/project.rb', line 22

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

#list_commitsObject



423
424
425
426
427
# File 'lib/cnvrg/project.rb', line 423

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

#remove_new_branchObject



308
309
310
311
312
# File 'lib/cnvrg/project.rb', line 308

def remove_new_branch
  config = YAML.load_file(@working_dir+"/.cnvrg/config.yml")
  new_config = config.except(:new_branch)
  File.open(@working_dir+"/.cnvrg/config.yml", "w+") { |f| f.write new_config.to_yaml }
end

#revert(working_dir) ⇒ Object



450
451
452
453
454
455
456
# File 'lib/cnvrg/project.rb', line 450

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



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/cnvrg/project.rb', line 93

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

#update_idx_with_commit!(commit) ⇒ Object



442
443
444
445
446
447
448
# File 'lib/cnvrg/project.rb', line 442

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



402
403
404
405
406
407
408
409
410
411
# File 'lib/cnvrg/project.rb', line 402

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



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

def update_ignore_list(new_ignore)
  if new_ignore.nil? or new_ignore.empty?
    return true
  end
  list = new_ignore.split(",")
  begin
    File.open(self.local_path+"/.cnvrgignore", "a+") do |f|
      f.puts("\n")
      list.each do |i|
        f.puts("#{i}\n")
      end
    end
    return true
  rescue
    return false
  end
end

#update_is_new_branch(new_branch) ⇒ Object



293
294
295
296
297
# File 'lib/cnvrg/project.rb', line 293

def update_is_new_branch(new_branch)
  config = YAML.load_file(@working_dir+"/.cnvrg/config.yml")
  config[:new_branch] = new_branch
  File.open(@working_dir+"/.cnvrg/config.yml", "w+") { |f| f.write config.to_yaml }
end

#urlObject



28
29
30
31
# File 'lib/cnvrg/project.rb', line 28

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