class FileInventory
The main class concentrates the methods to create a list-file or the final inventory-list in HTML-format.
Public Class Methods
new()
click to toggle source
# File lib/file_inventory.rb, line 36 def initialize() init_logger(STDOUT) end
Public Instance Methods
check_files()
click to toggle source
verifies the existenc and status of some files
# File lib/file_inventory.rb, line 180 def check_files if(File::exist?(@list_file)) if(!File::readable?(@list_file)) msg = 'List-file ' << @list_file << ' exists but is not readable! Aborting' @log.error(msg) exit false end end if(!File::exist?(@template) || !File::readable?(@template)) msg = 'Check template ' << @template << '! The file must exist and be readable!' @log.error(msg) exit false end end
create_inventory()
click to toggle source
Creates a HTML file from the contents of the list-file.
# File lib/file_inventory.rb, line 41 def create_inventory check_files # list = @pwd.dup << File::Separator << LIST @log.debug('List is ' << @list_file) html_output = '' if(File.exist?(@list_file) && File.readable?(@list_file)) File.open(@list_file) do |fl| paras = fl.read().split("\n\n") html_output << "<!-- Begin file_inventory generator output -->\n" paras.each do |para| para.chomp! para.strip! contents = '' @log.debug('para is ' << para) if(!para.empty?) first = true filenames = Array.new comment = '' lines = para.split("\n") lines.each do |l| content = l.split(SPLT) @log.debug('content is ' << content.to_s) if(content && !content.empty?) html_output << "<tr>" filename = content[0] date = content[1] if(first) html_output << "<th class='first_line'>" else html_output << "<th>" end html_output << "<a href='" << filename << "'>" html_output << filename << "</a></th>" first = false html_output << "<td>" << date.to_s << "</td>" if(content.length > 2) comment = content[2] html_output << "<td>" << comment << "</td>" else html_output << "<td></td>" end html_output << "</tr>" end end @log.debug('html is ' << html_output) else @log.warn('ATTN! the list file does not contain lines of text') exit false end end html_output << "\n<!-- End file_inventory generator output -->" end else @log.error('not a readable file: ' << @list_file) end if html_output && !html_output.empty? repl = {:list => html_output, :directory => @pwd.split(File::Separator).last()} make_html(repl) end end
create_list_file()
click to toggle source
Generates a list of files from a given directory in text-format.
# File lib/file_inventory.rb, line 130 def create_list_file entries = "" if(! @dir.entries.include?(@list_file)) @log.debug('will create a file-list for you to check') entries = @dir.entries() @log.debug('entries is ' << entries.to_s) @log.debug('sought extensions are ' << EXTENSIONS.to_s) count = 0 files = Hash.new if(!entries.empty?) entries.each do |e| epath = File.absolute_path(@pwd.dup << File::Separator << e) mtime = File.mtime(epath).to_date.strftime('%d. %b %Y') ext = File.extname(e) basename = File.basename(e, ext) if(EXTENSIONS.include?(ext) ) count += 1 if(files.keys.include?(basename)) files[basename] << [e, mtime] else files[basename] = [ [e, mtime] ] end end end if(count == 0) @log.debug('None of the file-extensions found in this directory, is supported' ) else list = @pwd.dup << File::Separator << @list_file File.open(list, 'w') do |list_file| files.keys.each do |k| files[k].each do |file_list| list_file.puts(file_list.join(SPLT)) end list_file.puts() end @log.debug('list of ' << count.to_s << ' files created in ' << list) end end else @log.debug('No objects found in directory ' << @pwd) end else @log.WARN('Inventory-list exists already! Doing nothing') end end
make_html(data)
click to toggle source
Creates a copy of the template-file and replaces placeholders by file entries and the name of the current directory.
# File lib/file_inventory.rb, line 107 def make_html(data) source = @pwd.dup << File::Separator << @template target = @pwd.dup << File::Separator << @html begin File.open(source, 'r') do |s| File.open(target, 'w') do |t| s.extend(Completable) s.field_delimiter = DELIM fc, msg = s.complete(data) do |chunk| t << chunk end end end rescue Exception => ex @log.error('Cannot create html output: ' << ex.message) end end
run(argv)
click to toggle source
—- main routine
# File lib/file_inventory.rb, line 198 def run(argv) if(argv && !argv.empty?) options = ArgParser.parse(argv) @log.level = options.log_level @log.debug('options are ' << options.to_s) else @log.error('No arguments! Call with parameter "-h" or "--help" to see the options.') exit false end if options.empty? || !options.dir || options.dir.strip.empty? @log.error('you must indicate at least the target directory!') exit false else begin present_dir = Dir.getwd Dir.chdir(options.dir) @pwd = Dir.getwd @dir = Dir.new(@pwd) Dir.chdir(present_dir) rescue Exception => ex @log.error('cannot work in directory ' << options.dir << ': ' << ex.message) exit false end @log.debug('working in directory ' << @dir.path) end @list_file = options.list @template = options.template @html = options.html @delim = options.delim @list_file ||= LIST @html ||= TARGET @delim ||= DELIM @template ||= SOURCE if(! @dir.entries.include?(File::basename(@list_file))) @log.debug('creating list') create_list_file else @log.debug('creating inventory') create_inventory end end