Class: Diggit::Dig

Inherits:
Object
  • Object
show all
Defined in:
lib/dgit/core.rb

Overview

Main diggit class. It must be runned in a folder containing a .dgit folder with a proper configuration. Access configuration, options, sources and journal from this object. It implements the singleton pattern. You can initialize it via Dig.init and retrieve the instance via Dig.it.

Constant Summary collapse

DGIT_FOLDER =
".dgit"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(folder) ⇒ Dig

Constructor. Should not be called directly. Use init and it instead.



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.expand_path(DGIT_FOLDER, folder))
  @plugin_loader = PluginLoader.instance
  @folder = folder
end

Instance Attribute Details

#configConfig (readonly)

Returns the config.

Returns:



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.load_options
    @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.expand_path(DGIT_FOLDER, folder)
    unless File.exist?(dgit_folder)
      FileUtils.mkdir(dgit_folder)
      Oj.to_file(File.expand_path(DGIT_CONFIG, dgit_folder), Config.empty_config)
      Oj.to_file(File.expand_path(DGIT_OPTIONS, dgit_folder), {})
      FileUtils.touch(File.expand_path(DGIT_SOURCES, dgit_folder))
      Oj.to_file(File.expand_path(DGIT_JOURNAL, dgit_folder), {})
    end
    FileUtils.mkdir(File.expand_path('sources', folder)) unless File.exist?(File.expand_path('sources', folder))
    unless File.exist?(File.expand_path("plugins", folder))
      FileUtils.mkdir_p(File.expand_path("plugins", folder))
      FileUtils.mkdir_p(File.expand_path("plugins/analysis", folder))
      FileUtils.mkdir_p(File.expand_path("plugins/addon", folder))
      FileUtils.mkdir_p(File.expand_path("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.expand_path(name, File.expand_path(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.expand_path(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.expand_path(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 load_options
    @options = Oj.load_file(config_path(DGIT_OPTIONS))
  end

  # Save the options to +.dgit/options+
  # @return [void]
  def save_options
    Oj.to_file(config_path(DGIT_OPTIONS), 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(@options)
        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(@options)
      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

#folderString (readonly)

Returns the folder in which diggit is running.

Returns:

  • (String)

    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.load_options
    @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.expand_path(DGIT_FOLDER, folder)
    unless File.exist?(dgit_folder)
      FileUtils.mkdir(dgit_folder)
      Oj.to_file(File.expand_path(DGIT_CONFIG, dgit_folder), Config.empty_config)
      Oj.to_file(File.expand_path(DGIT_OPTIONS, dgit_folder), {})
      FileUtils.touch(File.expand_path(DGIT_SOURCES, dgit_folder))
      Oj.to_file(File.expand_path(DGIT_JOURNAL, dgit_folder), {})
    end
    FileUtils.mkdir(File.expand_path('sources', folder)) unless File.exist?(File.expand_path('sources', folder))
    unless File.exist?(File.expand_path("plugins", folder))
      FileUtils.mkdir_p(File.expand_path("plugins", folder))
      FileUtils.mkdir_p(File.expand_path("plugins/analysis", folder))
      FileUtils.mkdir_p(File.expand_path("plugins/addon", folder))
      FileUtils.mkdir_p(File.expand_path("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.expand_path(name, File.expand_path(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.expand_path(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.expand_path(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 load_options
    @options = Oj.load_file(config_path(DGIT_OPTIONS))
  end

  # Save the options to +.dgit/options+
  # @return [void]
  def save_options
    Oj.to_file(config_path(DGIT_OPTIONS), 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(@options)
        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(@options)
      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

#journalJournal (readonly)

Returns the journal.

Returns:



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.load_options
    @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.expand_path(DGIT_FOLDER, folder)
    unless File.exist?(dgit_folder)
      FileUtils.mkdir(dgit_folder)
      Oj.to_file(File.expand_path(DGIT_CONFIG, dgit_folder), Config.empty_config)
      Oj.to_file(File.expand_path(DGIT_OPTIONS, dgit_folder), {})
      FileUtils.touch(File.expand_path(DGIT_SOURCES, dgit_folder))
      Oj.to_file(File.expand_path(DGIT_JOURNAL, dgit_folder), {})
    end
    FileUtils.mkdir(File.expand_path('sources', folder)) unless File.exist?(File.expand_path('sources', folder))
    unless File.exist?(File.expand_path("plugins", folder))
      FileUtils.mkdir_p(File.expand_path("plugins", folder))
      FileUtils.mkdir_p(File.expand_path("plugins/analysis", folder))
      FileUtils.mkdir_p(File.expand_path("plugins/addon", folder))
      FileUtils.mkdir_p(File.expand_path("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.expand_path(name, File.expand_path(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.expand_path(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.expand_path(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 load_options
    @options = Oj.load_file(config_path(DGIT_OPTIONS))
  end

  # Save the options to +.dgit/options+
  # @return [void]
  def save_options
    Oj.to_file(config_path(DGIT_OPTIONS), 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(@options)
        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(@options)
      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

#optionsHash<String,Object> (readonly)

Returns the options.

Returns:

  • (Hash<String,Object>)

    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.load_options
    @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.expand_path(DGIT_FOLDER, folder)
    unless File.exist?(dgit_folder)
      FileUtils.mkdir(dgit_folder)
      Oj.to_file(File.expand_path(DGIT_CONFIG, dgit_folder), Config.empty_config)
      Oj.to_file(File.expand_path(DGIT_OPTIONS, dgit_folder), {})
      FileUtils.touch(File.expand_path(DGIT_SOURCES, dgit_folder))
      Oj.to_file(File.expand_path(DGIT_JOURNAL, dgit_folder), {})
    end
    FileUtils.mkdir(File.expand_path('sources', folder)) unless File.exist?(File.expand_path('sources', folder))
    unless File.exist?(File.expand_path("plugins", folder))
      FileUtils.mkdir_p(File.expand_path("plugins", folder))
      FileUtils.mkdir_p(File.expand_path("plugins/analysis", folder))
      FileUtils.mkdir_p(File.expand_path("plugins/addon", folder))
      FileUtils.mkdir_p(File.expand_path("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.expand_path(name, File.expand_path(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.expand_path(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.expand_path(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 load_options
    @options = Oj.load_file(config_path(DGIT_OPTIONS))
  end

  # Save the options to +.dgit/options+
  # @return [void]
  def save_options
    Oj.to_file(config_path(DGIT_OPTIONS), 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(@options)
        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(@options)
      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_loaderPluginLoader (readonly)

Returns utility classes to load plugins.

Returns:



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.load_options
    @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.expand_path(DGIT_FOLDER, folder)
    unless File.exist?(dgit_folder)
      FileUtils.mkdir(dgit_folder)
      Oj.to_file(File.expand_path(DGIT_CONFIG, dgit_folder), Config.empty_config)
      Oj.to_file(File.expand_path(DGIT_OPTIONS, dgit_folder), {})
      FileUtils.touch(File.expand_path(DGIT_SOURCES, dgit_folder))
      Oj.to_file(File.expand_path(DGIT_JOURNAL, dgit_folder), {})
    end
    FileUtils.mkdir(File.expand_path('sources', folder)) unless File.exist?(File.expand_path('sources', folder))
    unless File.exist?(File.expand_path("plugins", folder))
      FileUtils.mkdir_p(File.expand_path("plugins", folder))
      FileUtils.mkdir_p(File.expand_path("plugins/analysis", folder))
      FileUtils.mkdir_p(File.expand_path("plugins/addon", folder))
      FileUtils.mkdir_p(File.expand_path("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.expand_path(name, File.expand_path(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.expand_path(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.expand_path(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 load_options
    @options = Oj.load_file(config_path(DGIT_OPTIONS))
  end

  # Save the options to +.dgit/options+
  # @return [void]
  def save_options
    Oj.to_file(config_path(DGIT_OPTIONS), 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(@options)
        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(@options)
      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.

Parameters:

  • folder (defaults to: '.')

    the path to the folder.

Returns:

  • (Dig)

    the instance.



391
392
393
394
395
396
397
# File 'lib/dgit/core.rb', line 391

def self.init(folder = '.')
  @diggit = Dig.new(folder)
  @diggit.load_options
  @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.

Parameters:

  • folder (defaults to: '.')

    the path to the folder.



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.expand_path(DGIT_FOLDER, folder)
  unless File.exist?(dgit_folder)
    FileUtils.mkdir(dgit_folder)
    Oj.to_file(File.expand_path(DGIT_CONFIG, dgit_folder), Config.empty_config)
    Oj.to_file(File.expand_path(DGIT_OPTIONS, dgit_folder), {})
    FileUtils.touch(File.expand_path(DGIT_SOURCES, dgit_folder))
    Oj.to_file(File.expand_path(DGIT_JOURNAL, dgit_folder), {})
  end
  FileUtils.mkdir(File.expand_path('sources', folder)) unless File.exist?(File.expand_path('sources', folder))
  unless File.exist?(File.expand_path("plugins", folder))
    FileUtils.mkdir_p(File.expand_path("plugins", folder))
    FileUtils.mkdir_p(File.expand_path("plugins/analysis", folder))
    FileUtils.mkdir_p(File.expand_path("plugins/addon", folder))
    FileUtils.mkdir_p(File.expand_path("plugins/join", folder))
  end
end

.itDig

Returns the diggit instance.

Returns:

  • (Dig)

    the 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.

Parameters:

  • source_ids (Array<Integer>) (defaults to: [])

    the ids of the sources.

  • analyses (Array<String>) (defaults to: [])

    the names of the analyses.

  • mode (Symbol) (defaults to: :run)

    the mode: :run, :rerun or :clean.



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(@options)
      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.

Parameters:

  • source_ids (Array<Integer>)

    the ids of the sources.



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

Parameters:

  • name (String)

    name of the file

Returns:

  • (String)

    the path to the file.



427
428
429
# File 'lib/dgit/core.rb', line 427

def config_path(name)
  File.expand_path(name, File.expand_path(DGIT_FOLDER, @folder))
end

#file_path(name) ⇒ String

Return the path of the given file in the diggit folder

Parameters:

  • name (String)

    name of the file

Returns:

  • (String)

    the path to the file.



434
435
436
# File 'lib/dgit/core.rb', line 434

def file_path(name)
  File.expand_path(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.

Parameters:

  • source_ids (Array<Integer>) (defaults to: [])

    the ids of the sources.

  • joins (Array<String>) (defaults to: [])

    the names of the analyses.

  • mode (Symbol) (defaults to: :run)

    the mode: :run, :rerun or :clean.



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(@options)
    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_configvoid

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_journalvoid

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_optionsvoid

This method returns an undefined value.

Load the options from .dgit/options



467
468
469
# File 'lib/dgit/core.rb', line 467

def load_options
  @options = Oj.load_file(config_path(DGIT_OPTIONS))
end

#save_configvoid

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_journalvoid

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_optionsvoid

This method returns an undefined value.

Save the options to .dgit/options



473
474
475
# File 'lib/dgit/core.rb', line 473

def save_options
  Oj.to_file(config_path(DGIT_OPTIONS), options)
end