Debian Codegraph @

vendor/download_mecab.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 base_dir = Pathname.new(__FILE__).expand_path.dirname.parent
13
14 mecab_version = (base_dir + "bundled_mecab_version").read.strip
15 mecab_naist_jdic_version = (base_dir + "bundled_mecab_naist_jdic_version").read.strip
16
17 mecab_base = "mecab-#{mecab_version}"
18 mecab_naist_jdic_base = "mecab-naist-jdic-#{mecab_naist_jdic_version}"
19
20 def extract_tar_gz(tar_gz_path)
21 Zlib::GzipReader.open(tar_gz_path) do |tar_io|
22 Gem::Package::TarReader.new(tar_io) do |tar|
23 tar.each do |entry|
24 p [entry.header.typeflag, entry.full_name] if @debug
25 if entry.directory?
26 FileUtils.mkdir_p(entry.full_name)
27 else
28 File.open(entry.full_name, "wb") do |file|
29 file.print(entry.read)
30 end
31 end
32 end
33 end
34 end
35 end
36
37 def download(url, base)
38 ssl_verify_mode = nil
39 if /mingw/ =~ RUBY_PLATFORM
40 ssl_verify_mode = OpenSSL::SSL::VERIFY_NONE
41 end
42
43 tar = "#{base}.tar"
44 tar_gz = "#{tar}.gz"
45 URI(url).open(:ssl_verify_mode => ssl_verify_mode) do |remote_tar_gz|
46 File.open(tar_gz, "wb") do |local_tar_gz|
47 local_tar_gz.print(remote_tar_gz.read)
48 end
49 end
50 FileUtils.rm_rf(base)
51 extract_tar_gz(tar_gz)
52 FileUtils.rm_rf(tar_gz)
53
54 return if base.start_with?("mecab-naist-jdic")
55
56 pull_requests = [71]
57 pull_requests.each do |pr|
58 patch_url = "https://github.com/taku910/mecab/pull/#{pr}.patch"
59 URI(patch_url).open(:ssl_verify_mode => ssl_verify_mode) do |patch|
60 Dir.chdir(base) do
61 IO.pipe do |in_read, in_write|
62 pid = spawn("patch", "-p2", in: in_read)
63 in_read.close
64 IO.copy_stream(patch, in_write)
65 in_write.close
66 Process.waitpid(pid)
67 end
68 end
69 end
70 end
71 end
72
73 download("https://packages.groonga.org/source/mecab/#{mecab_base}.tar.gz",
74 mecab_base)
75
76 download("https://packages.groonga.org/source/mecab-naist-jdic/#{mecab_naist_jdic_base}.tar.gz",
77 mecab_naist_jdic_base)