Skip to content

Instantly share code, notes, and snippets.

@davelyon
Created July 20, 2010 19:58
Show Gist options
  • Save davelyon/483467 to your computer and use it in GitHub Desktop.
Save davelyon/483467 to your computer and use it in GitHub Desktop.
require 'pathname'
require 'fileutils'
# @a = "/Users/dave/Desktop/hq-feeder/test-dirs/"
# @b = "/Users/dave/Desktop/hq-feeder/test-dirs2/"
# EXPORT_DIR = "/Users/dave/Desktop/hq-feeder/test-export-dirs/"
class ImageLoader
attr_reader :files
def initialize(import_dirs)
import_dirs.each do |dir|
path = Pathname.new(dir)
files = scan_directories(path)
moved_files = move_files(files, dir)
install(moved_files)
end
end
def scan_directories(dir)
files ||= []
dir.children.each do |subdir|
if subdir.directory?
files += scan_directories(subdir)
else
files << subdir if subdir.fnmatch?("*.jp2")
end
end
files
end
def move_files(files, import_dir)
movedfiles = []
files.each do |file|
dest = file.sub(import_dir, EXPORT_DIR)
FileUtils.mkpath(dest.dirname) # Make a path
success = FileUtils.move(file.to_s, dest.to_s) # Move it
movedfiles << dest if success == 0
end
movedfiles
end
def install(files)
@installer = "/var/www/install/install.py"
@destination = "/var/www/jp2/v0.8"
@dbname="hvdb"
@dbuser="helioviewer"
@dbpass="helioviewer"
@dbtype="mysql"
chunks = files.size / 500 + 1
chunks.times do |count|
images = files.slice!(0..500)
imageString =""; images.each {|img| imageString << " #{img}"}
puts %x{"python #{@installer} --update -d #{@dbname} -u #{@dbuser} -p #{@dbpass} -m #{@dbtype} -b #{@destination} #{imageString}"}
end
end
end
# a = ImageLoader.new([@a, @b])
# TO TEST:
# EXPORT_DIR = "/path/to/export/base/directory"
# sources = %w{"/where/is/dir1", "/where/is/dir1"}
# ImageLoader.new(sources)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment