Improve the logic, and make it more modular.
This commit is contained in:
parent
d0d051e072
commit
7b5660f82a
1 changed files with 130 additions and 112 deletions
|
@ -15,7 +15,109 @@ require 'date'
|
||||||
require 'fileutils'
|
require 'fileutils'
|
||||||
require 'property_file_reader'
|
require 'property_file_reader'
|
||||||
|
|
||||||
NAMESPACES_FILENAME = 'file_storage_namespaces.properties'
|
|
||||||
|
# ------------------------------------------------------------------------------------
|
||||||
|
# FileInfoFile class
|
||||||
|
# ------------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
class FileInfoFile
|
||||||
|
attr_reader :default_namespace
|
||||||
|
attr_reader :data
|
||||||
|
|
||||||
|
def parse_info_line(line)
|
||||||
|
# Lines are in this form: "URI","filename"
|
||||||
|
match = line.match(/^"([^"]*)","([^"]*)"$/)
|
||||||
|
raise "Can't parse this line: '#{line}'" if !match
|
||||||
|
return match.captures[0], match.captures[1]
|
||||||
|
end
|
||||||
|
|
||||||
|
def parse_default_namespace(uri)
|
||||||
|
match = /^(.*)individual/.match(uri)
|
||||||
|
raise "Can't find default namespace: '#{uri}'" if match == nil
|
||||||
|
"#{match.captures[0]}individual/"
|
||||||
|
end
|
||||||
|
|
||||||
|
public
|
||||||
|
|
||||||
|
def initialize(filepath)
|
||||||
|
@data = []
|
||||||
|
File.open(filepath) do |f|
|
||||||
|
f.each() do |line|
|
||||||
|
@data.push(parse_info_line(line))
|
||||||
|
end
|
||||||
|
end
|
||||||
|
puts "parsed #{@data.length} lines."
|
||||||
|
|
||||||
|
@default_namespace = parse_default_namespace(@data[0][0])
|
||||||
|
puts "default namespace is '#{@default_namespace}'"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
# ------------------------------------------------------------------------------------
|
||||||
|
# NamespacesFile class
|
||||||
|
#
|
||||||
|
# Read, query, modify and write the namespace-prefixes file.
|
||||||
|
# ------------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
class NamespacesFile
|
||||||
|
NAMESPACES_FILENAME = 'file_storage_namespaces.properties'
|
||||||
|
|
||||||
|
public
|
||||||
|
|
||||||
|
def initialize(uploads_directory, scan_only)
|
||||||
|
@uploads_directory = uploads_directory
|
||||||
|
@scan_only = scan_only
|
||||||
|
|
||||||
|
Dir.chdir(@uploads_directory) do |dir|
|
||||||
|
@namespaces = {}
|
||||||
|
if File.file?(NAMESPACES_FILENAME)
|
||||||
|
@namespaces = PropertyFileReader.read(NAMESPACES_FILENAME)
|
||||||
|
@namespaces.delete("properties_file_path")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def add_namespace(namespace)
|
||||||
|
if @namespaces.has_value?(namespace)
|
||||||
|
puts "found prefix for #{namespace}"
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
'abcdefghijklmnopqrstuvwxyz'.split("").each do |this_char|
|
||||||
|
if (!@namespaces.has_key?(this_char))
|
||||||
|
@namespaces[this_char] = namespace
|
||||||
|
puts "assigned prefix = '#{this_char}'"
|
||||||
|
return
|
||||||
|
end
|
||||||
|
end
|
||||||
|
raise "all prefixes are used!"
|
||||||
|
end
|
||||||
|
|
||||||
|
def prefix(namespace)
|
||||||
|
@namespaces.each() do | key, value |
|
||||||
|
return key if value == namespace
|
||||||
|
end
|
||||||
|
raise "no prefix for '#{namespace}'"
|
||||||
|
end
|
||||||
|
|
||||||
|
def write()
|
||||||
|
if @scan_only
|
||||||
|
puts "Scan-only: not writing namespaces file"
|
||||||
|
else
|
||||||
|
Dir.chdir(@uploads_directory) do |dir|
|
||||||
|
File.open(NAMESPACES_FILENAME, "w") do |f|
|
||||||
|
@namespaces.each do |prefix, namespace|
|
||||||
|
f.puts "#{prefix} = #{namespace}"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
# ------------------------------------------------------------------------------------
|
||||||
|
# Main class - UploadFileFaker
|
||||||
|
# ------------------------------------------------------------------------------------
|
||||||
|
|
||||||
class UploadFileFaker
|
class UploadFileFaker
|
||||||
#
|
#
|
||||||
|
@ -38,103 +140,18 @@ class UploadFileFaker
|
||||||
end
|
end
|
||||||
|
|
||||||
#
|
#
|
||||||
# Look in the file_info file and find the default namespace. Lines are in
|
# Check each location that should contain an image, and if we're not just
|
||||||
# this form:
|
# scanning, put one there.
|
||||||
# Uv::http://vivo.scripps.edu/individual/n2938: Lv:0::_main_image_testtubes.jpg:
|
|
||||||
#
|
#
|
||||||
# The first line may be a header line, so use the second one.
|
def create_image_files_where_needed()
|
||||||
#
|
@file_info.data.each do |line|
|
||||||
def find_default_namespace()
|
uri, filename = line
|
||||||
second_line = IO.readlines(@file_info_file)[1]
|
process_file_info(uri, filename)
|
||||||
match = /Uv::(.*)individual/.match(second_line)
|
|
||||||
raise "Can't find default namespace: '#{second_line}'" if match == nil
|
|
||||||
@default_namespace = "#{match.captures[0]}individual/"
|
|
||||||
puts "default namespace is '#{@default_namespace}'"
|
|
||||||
end
|
|
||||||
|
|
||||||
def adjust_namespaces_file()
|
|
||||||
namespaces = read_namespaces_file()
|
|
||||||
namespaces = add_namespace_if_needed(namespaces)
|
|
||||||
write_namespaces_file(namespaces)
|
|
||||||
|
|
||||||
namespaces.each() do |key, value|
|
|
||||||
if value == @default_namespace
|
|
||||||
@prefix = key
|
|
||||||
puts "prefix is #{@prefix}"
|
|
||||||
break
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def read_namespaces_file()
|
def process_file_info(uri, filename)
|
||||||
Dir.chdir(@uploads_directory) do |dir|
|
full_path = figure_full_path(uri, filename)
|
||||||
namespaces = {}
|
|
||||||
if File.file?(NAMESPACES_FILENAME)
|
|
||||||
namespaces = PropertyFileReader.read(NAMESPACES_FILENAME)
|
|
||||||
namespaces.delete("properties_file_path")
|
|
||||||
puts "namespaces is already set to '#{namespaces}'"
|
|
||||||
end
|
|
||||||
return namespaces
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def add_namespace_if_needed(namespaces)
|
|
||||||
if namespaces.has_value?(@default_namespace)
|
|
||||||
puts "found prefix"
|
|
||||||
else
|
|
||||||
@prefix = ''
|
|
||||||
'abcdefghijklmnopqrstuvwxyz'.chars() do |this_char|
|
|
||||||
if (!namespaces.has_key?(this_char))
|
|
||||||
namespaces[@prefix] = @default_namespace
|
|
||||||
puts "assigned prefix = '#{@this_char}'"
|
|
||||||
break
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
return namespaces
|
|
||||||
end
|
|
||||||
|
|
||||||
def write_namespaces_file(namespaces)
|
|
||||||
if @scan_only
|
|
||||||
puts "Scan-only: not writing namespaces file"
|
|
||||||
else
|
|
||||||
Dir.chdir(@uploads_directory) do |dir|
|
|
||||||
File.open(NAMESPACES_FILENAME, "w") do |f|
|
|
||||||
namespaces.each do |prefix, namespace|
|
|
||||||
f.puts "#{prefix} = #{namespace}"
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
#
|
|
||||||
# Second pass -- figure out the location for each file. If the file
|
|
||||||
# doesn't exist, copy the template to that location.
|
|
||||||
#
|
|
||||||
def second_pass()
|
|
||||||
File.open(@file_info_file) do |f|
|
|
||||||
f.each() do |line|
|
|
||||||
next if header_line?(line.chomp)
|
|
||||||
process_file_info_line(line.chomp)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def header_line?(line)
|
|
||||||
return false unless line.match("^bytestreamUri")
|
|
||||||
|
|
||||||
puts "Skipping header line: '#{line}'"
|
|
||||||
return true
|
|
||||||
end
|
|
||||||
|
|
||||||
def process_file_info_line(line)
|
|
||||||
uri, filename = parse_info_line(line)
|
|
||||||
puts "URI is '#{uri}'"
|
|
||||||
puts "Filename is '#{filename}'"
|
|
||||||
prefixed_uri = substitute_prefix_for_namespace(uri)
|
|
||||||
full_path = construct_full_path(prefixed_uri, filename)
|
|
||||||
puts "Full path is '#{full_path}'"
|
|
||||||
|
|
||||||
if File.file?(full_path)
|
if File.file?(full_path)
|
||||||
puts "File already exists at: '#{full_path}'"
|
puts "File already exists at: '#{full_path}'"
|
||||||
|
@ -147,16 +164,14 @@ class UploadFileFaker
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def parse_info_line(line)
|
def figure_full_path(uri, filename)
|
||||||
# Lines are in this form: Uv::URI: Lv:0::FILENAME:
|
prefixed_uri = substitute_prefix_for_namespace(uri)
|
||||||
match = line.match(/Uv::(.*):\s+Lv:0::(.*):/)
|
construct_full_path(prefixed_uri, filename)
|
||||||
raise "Can't parse this line: '#{line}'" if !match
|
|
||||||
return match.captures[0], match.captures[1]
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def substitute_prefix_for_namespace(uri)
|
def substitute_prefix_for_namespace(uri)
|
||||||
if uri[0, @default_namespace.length] == @default_namespace
|
if uri[0, @namespace.length] == @namespace
|
||||||
return uri.sub(@default_namespace, "#{@prefix}~")
|
uri.sub(@namespace, "#{@prefix}~")
|
||||||
else
|
else
|
||||||
raise "Doesn't start with default namespace: '#{uri}'"
|
raise "Doesn't start with default namespace: '#{uri}'"
|
||||||
end
|
end
|
||||||
|
@ -167,13 +182,10 @@ class UploadFileFaker
|
||||||
0.step(prefixed_uri.size - 1, 3) do |i|
|
0.step(prefixed_uri.size - 1, 3) do |i|
|
||||||
path = "#{path}/#{prefixed_uri[i, 3]}"
|
path = "#{path}/#{prefixed_uri[i, 3]}"
|
||||||
end
|
end
|
||||||
puts "path is: '#{path}'"
|
"#{@uploads_directory}/file_storage_root#{path}/#{filename}"
|
||||||
return "#{@uploads_directory}/file_storage_root#{path}/#{filename}"
|
|
||||||
end
|
end
|
||||||
|
|
||||||
# ------------------------------------------------------------------------------------
|
|
||||||
public
|
public
|
||||||
# ------------------------------------------------------------------------------------
|
|
||||||
|
|
||||||
#
|
#
|
||||||
# Setup and get ready to process.
|
# Setup and get ready to process.
|
||||||
|
@ -192,12 +204,18 @@ class UploadFileFaker
|
||||||
end
|
end
|
||||||
|
|
||||||
#
|
#
|
||||||
# Start the recursive scanning (and copying).
|
# Start the scanning (and copying).
|
||||||
#
|
#
|
||||||
def process()
|
def process()
|
||||||
find_default_namespace()
|
@file_info = FileInfoFile.new(@file_info_file)
|
||||||
adjust_namespaces_file()
|
@namespace = @file_info.default_namespace
|
||||||
second_pass()
|
|
||||||
|
namespaces_file = NamespacesFile.new(@uploads_directory, @scan_only)
|
||||||
|
namespaces_file.add_namespace(@namespace)
|
||||||
|
namespaces_file.write()
|
||||||
|
@prefix = namespaces_file.prefix(@namespace)
|
||||||
|
|
||||||
|
create_image_files_where_needed()
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue