~ubuntu-branches/ubuntu/trusty/ruby1.9/trusty

« back to all changes in this revision

Viewing changes to lib/rubygems/commands/mirror_command.rb

  • Committer: Bazaar Package Importer
  • Author(s): Stephan Hermann
  • Date: 2008-01-24 11:42:29 UTC
  • mfrom: (1.1.9 upstream)
  • Revision ID: james.westby@ubuntu.com-20080124114229-jw2f87rdxlq6gp11
Tags: 1.9.0.0-2ubuntu1
* Merge from debian unstable, remaining changes:
  - Robustify check for target_os, fixing build failure on lpia.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
require 'yaml'
 
2
require 'zlib'
 
3
 
 
4
require 'rubygems/command'
 
5
require 'rubygems/gem_open_uri'
 
6
 
 
7
class Gem::Commands::MirrorCommand < Gem::Command
 
8
 
 
9
  def initialize
 
10
    super 'mirror', 'Mirror a gem repository'
 
11
  end
 
12
 
 
13
  def description # :nodoc:
 
14
    <<-EOF
 
15
The mirror command uses the ~/.gemmirrorrc config file to mirror remote gem
 
16
repositories to a local path. The config file is a YAML document that looks
 
17
like this:
 
18
 
 
19
  ---
 
20
  - from: http://gems.example.com # source repository URI
 
21
    to: /path/to/mirror           # destination directory
 
22
 
 
23
Multiple sources and destinations may be specified.
 
24
    EOF
 
25
  end
 
26
 
 
27
  def execute
 
28
    config_file = File.join Gem.user_home, '.gemmirrorrc'
 
29
 
 
30
    raise "Config file #{config_file} not found" unless File.exist? config_file
 
31
 
 
32
    mirrors = YAML.load_file config_file
 
33
 
 
34
    raise "Invalid config file #{config_file}" unless mirrors.respond_to? :each
 
35
 
 
36
    mirrors.each do |mir|
 
37
      raise "mirror missing 'from' field" unless mir.has_key? 'from'
 
38
      raise "mirror missing 'to' field" unless mir.has_key? 'to'
 
39
 
 
40
      get_from = mir['from']
 
41
      save_to = File.expand_path mir['to']
 
42
 
 
43
      raise "Directory not found: #{save_to}" unless File.exist? save_to
 
44
      raise "Not a directory: #{save_to}" unless File.directory? save_to
 
45
 
 
46
      gems_dir = File.join save_to, "gems"
 
47
 
 
48
      if File.exist? gems_dir then
 
49
        raise "Not a directory: #{gems_dir}" unless File.directory? gems_dir
 
50
      else
 
51
        Dir.mkdir gems_dir
 
52
      end
 
53
 
 
54
      sourceindex_data = ''
 
55
 
 
56
      say "fetching: #{get_from}/Marshal.#{Gem.marshal_version}.Z"
 
57
 
 
58
      get_from = URI.parse get_from
 
59
 
 
60
      if get_from.scheme.nil? then
 
61
        get_from = get_from.to_s
 
62
      elsif get_from.scheme == 'file' then
 
63
        # check if specified URI contains a drive letter (file:/D:/Temp)
 
64
        get_from = get_from.to_s
 
65
        get_from = if get_from =~ /^file:.*[a-z]:/i then
 
66
                     get_from[6..-1]
 
67
                   else
 
68
                     get_from[5..-1]
 
69
                   end
 
70
      end
 
71
 
 
72
      open File.join(get_from.to_s, "Marshal.#{Gem.marshal_version}.Z"), "rb" do |y|
 
73
        sourceindex_data = Zlib::Inflate.inflate y.read
 
74
        open File.join(save_to, "Marshal.#{Gem.marshal_version}"), "wb" do |out|
 
75
          out.write sourceindex_data
 
76
        end
 
77
      end
 
78
 
 
79
      sourceindex = Marshal.load(sourceindex_data)
 
80
 
 
81
      progress = ui.progress_reporter sourceindex.size,
 
82
                                      "Fetching #{sourceindex.size} gems"
 
83
      sourceindex.each do |fullname, gem|
 
84
        gem_file = "#{fullname}.gem"
 
85
        gem_dest = File.join gems_dir, gem_file
 
86
 
 
87
        unless File.exist? gem_dest then
 
88
          begin
 
89
            open "#{get_from}/gems/#{gem_file}", "rb" do |g|
 
90
              contents = g.read
 
91
              open gem_dest, "wb" do |out|
 
92
                out.write contents
 
93
              end
 
94
            end
 
95
          rescue
 
96
            old_gf = gem_file
 
97
            gem_file = gem_file.downcase
 
98
            retry if old_gf != gem_file
 
99
            alert_error $!
 
100
          end
 
101
        end
 
102
 
 
103
        progress.updated gem_file
 
104
      end
 
105
 
 
106
      progress.done
 
107
    end
 
108
  end
 
109
 
 
110
end
 
111