Module: Twin::Scanner
- Defined in:
- lib/twin/scanner.rb
Class Method Summary collapse
- .build_job(r) ⇒ Object
- .group(jobs) ⇒ Object
- .load_jobs(cfg, scan_path: nil) ⇒ Object
- .load_programs(cfg, file: nil, label: nil, show_all: false) ⇒ Object
-
.resolve_file_arg(cfg, file) ⇒ Object
Returns [scan_path, name_filter] for a given file argument.
- .stat(path) ⇒ Object
Class Method Details
.build_job(r) ⇒ Object
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/twin/scanner.rb', line 101 def build_job(r) path = r["Path"].to_s source = r["Source"].to_s target = r["Target"].to_s return nil if path.empty? || source.empty? || target.empty? excludes = (r["Exclude"] || "").split(",").map(&:strip).reject(&:empty?) src_full = File.join(source, path) tgt_full = File.join(target, path) src_exists, src_mtime = stat(src_full) tgt_exists, tgt_mtime = stat(tgt_full) conflict = src_exists && tgt_exists && tgt_mtime && src_mtime && tgt_mtime > src_mtime Job.new( program: r["Program"].to_s, path: path, description: r["Description"].to_s, active: (r["Active"] || 0).to_i, excludes: excludes, label: r["Label"].to_s, source: source, target: target, cmd: r["Cmd"].to_s, sync_file: r["_note_file"].to_s, source_exists: src_exists, target_exists: tgt_exists, source_mtime: src_mtime, target_mtime: tgt_mtime, conflict: !!conflict, ) end |
.group(jobs) ⇒ Object
96 97 98 99 |
# File 'lib/twin/scanner.rb', line 96 def group(jobs) jobs.group_by { |j| [j.program, j.sync_file] } .map { |(name, _file), js| Program.new(name: name, jobs: js) } end |
.load_jobs(cfg, scan_path: nil) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/twin/scanner.rb', line 54 def load_jobs(cfg, scan_path: nil) raise "grubber not found in PATH" unless system("command -v grubber > /dev/null 2>&1") dir = scan_path || cfg.sync_dir stdout, stderr, status = Open3.capture3( "grubber", "extract", dir, "-b", "--format", "json" ) raise "grubber: #{stderr.force_encoding('UTF-8').strip}" unless status.success? begin records = JSON.parse(stdout.force_encoding("UTF-8")) rescue JSON::ParserError => e raise "grubber returned invalid JSON: #{e.}" end records.filter_map { |r| build_job(r) } end |
.load_programs(cfg, file: nil, label: nil, show_all: false) ⇒ Object
71 72 73 74 75 76 77 78 |
# File 'lib/twin/scanner.rb', line 71 def load_programs(cfg, file: nil, label: nil, show_all: false) scan_path, name_filter = resolve_file_arg(cfg, file) jobs = load_jobs(cfg, scan_path: scan_path) jobs = jobs.select { |j| j.sync_file.include?(name_filter) } if name_filter jobs = jobs.select { |j| j.label == label } if label && !label.empty? jobs = jobs.select { |j| j.active == 1 } unless show_all group(jobs) end |
.resolve_file_arg(cfg, file) ⇒ Object
Returns [scan_path, name_filter] for a given file argument.
-
nil / empty → [nil, nil] scan sync_dir, no filter
-
path to a dir → [dir, nil] scan that dir, no filter
-
path to a file → [dirname, basename] scan parent dir, filter by filename
-
bare name (no /) → [nil, name] scan sync_dir, filter by name
85 86 87 88 89 90 91 92 93 94 |
# File 'lib/twin/scanner.rb', line 85 def resolve_file_arg(cfg, file) return [nil, nil] if file.nil? || file.empty? if file.include?("/") || file == "." || file == ".." = File.(file) return [, nil] if File.directory?() return [File.dirname(), File.basename()] if File.file?() raise "not found: #{file}" end [nil, file] end |
.stat(path) ⇒ Object
134 135 136 137 138 139 |
# File 'lib/twin/scanner.rb', line 134 def stat(path) st = File.stat(path) [true, st.mtime] rescue Errno::ENOENT, Errno::EACCES [false, nil] end |