Class: Jenkins::Plugin::Tools::Hpi

Inherits:
Object
  • Object
show all
Defined in:
lib/jenkins/plugin/tools/hpi.rb

Overview

class for parsing hpi file and its manifests

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file) ⇒ Hpi

take the path name to the plugin file



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/jenkins/plugin/tools/hpi.rb', line 14

def initialize(file)
  @file = file

  # load and parse manifests
  Zip::File.open(@file) do |zip|
    zip.get_input_stream("META-INF/MANIFEST.MF") do |m|
      # main section of the manifest
      @manifest = parse_manifest(m.read)[0]
    end
  end

  # parse dependencies into hash
  @dependencies = {}
  deps = @manifest["Plugin-Dependencies"]
  if deps
    deps.split(",").each do |token|
      token = token.gsub(/;.+/,"")  # trim off the optional portions
      name,ver = token.split(":")
      @dependencies[name] = ver
    end
  end
end

Instance Attribute Details

#fileObject (readonly)

Returns the value of attribute file.



11
12
13
# File 'lib/jenkins/plugin/tools/hpi.rb', line 11

def file
  @file
end

#manifestObject (readonly)

Returns the value of attribute manifest.



11
12
13
# File 'lib/jenkins/plugin/tools/hpi.rb', line 11

def manifest
  @manifest
end

Class Method Details

.fetch(uri, limit = 10) ⇒ Object

download with redirect support

Raises:

  • (ArgumentError)


65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/jenkins/plugin/tools/hpi.rb', line 65

def self.fetch(uri, limit = 10)
  # You should choose better exception.
  raise ArgumentError, 'HTTP redirect too deep' if limit == 0

  http = if ENV['HTTP_PROXY'] || ENV['http_proxy']
           proxy_uri = URI.parse(ENV['HTTP_PROXY'] || ENV['http_proxy'])
           Net::HTTP::Proxy(proxy_uri.host,
                            proxy_uri.port,
                            proxy_uri.user,
                            proxy_uri.password)
         else
           Net::HTTP
         end
  response = http.get_response(URI.parse(uri))
  case response
  when Net::HTTPSuccess     then response
  when Net::HTTPRedirection then fetch(response['location'], limit - 1)
  else
    response.error!
  end
end

.resolve(short_name, version) ⇒ Object

given the plugin short name and the version number, return the path name of the .hpi file by either locating the plugin locally or downloading it.



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/jenkins/plugin/tools/hpi.rb', line 39

def self.resolve(short_name,version)
  # this is where we expect the retrieved file to be
  cache = File.expand_path "~/.jenkins/cache/plugins/#{short_name}/#{version}/#{short_name}.hpi"

  return cache if File.exists?(cache)

  # now we start looking for places to find them

  # is it in the local maven2 repository?
  maven = File.expand_path "~/.m2/repository/org/jenkins-ci/plugins/#{short_name}/#{version}/#{short_name}-#{version}.hpi"
  return maven if File.exists?(maven)

  # download from the community update center
  FileUtils.mkdir_p(File.dirname(cache))
  open(cache+".tmp","wb") do |f|
    puts "Downloading #{short_name} #{version}"
    url = "http://updates.jenkins-ci.org/download/plugins/#{short_name}/#{version}/#{short_name}.hpi?for=ruby-plugin"
    puts "  from #{url}"
    f.write fetch(url).body
  end
  FileUtils.mv cache+".tmp", cache

  return cache
end

Instance Method Details

#parse_manifest(txt) ⇒ Object

parse manifest file text into a hash



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/jenkins/plugin/tools/hpi.rb', line 88

def parse_manifest(txt)
  # separators
  nl = /\r\n|\n|\r[^\n]/
  secsep = /(#{nl}){2}/

  txt.split(secsep).reject { |s| s.chomp.length==0 }.map do |section|
    lines = []
    section.split(nl).each do |line|
      if line[0]==0x20
        lines.last << line[1..-1] # continuation of the previous line
      else
        lines << line
      end
    end

    # convert to hash
    hash = {}
    lines.each do |l|
      (k,v) = l.split(/: /,2)
      hash[k] = v
    end

    hash
  end
end