fetcher.rb 2.59 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#
# Copyright:: Copyright (c) 2012 Opscode, Inc.
# License:: Apache License, Version 2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# 
#     http://www.apache.org/licenses/LICENSE-2.0
# 
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

18
19
20
require 'pp'

module Omnibus
21

22
  # Base class for classes that fetch project sources from the internet.
23
24
  class Fetcher

25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
    class ErrorReporter

      def initialize(error, fetcher)
        @error, @fetcher = error, fetcher
      end

      def e
        @error
      end

      def explain(why)
        $stderr.puts "* " * 40
        $stderr.puts why
        $stderr.puts "Fetcher params:"
        $stderr.puts indent(@fetcher.description, 2)
        $stderr.puts "Exception:"
        $stderr.puts indent("#{e.class}: #{e.message.strip}", 2)
Daniel DeLeo's avatar
Daniel DeLeo committed
42
        Array(e.backtrace).each {|l| $stderr.puts indent(l, 4) }
43
44
45
46
47
48
49
50
51
52
53
        $stderr.puts "* " * 40
      end

      private

      def indent(string, n)
        string.split("\n").map {|l| " ".rjust(n) << l }.join("\n")
      end

    end

54
55
56
    class UnsupportedSourceLocation < ArgumentError
    end

Daniel DeLeo's avatar
Daniel DeLeo committed
57
58
    NULL_ARG = Object.new

59
60

    def self.for(software)
61
62
63
64
65
66
      if software.source
        if software.source[:url] && Omnibus.config.use_s3_caching
          S3CacheFetcher.new(software)
        else
          without_caching_for(software)
        end
Daniel DeLeo's avatar
Daniel DeLeo committed
67
      else
68
        Fetcher.new(software)
Daniel DeLeo's avatar
Daniel DeLeo committed
69
70
71
72
      end
    end

    def self.without_caching_for(software)
73
74
75
76
      if software.source[:url]
        NetFetcher.new(software)
      elsif software.source[:git]
        GitFetcher.new(software)
77
78
      elsif software.source[:path]
        PathFetcher.new(software)
79
80
81
82
83
      else
        raise UnsupportedSourceLocation, "Don't know how to fetch software project #{software}"
      end
    end

Daniel DeLeo's avatar
Daniel DeLeo committed
84
85
86
87
88
89
    def self.name(name=NULL_ARG)
      @name = name unless name.equal?(NULL_ARG)
      @name
    end

    attr_reader :name
Marc Paradise's avatar
Marc Paradise committed
90
91
92
93
    attr_reader :source_timefile

    def initialize(software)
    end
Daniel DeLeo's avatar
Daniel DeLeo committed
94
95
96
97
98

    def log(message)
      puts "[fetcher:#{self.class.name}::#{name}] #{message}"
    end

99
100
101
102
    def description
      # Not as pretty as we'd like, but it's a sane default:
      inspect
    end
Marc Paradise's avatar
Marc Paradise committed
103

Marc Paradise's avatar
Marc Paradise committed
104
105
106
107
    def fetch_required?
      false
    end

Marc Paradise's avatar
Marc Paradise committed
108
109
    def clean
    end
110

Marc Paradise's avatar
Marc Paradise committed
111
112
113
    def fetch 
    end

114
115
  end
end