]>
Commit | Line | Data |
---|---|---|
e2489d8e ER |
1 | #!/usr/bin/env ruby |
2 | #-- | |
3 | # Copyright 2010 Per Øyvind Karlsen <peroyvind@mandriva.org> | |
4 | # This program is free software. It may be redistributed and/or modified under | |
5 | # the terms of the LGPL version 2.1 (or later). | |
6 | # | |
7 | # FIXME: Someone with actual ruby skills should really clean up and sanitize | |
8 | # this! fugliness obvious... | |
9 | #++ | |
10 | ||
11 | require 'optparse' | |
12 | require 'rbconfig' | |
13 | ||
14 | provides = false | |
15 | requires = false | |
16 | ||
17 | opts = OptionParser.new("#{$0} <--provides|--requires>") | |
18 | opts.on("-P", "--provides", "Print provides") do |val| | |
19 | provides = true | |
20 | end | |
21 | opts.on("-R", "--requires", "Print requires") do |val| | |
22 | requires= true | |
23 | end | |
24 | ||
25 | rest = opts.permute(ARGV) | |
26 | ||
27 | if rest.size != 0 or (!provides and !requires) or (provides and requires) | |
28 | $stderr.puts "Use either --provides OR --requires" | |
29 | $stderr.puts opts | |
30 | exit(1) | |
31 | end | |
32 | ||
33 | specpatt = RbConfig::CONFIG["rubylibdir"].sub(RbConfig::CONFIG["ruby_version"], ".*/specifications/.*\.gemspec$") | |
34 | gems = [] | |
35 | ruby_versioned = false | |
36 | abi_provide = false | |
37 | ||
38 | for path in $stdin.readlines | |
39 | # way fugly, but we make the assumption that if the package has | |
40 | # this file, the package is the current ruby version, and should | |
41 | # therefore provide ruby(abi) = version | |
42 | if provides and path.match(RbConfig::CONFIG["archdir"] + "/rbconfig.rb") | |
43 | abi_provide = true | |
44 | ruby_versioned = true | |
45 | elsif path.match(specpatt) | |
46 | ruby_versioned = true | |
47 | gems.push(path.chomp) | |
48 | # this is quite ugly and lame, but the assumption made is that if any files | |
49 | # found in any of these directories specific to this ruby version, the | |
50 | # package is dependent on this specific version. | |
51 | # FIXME: only supports current ruby version | |
52 | elsif not ruby_versioned | |
53 | if path.match(RbConfig::CONFIG["rubylibdir"]) | |
54 | ruby_versioned = true | |
55 | elsif path.match(RbConfig::CONFIG["archdir"]) | |
56 | ruby_versioned = true | |
57 | elsif path.match(RbConfig::CONFIG["sitelibdir"]) | |
58 | ruby_versioned = true | |
59 | elsif path.match(RbConfig::CONFIG["sitearchdir"]) | |
60 | ruby_versioned = true | |
61 | elsif path.match(RbConfig::CONFIG["vendorlibdir"]) | |
62 | ruby_versioned = true | |
63 | elsif path.match(RbConfig::CONFIG["vendorarchdir"]) | |
64 | ruby_versioned = true | |
65 | end | |
66 | end | |
67 | end | |
68 | ||
69 | if requires or abi_provide | |
70 | abidep = "ruby(abi)" | |
71 | if ruby_versioned | |
72 | abidep += " = %s" % RbConfig::CONFIG["ruby_version"] | |
73 | end | |
74 | print abidep + "\n" | |
75 | end | |
76 | ||
77 | if gems.length > 0 | |
78 | require 'rubygems' | |
79 | ||
80 | if requires | |
81 | ||
82 | module Gem | |
83 | class Requirement | |
84 | def rpm_dependency_transform(name, version) | |
85 | pessimistic = "" | |
86 | if version == "> 0.0.0" or version == ">= 0" | |
87 | version = "" | |
88 | else | |
89 | if version[0..1] == "~>" | |
90 | pessimistic = "rubygem(%s) < %s\n" % [name, Gem::Version.create(version[3..-1]).bump] | |
91 | version = version.gsub(/\~>/, '=>') | |
92 | end | |
93 | version = version.gsub(/^/, ' ') | |
94 | end | |
95 | version = "rubygem(%s)%s\n%s" % [name, version, pessimistic] | |
96 | end | |
97 | ||
98 | def to_rpm(name) | |
99 | result = as_list | |
100 | return result.map { |version| rpm_dependency_transform(name, version) } | |
101 | end | |
102 | ||
103 | end | |
104 | end | |
105 | end | |
106 | ||
107 | for gem in gems | |
108 | data = File.read(gem) | |
109 | spec = eval(data) | |
110 | if provides | |
111 | print "rubygem(%s) = %s\n" % [spec.name, spec.version] | |
112 | end | |
113 | if requires | |
114 | for d in spec.dependencies | |
115 | print d.requirement.to_rpm(d.name)[0] unless d.type != :runtime | |
116 | end | |
117 | for d in spec.required_rubygems_version.to_rpm("rubygems") | |
118 | print d.gsub(/(rubygem\()|(\))/, "") | |
119 | end | |
120 | end | |
121 | end | |
122 | end |