cnvrg 1.6.38 → 1.9.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,31 @@
1
+ module Cnvrg
2
+ class ConnectJobSsh
3
+ def initialize(job_id)
4
+ home_dir = File.expand_path('~')
5
+ config = YAML.load_file(home_dir+"/.cnvrg/config.yml")
6
+ @owner = config.to_h[:owner]
7
+ @job_id = job_id
8
+ rescue => e
9
+ @owner = ""
10
+ Cnvrg::Logger.log_info("cnvrg is not configured")
11
+ end
12
+
13
+ def start(username, password)
14
+ Cnvrg::API_V2.request("#{@owner}/job_ssh/#{@job_id}/start" , 'POST', {username: username, password: password})
15
+ end
16
+
17
+ def status()
18
+ Cnvrg::API_V2.request("#{@owner}/job_ssh/#{@job_id}/status" , 'GET', nil)
19
+ end
20
+
21
+ def run_portforward_command(pod_name, port, kubeconfig, namespace)
22
+ command = "kubectl"
23
+ if kubeconfig.present?
24
+ command = "kubectl --kubeconfig=#{kubeconfig}"
25
+ end
26
+ bashCommand = "#{command} -n #{namespace} port-forward #{pod_name} #{port}:22"
27
+ puts("\nrunning command #{bashCommand}")
28
+ `#{bashCommand}`
29
+ end
30
+ end
31
+ end
@@ -128,6 +128,7 @@ module Cnvrg
128
128
  message = options["message"]
129
129
  cli.sync_data_new(new_branch, force, verbose, commit, all_files, tags, parallel, chunk_size, init, message)
130
130
  end
131
+
131
132
  desc 'data download', 'Download files from remote server'
132
133
  method_option :new_branch, :type => :boolean, :aliases => ["-nb"], :desc => "create new branch of commits", :default => false
133
134
  method_option :verbose, :type => :boolean, :aliases => ["-v"], :default => false
@@ -151,27 +152,52 @@ module Cnvrg
151
152
  method_option :read, :type => :boolean, :aliases => ["-r", "--read"], :default => false
152
153
  method_option :remote, :type => :boolean, :aliases => ["-h", "--remote"], :default => false
153
154
  method_option :relative, :type => :boolean, :aliases => ["-rel", "--relative"], :default => false
154
-
155
+ method_option :flatten, :type => :boolean, :aliases => ["-f", "--flatten"], :default => false
156
+ method_option :soft, :type => :boolean, :aliases => ["-s", "--soft"], :default => false, :hide => true
155
157
  def clone(dataset_url)
156
- #test
157
158
  cli = Cnvrg::CLI.new()
158
159
  only_tree =options[:only_tree]
159
160
  commit =options[:commit]
160
161
  query =options[:query]
161
162
  read = options[:read]
162
163
  remote = options[:remote]
163
- cli.clone_data(dataset_url, only_tree=only_tree,commit=commit, query=query, read=read, remote=remote, relative: options[:relative])
164
+ soft = options[:soft]
165
+ flatten = options[:flatten]
166
+ cli.clone_data(
167
+ dataset_url,
168
+ only_tree=only_tree,
169
+ commit=commit,
170
+ query=query,
171
+ read=read,
172
+ remote=remote,
173
+ flatten: flatten,
174
+ relative: options[:relative],
175
+ soft: soft
176
+ )
164
177
  end
165
178
 
166
179
  desc 'data verify DATASETS_TITLES', 'verify datasets', :hide => true
167
- method_option :timeout, :type => :numeric, :aliases => ["-t", "--timeout"], :desc => "Time to wait before returning final answer", :default => 15
168
-
180
+ method_option :timeout, :type => :numeric, :aliases => ["-t", "--timeout"], :desc => "Time to wait before returning final answer", :default => nil
169
181
  def verify(*dataset_titles)
170
182
  cli = Cnvrg::CLI.new()
171
183
  timeout =options[:timeout]
172
184
  cli.verify_datasets(dataset_titles, timeout)
173
185
  end
174
186
 
187
+ desc 'data scan', 'lookup datasets', :hide => true
188
+ def scan()
189
+ cli = Cnvrg::CLI.new()
190
+ cli.scan_datasets()
191
+ end
192
+
193
+ desc "data block DATASET_TITLES", 'verifying that datasets exists', hide: true
194
+ def block(*dataset_slugs)
195
+ not_verified = true
196
+ while not_verified
197
+ not_verified = dataset_slugs.select{|slug| not Dataset.verify_dataset(slug)}.present?
198
+ end
199
+ end
200
+
175
201
  desc 'data set --url=DATASET_URL', 'Set dataset url to other url'
176
202
  method_option :url, :type => :string, :aliases => ["--url"], :default => ''
177
203
  def set
@@ -186,22 +212,48 @@ module Cnvrg
186
212
  desc 'data put DATASET_URL FILES_PREFIX', 'Upload selected files from local dataset directory to remote server'
187
213
  method_option :dir, :type => :string, :aliases => ["-d", "--dir"], :default => ''
188
214
  method_option :commit, :type => :string, :aliases => ["-c", "--commit"], :default => ''
215
+ method_option :force, :type => :boolean, :aliases => ["-f","--force"], :default => false
216
+ method_option :threads, :type => :numeric, :aliases => ["-t","--threads"], :default => 15
217
+ method_option :chunk_size, :type => :numeric, :aliases => ["-cs","--chunk"], :default => 1000
189
218
  method_option :message, :type => :string, :aliases => ["--message"], :desc => "create commit with message", :default => nil
190
219
  def put(dataset_url, *files)
191
220
  cli = Cnvrg::CLI.new()
192
221
  dir = options[:dir]
222
+ force = options["force"]
193
223
  commit = options[:commit]
194
224
  message = options[:message]
225
+ threads = options[:threads]
226
+ chunk_size = options[:chunk_size]
227
+ cli.data_put(
228
+ dataset_url,
229
+ files: files,
230
+ dir: dir,
231
+ commit: commit,
232
+ force: force,
233
+ threads: threads,
234
+ chunk_size: chunk_size,
235
+ message: message
236
+ )
237
+ end
195
238
 
196
- cli.data_put(dataset_url, files: files, dir: dir, commit: commit, message: message)
239
+ desc 'data rm DATASET_URL FILES_PREFIX', 'Delete selected files from remote server'
240
+ method_option :message, :type => :string, :aliases => ["--message"], :desc => "create commit with message", :default => nil
241
+ def rm(dataset_url, *regex_list)
242
+ cli = Cnvrg::CLI.new()
243
+ message = options[:message]
244
+ cli.data_rm(dataset_url, regex_list: regex_list, message: message)
197
245
  end
198
246
 
199
247
  desc 'data clone_query --query=QUERY_SLUG DATASET_URL', 'Clone dataset with specific query'
200
248
  method_option :query, :type => :string, :aliases => ["-q", "--query"], :default => nil
249
+ method_option :soft, :type => :boolean, :aliases => ["-s", "--soft"], :default => false, :hide => true
250
+ method_option :flatten, :type => :boolean, :aliases => ["-f", "--flatten"], :default => false
201
251
  def clone_query(dataset_url)
202
252
  cli = Cnvrg::CLI.new()
203
- query =options[:query]
204
- cli.clone_data_query(dataset_url,query=query)
253
+ query = options[:query]
254
+ flatten = options[:flatten]
255
+ soft =options[:soft]
256
+ cli.clone_data_query(dataset_url,query=query, flatten, soft: soft)
205
257
  end
206
258
 
207
259
  desc 'data delete DATASET_SLUG', 'Delete dataset'
@@ -217,12 +269,13 @@ module Cnvrg
217
269
  cli.list_dataset()
218
270
 
219
271
  end
220
- desc 'data commits', 'List all commits for a current dataset'
221
272
 
222
- def commits()
273
+ desc 'data commits URL/SLUG', 'List all commits for a given dataset'
274
+ method_option :commit_sha1, :type => :string, :aliases => ["-c", "--commit"], :default => nil
275
+ def commits(dataset_url)
223
276
  cli = Cnvrg::CLI.new()
224
- cli.list_dataset_commits()
225
-
277
+ commit_sha1 = options[:commit_sha1]
278
+ cli.list_dataset_commits(dataset_url, commit_sha1:commit_sha1)
226
279
  end
227
280
 
228
281
  desc 'data files DATASET_URL', 'Show list of dataset files'
@@ -9,8 +9,8 @@ module Cnvrg
9
9
  class Datafiles
10
10
  ParallelThreads ||= Cnvrg::Helpers.parallel_threads
11
11
 
12
- LARGE_FILE = 1024 * 1024 * 5
13
- MULTIPART_SPLIT = 10000000
12
+ LARGE_FILE=1024*1024*5
13
+ MULTIPART_SPLIT=10000000
14
14
 
15
15
  attr_reader :base_resource
16
16
 
@@ -25,7 +25,7 @@ module Cnvrg
25
25
 
26
26
  def refresh_storage_token
27
27
  current_time = Time.current
28
- if current_time - @token_issue_time > 3.hours
28
+ if current_time - @token_issue_time > 1.hours
29
29
  @downloader = @dataset.get_storage_client
30
30
  @token_issue_time = Time.current
31
31
  end
@@ -33,7 +33,7 @@ module Cnvrg
33
33
 
34
34
  def check_file_sha1(filename, org_sha1, tag: 'conflict')
35
35
  file_loc = "#{Dir.pwd}/#{filename}"
36
- sha1 = OpenSSL::Digest::SHA1.file(file_loc).hexdigest
36
+ sha1 = OpenSSL::Digest::SHA1.file(file_loc).hexdigest
37
37
  return 0 if sha1 == org_sha1
38
38
  FileUtils.cp(file_loc, "#{file_loc}.#{tag}")
39
39
  return 1
@@ -52,7 +52,27 @@ module Cnvrg
52
52
  end
53
53
  raise SignalException.new(1, "Cant find file #{file}") unless File.exists? "#{Dir.pwd}/#{file}"
54
54
  end
55
- paths.map { |p| p.gsub(/^\.\//, '') }
55
+ paths.map{|p| p.gsub(/^\.\//, '')}
56
+ end
57
+
58
+ def get_files_and_folders(paths)
59
+ files_and_folders = {}
60
+ paths.each do |file|
61
+ if File.exists? file
62
+ if File.directory? file
63
+ Dir.glob("#{file}/**/*").select do |f|
64
+ files_and_folders["#{f}/"] = "folder" if File.directory? f
65
+ files_and_folders[f] = "file" if File.file? f
66
+ end
67
+ files_and_folders["#{file}/"] = "folder"
68
+ else
69
+ files_and_folders[file] = "file"
70
+ end
71
+ next
72
+ end
73
+ raise SignalException.new(1, "Cant find file #{file}") unless File.exists? "#{Dir.pwd}/#{file}"
74
+ end
75
+ return files_and_folders
56
76
  end
57
77
 
58
78
  def check_files_sha1(files, resolver, tag)
@@ -83,11 +103,11 @@ module Cnvrg
83
103
  prefix = @dataset.get_dataset["bucket_prefix"]
84
104
  batch_size = 10000
85
105
  pbar = ProgressBar.create(:title => "Download Progress",
86
- :progress_mark => '=',
87
- :format => "%b%i| %c Files downloaded",
88
- :starting_at => 0,
89
- :total => nil,
90
- :autofinish => true)
106
+ :progress_mark => '=',
107
+ :format => "%b%i| %c Files downloaded",
108
+ :starting_at => 0,
109
+ :total => nil,
110
+ :autofinish => true)
91
111
  parallel_options = {
92
112
  in_threads: ParallelThreads,
93
113
  in_processes: Cnvrg::CLI::ParallelProcesses,
@@ -110,18 +130,36 @@ module Cnvrg
110
130
  end
111
131
  end
112
132
 
113
-
133
+ # This is for backwards compatibility only and should be removed in future versions:
114
134
  def put_commit(commit_sha1)
115
- response = Cnvrg::API.request("#{@base_resource}/commit/latest", 'PUT', {commit_sha1: commit_sha1})
135
+ response = Cnvrg::API.request(
136
+ "#{@base_resource}/commit/latest",
137
+ 'PUT',
138
+ {
139
+ commit_sha1: commit_sha1,
140
+ ignore: true # tells the new server to ignore this api call since its coming from the new CLI
141
+ }
142
+ )
116
143
  if response.present?
117
144
  msg = response['result']
118
145
  else
119
- msg = "Cant save changes in the dataset"
146
+ msg = "Can't save changes in the dataset"
120
147
  end
121
148
 
122
149
  Cnvrg::Result.new(Cnvrg::CLI.is_response_success(response, false), msg)
123
150
  end
124
151
 
152
+ def create_progressbar(title, total)
153
+ return ProgressBar.create(
154
+ :title => title,
155
+ :progress_mark => '=',
156
+ :format => "%b>>%i| %p%% %t",
157
+ :starting_at => 0,
158
+ :total => total,
159
+ :autofinish => true
160
+ )
161
+ end
162
+
125
163
 
126
164
  def upload_multiple_files(commit_sha1, tree, threads: ParallelThreads, force: false, new_branch: false, prefix: '', partial_commit: nil, total: nil)
127
165
  begin
@@ -158,6 +196,9 @@ module Cnvrg
158
196
  end
159
197
 
160
198
  files = results['files']
199
+
200
+ progressbar.progress += tree.keys.length - files.length if progressbar.present?
201
+ progress_semaphore = Mutex.new
161
202
  upload_error_files = []
162
203
  @temp_upload_progressbar.progress += tree.keys.length - files.length if @temp_upload_progressbar.present?
163
204
  Parallel.map((files.keys), in_threads: threads) do |k|
@@ -169,11 +210,12 @@ module Cnvrg
169
210
  tree.except!(k)
170
211
  Cnvrg::Logger.log_error_message("Error while upload single file #{o["path"]}")
171
212
  end
172
- @temp_upload_progressbar.progress += 1 if @temp_upload_progressbar.present?
213
+ progress_semaphore.synchronize { progressbar.progress += 1 if progressbar.present? }
173
214
  end
174
- blob_ids = files.values.map { |f| f['bv_id'] }
215
+
216
+ blob_ids = files.values.map {|f| f['bv_id']}
175
217
  if blob_ids.present?
176
- dirs = tree.keys.select { |k| tree[k].nil? } || []
218
+ dirs = tree.keys.select {|k| tree[k].nil?} || []
177
219
  Cnvrg::Logger.info("Sending Upload files save")
178
220
  upload_resp = Cnvrg::API.request(@base_resource + "upload_files_save", "POST", {commit: commit_sha1, blob_ids: blob_ids, dirs: dirs})
179
221
  unless Cnvrg::CLI.is_response_success(upload_resp, false)
@@ -190,17 +232,231 @@ module Cnvrg
190
232
  end
191
233
  end
192
234
 
193
- def upload_single_file(file)
235
+ def delete_multiple_files(commit_sha1, regex_list)
236
+ begin
237
+ Cnvrg::Logger.log_info("Sending Delete Files request")
238
+ resp = Cnvrg::API.request(
239
+ @base_resource + "delete_files",
240
+ 'POST_JSON',
241
+ {
242
+ commit_sha1: commit_sha1,
243
+ regex_list: regex_list,
244
+ }
245
+ )
246
+ unless Cnvrg::CLI.is_response_success(resp, false)
247
+ Cnvrg::Logger.log_method(bind: binding)
248
+ raise Exception.new("Got an error message from server, #{resp.try(:fetch, "message")}")
249
+ end
250
+ Cnvrg::Logger.log_info("Delete Files request Successful")
251
+ return resp["files"], resp["folders"], resp["job_id"]
252
+ rescue => e
253
+ Cnvrg::Logger.log_method(bind: binding)
254
+ Cnvrg::Logger.log_error(e)
255
+ raise e
256
+ end
257
+ end
258
+
259
+ def delete_file_chunk(commit_sha1, regex_list, chunk_size, offset)
260
+ begin
261
+ resp = Cnvrg::API.request(
262
+ @base_resource + "delete_files_by_chunk",
263
+ 'POST_JSON',
264
+ {
265
+ commit_sha1: commit_sha1,
266
+ regex_list: regex_list,
267
+ chunk_size: chunk_size,
268
+ offset: offset
269
+ }
270
+ )
271
+ unless Cnvrg::CLI.is_response_success(resp, false)
272
+ Cnvrg::Logger.log_method(bind: binding)
273
+ raise Exception.new("Got an error message from server, #{resp.try(:fetch, "message")}")
274
+ end
275
+ return resp["total_changes"]
276
+ rescue => e
277
+ Cnvrg::Logger.log_method(bind: binding)
278
+ Cnvrg::Logger.log_error(e)
279
+ raise e
280
+ end
281
+ end
282
+
283
+ def get_delete_progress(commit_sha1, job_id)
284
+ begin
285
+ resp = Cnvrg::API.request(
286
+ @base_resource + "get_delete_progress",
287
+ 'POST_JSON',
288
+ {
289
+ commit_sha1: commit_sha1,
290
+ job_id: job_id
291
+ }
292
+ )
293
+ unless Cnvrg::CLI.is_response_success(resp, false)
294
+ Cnvrg::Logger.log_method(bind: binding)
295
+ raise Exception.new("Got an error message from server, #{resp.try(:fetch, "message")}")
296
+ end
297
+ return resp["total_deleted"]
298
+ rescue => e
299
+ Cnvrg::Logger.log_method(bind: binding)
300
+ Cnvrg::Logger.log_error(e)
301
+ raise e
302
+ end
303
+ end
304
+
305
+ def request_upload_files(commit_sha1, tree, force, new_branch, partial_commit)
306
+ retry_count = 0
307
+ loop do
308
+ upload_resp = Cnvrg::API.request(@base_resource + "upload_files", 'POST_JSON', {
309
+ commit_sha1: commit_sha1,
310
+ tree: tree,
311
+ force: force,
312
+ is_branch: new_branch,
313
+ partial_commit: partial_commit
314
+ })
315
+ if not (Cnvrg::CLI.is_response_success(upload_resp, false))
316
+ #Cnvrg::Logger.log_method(bind: binding)
317
+ retry_count += 1
318
+
319
+ puts "Failed request upload files: #{Time.current}"
320
+ puts upload_resp
321
+
322
+ if retry_count > 5
323
+ raise Exception.new("Got an error message from server, #{upload_resp.try(:fetch, "message")}")
324
+ end
325
+ sleep 5
326
+ next
327
+ end
328
+ return upload_resp['result'].with_indifferent_access
329
+ end
330
+
331
+ end
332
+
333
+ def upload_multiple_files_optimized(files, commit_sha1, threads: ParallelThreads, chunk_size: 1000, force: false, new_branch: false, prefix: '', partial_commit: nil)
334
+ cli = CLI.new
335
+ cli.log_message("Using #{threads} threads with chunk size of #{chunk_size}.", Thor::Shell::Color::GREEN)
336
+
337
+ progressbar = create_progressbar("Upload Progress", files.size)
338
+ cli = CLI.new
339
+
340
+ # Vars to handle the parallelism
341
+ progress_mutex = Mutex.new
342
+ file_queue = Queue.new
343
+ progress_queue = Queue.new
344
+ worker_threads = []
345
+
346
+ # Vars to keep track of uploaded files and directories
347
+ dirs = []
348
+ uploaded_files = []
349
+
350
+ begin
351
+ # Init working threads that handle the upload of the files:
352
+ threads.times do |i|
353
+ worker_threads[i] = Thread.new do
354
+ # wait for file_queue.close to break the loop
355
+ while file = file_queue.deq
356
+ success = upload_single_file(cli, file)
357
+ file[:success] = success
358
+ if not success
359
+ cli.log_message("Error while uploading file: #{file[:absolute_path]}", Thor::Shell::Color::RED)
360
+ Cnvrg::Logger.log_error_message("Error while upload single file #{file["path"]}")
361
+ end
362
+ progress_queue << file
363
+ end
364
+ end
365
+ end
366
+
367
+ # init the thread that handles the file upload progress and saving them in the server
368
+ progress_thread = Thread.new do
369
+ loop do
370
+ file = progress_queue.deq(non_block: true) rescue nil # to prevent deadlocks
371
+ unless file.nil?
372
+ progress_mutex.synchronize {
373
+ progressbar.progress += 1
374
+ uploaded_files.append(file) if file[:success]
375
+ }
376
+
377
+ if uploaded_files.size == chunk_size or progressbar.finished?
378
+ refresh_storage_token
379
+ # puts "progress: #{progress_queue.length}"
380
+ # puts "files: #{file_queue.length}"
381
+ Cnvrg::Logger.info("Sending Upload files save")
382
+ blob_ids = uploaded_files.map {|f| f['bv_id']}
383
+ upload_resp = Cnvrg::API.request(@base_resource + "upload_files_save", "POST", {commit: commit_sha1, blob_ids: blob_ids, dirs: dirs})
384
+ unless Cnvrg::CLI.is_response_success(upload_resp, false)
385
+ Cnvrg::Logger.log_method(bind: binding)
386
+ raise Exception.new("Got an error message from server, #{upload_resp.try(:fetch, "message")}")
387
+ end
388
+ # cli.log_message("Saved file chunk to server", Thor::Shell::Color::GREEN)
389
+ uploaded_files = []
390
+ dirs = []
391
+ end
392
+ else
393
+ sleep(0.1)
394
+ end
395
+
396
+ if progressbar.finished?
397
+ # puts "finished"
398
+ file_queue.close()
399
+ progress_queue.close()
400
+ Thread.exit
401
+ end
402
+ end
403
+ end
404
+
405
+ file_chunks = files.each_slice(chunk_size).to_a
406
+ # Fetch the required files from the server:
407
+ Parallel.map((file_chunks), in_threads: 10) do |files_chunk|
408
+
409
+ tree = @dataset.generate_chunked_idx(files_chunk, prefix: prefix)
410
+ results = request_upload_files(commit_sha1, tree, force, new_branch, partial_commit)
411
+
412
+ # puts "Got #{results['files'].size} files to upload from #{files_chunk.size} files"
413
+
414
+ if results['files'].blank?
415
+ progress_mutex.synchronize { progressbar.progress += tree.keys.length }
416
+ next
417
+ end
418
+
419
+ # Handle directories:
420
+ new_dirs = tree.keys.select {|k| tree[k].nil?}
421
+ dirs += new_dirs
422
+
423
+ files_to_upload = results['files']
424
+ progress_mutex.synchronize {
425
+ progressbar.progress += tree.keys.length - files_to_upload.length
426
+ }
427
+
428
+ files_to_upload.keys.each do |key|
429
+ while file_queue.size > 5000
430
+ sleep(0.1)
431
+ end
432
+ file_queue.push tree[key].merge(files_to_upload[key])
433
+ end
434
+ end
435
+
436
+ progress_thread.join()
437
+ worker_threads.each(&:join)
438
+
439
+ rescue => e
440
+ puts e
441
+ Cnvrg::Logger.log_method(bind: binding)
442
+ Cnvrg::Logger.log_error(e)
443
+ raise e
444
+ end
445
+ end
446
+
447
+ def upload_single_file(cli, file)
448
+ success = false
194
449
  begin
195
450
  file = file.as_json
196
451
  Cnvrg::Logger.log_info("Uploading #{file["absolute_path"]}")
197
452
  @downloader.safe_upload(file["path"], file["absolute_path"])
453
+ success = true
198
454
  Cnvrg::Logger.log_info("#{file["absolute_path"]} uploaded.")
199
455
  rescue => e
200
456
  Cnvrg::Logger.log_error_message("Error while upload single file #{file["path"]}")
201
457
  Cnvrg::Logger.log_error(e)
202
- return false
203
458
  end
459
+ success
204
460
  end
205
461
 
206
462
  def upload_file(absolute_path, relative_path, commit_sha1)
@@ -208,12 +464,12 @@ module Cnvrg
208
464
  file_size = File.size(absolute_path).to_f
209
465
  mime_type = MimeMagic.by_path(absolute_path)
210
466
  content_type = !(mime_type.nil? or mime_type.text?) ? mime_type.type : "text/plain"
211
- sha1 = OpenSSL::Digest::SHA1.file(absolute_path).hexdigest
467
+ sha1 = OpenSSL::Digest::SHA1.file(absolute_path).hexdigest
212
468
  if (absolute_path.include? "_tags.yml" or absolute_path.include? "_tags.yaml")
213
469
  is_valid = false
214
470
  begin
215
- content = open(absolute_path).read()
216
- hash = YAML.load(open(absolute_path).read())
471
+ content = open(absolute_path).read()
472
+ hash = YAML.load(open(absolute_path).read())
217
473
  # if level 1 keys count is 1
218
474
  if hash.keys.count == 1
219
475
  if hash["tags"].present?
@@ -221,7 +477,7 @@ module Cnvrg
221
477
  elsif hash[hash.keys.first].class != Hash
222
478
  is_valid = true
223
479
  end
224
- # if level 1 keys count is greater than 1
480
+ # if level 1 keys count is greater than 1
225
481
  elsif hash.keys.count > 1
226
482
  if hash["tags"].present? and hash["tags"].class == Hash
227
483
  is_valid = false
@@ -255,7 +511,6 @@ module Cnvrg
255
511
  return s3_res
256
512
  end
257
513
  end
258
-
259
514
  def upload_tar_file(absolute_path, relative_path, commit_sha1)
260
515
  begin
261
516
  file_name = File.basename relative_path
@@ -263,24 +518,24 @@ module Cnvrg
263
518
  mime_type = MimeMagic.by_path(absolute_path)
264
519
  content_type = !(mime_type.nil? or mime_type.text?) ? mime_type.type : "text/plain"
265
520
  begin
266
- chunked_bytes = [100, (file_size * 0.01)].min
521
+ chunked_bytes = [100, (file_size*0.01)].min
267
522
  total_yanked = ""
268
523
  open(absolute_path, "rb") do |f|
269
524
  total_yanked = f.read(chunked_bytes)
270
525
  end
271
526
  if !total_yanked.empty?
272
- sha1 = OpenSSL::Digest::SHA1.hexdigest(total_yanked)
527
+ sha1 = OpenSSL::Digest::SHA1.hexdigest(total_yanked)
273
528
  else
274
- sha1 = OpenSSL::Digest::SHA1.file(absolute_path).hexdigest
529
+ sha1 = OpenSSL::Digest::SHA1.file(absolute_path).hexdigest
275
530
  end
276
531
  rescue
277
- sha1 = OpenSSL::Digest::SHA1.file(absolute_path).hexdigest
532
+ sha1 = OpenSSL::Digest::SHA1.file(absolute_path).hexdigest
278
533
  end
279
534
 
280
535
  upload_resp = Cnvrg::API.request(@base_resource + "upload_tar_file", 'POST_FILE', {absolute_path: absolute_path, relative_path: relative_path,
281
536
  commit_sha1: commit_sha1, file_name: file_name,
282
537
  file_size: file_size, file_content_type: content_type, sha1: sha1,
283
- new_version: true})
538
+ new_version:true})
284
539
  if Cnvrg::CLI.is_response_success(upload_resp, false)
285
540
  path = upload_resp["result"]["path"]
286
541
  s3_res = upload_large_files_s3(upload_resp, absolute_path)
@@ -316,15 +571,14 @@ module Cnvrg
316
571
  return false
317
572
 
318
573
  end
319
-
320
- def upload_data_log_file(absolute_path, relative_path, data_commit_sha)
574
+ def upload_data_log_file(absolute_path, relative_path,data_commit_sha)
321
575
  file_name = File.basename relative_path
322
576
  file_size = File.size(absolute_path).to_f
323
577
  content_type = "text/x-log"
324
578
  upload_resp = Cnvrg::API.request("/users/#{@owner}/" + "upload_data_log", 'POST_FILE', {absolute_path: absolute_path, relative_path: relative_path,
325
- file_name: file_name, log_date: Time.now,
326
- file_size: file_size, file_content_type: content_type,
327
- data_commit_sha1: data_commit_sha})
579
+ file_name: file_name, log_date: Time.now,
580
+ file_size: file_size, file_content_type: content_type,
581
+ data_commit_sha1:data_commit_sha})
328
582
  if Cnvrg::CLI.is_response_success(upload_resp, false)
329
583
  path = upload_resp["result"]["path"]
330
584
  s3_res = upload_small_files_s3(path, absolute_path, "text/plain")
@@ -415,15 +669,15 @@ module Cnvrg
415
669
 
416
670
  end
417
671
 
418
- def upload_cnvrg_image(absolute_path, image_name, secret)
672
+ def upload_cnvrg_image(absolute_path, image_name,secret)
419
673
  file_name = File.basename relative_path
420
674
  file_size = File.size(absolute_path).to_f
421
675
  mime_type = MimeMagic.by_path(absolute_path)
422
676
  content_type = !(mime_type.nil? or mime_type.text?) ? mime_type.type : "text/plain"
423
677
  upload_resp = Cnvrg::API.request("images/#{image_name}/upload_file", 'POST_FILE', {absolute_path: absolute_path, relative_path: absolute_path,
424
- file_name: file_name,
425
- file_size: file_size, file_content_type: content_type,
426
- secret: secret})
678
+ file_name: file_name,
679
+ file_size: file_size, file_content_type: content_type,
680
+ secret: secret})
427
681
  if Cnvrg::CLI.is_response_success(upload_resp, false)
428
682
  path = upload_resp["result"]["path"]
429
683
  s3_res = upload_large_files_s3(upload_resp, absolute_path)
@@ -467,7 +721,7 @@ module Cnvrg
467
721
  sts_path = upload_resp["result"]["path_sts"]
468
722
 
469
723
  retries = 0
470
- success = false
724
+ success= false
471
725
  while !success and retries < 20
472
726
  begin
473
727
  if !Helpers.is_verify_ssl
@@ -477,7 +731,7 @@ module Cnvrg
477
731
  end
478
732
  success = true
479
733
  rescue => e
480
- retries += 1
734
+ retries +=1
481
735
  sleep(5)
482
736
 
483
737
  end
@@ -489,33 +743,33 @@ module Cnvrg
489
743
  key = split[0]
490
744
  iv = split[1]
491
745
  is_s3 = upload_resp["result"]["is_s3"]
492
- access = Cnvrg::Helpers.decrypt(key, iv, upload_resp["result"]["sts_a"])
746
+ access = Cnvrg::Helpers.decrypt(key, iv, upload_resp["result"]["sts_a"])
493
747
 
494
- secret = Cnvrg::Helpers.decrypt(key, iv, upload_resp["result"]["sts_s"])
748
+ secret = Cnvrg::Helpers.decrypt(key,iv, upload_resp["result"]["sts_s"])
495
749
 
496
- session = Cnvrg::Helpers.decrypt(key, iv, upload_resp["result"]["sts_st"])
497
- region = Cnvrg::Helpers.decrypt(key, iv, upload_resp["result"]["region"])
750
+ session = Cnvrg::Helpers.decrypt(key,iv, upload_resp["result"]["sts_st"])
751
+ region = Cnvrg::Helpers.decrypt(key,iv, upload_resp["result"]["region"])
498
752
 
499
- bucket = Cnvrg::Helpers.decrypt(key, iv, upload_resp["result"]["bucket"])
500
- server_side_encryption = upload_resp["result"]["server_side_encryption"]
753
+ bucket = Cnvrg::Helpers.decrypt(key,iv, upload_resp["result"]["bucket"])
754
+ server_side_encryption =upload_resp["result"]["server_side_encryption"]
501
755
  use_accelerate_endpoint = false
502
756
  if is_s3 or is_s3.nil?
503
757
 
504
- use_accelerate_endpoint = true
758
+ use_accelerate_endpoint =true
505
759
  client = Aws::S3::Client.new(
506
- :access_key_id => access,
760
+ :access_key_id =>access,
507
761
  :secret_access_key => secret,
508
762
  :session_token => session,
509
763
  :region => region,
510
764
  :http_open_timeout => 60, :retry_limit => 20)
511
765
  else
512
- endpoint = Cnvrg::Helpers.decrypt(key, iv, upload_resp["result"]["endpoint"])
766
+ endpoint = Cnvrg::Helpers.decrypt(key,iv, upload_resp["result"]["endpoint"])
513
767
  use_accelerate_endpoint = false
514
768
  client = Aws::S3::Client.new(
515
- :access_key_id => access,
769
+ :access_key_id =>access,
516
770
  :secret_access_key => secret,
517
771
  :region => region,
518
- :endpoint => endpoint, :force_path_style => true, :ssl_verify_peer => false,
772
+ :endpoint=> endpoint,:force_path_style=> true,:ssl_verify_peer=>false,
519
773
  :http_open_timeout => 60, :retry_limit => 20)
520
774
  end
521
775
 
@@ -527,11 +781,13 @@ module Cnvrg
527
781
  end
528
782
 
529
783
 
784
+
785
+
530
786
  s3 = Aws::S3::Resource.new(client: client)
531
787
 
532
788
  resp = s3.bucket(bucket).
533
- object(upload_resp["result"]["path"] + "/" + File.basename(file_path)).
534
- upload_file(file_path, options)
789
+ object(upload_resp["result"]["path"]+"/"+File.basename(file_path)).
790
+ upload_file(file_path,options)
535
791
 
536
792
 
537
793
  return resp
@@ -588,7 +844,6 @@ module Cnvrg
588
844
  response = Cnvrg::API.request(@base_resource + "create_dir", 'POST', {absolute_path: absolute_path, relative_path: relative_path, commit_sha1: commit_sha1})
589
845
  return Cnvrg::CLI.is_response_success(response, false)
590
846
  end
591
-
592
847
  def download_list_files_in_query(response, dataset_home)
593
848
  sts_path = response["path_sts"]
594
849
  if !Helpers.is_verify_ssl
@@ -600,29 +855,29 @@ module Cnvrg
600
855
  key = split[0]
601
856
  iv = split[1]
602
857
 
603
- access = Cnvrg::Helpers.decrypt(key, iv, response["sts_a"])
858
+ access = Cnvrg::Helpers.decrypt(key, iv, response["sts_a"])
604
859
 
605
- secret = Cnvrg::Helpers.decrypt(key, iv, response["sts_s"])
860
+ secret = Cnvrg::Helpers.decrypt(key,iv, response["sts_s"])
606
861
 
607
- session = Cnvrg::Helpers.decrypt(key, iv, response["sts_st"])
608
- region = Cnvrg::Helpers.decrypt(key, iv, response["region"])
862
+ session = Cnvrg::Helpers.decrypt(key,iv, response["sts_st"])
863
+ region = Cnvrg::Helpers.decrypt(key,iv, response["region"])
609
864
 
610
- bucket = Cnvrg::Helpers.decrypt(key, iv, response["bucket"])
865
+ bucket = Cnvrg::Helpers.decrypt(key,iv, response["bucket"])
611
866
  is_s3 = response["is_s3"]
612
867
  if is_s3 or is_s3.nil?
613
868
  client = Aws::S3::Client.new(
614
- :access_key_id => access,
869
+ :access_key_id =>access,
615
870
  :secret_access_key => secret,
616
871
  :session_token => session,
617
872
  :region => region,
618
873
  :http_open_timeout => 60, :retry_limit => 20)
619
874
  else
620
- endpoint = Cnvrg::Helpers.decrypt(key, iv, response["endpoint_url"])
875
+ endpoint = Cnvrg::Helpers.decrypt(key,iv, response["endpoint_url"])
621
876
  client = Aws::S3::Client.new(
622
- :access_key_id => access,
877
+ :access_key_id =>access,
623
878
  :secret_access_key => secret,
624
879
  :region => region,
625
- :endpoint => endpoint, :force_path_style => true, :ssl_verify_peer => false,
880
+ :endpoint=> endpoint,:force_path_style=> true,:ssl_verify_peer=>false,
626
881
  :http_open_timeout => 60, :retry_limit => 20)
627
882
  end
628
883
  list_files = response["files"]
@@ -639,18 +894,18 @@ module Cnvrg
639
894
  }
640
895
  download_count = 0
641
896
  Parallel.map((list_files), parallel_options) do |f|
642
- file_key = Cnvrg::Helpers.decrypt(key, iv, f["path"])
897
+ file_key = Cnvrg::Helpers.decrypt(key,iv, f["path"])
643
898
  begin
644
- begin
645
- dir = File.dirname f["fullpath"]
646
- FileUtils.mkdir_p(dataset_home + "/" + dir) unless File.exist? (dataset_home + "/" + dir)
647
- end
899
+ begin
900
+ dir = File.dirname f["fullpath"]
901
+ FileUtils.mkdir_p(dataset_home+"/"+ dir) unless File.exist? (dataset_home+"/"+ dir)
902
+ end
648
903
 
649
- File.open(dataset_home + "/" + f["fullpath"], 'w+') do |file|
650
- resp = client.get_object({bucket: bucket,
651
- key: file_key}, target: file)
652
- end
653
- download_count += 1
904
+ File.open(dataset_home+"/"+f["fullpath"], 'w+') do |file|
905
+ resp = client.get_object({bucket:bucket,
906
+ key:file_key}, target: file)
907
+ end
908
+ download_count += 1
654
909
  rescue
655
910
  end
656
911
 
@@ -662,95 +917,97 @@ module Cnvrg
662
917
  end
663
918
 
664
919
 
920
+
921
+
665
922
  end
666
923
 
667
924
 
668
- def download_file_s3(absolute_path, relative_path, project_home, conflict = false, commit_sha1 = nil, as_link = false)
925
+ def download_file_s3(absolute_path, relative_path, project_home, conflict=false, commit_sha1=nil, as_link=false)
669
926
  begin
670
- res = Cnvrg::API.request(@base_resource + "download_file", 'POST', {absolute_path: absolute_path, relative_path: relative_path, commit_sha1: commit_sha1, new_version: true, as_link: as_link})
927
+ res = Cnvrg::API.request(@base_resource + "download_file", 'POST', {absolute_path: absolute_path, relative_path: relative_path, commit_sha1: commit_sha1 ,new_version:true, as_link:as_link})
671
928
  Cnvrg::CLI.is_response_success(res, false)
672
- if res["result"]
673
- file_url = res["result"]["file_url"]
929
+ if res["result"]
930
+ file_url = res["result"]["file_url"]
674
931
 
675
- if as_link
676
- return res["result"]
677
- end
678
- # begin
679
- # if !Helpers.is_verify_ssl
680
- # tempfile = Down.download(file_url,open_timeout: 60,ssl_verify_mode: OpenSSL::SSL::VERIFY_NONE )
681
- #
682
- # else
683
- # tempfile = Down.download(file_url,open_timeout: 60)
684
- #
685
- # end
686
- #
687
- # FileUtils.move(tempfile.path, project_home+"/"+ absolute_path)
688
- # return true
689
- # rescue
690
- #
691
- # end
692
- download_resp = res
693
- filename = download_resp["result"]["filename"]
932
+ if as_link
933
+ return res["result"]
934
+ end
935
+ # begin
936
+ # if !Helpers.is_verify_ssl
937
+ # tempfile = Down.download(file_url,open_timeout: 60,ssl_verify_mode: OpenSSL::SSL::VERIFY_NONE )
938
+ #
939
+ # else
940
+ # tempfile = Down.download(file_url,open_timeout: 60)
941
+ #
942
+ # end
943
+ #
944
+ # FileUtils.move(tempfile.path, project_home+"/"+ absolute_path)
945
+ # return true
946
+ # rescue
947
+ #
948
+ # end
949
+ download_resp = res
950
+ filename = download_resp["result"]["filename"]
951
+
952
+ absolute_path += ".conflict" if conflict
953
+ sts_path = download_resp["result"]["path_sts"]
954
+ retries = 0
955
+ success= false
956
+ while !success and retries < 20
957
+ begin
958
+ if !Helpers.is_verify_ssl
959
+ body = open(sts_path, {ssl_verify_mode: OpenSSL::SSL::VERIFY_NONE}).read
960
+ else
961
+ body = open(sts_path).read
962
+ end
963
+ success = true
964
+ rescue => e
965
+ retries +=1
966
+ sleep(5)
694
967
 
695
- absolute_path += ".conflict" if conflict
696
- sts_path = download_resp["result"]["path_sts"]
697
- retries = 0
698
- success = false
699
- while !success and retries < 20
700
- begin
701
- if !Helpers.is_verify_ssl
702
- body = open(sts_path, {ssl_verify_mode: OpenSSL::SSL::VERIFY_NONE}).read
703
- else
704
- body = open(sts_path).read
705
968
  end
706
- success = true
707
- rescue => e
708
- retries += 1
709
- sleep(5)
710
-
711
969
  end
712
- end
713
- if !success
714
- return false
715
- end
716
- split = body.split("\n")
717
- key = split[0]
718
- iv = split[1]
970
+ if !success
971
+ return false
972
+ end
973
+ split = body.split("\n")
974
+ key = split[0]
975
+ iv = split[1]
719
976
 
720
- access = Cnvrg::Helpers.decrypt(key, iv, download_resp["result"]["sts_a"])
977
+ access = Cnvrg::Helpers.decrypt(key, iv, download_resp["result"]["sts_a"])
721
978
 
722
- secret = Cnvrg::Helpers.decrypt(key, iv, download_resp["result"]["sts_s"])
979
+ secret = Cnvrg::Helpers.decrypt(key,iv, download_resp["result"]["sts_s"])
723
980
 
724
- session = Cnvrg::Helpers.decrypt(key, iv, download_resp["result"]["sts_st"])
725
- region = Cnvrg::Helpers.decrypt(key, iv, download_resp["result"]["region"])
981
+ session = Cnvrg::Helpers.decrypt(key,iv, download_resp["result"]["sts_st"])
982
+ region = Cnvrg::Helpers.decrypt(key,iv, download_resp["result"]["region"])
726
983
 
727
- bucket = Cnvrg::Helpers.decrypt(key, iv, download_resp["result"]["bucket"])
728
- file_key = Cnvrg::Helpers.decrypt(key, iv, download_resp["result"]["key"])
984
+ bucket = Cnvrg::Helpers.decrypt(key,iv, download_resp["result"]["bucket"])
985
+ file_key = Cnvrg::Helpers.decrypt(key,iv, download_resp["result"]["key"])
729
986
 
730
- is_s3 = download_resp["result"]["is_s3"]
731
- if is_s3 or is_s3.nil?
732
- client = Aws::S3::Client.new(
733
- :access_key_id => access,
734
- :secret_access_key => secret,
735
- :session_token => session,
736
- :region => region,
737
- :http_open_timeout => 60, :retry_limit => 20)
738
- else
739
- endpoint = Cnvrg::Helpers.decrypt(key, iv, download_resp["result"]["endpoint_url"])
740
- client = Aws::S3::Client.new(
741
- :access_key_id => access,
742
- :secret_access_key => secret,
743
- :region => region,
744
- :endpoint => endpoint, :force_path_style => true, :ssl_verify_peer => false,
745
- :http_open_timeout => 60, :retry_limit => 20)
746
- end
987
+ is_s3 = download_resp["result"]["is_s3"]
988
+ if is_s3 or is_s3.nil?
989
+ client = Aws::S3::Client.new(
990
+ :access_key_id =>access,
991
+ :secret_access_key => secret,
992
+ :session_token => session,
993
+ :region => region,
994
+ :http_open_timeout => 60, :retry_limit => 20)
995
+ else
996
+ endpoint = Cnvrg::Helpers.decrypt(key,iv, download_resp["result"]["endpoint_url"])
997
+ client = Aws::S3::Client.new(
998
+ :access_key_id =>access,
999
+ :secret_access_key => secret,
1000
+ :region => region,
1001
+ :endpoint=> endpoint,:force_path_style=> true,:ssl_verify_peer=>false,
1002
+ :http_open_timeout => 60, :retry_limit => 20)
1003
+ end
747
1004
 
748
- File.open(project_home + "/" + absolute_path, 'w+') do |file|
749
- resp = client.get_object({bucket: bucket,
750
- key: file_key}, target: file)
1005
+ File.open(project_home+"/"+absolute_path, 'w+') do |file|
1006
+ resp = client.get_object({bucket:bucket,
1007
+ key:file_key}, target: file)
1008
+ end
1009
+ return true
751
1010
  end
752
- return true
753
- end
754
1011
 
755
1012
  rescue => e
756
1013
  return false
@@ -760,7 +1017,7 @@ module Cnvrg
760
1017
 
761
1018
  def download_data_file(commit_sha1, dataset_home)
762
1019
  begin
763
- res = Cnvrg::API.request(@base_resource + "download_data_file", 'POST', {commit_sha1: commit_sha1, new_version: true})
1020
+ res = Cnvrg::API.request(@base_resource + "download_data_file", 'POST', {commit_sha1: commit_sha1,new_version:true})
764
1021
  Cnvrg::CLI.is_response_success(res, false)
765
1022
  if res["result"]
766
1023
  download_resp = res
@@ -777,38 +1034,38 @@ module Cnvrg
777
1034
  iv = split[1]
778
1035
 
779
1036
 
780
- access = Cnvrg::Helpers.decrypt(key, iv, download_resp["result"]["sts_a"])
1037
+ access = Cnvrg::Helpers.decrypt(key, iv, download_resp["result"]["sts_a"])
781
1038
 
782
- secret = Cnvrg::Helpers.decrypt(key, iv, download_resp["result"]["sts_s"])
1039
+ secret = Cnvrg::Helpers.decrypt(key,iv, download_resp["result"]["sts_s"])
783
1040
 
784
- session = Cnvrg::Helpers.decrypt(key, iv, download_resp["result"]["sts_st"])
785
- region = Cnvrg::Helpers.decrypt(key, iv, download_resp["result"]["region"])
1041
+ session = Cnvrg::Helpers.decrypt(key,iv, download_resp["result"]["sts_st"])
1042
+ region = Cnvrg::Helpers.decrypt(key,iv, download_resp["result"]["region"])
786
1043
 
787
- bucket = Cnvrg::Helpers.decrypt(key, iv, download_resp["result"]["bucket"])
788
- file_key = Cnvrg::Helpers.decrypt(key, iv, download_resp["result"]["key"])
1044
+ bucket = Cnvrg::Helpers.decrypt(key,iv, download_resp["result"]["bucket"])
1045
+ file_key = Cnvrg::Helpers.decrypt(key,iv, download_resp["result"]["key"])
789
1046
 
790
1047
  is_s3 = download_resp["result"]["is_s3"]
791
1048
  if is_s3 or is_s3.nil?
792
1049
  client = Aws::S3::Client.new(
793
- :access_key_id => access,
1050
+ :access_key_id =>access,
794
1051
  :secret_access_key => secret,
795
1052
  :session_token => session,
796
1053
  :region => region,
797
1054
  :http_open_timeout => 60, :retry_limit => 20)
798
1055
  else
799
- endpoint = Cnvrg::Helpers.decrypt(key, iv, download_resp["result"]["endpoint_url"])
1056
+ endpoint = Cnvrg::Helpers.decrypt(key,iv, download_resp["result"]["endpoint_url"])
800
1057
  client = Aws::S3::Client.new(
801
- :access_key_id => access,
1058
+ :access_key_id =>access,
802
1059
  :secret_access_key => secret,
803
1060
  :region => region,
804
- :endpoint => endpoint, :force_path_style => true, :ssl_verify_peer => false,
1061
+ :endpoint=> endpoint,:force_path_style=> true,:ssl_verify_peer=>false,
805
1062
  :http_open_timeout => 60, :retry_limit => 20)
806
1063
  end
807
1064
 
808
1065
 
809
- File.open(dataset_home + "/" + filename, 'w+') do |file|
1066
+ File.open(dataset_home+"/"+filename, 'w+') do |file|
810
1067
  resp = client.get_object({bucket: bucket,
811
- key: file_key}, target: file)
1068
+ key: file_key }, target: file)
812
1069
  end
813
1070
  return filename
814
1071
  end
@@ -819,7 +1076,7 @@ module Cnvrg
819
1076
  end
820
1077
  end
821
1078
 
822
- def download_file(absolute_path, relative_path, project_home, conflict = false)
1079
+ def download_file(absolute_path, relative_path, project_home, conflict=false)
823
1080
  res = Cnvrg::API.request(@base_resource + "download_file", 'POST', {absolute_path: absolute_path, relative_path: relative_path})
824
1081
  Cnvrg::CLI.is_response_success(res, false)
825
1082
  if res["result"]
@@ -857,7 +1114,7 @@ module Cnvrg
857
1114
  puts FileUtils.rmtree("#{absolute_path}")
858
1115
  end
859
1116
 
860
- def revoke_download_file(absolute_path, filename, conflict = false)
1117
+ def revoke_download_file(absolute_path, filename, conflict=false)
861
1118
  begin
862
1119
  file_location = absolute_path.gsub(/#{filename}\/?$/, "")
863
1120
 
@@ -871,16 +1128,12 @@ module Cnvrg
871
1128
 
872
1129
  def revoke_download(tar_files, extracted_files)
873
1130
  begin
874
-
875
1131
  FileUtils.rm_rf(tar_files) unless (tar_files.nil? or tar_files.empty?)
876
1132
  FileUtils.rm_rf(extracted_files) unless (extracted_files.nil? or extracted_files.empty?)
877
-
878
1133
  rescue => e
879
1134
  return false
880
1135
  end
881
-
882
1136
  return true
883
-
884
1137
  end
885
1138
 
886
1139
  def delete_commit(commit_sha1)
@@ -895,17 +1148,29 @@ module Cnvrg
895
1148
  return response
896
1149
  end
897
1150
 
898
- def start_commit(new_branch, force = false, delete_commit = nil, chunks: 0, dataset: @dataset, message: nil)
1151
+ def start_commit(new_branch, force=false, chunks: 0, dataset: @dataset, message:nil)
899
1152
  begin
900
1153
  #if we are pushing with force or to branch we dont need to send current/next commit cause we want to
901
1154
  # create a new commit.
902
1155
  idx = (force || new_branch) ? {} : dataset.get_idx
903
1156
  commit = idx[:commit]
904
1157
  next_commit = idx[:next_commit]
905
- response = Cnvrg::API.request("#{base_resource}/commit/start", 'POST', {dataset_slug: @dataset_slug, new_branch: new_branch, force: force,
906
- username: @owner, current_commit: commit, next_commit: next_commit, total_chunks: chunks, message: message})
907
- Cnvrg::CLI.is_response_success(response, true)
908
- return response
1158
+ response = Cnvrg::API.request(
1159
+ "#{base_resource}/commit/start",
1160
+ 'POST',
1161
+ {
1162
+ dataset_slug: @dataset_slug,
1163
+ new_branch: new_branch,
1164
+ force:force,
1165
+ username: @owner,
1166
+ current_commit: commit,
1167
+ next_commit: next_commit,
1168
+ total_chunks: chunks,
1169
+ message: message
1170
+ }
1171
+ )
1172
+ Cnvrg::CLI.is_response_success(response, true)
1173
+ return response
909
1174
  rescue => e
910
1175
  return false
911
1176
  end
@@ -923,15 +1188,24 @@ module Cnvrg
923
1188
  end
924
1189
  end
925
1190
 
926
- def end_commit(commit_sha1, force, success: true, uploaded_files: 0)
1191
+ def end_commit(commit_sha1, force, success: true, uploaded_files: 0, commit_type: nil)
927
1192
  begin
928
- response = Cnvrg::API.request("#{base_resource}/commit/end", 'POST', {commit_sha1: commit_sha1, force: force, success: success, uploaded_files: uploaded_files})
1193
+ response = Cnvrg::API.request(
1194
+ "#{base_resource}/commit/end",
1195
+ 'POST',
1196
+ {
1197
+ commit_sha1: commit_sha1,
1198
+ force:force,
1199
+ success: success,
1200
+ uploaded_files: uploaded_files,
1201
+ commit_type: commit_type
1202
+ }
1203
+ )
929
1204
  Cnvrg::CLI.is_response_success(response, true)
930
1205
  return response
931
1206
  rescue => e
932
1207
  return false
933
1208
  end
934
-
935
1209
  end
936
1210
 
937
1211
  def end_commit_tar(commit_sha1, cur_idx)
@@ -951,13 +1225,13 @@ module Cnvrg
951
1225
  end
952
1226
 
953
1227
  def get_trees(commit: "latest")
954
- response = Cnvrg::API.request("#{@base_resource}/clone_trees", 'POST', {commit: commit})
1228
+ response = Cnvrg::API.request("#{@base_resource}/clone_trees", 'POST',{commit: commit})
955
1229
  return nil unless Cnvrg::CLI.is_response_success(response, false)
956
1230
  response['result']['files']
957
1231
  end
958
1232
 
959
1233
  def get_clone_chunk(latest_id: nil, chunk_size: 1000, commit: 'latest')
960
- response = Cnvrg::API.request("#{@base_resource}/clone_chunk", 'POST', {commit: commit, chunk_size: chunk_size, latest_id: latest_id})
1234
+ response = Cnvrg::API.request("#{@base_resource}/clone_chunk", 'POST',{commit: commit, chunk_size: chunk_size, latest_id: latest_id})
961
1235
  unless Cnvrg::CLI.is_response_success(response, false)
962
1236
  Cnvrg::Logger.log_info("#{{commit: commit, chunk_size: chunk_size, latest_id: latest_id}}")
963
1237
  return nil
@@ -988,37 +1262,43 @@ module Cnvrg
988
1262
  end
989
1263
  end
990
1264
 
991
- def download_multiple_chunks(commit, chunk_size = 1000, progress: nil)
1265
+ def download_multiple_chunks(commit, chunk_size=1000, progress: nil)
992
1266
  begin
993
- last_chunk_size = chunk_size
994
- q = {commit: commit, chunk_size: chunk_size}
995
- overall = 0
996
- while last_chunk_size > 0
997
- response = Cnvrg::API.request("users/#{@owner}/datasets/#{@dataset_slug}/clone", 'POST', q)
998
- if Cnvrg::CLI.is_response_success(response, false)
999
- files = response['files']
1000
- data_home = "#{Dir.pwd}/#{response['name']}"
1001
- last_chunk_size = files['keys'].length
1002
- break if last_chunk_size == 0
1003
- res = download_multiple_files_s3(files, data_home, read_only: false, progressbar: progress)
1004
- overall += last_chunk_size
1005
- q[:latest] = files['latest']
1006
- else
1007
- last_chunk_size = 0
1008
- end
1267
+ last_chunk_size = chunk_size
1268
+ q = { commit: commit, chunk_size: chunk_size}
1269
+ overall = 0
1270
+ while last_chunk_size > 0
1271
+ response = Cnvrg::API.request("users/#{@owner}/datasets/#{@dataset_slug}/clone", 'POST', q)
1272
+ if Cnvrg::CLI.is_response_success(response, false)
1273
+ files = response['files']
1274
+ data_home = "#{Dir.pwd}/#{response['name']}"
1275
+ last_chunk_size = files['keys'].length
1276
+ break if last_chunk_size == 0
1277
+ res = download_multiple_files_s3(files, data_home, read_only: false, progressbar: progress)
1278
+ overall += last_chunk_size
1279
+ q[:latest] = files['latest']
1280
+ else
1281
+ last_chunk_size = 0
1009
1282
  end
1010
- Cnvrg::Result.new(true, "Cloned #{overall} files!")
1283
+ end
1284
+ Cnvrg::Result.new(true, "Cloned #{overall} files!")
1011
1285
  rescue => e
1012
1286
  Cnvrg::Result.new(false, "Cant download chunk", e.message, e.backtrace)
1013
1287
 
1014
1288
  end
1015
1289
  end
1016
1290
 
1017
- def generate_parallel_idx
1018
-
1291
+ def last_valid_commit()
1292
+ begin
1293
+ response = Cnvrg::API.request("#{base_resource}/last_valid_commit", 'GET')
1294
+ Cnvrg::CLI.is_response_success(response, true)
1295
+ return response
1296
+ rescue => e
1297
+ return false
1298
+ end
1019
1299
  end
1020
1300
 
1021
- def download_multiple_files_s3(files, project_home, conflict: false, progressbar: nil, read_only: false)
1301
+ def download_multiple_files_s3(files, project_home, conflict: false, progressbar: nil, read_only:false, flatten: false)
1022
1302
  begin
1023
1303
  refresh_storage_token
1024
1304
  parallel_options = {
@@ -1027,14 +1307,16 @@ module Cnvrg
1027
1307
  }
1028
1308
  Parallel.map(files["keys"], parallel_options) do |f|
1029
1309
  begin
1030
- local_path = @dataset.local_path + '/' + f['name']
1310
+ file_path = f['name']
1311
+ file_path = File.basename(f['name']) if flatten
1312
+ local_path = @dataset.local_path + '/' + file_path
1031
1313
  Cnvrg::Logger.log_info("Downloading #{local_path}")
1032
1314
  progressbar.progress += 1 if progressbar.present?
1033
1315
  if local_path.end_with? "/"
1034
1316
  @downloader.mkdir(local_path, recursive: true)
1035
1317
  next
1036
1318
  end
1037
- # blob
1319
+ # blob
1038
1320
  local_path = "#{local_path}.conflict" if conflict
1039
1321
  storage_path = f["path"]
1040
1322
  if File.exists? local_path
@@ -1047,11 +1329,11 @@ module Cnvrg
1047
1329
  Cnvrg::Logger.log_error(e)
1048
1330
  end
1049
1331
  end
1050
- return Cnvrg::Result.new(true, "Downloaded successfully")
1332
+ return Cnvrg::Result.new(true,"Downloaded successfully")
1051
1333
  rescue => e
1052
- Cnvrg::Logger.log_error(e)
1053
- return Cnvrg::Result.new(false, "Could not download some files", e.message, e.backtrace)
1334
+ Cnvrg::Logger.log_error(e)
1335
+ return Cnvrg::Result.new(false,"Could not download some files", e.message, e.backtrace)
1336
+ end
1054
1337
  end
1055
- end
1056
1338
  end
1057
1339
  end