Class: Patriot::Tool::BatchParser

Inherits:
Object
  • Object
show all
Includes:
Util::CronFormatParser, Util::DateUtil, Util::Logger, Util::Script
Defined in:
lib/patriot/tool/batch_parser.rb

Overview

PBC parser

Constant Summary collapse

DEFAULT_INTERVAL =

default interval is daily

'0 0 * * *'

Constants included from Util::CronFormatParser

Util::CronFormatParser::DAYS, Util::CronFormatParser::DAY_IN_SECOND, Util::CronFormatParser::DEFAULT_CRON_FIELD, Util::CronFormatParser::END_OF_EVERY_MONTH, Util::CronFormatParser::HOURS, Util::CronFormatParser::MINUTES, Util::CronFormatParser::MONTHS, Util::CronFormatParser::WEEKS

Constants included from Util::Config

Util::Config::ADMIN_USER_KEY, Util::Config::DEFAULT_CONFIG, Util::Config::DEFAULT_PLUGIN_DIR, Util::Config::INFO_SERVER_PORT_KEY, Util::Config::PASSWORD_KEY, Util::Config::PLUGIN_DIR_KEY, Util::Config::PLUGIN_INIT_SCRIPT, Util::Config::PLUGIN_KEY, Util::Config::PLUGIN_LIB_DIR, Util::Config::USERNAME_KEY, Util::Config::WORKER_HOST_KEY, Util::Config::WORKER_USER_KEY

Instance Method Summary collapse

Methods included from Util::CronFormatParser

#expand_on_date, #is_target_day?, #parse_field, #target_hours, #target_minutes

Methods included from Util::Script

#get_batch_files

Methods included from Util::DateUtil

#date_add, #date_add_year, #date_format, #date_sub, #date_sub_year, #date_to_month, #days_of_month, #days_of_month_until, #days_of_week, #hours, #month_add, #month_sub, #to_date_obj, #to_end_of_last_month, #to_end_of_month, #to_month, #to_start_of_month

Methods included from Util::Logger

#create_logger

Methods included from Util::Config

#load_config, #load_plugins

Constructor Details

#initialize(config) ⇒ BatchParser

Returns a new instance of BatchParser.

Parameters:



14
15
16
17
# File 'lib/patriot/tool/batch_parser.rb', line 14

def initialize(config)
  @config = config
  @logger = create_logger(config)
end

Instance Method Details

#dsl_parserObject

return parser instance



98
99
100
101
# File 'lib/patriot/tool/batch_parser.rb', line 98

def dsl_parser
  # CommandGroup includes the Patriot::Parser module
  return Patriot::Command::CommandGroup.new(@config)
end

#parse(date, files, options = {}, &blk) ⇒ Object

parse PBC files and return a set of commands specified in the PBC files

Parameters:

  • date (String)

    date (in yyyy-MM-dd) of which jobs are built by this parser

  • files (String|Array)

    PBC files to be parsed

  • options (defaults to: {})

    parse options

  • blk

    block to process each command

Options Hash (options):

  • :filter (String)

    a regular expression to extract target commands

Returns:

  • an array of commands



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/patriot/tool/batch_parser.rb', line 49

def parse(date, files, options = {}, &blk)
  return if files.empty?
  ds = date.split('-')
  raise "illegal format of date #{date}" unless ds.size == 3
  datetime = Time.new(ds[0], ds[1], ds[2])
  # for backward compatibility to be removed
  $dt    = date
  $month = date.split('-').values_at(0,1).join('-')
  commands = []
  filter = (options[:filter]) ? Regexp.new(options[:filter]) : nil

  files = [files] unless files.is_a?(Array)
  files.each do |file|
    @logger.info "parsing #{file}"
    open(file) do |f|
      exp = ""
      preprocess = []
      while((line = f.gets) != nil) do
        if(exp.empty? && line.start_with?('#'))
          preprocess << line
        end
        exp << line
      end
      context = {:interval => DEFAULT_INTERVAL}.merge(parse_preprocess(preprocess))
      expand_on_date(datetime, context[:interval]).each do |dt|
        dsl_parser.parse(dt, exp).flatten.each do |cmd|
          next unless filter.nil? || cmd.job_id =~ filter
          yield(cmd, {:path => file}) if block_given?
          commands << cmd
        end
      end
    end
  end
  return commands
end

#parse_preprocess(pre_process) ⇒ Object

parse pre processors

Parameters:

  • pre_process (Array<String>)

    a list of pre processors



87
88
89
90
91
92
93
94
95
# File 'lib/patriot/tool/batch_parser.rb', line 87

def parse_preprocess(pre_process)
  context = {}
  pre_process.each do |op|
    if op.start_with?('#interval ')
      context[:interval] = op.split(' ', 2)[1].strip
    end
  end
  return context
end

#process(date, paths, options = {}) {|cmd, source| ... } ⇒ Object

parse PBC files and process commands specified in the PBC files

Parameters:

  • date (String)

    a date (yyyy-MM-dd) or range of dates (yyyy-MM-dd,yyyy-MM-dd)

  • paths (String|Array)

    paths to PBC files to be parsed

  • options (defaults to: {})

    parse options

Options Hash (options):

  • :filter (String)

    a regular expression to extract target commands

Yields:

  • block to process each command

Yield Parameters:

  • cmd (Patriot::Command::Base)

    parsed command

  • source (Hash)

    location of the file has the command. (:path => <path to the file>)

Returns:

  • an array of commands



28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/patriot/tool/batch_parser.rb', line 28

def process(date, paths, options = {}, &blk)
  dates = validate_and_parse_dates(date)
  commands = []
  dates.each do |d|
    files  = paths.map {|path| get_batch_files(path, date)}.flatten
    if files.nil? || files.size == 0
      @logger.warn "ERROR: no pbc exists #{paths}"
      next
    end
    commands |= parse(d, files, options){|cmd, source| yield(cmd, source) if block_given?}
  end
  return commands
end