Debian Codegraph @

vendor/download_groonga_normalizer_mysql.rb

raw
1 #!/usr/bin/env ruby
2
3 require "pathname"
4 require "fileutils"
5 require "open-uri"
6 require "openssl"
7 require "rubygems/package"
8 require "zlib"
9
10 @debug = (ENV["DEBUG"] == "true" or ARGV.include?("--debug"))
11
12 groonga_nromalizer_mysql_base =
13 Pathname.new(__dir__).expand_path + "plugins" + "groonga-normalizer-mysql"
14
15 def extract_tar_gz(tar_gz_path, base)
16 Zlib::GzipReader.open(tar_gz_path) do |tar_io|
17 Gem::Package::TarReader.new(tar_io) do |tar|
18 metadata = nil
19 tar.each do |entry|
20 path = entry.full_name
21 path = metadata["path"] || path if metadata
22 path = path.gsub(/\A[^\/]+/) {base.to_s}
23 p [entry.header.typeflag, entry.full_name, path, metadata] if @debug
24 if entry.directory?
25 FileUtils.mkdir_p(path)
26 elsif entry.file?
27 File.open(path, "wb") do |file|
28 file.print(entry.read)
29 end
30 elsif entry.header.typeflag == "x"
31 metadata = {}
32 entry.read.each_line do |line|
33 _size, key_value = line.chomp.split(" ", 2)
34 key, value = key_value.split("=", 2)
35 metadata[key] = value
36 end
37 next
38 end
39 metadata = nil
40 end
41 end
42 end
43 end
44
45 def download(url, base)
46 ssl_verify_mode = nil
47 if /mingw/ =~ RUBY_PLATFORM
48 ssl_verify_mode = OpenSSL::SSL::VERIFY_NONE
49 end
50
51 FileUtils.mkdir_p(base.parent.to_s)
52
53 tar = "#{base}.tar"
54 tar_gz = "#{tar}.gz"
55 URI(url).open(:ssl_verify_mode => ssl_verify_mode) do |remote_tar_gz|
56 File.open(tar_gz, "wb") do |local_tar_gz|
57 local_tar_gz.print(remote_tar_gz.read)
58 end
59 end
60 FileUtils.rm_rf(base)
61 extract_tar_gz(tar_gz, base)
62 FileUtils.rm_rf(tar_gz)
63 end
64
65 download("https://packages.groonga.org/source/groonga-normalizer-mysql/groonga-normalizer-mysql-latest.tar.gz",
66 groonga_nromalizer_mysql_base)