Skip to content

Instantly share code, notes, and snippets.

@julik
Created March 31, 2012 13:07
Show Gist options
  • Save julik/2264025 to your computer and use it in GitHub Desktop.
Save julik/2264025 to your computer and use it in GitHub Desktop.
Automatically package all Batch Import node sources from network disks
#!/usr/bin/env ruby
require "rexml/document"
require 'sequencer'
require 'progressbar'
require "fileutils"
class Collector
def collect_import_node(node, setup_path)
name = REXML::XPath.first(node, "Name").text
setup_dir = File.join(File.dirname(setup_path), File.basename(setup_path).gsub(/\.batch$/, ''))
# Pick the node by name
import_node_path = File.join(setup_dir, "%s.import_node" % name)
doc = REXML::Document.new(File.read(import_node_path))
path_to_first_sequence_element = REXML::XPath.first(doc, 'Setup/State/FileName').text
sequence = Sequencer.from_single_file(path_to_first_sequence_element)
puts "\t found import node #{name.inspect} attached to sequence #{sequence.inspect}"
@sequences_to_copy.push(sequence)
end
def collect_imports(setup_path)
puts "Collecting imports from #{setup_path}"
# Find all the Node nodes which have <Type>ImportFile</Type>
# extract their names
doc = REXML::Document.new(File.read(setup_path))
REXML::XPath.each(doc, '//Node/Type' ) do | type_node |
collect_import_node(type_node.parent, setup_path) if type_node.text == "ImportFile"
end
end
def pack_imports
# Ensure only unique seqs are copied
@sequences_to_copy.uniq!
# Prep the copy
all_the_files = @sequences_to_copy.map{|s| s.to_paths }.flatten.uniq
puts "=== WILL COPY %d FILES ===" % all_the_files.length
pbar = ProgressBar.new("Copying", all_the_files.length, $stdout)
all_the_files.each do | path |
pbar.inc
FileUtils.cp(path, @destination + "/")
end
pbar.finish
end
def initialize(dir_of_batches, destination)
batch_files = Dir.glob(dir_of_batches + "/*.batch")
raise "No .batch files found, go to the directory where the setups are and retry" if batch_files.empty?
raise "No destination directory provided" if destination.nil?
@destination = destination
@sequences_to_copy = []
batch_files.each(&method(:collect_imports))
pack_imports
end
end
Collector.new(Dir.pwd, ARGV.pop)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment