Skip to content

Instantly share code, notes, and snippets.

@FiXato
Created July 11, 2012 23:40
Show Gist options
  • Save FiXato/3094480 to your computer and use it in GitHub Desktop.
Save FiXato/3094480 to your computer and use it in GitHub Desktop.
Rather ugly Ruby script to select all needed system roms for openMSX from source directories
#!/usr/bin/env ruby
# encoding: utf-8
ROMDIRS = [File.expand_path("~/.openMSX/share/systemroms/old/*"),File.expand_path("~/.openMSX/share/systemroms/downloaded/**/*")]
TARGETDIR = File.expand_path("~/.openMSX/share/systemroms")
SHAREDIR = "~/Development/openmSX/share/"
require 'fileutils'
sha1s = `grep -hEro '<sha1>([a-f0-9]+)<\/sha1>' #{SHAREDIR}|sed 's/<\\/sha1>//'|sed 's/<sha1>//'|uniq|sort`.split("\n").map{|l|l.strip.downcase}.uniq.sort
copied = []
extra = []
roms = []
skipped = []
ROMDIRS.each do |dir|
roms << Dir.glob(dir)
end
roms.flatten.each do |f|
next if File.directory?(f)
sha1 = `shasum "#{f}"`.split.first.downcase.strip
ext = File.basename(f).split('.').last
if sha1s.include?(sha1)
target_file = File.join(TARGETDIR,"#{sha1}.#{ext.downcase}")
if File.exist?(target_file)
skipped << target_file
else
FileUtils.mv(f,target_file)
copied << sha1
end
else
extra << [f,sha1]
end
end
copied.uniq!
copied.sort!
missing = sha1s - copied
puts "Copied:", copied
puts "Extra:", extra
puts "Skipped:", skipped
puts "Missing while copying:", missing
puts '='*80
puts
found_roms = Dir.glob(File.join(TARGETDIR,'*'))
puts "Found #{found_roms.size} roms in #{TARGETDIR}"
# puts found_roms
found_sha1s = found_roms.map do |f|
next if File.directory?(f)
sha = `shasum "#{f}"`
puts "nil sha: #{f}" if sha.nil?
sha.split.first.downcase.strip
end.compact.uniq.sort
puts "Found #{found_sha1s.size} unique SHA1 sums in #{TARGETDIR}:"
puts found_sha1s
puts "Found #{sha1s.size} unique SHA1 sums in the hardware config files:"
puts sha1s
missing = (sha1s - found_sha1s).uniq.sort
puts "Found #{missing.size} unique SHA1 sums missing:"
puts missing
puts '='*80
missing.each do |sha|
puts sha
puts `ack -r #{sha} #{SHAREDIR}`.strip
puts `ack -rC4 #{sha} #{SHAREDIR}|grep "<filename>"`
puts '-'*80
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment