Class: Cnvrg::Dataset
- Inherits:
-
Object
- Object
- Cnvrg::Dataset
- Defined in:
- lib/cnvrg/dataset.rb
Instance Attribute Summary collapse
-
#local_path ⇒ Object
readonly
Returns the value of attribute local_path.
-
#owner ⇒ Object
readonly
Returns the value of attribute owner.
-
#slug ⇒ Object
readonly
Returns the value of attribute slug.
-
#title ⇒ Object
readonly
Returns the value of attribute title.
-
#working_dir ⇒ Object
readonly
Returns the value of attribute working_dir.
Class Method Summary collapse
- .blank_clone(owner, dataset_name, dataset_slug) ⇒ Object
- .clone(owner, dataset_name, dataset_slug, remote = false) ⇒ Object
- .clone_tree(commit: 'latest', dataset_home: nil) ⇒ Object
- .delete(dataset_slug, owner) ⇒ Object
- .init(owner, dataset_name, is_public = false, bucket: nil) ⇒ Object
- .init_container(owner, dataset_slug, dataset_name) ⇒ Object
- .link_dataset(owner: nil, slug: nil) ⇒ Object
- .scan_datasets ⇒ Object
- .stop_if_dataset_present(dataset_home, dataset_name, commit: nil) ⇒ Object
- .validate_config ⇒ Object
- .verify_cnvrgignore_exist(dataset_name, remote) ⇒ Object
- .verify_dataset(dataset_slug) ⇒ Object
- .verify_datasets(dataset_titles, timeout = nil) ⇒ Object
Instance Method Summary collapse
- #backup_idx ⇒ Object
- #change_url(owner: '', slug: '', title: '') ⇒ Object
- #clone(commit) ⇒ Object
- #compare_commit(commit) ⇒ Object
- #compare_commits(commit) ⇒ Object
- #compare_idx(new_branch, commit = last_local_commit, local_idx = nil, force = false, next_commit = nil) ⇒ Object
- #compare_idx_download(all_files: false, desired_commit: nil) ⇒ Object
- #create_volume ⇒ Object
- #current_status(new_branch) ⇒ Object
- #download_tags_yaml ⇒ Object
- #download_updated_data(current_commit) ⇒ Object
- #generate_chunked_idx(list_files = [], threads: IDXParallelThreads, prefix: '') ⇒ Object
- #generate_cnvrg_ignore ⇒ Object
- #generate_idx(show_progress = false) ⇒ Object
- #get_clone_chunk(latest_id: nil, chunk_size: 1000, offset: 0, commit: 'latest') ⇒ Object
- #get_dataset(commit: nil, query: nil) ⇒ Object
- #get_idx ⇒ Object
- #get_ignore_list ⇒ Object
- #get_next_commit ⇒ Object
- #get_partial_commit ⇒ Object
- #get_query_file(query_slug) ⇒ Object
- #get_stats(commit: nil, query: nil) ⇒ Object
- #get_storage_client ⇒ Object
- #get_storage_client_fallback ⇒ Object
- #init_home(remote: false) ⇒ Object
-
#initialize(project_home = '', dataset_url: '', dataset_info: '') ⇒ Dataset
constructor
A new instance of Dataset.
- #last_local_commit ⇒ Object
- #list(owner) ⇒ Object
- #list_all_files(with_ignore = false) ⇒ Object
- #list_commits(commit_sha1: nil) ⇒ Object
- #list_files(commit_sha1: "latest", limit: 1000, offset: 0, expires: 3600) ⇒ Object
- #remove_next_commit ⇒ Object
- #restore_idx ⇒ Object
- #revert(working_dir) ⇒ Object
- #revert_next_commit ⇒ Object
- #revert_to_last_commit(commit: nil) ⇒ Object
- #search_queries ⇒ Object
- #send_ignore_list ⇒ Object
- #set_idx(idx) ⇒ Object
- #set_next_commit(commit_sha1) ⇒ Object
- #set_partial_commit(commit_sha1) ⇒ Object
- #snapshot ⇒ Object
- #soft_linked? ⇒ Boolean
- #softlinked? ⇒ Boolean
- #update_idx(idx) ⇒ Object
- #update_idx_with_commit!(commit) ⇒ Object
- #update_idx_with_files_commits!(files, commit_time) ⇒ Object
- #update_ignore_list(new_ignore) ⇒ Object
- #upload_tags_via_yml(tag_file = nil) ⇒ Object
- #url ⇒ Object
- #write_idx(tree = nil, commit = nil) ⇒ Object
- #write_success(in_folder = false) ⇒ Object
- #write_tree(tree) ⇒ Object
Constructor Details
#initialize(project_home = '', dataset_url: '', dataset_info: '') ⇒ Dataset
Returns a new instance of Dataset.
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/cnvrg/dataset.rb', line 10 def initialize(project_home = '', dataset_url: '', dataset_info: '') begin @info = {} if project_home.present? @local_path = project_home @working_dir = project_home config = YAML.load_file(project_home + "/.cnvrg/config.yml") @title = config[:dataset_name] @slug = config[:dataset_slug] @owner = config[:owner] elsif dataset_info.present? @title = dataset_info[:slug] @slug = dataset_info[:slug] @owner = dataset_info[:owner] @local_path = Dir.pwd else owner, slug = Cnvrg::Helpers.extract_owner_slug_from_url(dataset_url, 'datasets') @title = slug @slug = slug @owner = owner @local_path = Dir.pwd end rescue => e end end |
Instance Attribute Details
#local_path ⇒ Object (readonly)
Returns the value of attribute local_path.
4 5 6 |
# File 'lib/cnvrg/dataset.rb', line 4 def local_path @local_path end |
#owner ⇒ Object (readonly)
Returns the value of attribute owner.
4 5 6 |
# File 'lib/cnvrg/dataset.rb', line 4 def owner @owner end |
#slug ⇒ Object (readonly)
Returns the value of attribute slug.
4 5 6 |
# File 'lib/cnvrg/dataset.rb', line 4 def slug @slug end |
#title ⇒ Object (readonly)
Returns the value of attribute title.
4 5 6 |
# File 'lib/cnvrg/dataset.rb', line 4 def title @title end |
#working_dir ⇒ Object (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
.blank_clone(owner, dataset_name, dataset_slug) ⇒ Object
351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 |
# File 'lib/cnvrg/dataset.rb', line 351 def self.blank_clone(owner, dataset_name, dataset_slug) list_dirs = ["#{dataset_slug}/.cnvrg" ] list_files = [ "#{dataset_slug}/.cnvrg/config.yml" ] create_ignore = false if !File.exist? ".cnvrgignore" list_files << "#{dataset_slug}/.cnvrgignore" create_ignore = true end cnvrgignore = Helpers.cnvrgignore_content begin config = {dataset_name: dataset_name, dataset_slug: dataset_slug, owner: owner} FileUtils.mkdir_p list_dirs FileUtils.touch list_files File.open("#{dataset_slug}/.cnvrg/config.yml", "w+") {|f| f.write config.to_yaml} File.open("#{dataset_slug}/.cnvrgignore", "w+") {|f| f.write cnvrgignore} unless !create_ignore rescue => e return false end return true end |
.clone(owner, dataset_name, dataset_slug, remote = false) ⇒ Object
442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 |
# File 'lib/cnvrg/dataset.rb', line 442 def self.clone(owner, dataset_name, dataset_slug, remote = false) begin list_dirs = [] if !remote list_dirs << dataset_name end list_dirs << "#{dataset_name}/.cnvrg" list_files = [ "#{dataset_name}/.cnvrg/config.yml", ] config = {dataset_name: dataset_name, dataset_slug: dataset_slug, owner: owner} FileUtils.mkdir_p list_dirs FileUtils.touch list_files File.open("#{dataset_name}/.cnvrg/config.yml", "w+") {|f| f.write config.to_yaml} rescue => e puts "Exception in clone request:#{e.}" return false end return true end |
.clone_tree(commit: 'latest', dataset_home: nil) ⇒ Object
474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 |
# File 'lib/cnvrg/dataset.rb', line 474 def self.clone_tree(commit: 'latest', dataset_home: nil) @dataset = Cnvrg::Dataset.new(dataset_home) @files = Cnvrg::Datafiles.new(@dataset.owner, @dataset.slug, dataset: @dataset) trees = @files.get_trees(commit: commit) return false if trees.nil? pb = ProgressBar.create(:title => "Download Progress", :progress_mark => '=', :format => "%b>>%i| %p%% %t", :starting_at => 0, :total => trees.size, :autofinish => true) trees.each do |tree| pb.progress += 1 @files.download_dir(dataset_home, tree) end pb.finish @dataset.write_success true end |
.delete(dataset_slug, owner) ⇒ Object
129 130 131 132 |
# File 'lib/cnvrg/dataset.rb', line 129 def self.delete(dataset_slug, owner) response = Cnvrg::API.request("users/#{owner}/datasets/#{dataset_slug}/delete", 'DELETE') return response end |
.init(owner, dataset_name, is_public = false, bucket: nil) ⇒ Object
283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 |
# File 'lib/cnvrg/dataset.rb', line 283 def self.init(owner, dataset_name, is_public = false, bucket: nil) list_dirs = [".cnvrg" ] list_files = [ ".cnvrg/config.yml" ] create_ignore = false if !File.exist? ".cnvrgignore" list_files << ".cnvrgignore" create_ignore = true end cnvrgignore = Helpers.cnvrgignore_content begin response = Cnvrg::API.request("cli/create_dataset", 'POST', {title: dataset_name, owner: owner, is_public: is_public, bucket: bucket}) 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 !create_ignore rescue => e return false end return true end |
.init_container(owner, dataset_slug, dataset_name) ⇒ Object
502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 |
# File 'lib/cnvrg/dataset.rb', line 502 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 |
.link_dataset(owner: nil, slug: nil) ⇒ Object
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/dataset.rb', line 316 def self.link_dataset(owner: nil, slug: nil) begin return false if owner.blank? or slug.blank? response = Cnvrg::API.request("users/#{owner}/datasets/#{slug}", 'GET') success = Cnvrg::CLI.is_response_success(response, false) return unless success result = response["result"] sha1 = result["init_commit_sha1"] # We need to write init IDX that contain init commit sha1 so the user will be able to doing actions on the dataset # so it only relevant for new server raise Exception.new("This feature is not available for your cnvrg version. Please contact support for more information") if sha1.blank? ## means this is old version of server config = {dataset_name: result["title"], dataset_slug: result["slug"], owner: owner} list_dirs = [".cnvrg"] list_files = [".cnvrg/config.yml"] FileUtils.mkdir_p list_dirs FileUtils.touch list_files File.open(".cnvrg/config.yml", "w+") {|f| f.write config.to_yaml} dataset = Dataset.new(Dir.pwd) dataset.write_idx({}, sha1) true rescue => e raise Exception.new(e) end end |
.scan_datasets ⇒ Object
414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 |
# File 'lib/cnvrg/dataset.rb', line 414 def self.scan_datasets() Cnvrg::Logger.log_info("Looking up datasets") datasets = Dir.entries(Dir.pwd).map do |entry| if File.directory? File.join(Dir.pwd,entry) and !(entry =='.' || entry == '..') begin config = YAML.load_file("#{Dir.pwd}/#{entry}/.cnvrg/config.yml") rescue nil local_commit = YAML.load_file("#{Dir.pwd}/#{entry}/.cnvrg/idx.yml")[:commit] rescue nil if config.present? and config[:success] == true and config[:dataset_name].present? and config[:dataset_slug].present? and local_commit.present? { "dataset_slug": config[:dataset_slug], "dataset_name": config[:dataset_name], "local_commit": local_commit, } else nil end rescue nil end end end.compact.uniq datasets end |
.stop_if_dataset_present(dataset_home, dataset_name, commit: nil) ⇒ Object
875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 |
# File 'lib/cnvrg/dataset.rb', line 875 def self.stop_if_dataset_present(dataset_home, dataset_name, commit: nil) cli = Cnvrg::CLI.new() config = YAML.load_file(dataset_home + "/.cnvrg/config.yml") if commit.present? local_commit = YAML.load_file(dataset_home + "/.cnvrg/idx.yml")[:commit] rescue nil return if commit != local_commit or local_commit.blank? end if config[:dataset_name] == dataset_name cli.("Dataset already present, clone aborted") exit(0) end rescue => e nil end |
.validate_config ⇒ Object
852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 |
# File 'lib/cnvrg/dataset.rb', line 852 def self.validate_config ## check that the .cnvrg folder exists: dot_cnvrg_exists = Dir[".cnvrg"].present? return {validation: Data::ConfigValidation::FAILED, message: ".cnvrg folder does not exists"} if not dot_cnvrg_exists ## check that the config.yml exists: config_file_exists = Dir[".cnvrg/*"].include? ".cnvrg/config.yml" return {validation: Data::ConfigValidation::FAILED, message: "config.yml exists"} if not config_file_exists ## check that the config.yml file not empty: config = YAML.load_file("#{Dir.getwd}/.cnvrg/config.yml") return {validation: Data::ConfigValidation::FAILED, message: "config.yml is empty"} if not config ## check that config.yml is valid: title = config[:dataset_name] slug = config[:dataset_slug] owner = config[:owner] return {validation: Data::ConfigValidation::FAILED, message: "config.yml is not valid or some keys are missing"} if title.blank? or slug.blank? or owner.blank? ## everything OK: return {validation: Data::ConfigValidation::SUCCESS, message: "Directory is already linked to #{slug}"} end |
.verify_cnvrgignore_exist(dataset_name, remote) ⇒ Object
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 |
# File 'lib/cnvrg/dataset.rb', line 212 def self.verify_cnvrgignore_exist(dataset_name, remote) path = ".cnvrgignore" if !File.exist? path path = "#{dataset_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 |
.verify_dataset(dataset_slug) ⇒ Object
386 387 388 389 |
# File 'lib/cnvrg/dataset.rb', line 386 def self.verify_dataset(dataset_slug) config = YAML.load_file("/data/#{dataset_title}/.cnvrg/config.yml") rescue {} config[:success] == true end |
.verify_datasets(dataset_titles, timeout = nil) ⇒ Object
391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 |
# File 'lib/cnvrg/dataset.rb', line 391 def self.verify_datasets(dataset_titles, timeout = nil) start_time = Time.now.to_i Cnvrg::Logger.log_info("Verifying datasets #{dataset_titles}") Cnvrg::Logger.log_info("Timeout is #{timeout}") while true begin current_time = Time.now.to_i return false if (timeout.present? and timeout < current_time - start_time) all_are_ready = dataset_titles.all? do |dataset_title| config = YAML.load_file("#{dataset_title}/.cnvrg/config.yml") config[:success] == true end return true if all_are_ready Cnvrg::Logger.log_info("Sleeping..") sleep 10 rescue => e Cnvrg::Logger.log_info("Got error") Cnvrg::Logger.log_error(e) sleep 10 end end end |
Instance Method Details
#backup_idx ⇒ Object
107 108 109 110 111 112 113 114 115 |
# File 'lib/cnvrg/dataset.rb', line 107 def backup_idx Cnvrg::Logger.log_info("Backup idx") if File.exists? "#{self.local_path}/.cnvrg/idx.yml" FileUtils.cp "#{self.local_path}/.cnvrg/idx.yml", "#{self.local_path}/.cnvrg/idx.yml.backup" else idx = {commit: nil, tree: {}} File.open("#{self.local_path}/.cnvrg/idx.yml.backup", 'w') {|f| f.write idx.to_yaml} end end |
#change_url(owner: '', slug: '', title: '') ⇒ Object
124 125 126 127 |
# File 'lib/cnvrg/dataset.rb', line 124 def change_url(owner: '', slug: '', title: '') config = {dataset_home: title, dataset_slug: slug, owner: owner} File.open(".cnvrg/config.yml", "w+") {|f| f.write config.to_yaml} end |
#clone(commit) ⇒ Object
438 439 440 |
# File 'lib/cnvrg/dataset.rb', line 438 def clone(commit) return end |
#compare_commit(commit) ⇒ Object
809 810 811 812 813 814 815 816 817 |
# File 'lib/cnvrg/dataset.rb', line 809 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
761 762 763 764 765 |
# File 'lib/cnvrg/dataset.rb', line 761 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, force = false, next_commit = nil) ⇒ Object
686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 |
# File 'lib/cnvrg/dataset.rb', line 686 def compare_idx(new_branch, commit = last_local_commit, local_idx = nil, force = false, next_commit = nil) if local_idx.nil? local_idx = self.generate_idx end ignore_list = self.get_ignore_list() if force added = [] if local_idx[:tree] added << local_idx[:tree].keys added.flatten! end response = {"result" => {"commit" => next_commit, "tree" => {"added" => added, "updated_on_server" => [], "updated_on_local" => [], "deleted" => [], "conflicts" => []}}} return response end 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, next_commit: next_commit}) CLI.is_response_success(response, false) return response end |
#compare_idx_download(all_files: false, desired_commit: nil) ⇒ Object
711 712 713 714 715 716 |
# File 'lib/cnvrg/dataset.rb', line 711 def compare_idx_download(all_files: false, desired_commit: nil) current_commit = self.last_local_commit next_commit = self.get_next_commit ignore_list = self.send_ignore_list() return Cnvrg::API.request("users/#{self.owner}/datasets/#{self.slug}/download_status", 'POST', {current_commit: current_commit, next_commit: next_commit, ignore: ignore_list, all_files: all_files, desired_commit: desired_commit.presence}) end |
#create_volume ⇒ Object
674 675 676 677 678 |
# File 'lib/cnvrg/dataset.rb', line 674 def create_volume response = Cnvrg::API.request("users/#{self.owner}/datasets/#{self.slug}/volumes/create", 'POST') CLI.is_response_success(response) return response end |
#current_status(new_branch) ⇒ Object
729 730 731 732 733 734 |
# File 'lib/cnvrg/dataset.rb', line 729 def current_status(new_branch) commit = last_local_commit response = Cnvrg::API.request("users/#{self.owner}/datasets/#{self.slug}/status_current", 'POST', {current_commit: commit, new_branch: new_branch}) CLI.is_response_success(response, true) return response end |
#download_tags_yaml ⇒ Object
175 176 177 178 179 180 181 182 183 184 185 |
# File 'lib/cnvrg/dataset.rb', line 175 def response = Cnvrg::API.request("users/#{self.owner}/datasets/#{self.slug}/tags_yml", 'GET') CLI.is_response_success(response) begin path = self.working_dir File.open("#{path}/#{response["results"]["filename"]}", "w+") {|f| f.write response["results"]["file_content"]} return true rescue return false end end |
#download_updated_data(current_commit) ⇒ Object
680 681 682 683 684 |
# File 'lib/cnvrg/dataset.rb', line 680 def download_updated_data(current_commit) response = Cnvrg::API.request("users/#{self.owner}/datasets/#{self.slug}/download_updated_data", 'POST', {current_commit: current_commit}) CLI.is_response_success(response, false) return response end |
#generate_chunked_idx(list_files = [], threads: IDXParallelThreads, prefix: '') ⇒ Object
544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 |
# File 'lib/cnvrg/dataset.rb', line 544 def generate_chunked_idx(list_files = [], threads: IDXParallelThreads, prefix: '') tree = {} Parallel.map(list_files, in_threads: threads) do |file| #check if prefix exists do prefix/path otherwise path label = file.gsub(self.local_path + "/", "") label = "#{prefix}/#{label}" if prefix.present? if not Cnvrg::Files.valid_file_name?(label) raise StandardError.new("#{label} is not a valid file name.") end if File.directory? file tree[label + "/"] = nil else sha1 = OpenSSL::Digest::SHA1.file(file).hexdigest file_name = File.basename file file_size = File.size(file).to_f mime_type = MimeMagic.by_path(file) content_type = !(mime_type.nil? or mime_type.text?) ? mime_type.type : "text/plain" relative_path = file.gsub(/^#{@local_path + "/"}/, "") relative_path = "#{prefix}/#{relative_path}" if prefix.present? tree[label] = {sha1: sha1, file_name: file_name, file_size: file_size, content_type: content_type, absolute_path: file, relative_path: relative_path} end end if prefix.present? #add the prefix as dirs to the files #lets say the prefix is a/b/c so we want that a/, a/b/, a/b/c/ will be in our files_list dirs = prefix.split('/') curr_path = [] dirs.each do |dir| curr_path << dir list_files << curr_path.join('/') end end return tree end |
#generate_cnvrg_ignore ⇒ Object
381 382 383 384 |
# File 'lib/cnvrg/dataset.rb', line 381 def generate_cnvrg_ignore cnvrgignore = Helpers.cnvrgignore_content File.open(self.local_path + "/.cnvrgignore", "w+") {|f| f.write cnvrgignore} end |
#generate_idx(show_progress = false) ⇒ Object
614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 |
# File 'lib/cnvrg/dataset.rb', line 614 def generate_idx(show_progress = false) 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() if show_progress = { :progress => { :title => "Checking Dataset", :progress_mark => '=', :format => "%b>>%i| %p%% %t", :starting_at => 0, :total => (list).size, :autofinish => true }, in_threads: IDXParallelThreads, isolation: true } else = { in_threads: IDXParallelThreads, isolation: true } end Parallel.map(list, ) 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 if !old_idx.nil? and !old_idx[:next_commit].nil? and !old_idx[:next_commit].empty? idx = {commit: old_idx.to_h[:commit], tree: tree_idx, next_commit: old_idx[:next_commit]} else idx = {commit: old_idx.to_h[:commit], tree: tree_idx} end idx_yaml = idx.to_yaml File.open("#{self.local_path}/.cnvrg/idx.yml", 'w') {|f| f.write idx_yaml} return idx end |
#get_clone_chunk(latest_id: nil, chunk_size: 1000, offset: 0, commit: 'latest') ⇒ Object
101 102 103 104 105 |
# File 'lib/cnvrg/dataset.rb', line 101 def get_clone_chunk(latest_id: nil, chunk_size: 1000, offset: 0, commit: 'latest') response = Cnvrg::API.request("users/#{@owner}/datasets/#{@slug}/clone_chunk", 'POST', {commit: commit, chunk_size: chunk_size, latest_id: latest_id, offset: offset}) return nil unless Cnvrg::CLI.is_response_success(response, false) response['result']['files']['keys'] end |
#get_dataset(commit: nil, query: nil) ⇒ Object
61 62 63 64 65 66 67 68 69 |
# File 'lib/cnvrg/dataset.rb', line 61 def get_dataset(commit: nil, query: nil) if @dataset_call return @dataset_call end response = Cnvrg::API.request("users/#{owner}/datasets/#{slug}/clone", 'POST',{ commit: commit, query:query}) Cnvrg::CLI.is_response_success(response,true) @dataset_call = response["result"] @dataset_call end |
#get_idx ⇒ Object
528 529 530 531 532 533 |
# File 'lib/cnvrg/dataset.rb', line 528 def get_idx if File.exists? "#{self.local_path}/.cnvrg/idx.yml" return YAML.load_file("#{self.local_path}/.cnvrg/idx.yml") end {commit: nil, tree: {}} end |
#get_ignore_list ⇒ Object
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 |
# File 'lib/cnvrg/dataset.rb', line 252 def get_ignore_list ### handle case when after clone .cnvrgignore doesnt exists if not File.exists?(self.local_path + "/.cnvrgignore") self.generate_cnvrg_ignore end ignore_list = [] if not File.exists? "#{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.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 |
#get_next_commit ⇒ Object
781 782 783 784 785 786 787 |
# File 'lib/cnvrg/dataset.rb', line 781 def get_next_commit() if !File.exist? "#{self.local_path}/.cnvrg/idx.yml" return nil end idx_hash = YAML.load_file("#{self.local_path}/.cnvrg/idx.yml") return idx_hash[:next_commit] end |
#get_partial_commit ⇒ Object
724 725 726 727 |
# File 'lib/cnvrg/dataset.rb', line 724 def get_partial_commit idx = self.get_idx idx.try(:fetch, :partial_commit) end |
#get_query_file(query_slug) ⇒ Object
165 166 167 168 169 170 171 172 173 |
# File 'lib/cnvrg/dataset.rb', line 165 def get_query_file(query_slug) response = Cnvrg::API.request("users/#{self.owner}/datasets/#{self.slug}/search/#{query_slug}", 'GET') CLI.is_response_success(response) row = [["Name", "Full path", "URL"]] response["results"]["query_files"].each do |file| row << [file["name"], file["fullpath"], file["s3_url"]] end return row end |
#get_stats(commit: nil, query: nil) ⇒ Object
95 96 97 98 99 |
# File 'lib/cnvrg/dataset.rb', line 95 def get_stats(commit: nil, query: nil) response = Cnvrg::API.request("users/#{@owner}/datasets/#{@slug}/clone", 'POST', {commit: commit, query: query}) Cnvrg::CLI.is_response_success(response, true) response['result'] end |
#get_storage_client ⇒ Object
76 77 78 79 80 81 82 83 84 |
# File 'lib/cnvrg/dataset.rb', line 76 def get_storage_client response = Cnvrg::API.request("users/#{@owner}/datasets/#{@slug}/client", 'GET') if Cnvrg::CLI.is_response_success(response, false) client_params = response['client'] else client_params = get_storage_client_fallback end Cnvrg::Downloader::Client.factory(client_params) end |
#get_storage_client_fallback ⇒ Object
86 87 88 89 90 91 92 93 |
# File 'lib/cnvrg/dataset.rb', line 86 def get_storage_client_fallback response = Cnvrg::API.request("users/#{@owner}/datasets/#{@slug}/download_multi", "POST", {files: []}) raise StandardError.new("Can't find dataset credentials") unless Cnvrg::CLI.is_response_success(response, false) files = response['files'] storage = files['is_s3'] ? 's3' : 'minio' files['storage'] = storage files end |
#init_home(remote: false) ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/cnvrg/dataset.rb', line 41 def init_home(remote: false) dataset_home = File.join(Dir.pwd, @slug) if Dir.exists? dataset_home if !remote Cnvrg::CLI.("Error: Conflict with dir #{@slug}", Thor::Shell::Color::RED) if Thor::Shell::Basic.new.no? "Sync to repository anyway? (current data might lost)", Thor::Shell::Color::YELLOW Cnvrg::CLI.("Remove dir in order to clone #{@slug}", Thor::Shell::Color::RED) exit(1) end end FileUtils.rm_rf(dataset_home) end # if Dataset.clone(owner, dataset_name, slug, remote) Dataset.clone(@owner, @slug, @slug, remote) @local_path = dataset_home Cnvrg::CLI.('') true end |
#last_local_commit ⇒ Object
134 135 136 137 138 139 140 |
# File 'lib/cnvrg/dataset.rb', line 134 def last_local_commit if !File.exist? "#{self.local_path}/.cnvrg/idx.yml" return nil end idx = YAML.load_file(@local_path + "/.cnvrg/idx.yml") return idx[:commit] end |
#list(owner) ⇒ Object
149 150 151 152 153 |
# File 'lib/cnvrg/dataset.rb', line 149 def list(owner) response = Cnvrg::API.request("users/#{owner}/datasets/list", 'GET') CLI.is_response_success(response) return response end |
#list_all_files(with_ignore = false) ⇒ Object
589 590 591 592 593 594 595 596 597 |
# File 'lib/cnvrg/dataset.rb', line 589 def list_all_files(with_ignore = false) 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/)} #we want that big files will list = list.sort_by {|fn| File.size(fn)} return list if with_ignore list_ignore = self.get_ignore_list.map {|ignore_file| "#{self.local_path}/#{ignore_file}"} (list - list_ignore) end |
#list_commits(commit_sha1: nil) ⇒ Object
187 188 189 190 191 192 193 194 |
# File 'lib/cnvrg/dataset.rb', line 187 def list_commits(commit_sha1: nil) response = Cnvrg::API.request( "users/#{self.owner}/datasets/#{self.slug}/list_commits?commit=#{commit_sha1}", 'GET' ) CLI.is_response_success(response) return response end |
#list_files(commit_sha1: "latest", limit: 1000, offset: 0, expires: 3600) ⇒ Object
468 469 470 471 472 |
# File 'lib/cnvrg/dataset.rb', line 468 def list_files(commit_sha1: "latest", limit: 1000, offset: 0, expires: 3600) response = Cnvrg::API.request("users/#{self.owner}/datasets/#{self.slug}/list", 'GET', {commit_sha1: commit_sha1, limit: limit, offset: offset, expires: expires}) return nil if response.blank? response.to_json end |
#remove_next_commit ⇒ Object
789 790 791 792 793 794 795 796 797 798 |
# File 'lib/cnvrg/dataset.rb', line 789 def remove_next_commit() if !File.exist? "#{self.local_path}/.cnvrg/idx.yml" return nil end idx_hash = YAML.load_file("#{self.local_path}/.cnvrg/idx.yml") idx = Hash.new() idx[:commit] = idx_hash[:next_commit] idx[:tree] = idx_hash[:tree] File.open("#{self.local_path}/.cnvrg/idx.yml", 'w') {|f| f.write idx.to_yaml} end |
#restore_idx ⇒ Object
117 118 119 120 121 122 |
# File 'lib/cnvrg/dataset.rb', line 117 def restore_idx Cnvrg::Logger.log_info("Restore idx because an error.") Cnvrg::Logger.log_method(bind: binding) idx = YAML.load_file("#{self.local_path}/.cnvrg/idx.yml.backup") self.set_idx(idx) end |
#revert(working_dir) ⇒ Object
846 847 848 849 850 |
# File 'lib/cnvrg/dataset.rb', line 846 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 |
#revert_next_commit ⇒ Object
800 801 802 803 804 805 806 807 |
# File 'lib/cnvrg/dataset.rb', line 800 def revert_next_commit() if !File.exist? "#{self.local_path}/.cnvrg/idx.yml" return nil end idx_hash = YAML.load_file("#{self.local_path}/.cnvrg/idx.yml") idx_hash = idx_hash.except(:next_commit) File.open("#{self.local_path}/.cnvrg/idx.yml", 'w') {|f| f.write idx_hash.to_yaml} end |
#revert_to_last_commit(commit: nil) ⇒ Object
578 579 580 581 582 583 584 585 586 587 |
# File 'lib/cnvrg/dataset.rb', line 578 def revert_to_last_commit(commit: nil) if commit.blank? resp = Cnvrg::API.request("users/#{self.owner}/datasets/#{self.slug}/last_valid_commit", 'GET') if CLI.is_response_success(resp, false) commit = resp['result']['commit_sha1'] end end self.update_idx_with_commit(commit) if commit.present? self.revert_next_commit end |
#search_queries ⇒ Object
155 156 157 158 159 160 161 162 163 |
# File 'lib/cnvrg/dataset.rb', line 155 def search_queries response = Cnvrg::API.request("users/#{self.owner}/datasets/#{self.slug}/queries/list", 'GET') CLI.is_response_success(response) row = [["name", "id", "created_at", "username"]] response["results"]["queries"].each do |query| row << [query["name"], query["slug"], query["created_at"].in_time_zone.to_s, query["username"]] end return row end |
#send_ignore_list ⇒ Object
736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 |
# File 'lib/cnvrg/dataset.rb', line 736 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 |
#set_idx(idx) ⇒ Object
535 536 537 |
# File 'lib/cnvrg/dataset.rb', line 535 def set_idx(idx) File.open("#{self.local_path}/.cnvrg/idx.yml", 'w+') {|f| f.write idx.to_yaml} end |
#set_next_commit(commit_sha1) ⇒ Object
767 768 769 770 771 772 773 774 775 776 777 778 779 |
# File 'lib/cnvrg/dataset.rb', line 767 def set_next_commit(commit_sha1) if !File.exist? "#{self.local_path}/.cnvrg/idx.yml" idx_hash = Hash.new() idx_hash[:commit] = "" idx_hash[:tree] = "" else idx_hash = YAML.load_file("#{self.local_path}/.cnvrg/idx.yml") end idx_hash[:next_commit] = commit_sha1 File.open("#{self.local_path}/.cnvrg/idx.yml", 'w') {|f| f.write idx_hash.to_yaml} return true end |
#set_partial_commit(commit_sha1) ⇒ Object
718 719 720 721 722 |
# File 'lib/cnvrg/dataset.rb', line 718 def set_partial_commit(commit_sha1) idx = self.get_idx idx[:partial_commit] = commit_sha1 self.set_idx(idx) end |
#snapshot ⇒ Object
142 143 144 145 146 147 |
# File 'lib/cnvrg/dataset.rb', line 142 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 |
#soft_linked? ⇒ Boolean
37 38 39 |
# File 'lib/cnvrg/dataset.rb', line 37 def soft_linked? @dataset_call["dataset_type"] == "soft_link_dataset" end |
#softlinked? ⇒ Boolean
71 72 73 |
# File 'lib/cnvrg/dataset.rb', line 71 def softlinked? get_dataset["dataset_type"] == "soft_link_dataset" end |
#update_idx(idx) ⇒ Object
833 834 835 836 |
# File 'lib/cnvrg/dataset.rb', line 833 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
838 839 840 841 842 843 844 |
# File 'lib/cnvrg/dataset.rb', line 838 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
819 820 821 822 823 824 825 826 827 828 829 830 831 |
# File 'lib/cnvrg/dataset.rb', line 819 def update_idx_with_files_commits!(files, commit_time) # files.flatten! idx_hash = YAML.load_file("#{self.local_path}/.cnvrg/idx.yml") # idx_hash[:commit] = commit files.each do |path| idx_hash[:tree].to_h[path].to_h[:commit_time] = commit_time end idx_hash[:next_commit] = idx_hash[:next_commit] 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
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 |
# File 'lib/cnvrg/dataset.rb', line 233 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 |
#upload_tags_via_yml(tag_file = nil) ⇒ Object
196 197 198 199 200 201 202 203 204 205 |
# File 'lib/cnvrg/dataset.rb', line 196 def (tag_file = nil) records_yml = YAML.load_file(tag_file) tag_file.close response = Cnvrg::API.request("users/#{self.owner}/datasets/#{self.slug}/data_tags_create", 'POST', {records_yml: records_yml}) if response["status"] == 200 return true else return false end end |
#url ⇒ Object
207 208 209 210 |
# File 'lib/cnvrg/dataset.rb', line 207 def url url = Cnvrg::Helpers.remote_url "#{url}/#{self.owner}/projects/#{self.slug}" end |
#write_idx(tree = nil, commit = nil) ⇒ Object
599 600 601 602 603 604 605 606 |
# File 'lib/cnvrg/dataset.rb', line 599 def write_idx(tree = nil, commit = nil) if tree.nil? tree = self.generate_idx[:tree] tree = tree.map {|k, v| (v.present?) ? [k, {sha1: v[:sha1], commit_time: Time.now}] : [k, v]}.to_h end idx = {tree: tree, commit: commit} File.open("#{self.local_path}/.cnvrg/idx.yml", 'w') {|f| f.write idx.to_yaml} end |
#write_success(in_folder = false) ⇒ Object
494 495 496 497 498 499 500 |
# File 'lib/cnvrg/dataset.rb', line 494 def write_success(in_folder = false) file_path = ".cnvrg/config.yml" file_path = File.join(@local_path || @working_dir, file_path) if File.exist?(file_path) File.open(file_path, "a") {|f| f.puts(":success: true")} end end |
#write_tree(tree) ⇒ Object
608 609 610 611 612 |
# File 'lib/cnvrg/dataset.rb', line 608 def write_tree(tree) idx = self.get_idx idx[:tree] = tree self.set_idx(idx) end |