Class: Diggit::Dig
- Inherits:
-
Object
- Object
- Diggit::Dig
- Defined in:
- lib/dgit/core.rb
Overview
Constant Summary collapse
- DGIT_FOLDER =
".dgit"
Instance Attribute Summary collapse
-
#config ⇒ Config
readonly
The config.
-
#folder ⇒ String
readonly
The folder in which diggit is running.
-
#journal ⇒ Journal
readonly
The journal.
-
#options ⇒ Hash<String,Object>
readonly
The options.
-
#plugin_loader ⇒ PluginLoader
readonly
Utility classes to load plugins.
Class Method Summary collapse
-
.init(folder = '.') ⇒ Dig
Initialize and return the diggit instance into the given folder.
-
.init_dir(folder = '.') ⇒ void
Initialize a folder to be a diggit folder by creating an empty configuration.
-
.it ⇒ Dig
Returns the diggit instance.
Instance Method Summary collapse
-
#analyze(source_ids = [], analyses = [], mode = :run) ⇒ void
Perform the given analyses on sources with the given ids using the given mode.
-
#clone(*source_ids) ⇒ void
Clone the repository of all sources with the given source ids.
-
#config_path(name) ⇒ String
Return the path of the given config file.
-
#file_path(name) ⇒ String
Return the path of the given file in the diggit folder.
-
#initialize(folder) ⇒ Dig
constructor
Constructor.
-
#join(source_ids = [], joins = [], mode = :run) ⇒ void
Perform the given joins on sources with the given ids using the given mode.
-
#load_config ⇒ void
Load the config from
.dgit/config. -
#load_journal ⇒ void
Load the journal from
.dgit/journal. -
#load_options ⇒ void
Load the options from
.dgit/options. -
#save_config ⇒ void
Save the config to
.dgit/config. -
#save_journal ⇒ void
Save the journal to
.dgit/journal. -
#save_options ⇒ void
Save the options to
.dgit/options.
Constructor Details
#initialize(folder) ⇒ Dig
441 442 443 444 445 |
# File 'lib/dgit/core.rb', line 441 def initialize(folder) fail "Folder #{folder} is not a diggit folder." unless File.exist?(File.(DGIT_FOLDER, folder)) @plugin_loader = PluginLoader.instance @folder = folder end |
Instance Attribute Details
#config ⇒ Config (readonly)
Returns the config.
368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 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 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 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 |
# File 'lib/dgit/core.rb', line 368 class Dig DGIT_FOLDER = ".dgit" DGIT_SOURCES = "sources" DGIT_CONFIG = "config" DGIT_OPTIONS = "options" DGIT_JOURNAL = "journal" private_constant :DGIT_SOURCES, :DGIT_CONFIG, :DGIT_OPTIONS, :DGIT_JOURNAL attr_reader :config, :options, :journal, :plugin_loader, :folder @diggit = nil # Returns the diggit instance. # @return [Dig] the instance. def self.it fail "Diggit has not been initialized." if @diggit.nil? @diggit end # Initialize and return the diggit instance into the given folder. # @param folder the path to the folder. # @return [Dig] the instance. def self.init(folder = '.') @diggit = Dig.new(folder) @diggit. @diggit.load_config @diggit.load_journal @diggit end # Initialize a folder to be a diggit folder by creating an empty configuration. # It creates a +.dgit+ folder containing a +journal+, +config+, +options+ files. # It creates a +sources+ folder. # It creates a +plugins+ folder. # Directory creation is skipped if folder already exist. # @param folder the path to the folder. # @return [void] def self.init_dir(folder = '.') dgit_folder = File.(DGIT_FOLDER, folder) unless File.exist?(dgit_folder) FileUtils.mkdir(dgit_folder) Oj.to_file(File.(DGIT_CONFIG, dgit_folder), Config.empty_config) Oj.to_file(File.(DGIT_OPTIONS, dgit_folder), {}) FileUtils.touch(File.(DGIT_SOURCES, dgit_folder)) Oj.to_file(File.(DGIT_JOURNAL, dgit_folder), {}) end FileUtils.mkdir(File.('sources', folder)) unless File.exist?(File.('sources', folder)) unless File.exist?(File.("plugins", folder)) FileUtils.mkdir_p(File.("plugins", folder)) FileUtils.mkdir_p(File.("plugins/analysis", folder)) FileUtils.mkdir_p(File.("plugins/addon", folder)) FileUtils.mkdir_p(File.("plugins/join", folder)) end end # Return the path of the given config file # @param name [String] name of the file # @return [String] the path to the file. def config_path(name) File.(name, File.(DGIT_FOLDER, @folder)) end # Return the path of the given file in the diggit folder # @param name [String] name of the file # @return [String] the path to the file. def file_path(name) File.(name, @folder) end # Constructor. Should not be called directly. # Use {.init} and {.it} instead. # @return [Dig] a diggit object. def initialize(folder) fail "Folder #{folder} is not a diggit folder." unless File.exist?(File.(DGIT_FOLDER, folder)) @plugin_loader = PluginLoader.instance @folder = folder end # Load the journal from +.dgit/journal+ # @return [void] def load_journal url_array = [] IO.readlines(config_path(DGIT_SOURCES)).each { |l| url_array << l.strip } saved_hash = Oj.load_file(config_path(DGIT_JOURNAL)) hash = { urls: url_array, sources: saved_hash[:sources], workspace: saved_hash[:workspace] } @journal = Journal.new(hash) end # Save the journal to +.dgit/journal+ # @return [void] def save_journal hash = @journal.to_hash File.open(config_path(DGIT_SOURCES), "w") { |f| hash[:urls].each { |u| f.puts(u) } } Oj.to_file(config_path(DGIT_JOURNAL), { sources: hash[:sources], workspace: hash[:workspace] }) end # Load the options from +.dgit/options+ # @return [void] def = Oj.load_file(config_path(DGIT_OPTIONS)) end # Save the options to +.dgit/options+ # @return [void] def Oj.to_file(config_path(DGIT_OPTIONS), ) end # Load the config from +.dgit/config+ # @return [void] def load_config @config = Config.new(Oj.load_file(config_path(DGIT_CONFIG))) end # Save the config to +.dgit/config+ # @return [void] def save_config config_hash = @config.to_hash Oj.to_file(config_path(DGIT_CONFIG), config_hash) end # Clone the repository of all sources with the given source ids. # @param source_ids [Array<Integer>] the ids of the sources. # @return [void] def clone(*source_ids) @journal.sources_by_ids(*source_ids).select(&:new?).each(&:clone) ensure save_journal end # Perform the given analyses on sources with the given ids using the given mode. # @param source_ids [Array<Integer>] the ids of the sources. # @param analyses [Array<String>] the names of the analyses. # @param mode [Symbol] the mode: +:run+, +:rerun+ or +:clean+. # @return [void] def analyze(source_ids = [], analyses = [], mode = :run) @journal.sources_by_ids(*source_ids).select(&:cloned?).each do |s| @config.get_analyses(*analyses).each do |klass| a = klass.new() s.load_repository a.source = s clean_analysis(s, a) if clean_mode?(mode) && s.all_analyses.include?(a.name) run_analysis(s, a) if run_mode?(mode) && !s.performed_analyses.include?(a.name) end end end # Perform the given joins on sources with the given ids using the given mode. # @param source_ids [Array<Integer>] the ids of the sources. # @param joins [Array<String>] the names of the analyses. # @param mode [Symbol] the mode: +:run+, +:rerun+ or +:clean+. # @return [void] def join(source_ids = [], joins = [], mode = :run) @config.get_joins(*joins).each do |klass| j = klass.new() j.clean if clean_mode?(mode) source_array = @journal.sources_by_ids(*source_ids).select do |s| s.cloned? && (klass.required_analyses - s.performed_analyses).empty? end run_join(j, source_array) if run_mode?(mode) && !source_array.empty? end end private def clean_mode?(mode) mode == :rerun || mode == :clean end def run_mode?(mode) mode == :rerun || mode == :run end def clean_analysis(s, a) a.clean s.del_analysis(a.name) ensure save_journal end def run_analysis(s, a) s.ongoing_analyses << a.name a.run s.ongoing_analyses.pop s.performed_analyses << a.name rescue => e Log.error "Error applying analysis #{a.name} on #{s.url}" s.error = Journal.dump_error(e) ensure save_journal end def run_join(j, source_array) j.sources = source_array j.run @journal.add_join(j.name) rescue => e Log.error "Error applying join #{j.name}" @journal.join_error = e ensure save_journal end end |
#folder ⇒ String (readonly)
Returns the folder in which diggit is running.
368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 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 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 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 |
# File 'lib/dgit/core.rb', line 368 class Dig DGIT_FOLDER = ".dgit" DGIT_SOURCES = "sources" DGIT_CONFIG = "config" DGIT_OPTIONS = "options" DGIT_JOURNAL = "journal" private_constant :DGIT_SOURCES, :DGIT_CONFIG, :DGIT_OPTIONS, :DGIT_JOURNAL attr_reader :config, :options, :journal, :plugin_loader, :folder @diggit = nil # Returns the diggit instance. # @return [Dig] the instance. def self.it fail "Diggit has not been initialized." if @diggit.nil? @diggit end # Initialize and return the diggit instance into the given folder. # @param folder the path to the folder. # @return [Dig] the instance. def self.init(folder = '.') @diggit = Dig.new(folder) @diggit. @diggit.load_config @diggit.load_journal @diggit end # Initialize a folder to be a diggit folder by creating an empty configuration. # It creates a +.dgit+ folder containing a +journal+, +config+, +options+ files. # It creates a +sources+ folder. # It creates a +plugins+ folder. # Directory creation is skipped if folder already exist. # @param folder the path to the folder. # @return [void] def self.init_dir(folder = '.') dgit_folder = File.(DGIT_FOLDER, folder) unless File.exist?(dgit_folder) FileUtils.mkdir(dgit_folder) Oj.to_file(File.(DGIT_CONFIG, dgit_folder), Config.empty_config) Oj.to_file(File.(DGIT_OPTIONS, dgit_folder), {}) FileUtils.touch(File.(DGIT_SOURCES, dgit_folder)) Oj.to_file(File.(DGIT_JOURNAL, dgit_folder), {}) end FileUtils.mkdir(File.('sources', folder)) unless File.exist?(File.('sources', folder)) unless File.exist?(File.("plugins", folder)) FileUtils.mkdir_p(File.("plugins", folder)) FileUtils.mkdir_p(File.("plugins/analysis", folder)) FileUtils.mkdir_p(File.("plugins/addon", folder)) FileUtils.mkdir_p(File.("plugins/join", folder)) end end # Return the path of the given config file # @param name [String] name of the file # @return [String] the path to the file. def config_path(name) File.(name, File.(DGIT_FOLDER, @folder)) end # Return the path of the given file in the diggit folder # @param name [String] name of the file # @return [String] the path to the file. def file_path(name) File.(name, @folder) end # Constructor. Should not be called directly. # Use {.init} and {.it} instead. # @return [Dig] a diggit object. def initialize(folder) fail "Folder #{folder} is not a diggit folder." unless File.exist?(File.(DGIT_FOLDER, folder)) @plugin_loader = PluginLoader.instance @folder = folder end # Load the journal from +.dgit/journal+ # @return [void] def load_journal url_array = [] IO.readlines(config_path(DGIT_SOURCES)).each { |l| url_array << l.strip } saved_hash = Oj.load_file(config_path(DGIT_JOURNAL)) hash = { urls: url_array, sources: saved_hash[:sources], workspace: saved_hash[:workspace] } @journal = Journal.new(hash) end # Save the journal to +.dgit/journal+ # @return [void] def save_journal hash = @journal.to_hash File.open(config_path(DGIT_SOURCES), "w") { |f| hash[:urls].each { |u| f.puts(u) } } Oj.to_file(config_path(DGIT_JOURNAL), { sources: hash[:sources], workspace: hash[:workspace] }) end # Load the options from +.dgit/options+ # @return [void] def = Oj.load_file(config_path(DGIT_OPTIONS)) end # Save the options to +.dgit/options+ # @return [void] def Oj.to_file(config_path(DGIT_OPTIONS), ) end # Load the config from +.dgit/config+ # @return [void] def load_config @config = Config.new(Oj.load_file(config_path(DGIT_CONFIG))) end # Save the config to +.dgit/config+ # @return [void] def save_config config_hash = @config.to_hash Oj.to_file(config_path(DGIT_CONFIG), config_hash) end # Clone the repository of all sources with the given source ids. # @param source_ids [Array<Integer>] the ids of the sources. # @return [void] def clone(*source_ids) @journal.sources_by_ids(*source_ids).select(&:new?).each(&:clone) ensure save_journal end # Perform the given analyses on sources with the given ids using the given mode. # @param source_ids [Array<Integer>] the ids of the sources. # @param analyses [Array<String>] the names of the analyses. # @param mode [Symbol] the mode: +:run+, +:rerun+ or +:clean+. # @return [void] def analyze(source_ids = [], analyses = [], mode = :run) @journal.sources_by_ids(*source_ids).select(&:cloned?).each do |s| @config.get_analyses(*analyses).each do |klass| a = klass.new() s.load_repository a.source = s clean_analysis(s, a) if clean_mode?(mode) && s.all_analyses.include?(a.name) run_analysis(s, a) if run_mode?(mode) && !s.performed_analyses.include?(a.name) end end end # Perform the given joins on sources with the given ids using the given mode. # @param source_ids [Array<Integer>] the ids of the sources. # @param joins [Array<String>] the names of the analyses. # @param mode [Symbol] the mode: +:run+, +:rerun+ or +:clean+. # @return [void] def join(source_ids = [], joins = [], mode = :run) @config.get_joins(*joins).each do |klass| j = klass.new() j.clean if clean_mode?(mode) source_array = @journal.sources_by_ids(*source_ids).select do |s| s.cloned? && (klass.required_analyses - s.performed_analyses).empty? end run_join(j, source_array) if run_mode?(mode) && !source_array.empty? end end private def clean_mode?(mode) mode == :rerun || mode == :clean end def run_mode?(mode) mode == :rerun || mode == :run end def clean_analysis(s, a) a.clean s.del_analysis(a.name) ensure save_journal end def run_analysis(s, a) s.ongoing_analyses << a.name a.run s.ongoing_analyses.pop s.performed_analyses << a.name rescue => e Log.error "Error applying analysis #{a.name} on #{s.url}" s.error = Journal.dump_error(e) ensure save_journal end def run_join(j, source_array) j.sources = source_array j.run @journal.add_join(j.name) rescue => e Log.error "Error applying join #{j.name}" @journal.join_error = e ensure save_journal end end |
#journal ⇒ Journal (readonly)
Returns the journal.
368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 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 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 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 |
# File 'lib/dgit/core.rb', line 368 class Dig DGIT_FOLDER = ".dgit" DGIT_SOURCES = "sources" DGIT_CONFIG = "config" DGIT_OPTIONS = "options" DGIT_JOURNAL = "journal" private_constant :DGIT_SOURCES, :DGIT_CONFIG, :DGIT_OPTIONS, :DGIT_JOURNAL attr_reader :config, :options, :journal, :plugin_loader, :folder @diggit = nil # Returns the diggit instance. # @return [Dig] the instance. def self.it fail "Diggit has not been initialized." if @diggit.nil? @diggit end # Initialize and return the diggit instance into the given folder. # @param folder the path to the folder. # @return [Dig] the instance. def self.init(folder = '.') @diggit = Dig.new(folder) @diggit. @diggit.load_config @diggit.load_journal @diggit end # Initialize a folder to be a diggit folder by creating an empty configuration. # It creates a +.dgit+ folder containing a +journal+, +config+, +options+ files. # It creates a +sources+ folder. # It creates a +plugins+ folder. # Directory creation is skipped if folder already exist. # @param folder the path to the folder. # @return [void] def self.init_dir(folder = '.') dgit_folder = File.(DGIT_FOLDER, folder) unless File.exist?(dgit_folder) FileUtils.mkdir(dgit_folder) Oj.to_file(File.(DGIT_CONFIG, dgit_folder), Config.empty_config) Oj.to_file(File.(DGIT_OPTIONS, dgit_folder), {}) FileUtils.touch(File.(DGIT_SOURCES, dgit_folder)) Oj.to_file(File.(DGIT_JOURNAL, dgit_folder), {}) end FileUtils.mkdir(File.('sources', folder)) unless File.exist?(File.('sources', folder)) unless File.exist?(File.("plugins", folder)) FileUtils.mkdir_p(File.("plugins", folder)) FileUtils.mkdir_p(File.("plugins/analysis", folder)) FileUtils.mkdir_p(File.("plugins/addon", folder)) FileUtils.mkdir_p(File.("plugins/join", folder)) end end # Return the path of the given config file # @param name [String] name of the file # @return [String] the path to the file. def config_path(name) File.(name, File.(DGIT_FOLDER, @folder)) end # Return the path of the given file in the diggit folder # @param name [String] name of the file # @return [String] the path to the file. def file_path(name) File.(name, @folder) end # Constructor. Should not be called directly. # Use {.init} and {.it} instead. # @return [Dig] a diggit object. def initialize(folder) fail "Folder #{folder} is not a diggit folder." unless File.exist?(File.(DGIT_FOLDER, folder)) @plugin_loader = PluginLoader.instance @folder = folder end # Load the journal from +.dgit/journal+ # @return [void] def load_journal url_array = [] IO.readlines(config_path(DGIT_SOURCES)).each { |l| url_array << l.strip } saved_hash = Oj.load_file(config_path(DGIT_JOURNAL)) hash = { urls: url_array, sources: saved_hash[:sources], workspace: saved_hash[:workspace] } @journal = Journal.new(hash) end # Save the journal to +.dgit/journal+ # @return [void] def save_journal hash = @journal.to_hash File.open(config_path(DGIT_SOURCES), "w") { |f| hash[:urls].each { |u| f.puts(u) } } Oj.to_file(config_path(DGIT_JOURNAL), { sources: hash[:sources], workspace: hash[:workspace] }) end # Load the options from +.dgit/options+ # @return [void] def = Oj.load_file(config_path(DGIT_OPTIONS)) end # Save the options to +.dgit/options+ # @return [void] def Oj.to_file(config_path(DGIT_OPTIONS), ) end # Load the config from +.dgit/config+ # @return [void] def load_config @config = Config.new(Oj.load_file(config_path(DGIT_CONFIG))) end # Save the config to +.dgit/config+ # @return [void] def save_config config_hash = @config.to_hash Oj.to_file(config_path(DGIT_CONFIG), config_hash) end # Clone the repository of all sources with the given source ids. # @param source_ids [Array<Integer>] the ids of the sources. # @return [void] def clone(*source_ids) @journal.sources_by_ids(*source_ids).select(&:new?).each(&:clone) ensure save_journal end # Perform the given analyses on sources with the given ids using the given mode. # @param source_ids [Array<Integer>] the ids of the sources. # @param analyses [Array<String>] the names of the analyses. # @param mode [Symbol] the mode: +:run+, +:rerun+ or +:clean+. # @return [void] def analyze(source_ids = [], analyses = [], mode = :run) @journal.sources_by_ids(*source_ids).select(&:cloned?).each do |s| @config.get_analyses(*analyses).each do |klass| a = klass.new() s.load_repository a.source = s clean_analysis(s, a) if clean_mode?(mode) && s.all_analyses.include?(a.name) run_analysis(s, a) if run_mode?(mode) && !s.performed_analyses.include?(a.name) end end end # Perform the given joins on sources with the given ids using the given mode. # @param source_ids [Array<Integer>] the ids of the sources. # @param joins [Array<String>] the names of the analyses. # @param mode [Symbol] the mode: +:run+, +:rerun+ or +:clean+. # @return [void] def join(source_ids = [], joins = [], mode = :run) @config.get_joins(*joins).each do |klass| j = klass.new() j.clean if clean_mode?(mode) source_array = @journal.sources_by_ids(*source_ids).select do |s| s.cloned? && (klass.required_analyses - s.performed_analyses).empty? end run_join(j, source_array) if run_mode?(mode) && !source_array.empty? end end private def clean_mode?(mode) mode == :rerun || mode == :clean end def run_mode?(mode) mode == :rerun || mode == :run end def clean_analysis(s, a) a.clean s.del_analysis(a.name) ensure save_journal end def run_analysis(s, a) s.ongoing_analyses << a.name a.run s.ongoing_analyses.pop s.performed_analyses << a.name rescue => e Log.error "Error applying analysis #{a.name} on #{s.url}" s.error = Journal.dump_error(e) ensure save_journal end def run_join(j, source_array) j.sources = source_array j.run @journal.add_join(j.name) rescue => e Log.error "Error applying join #{j.name}" @journal.join_error = e ensure save_journal end end |
#options ⇒ Hash<String,Object> (readonly)
Returns the options.
368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 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 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 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 |
# File 'lib/dgit/core.rb', line 368 class Dig DGIT_FOLDER = ".dgit" DGIT_SOURCES = "sources" DGIT_CONFIG = "config" DGIT_OPTIONS = "options" DGIT_JOURNAL = "journal" private_constant :DGIT_SOURCES, :DGIT_CONFIG, :DGIT_OPTIONS, :DGIT_JOURNAL attr_reader :config, :options, :journal, :plugin_loader, :folder @diggit = nil # Returns the diggit instance. # @return [Dig] the instance. def self.it fail "Diggit has not been initialized." if @diggit.nil? @diggit end # Initialize and return the diggit instance into the given folder. # @param folder the path to the folder. # @return [Dig] the instance. def self.init(folder = '.') @diggit = Dig.new(folder) @diggit. @diggit.load_config @diggit.load_journal @diggit end # Initialize a folder to be a diggit folder by creating an empty configuration. # It creates a +.dgit+ folder containing a +journal+, +config+, +options+ files. # It creates a +sources+ folder. # It creates a +plugins+ folder. # Directory creation is skipped if folder already exist. # @param folder the path to the folder. # @return [void] def self.init_dir(folder = '.') dgit_folder = File.(DGIT_FOLDER, folder) unless File.exist?(dgit_folder) FileUtils.mkdir(dgit_folder) Oj.to_file(File.(DGIT_CONFIG, dgit_folder), Config.empty_config) Oj.to_file(File.(DGIT_OPTIONS, dgit_folder), {}) FileUtils.touch(File.(DGIT_SOURCES, dgit_folder)) Oj.to_file(File.(DGIT_JOURNAL, dgit_folder), {}) end FileUtils.mkdir(File.('sources', folder)) unless File.exist?(File.('sources', folder)) unless File.exist?(File.("plugins", folder)) FileUtils.mkdir_p(File.("plugins", folder)) FileUtils.mkdir_p(File.("plugins/analysis", folder)) FileUtils.mkdir_p(File.("plugins/addon", folder)) FileUtils.mkdir_p(File.("plugins/join", folder)) end end # Return the path of the given config file # @param name [String] name of the file # @return [String] the path to the file. def config_path(name) File.(name, File.(DGIT_FOLDER, @folder)) end # Return the path of the given file in the diggit folder # @param name [String] name of the file # @return [String] the path to the file. def file_path(name) File.(name, @folder) end # Constructor. Should not be called directly. # Use {.init} and {.it} instead. # @return [Dig] a diggit object. def initialize(folder) fail "Folder #{folder} is not a diggit folder." unless File.exist?(File.(DGIT_FOLDER, folder)) @plugin_loader = PluginLoader.instance @folder = folder end # Load the journal from +.dgit/journal+ # @return [void] def load_journal url_array = [] IO.readlines(config_path(DGIT_SOURCES)).each { |l| url_array << l.strip } saved_hash = Oj.load_file(config_path(DGIT_JOURNAL)) hash = { urls: url_array, sources: saved_hash[:sources], workspace: saved_hash[:workspace] } @journal = Journal.new(hash) end # Save the journal to +.dgit/journal+ # @return [void] def save_journal hash = @journal.to_hash File.open(config_path(DGIT_SOURCES), "w") { |f| hash[:urls].each { |u| f.puts(u) } } Oj.to_file(config_path(DGIT_JOURNAL), { sources: hash[:sources], workspace: hash[:workspace] }) end # Load the options from +.dgit/options+ # @return [void] def = Oj.load_file(config_path(DGIT_OPTIONS)) end # Save the options to +.dgit/options+ # @return [void] def Oj.to_file(config_path(DGIT_OPTIONS), ) end # Load the config from +.dgit/config+ # @return [void] def load_config @config = Config.new(Oj.load_file(config_path(DGIT_CONFIG))) end # Save the config to +.dgit/config+ # @return [void] def save_config config_hash = @config.to_hash Oj.to_file(config_path(DGIT_CONFIG), config_hash) end # Clone the repository of all sources with the given source ids. # @param source_ids [Array<Integer>] the ids of the sources. # @return [void] def clone(*source_ids) @journal.sources_by_ids(*source_ids).select(&:new?).each(&:clone) ensure save_journal end # Perform the given analyses on sources with the given ids using the given mode. # @param source_ids [Array<Integer>] the ids of the sources. # @param analyses [Array<String>] the names of the analyses. # @param mode [Symbol] the mode: +:run+, +:rerun+ or +:clean+. # @return [void] def analyze(source_ids = [], analyses = [], mode = :run) @journal.sources_by_ids(*source_ids).select(&:cloned?).each do |s| @config.get_analyses(*analyses).each do |klass| a = klass.new() s.load_repository a.source = s clean_analysis(s, a) if clean_mode?(mode) && s.all_analyses.include?(a.name) run_analysis(s, a) if run_mode?(mode) && !s.performed_analyses.include?(a.name) end end end # Perform the given joins on sources with the given ids using the given mode. # @param source_ids [Array<Integer>] the ids of the sources. # @param joins [Array<String>] the names of the analyses. # @param mode [Symbol] the mode: +:run+, +:rerun+ or +:clean+. # @return [void] def join(source_ids = [], joins = [], mode = :run) @config.get_joins(*joins).each do |klass| j = klass.new() j.clean if clean_mode?(mode) source_array = @journal.sources_by_ids(*source_ids).select do |s| s.cloned? && (klass.required_analyses - s.performed_analyses).empty? end run_join(j, source_array) if run_mode?(mode) && !source_array.empty? end end private def clean_mode?(mode) mode == :rerun || mode == :clean end def run_mode?(mode) mode == :rerun || mode == :run end def clean_analysis(s, a) a.clean s.del_analysis(a.name) ensure save_journal end def run_analysis(s, a) s.ongoing_analyses << a.name a.run s.ongoing_analyses.pop s.performed_analyses << a.name rescue => e Log.error "Error applying analysis #{a.name} on #{s.url}" s.error = Journal.dump_error(e) ensure save_journal end def run_join(j, source_array) j.sources = source_array j.run @journal.add_join(j.name) rescue => e Log.error "Error applying join #{j.name}" @journal.join_error = e ensure save_journal end end |
#plugin_loader ⇒ PluginLoader (readonly)
Returns utility classes to load plugins.
368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 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 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 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 |
# File 'lib/dgit/core.rb', line 368 class Dig DGIT_FOLDER = ".dgit" DGIT_SOURCES = "sources" DGIT_CONFIG = "config" DGIT_OPTIONS = "options" DGIT_JOURNAL = "journal" private_constant :DGIT_SOURCES, :DGIT_CONFIG, :DGIT_OPTIONS, :DGIT_JOURNAL attr_reader :config, :options, :journal, :plugin_loader, :folder @diggit = nil # Returns the diggit instance. # @return [Dig] the instance. def self.it fail "Diggit has not been initialized." if @diggit.nil? @diggit end # Initialize and return the diggit instance into the given folder. # @param folder the path to the folder. # @return [Dig] the instance. def self.init(folder = '.') @diggit = Dig.new(folder) @diggit. @diggit.load_config @diggit.load_journal @diggit end # Initialize a folder to be a diggit folder by creating an empty configuration. # It creates a +.dgit+ folder containing a +journal+, +config+, +options+ files. # It creates a +sources+ folder. # It creates a +plugins+ folder. # Directory creation is skipped if folder already exist. # @param folder the path to the folder. # @return [void] def self.init_dir(folder = '.') dgit_folder = File.(DGIT_FOLDER, folder) unless File.exist?(dgit_folder) FileUtils.mkdir(dgit_folder) Oj.to_file(File.(DGIT_CONFIG, dgit_folder), Config.empty_config) Oj.to_file(File.(DGIT_OPTIONS, dgit_folder), {}) FileUtils.touch(File.(DGIT_SOURCES, dgit_folder)) Oj.to_file(File.(DGIT_JOURNAL, dgit_folder), {}) end FileUtils.mkdir(File.('sources', folder)) unless File.exist?(File.('sources', folder)) unless File.exist?(File.("plugins", folder)) FileUtils.mkdir_p(File.("plugins", folder)) FileUtils.mkdir_p(File.("plugins/analysis", folder)) FileUtils.mkdir_p(File.("plugins/addon", folder)) FileUtils.mkdir_p(File.("plugins/join", folder)) end end # Return the path of the given config file # @param name [String] name of the file # @return [String] the path to the file. def config_path(name) File.(name, File.(DGIT_FOLDER, @folder)) end # Return the path of the given file in the diggit folder # @param name [String] name of the file # @return [String] the path to the file. def file_path(name) File.(name, @folder) end # Constructor. Should not be called directly. # Use {.init} and {.it} instead. # @return [Dig] a diggit object. def initialize(folder) fail "Folder #{folder} is not a diggit folder." unless File.exist?(File.(DGIT_FOLDER, folder)) @plugin_loader = PluginLoader.instance @folder = folder end # Load the journal from +.dgit/journal+ # @return [void] def load_journal url_array = [] IO.readlines(config_path(DGIT_SOURCES)).each { |l| url_array << l.strip } saved_hash = Oj.load_file(config_path(DGIT_JOURNAL)) hash = { urls: url_array, sources: saved_hash[:sources], workspace: saved_hash[:workspace] } @journal = Journal.new(hash) end # Save the journal to +.dgit/journal+ # @return [void] def save_journal hash = @journal.to_hash File.open(config_path(DGIT_SOURCES), "w") { |f| hash[:urls].each { |u| f.puts(u) } } Oj.to_file(config_path(DGIT_JOURNAL), { sources: hash[:sources], workspace: hash[:workspace] }) end # Load the options from +.dgit/options+ # @return [void] def = Oj.load_file(config_path(DGIT_OPTIONS)) end # Save the options to +.dgit/options+ # @return [void] def Oj.to_file(config_path(DGIT_OPTIONS), ) end # Load the config from +.dgit/config+ # @return [void] def load_config @config = Config.new(Oj.load_file(config_path(DGIT_CONFIG))) end # Save the config to +.dgit/config+ # @return [void] def save_config config_hash = @config.to_hash Oj.to_file(config_path(DGIT_CONFIG), config_hash) end # Clone the repository of all sources with the given source ids. # @param source_ids [Array<Integer>] the ids of the sources. # @return [void] def clone(*source_ids) @journal.sources_by_ids(*source_ids).select(&:new?).each(&:clone) ensure save_journal end # Perform the given analyses on sources with the given ids using the given mode. # @param source_ids [Array<Integer>] the ids of the sources. # @param analyses [Array<String>] the names of the analyses. # @param mode [Symbol] the mode: +:run+, +:rerun+ or +:clean+. # @return [void] def analyze(source_ids = [], analyses = [], mode = :run) @journal.sources_by_ids(*source_ids).select(&:cloned?).each do |s| @config.get_analyses(*analyses).each do |klass| a = klass.new() s.load_repository a.source = s clean_analysis(s, a) if clean_mode?(mode) && s.all_analyses.include?(a.name) run_analysis(s, a) if run_mode?(mode) && !s.performed_analyses.include?(a.name) end end end # Perform the given joins on sources with the given ids using the given mode. # @param source_ids [Array<Integer>] the ids of the sources. # @param joins [Array<String>] the names of the analyses. # @param mode [Symbol] the mode: +:run+, +:rerun+ or +:clean+. # @return [void] def join(source_ids = [], joins = [], mode = :run) @config.get_joins(*joins).each do |klass| j = klass.new() j.clean if clean_mode?(mode) source_array = @journal.sources_by_ids(*source_ids).select do |s| s.cloned? && (klass.required_analyses - s.performed_analyses).empty? end run_join(j, source_array) if run_mode?(mode) && !source_array.empty? end end private def clean_mode?(mode) mode == :rerun || mode == :clean end def run_mode?(mode) mode == :rerun || mode == :run end def clean_analysis(s, a) a.clean s.del_analysis(a.name) ensure save_journal end def run_analysis(s, a) s.ongoing_analyses << a.name a.run s.ongoing_analyses.pop s.performed_analyses << a.name rescue => e Log.error "Error applying analysis #{a.name} on #{s.url}" s.error = Journal.dump_error(e) ensure save_journal end def run_join(j, source_array) j.sources = source_array j.run @journal.add_join(j.name) rescue => e Log.error "Error applying join #{j.name}" @journal.join_error = e ensure save_journal end end |
Class Method Details
.init(folder = '.') ⇒ Dig
Initialize and return the diggit instance into the given folder.
391 392 393 394 395 396 397 |
# File 'lib/dgit/core.rb', line 391 def self.init(folder = '.') @diggit = Dig.new(folder) @diggit. @diggit.load_config @diggit.load_journal @diggit end |
.init_dir(folder = '.') ⇒ void
This method returns an undefined value.
Initialize a folder to be a diggit folder by creating an empty configuration. It creates a .dgit folder containing a journal, config, options files. It creates a sources folder. It creates a plugins folder. Directory creation is skipped if folder already exist.
406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 |
# File 'lib/dgit/core.rb', line 406 def self.init_dir(folder = '.') dgit_folder = File.(DGIT_FOLDER, folder) unless File.exist?(dgit_folder) FileUtils.mkdir(dgit_folder) Oj.to_file(File.(DGIT_CONFIG, dgit_folder), Config.empty_config) Oj.to_file(File.(DGIT_OPTIONS, dgit_folder), {}) FileUtils.touch(File.(DGIT_SOURCES, dgit_folder)) Oj.to_file(File.(DGIT_JOURNAL, dgit_folder), {}) end FileUtils.mkdir(File.('sources', folder)) unless File.exist?(File.('sources', folder)) unless File.exist?(File.("plugins", folder)) FileUtils.mkdir_p(File.("plugins", folder)) FileUtils.mkdir_p(File.("plugins/analysis", folder)) FileUtils.mkdir_p(File.("plugins/addon", folder)) FileUtils.mkdir_p(File.("plugins/join", folder)) end end |
.it ⇒ Dig
Returns the diggit instance.
383 384 385 386 |
# File 'lib/dgit/core.rb', line 383 def self.it fail "Diggit has not been initialized." if @diggit.nil? @diggit end |
Instance Method Details
#analyze(source_ids = [], analyses = [], mode = :run) ⇒ void
This method returns an undefined value.
Perform the given analyses on sources with the given ids using the given mode.
504 505 506 507 508 509 510 511 512 513 514 |
# File 'lib/dgit/core.rb', line 504 def analyze(source_ids = [], analyses = [], mode = :run) @journal.sources_by_ids(*source_ids).select(&:cloned?).each do |s| @config.get_analyses(*analyses).each do |klass| a = klass.new() s.load_repository a.source = s clean_analysis(s, a) if clean_mode?(mode) && s.all_analyses.include?(a.name) run_analysis(s, a) if run_mode?(mode) && !s.performed_analyses.include?(a.name) end end end |
#clone(*source_ids) ⇒ void
This method returns an undefined value.
Clone the repository of all sources with the given source ids.
493 494 495 496 497 |
# File 'lib/dgit/core.rb', line 493 def clone(*source_ids) @journal.sources_by_ids(*source_ids).select(&:new?).each(&:clone) ensure save_journal end |
#config_path(name) ⇒ String
Return the path of the given config file
427 428 429 |
# File 'lib/dgit/core.rb', line 427 def config_path(name) File.(name, File.(DGIT_FOLDER, @folder)) end |
#file_path(name) ⇒ String
Return the path of the given file in the diggit folder
434 435 436 |
# File 'lib/dgit/core.rb', line 434 def file_path(name) File.(name, @folder) end |
#join(source_ids = [], joins = [], mode = :run) ⇒ void
This method returns an undefined value.
Perform the given joins on sources with the given ids using the given mode.
521 522 523 524 525 526 527 528 529 530 |
# File 'lib/dgit/core.rb', line 521 def join(source_ids = [], joins = [], mode = :run) @config.get_joins(*joins).each do |klass| j = klass.new() j.clean if clean_mode?(mode) source_array = @journal.sources_by_ids(*source_ids).select do |s| s.cloned? && (klass.required_analyses - s.performed_analyses).empty? end run_join(j, source_array) if run_mode?(mode) && !source_array.empty? end end |
#load_config ⇒ void
This method returns an undefined value.
Load the config from .dgit/config
479 480 481 |
# File 'lib/dgit/core.rb', line 479 def load_config @config = Config.new(Oj.load_file(config_path(DGIT_CONFIG))) end |
#load_journal ⇒ void
This method returns an undefined value.
Load the journal from .dgit/journal
449 450 451 452 453 454 455 |
# File 'lib/dgit/core.rb', line 449 def load_journal url_array = [] IO.readlines(config_path(DGIT_SOURCES)).each { |l| url_array << l.strip } saved_hash = Oj.load_file(config_path(DGIT_JOURNAL)) hash = { urls: url_array, sources: saved_hash[:sources], workspace: saved_hash[:workspace] } @journal = Journal.new(hash) end |
#load_options ⇒ void
This method returns an undefined value.
Load the options from .dgit/options
467 468 469 |
# File 'lib/dgit/core.rb', line 467 def = Oj.load_file(config_path(DGIT_OPTIONS)) end |
#save_config ⇒ void
This method returns an undefined value.
Save the config to .dgit/config
485 486 487 488 |
# File 'lib/dgit/core.rb', line 485 def save_config config_hash = @config.to_hash Oj.to_file(config_path(DGIT_CONFIG), config_hash) end |
#save_journal ⇒ void
This method returns an undefined value.
Save the journal to .dgit/journal
459 460 461 462 463 |
# File 'lib/dgit/core.rb', line 459 def save_journal hash = @journal.to_hash File.open(config_path(DGIT_SOURCES), "w") { |f| hash[:urls].each { |u| f.puts(u) } } Oj.to_file(config_path(DGIT_JOURNAL), { sources: hash[:sources], workspace: hash[:workspace] }) end |
#save_options ⇒ void
This method returns an undefined value.
Save the options to .dgit/options
473 474 475 |
# File 'lib/dgit/core.rb', line 473 def Oj.to_file(config_path(DGIT_OPTIONS), ) end |