Skip to content

Instantly share code, notes, and snippets.

@miau
Created July 16, 2011 14:34
Show Gist options
  • Select an option

  • Save miau/1086405 to your computer and use it in GitHub Desktop.

Select an option

Save miau/1086405 to your computer and use it in GitHub Desktop.
# -*- coding: utf-8
# カレントディレクトリ配下のファイル中に含まれる GUID を置換するスクリプト
require 'rubygems'
require 'uuidtools'
# 置換対象外の GUID とその理由(必要に応じて追加)
# GUID は UUIDTools::UUID#to_s の記法にあわせて、すべて小文字で記載すること
guid_shouldnot_replace = {
"8a885d04-1ceb-11c9-9fe8-08002b104860" => "_RpcTransferSyntax",
"00000000-0000-0000-c000-000000000046" => "IUnknown, ver. 0.0,",
"00020400-0000-0000-c000-000000000046" => "IDispatch, ver. 0.0",
}
# 以下のような GUID 表記に対応する正規表現/フォーマット
#
# IMPLEMENT_OLECREATE(<<class>>, <<external_name>>,
# 0x4080db5e, 0x3453, 0x4c40, 0x8f, 0xf6, 0x13, 0xfe, 0x38, 0x67, 0xf5, 0x1f);
#
# DEFINE_GUID(<<name>>,
# 0x4080db5e, 0x3453, 0x4c40, 0x8f, 0xf6, 0x13, 0xfe, 0x38, 0x67, 0xf5, 0x1f);
re_define_guid = Regexp.new(
'0x([0-9a-f]{8})' + ',\s*0x([0-9a-f]{4})' * 2 + ',\s*0x([0-9a-f]{2})' * 8,
Regexp::IGNORECASE
)
format_define_guid = "0x%08x, 0x%04x, 0x%04x, 0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x"
# 以下のような GUID 表記に対応する正規表現/フォーマット
#
# static const GUID <<name>> =
# { 0x4080db5e, 0x3453, 0x4c40, { 0x8f, 0xf6, 0x13, 0xfe, 0x38, 0x67, 0xf5, 0x1f } };
re_guid_structure = Regexp.new(
'0x([0-9a-f]{8})' + ',\s*0x([0-9a-f]{4})' * 2 + ',\s*\{\s*0x([0-9a-f]{2})' + ',\s*0x([0-9a-f]{2})' * 7 + '\s*\}',
Regexp::IGNORECASE
)
format_guid_structure = "0x%08x, 0x%04x, 0x%04x, { 0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x }"
# 以下のような GUID 表記に対応する正規表現/フォーマット
#
# {4080DB5E-3453-4C40-8FF6-13FE3867F51F}
#
# [Guid("4080DB5E-3453-4C40-8FF6-13FE3867F51F")]
#
# <Guid("4080DB5E-3453-4C40-8FF6-13FE3867F51F")>
re_guid = Regexp.new(
'([0-9a-f]{8})' + '-([0-9a-f]{4})' * 2 + '-([0-9a-f]{2})([0-9a-f]{2})-' + '([0-9a-f]{2})' * 6,
Regexp::IGNORECASE
)
format_guid = "%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X"
rules = {
re_define_guid => format_define_guid,
re_guid_structure => format_guid_structure,
re_guid => format_guid,
}
guid_map = {}
Dir.glob("**/*").each do |file|
next unless FileTest.file?(file)
next if file == __FILE__
puts file
org_data = File.open(file, "rb") {|f| f.read }
data = org_data.dup
rules.each do |re, format|
data.gsub!(re) do |org_str|
m = Regexp.last_match.captures.map(&:hex)
org_guid = UUIDTools::UUID.new(*m[0..4].push(m[5..-1]))
if guid_shouldnot_replace.has_key? org_guid.to_s
puts " #{org_guid} -> (skipped - #{guid_shouldnot_replace[org_guid.to_s]})"
org_str
else
new_guid = guid_map[org_guid] ||= UUIDTools::UUID.random_create
puts " #{org_guid} -> #{new_guid}"
format % new_guid.raw.unpack("Nn2C8")
end
end
end
if data != org_data
File.open(file, "wb") do |f|
f.write data
end
end
end
puts
puts "=== Summary ==="
guid_map.each do |org_guid, new_guid|
puts "#{org_guid} -> #{new_guid}"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment