/usr/share/obs/api/lib/wizard.rb is in obs-api 2.7.4-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 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 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 | require 'rexml/document'
class Wizard
def initialize(text = nil)
if !text || text.empty?
@data = DirtyHash.new
@guess = DirtyHash.new
@version = 1
@dirty = false
return
end
data = {}
guess = {}
xml = REXML::Document.new(text)
xml.elements.each("wizard/data") do |element|
data[element.attributes["name"]] = element.text
end
xml.elements.each("wizard/guess") do |element|
guess[element.attributes["name"]] = element.text
end
@data = DirtyHash.new(data)
@guess = DirtyHash.new(guess)
@version = xml.root ? (xml.root.attributes["version"] || 1) : 1
@version = @version.to_i
@dirty = false
end
def serialize
xml = REXML::Document.new
xml.add_element(REXML::Element.new("wizard"))
xml.root.attributes["version"] = @version.to_s
@data.each do |name, value|
e = REXML::Element.new("data")
e.attributes["name"] = name
e.text = value
xml.root.add_element(e)
end
@guess.each do |name, value|
e = REXML::Element.new("guess")
e.attributes["name"] = name
e.text = value
xml.root.add_element(e)
end
res = ""
xml.write(res)
return res
end
def dirty
return @dirty || @data.dirty || @guess.dirty
end
def [](name)
return @data[name] || @guess[name]
end
def []=(name, value)
@data[name] = value
# to be removed
case name
when "name"
if value =~ /^perl-/i
@guess["packtype"] = "perl"
elsif value =~ /^python-/i
@guess["packtype"] = "python"
end
end
end
def run
@questions = nil
ask "name"
return @questions if @questions
ask "sourcefile"
ask "generator"
return @questions if @questions
ask "summary"
ask "description"
return @questions if @questions
# ask "license"
# ask "group"
# return @questions if @questions
return nil
end
def generate_spec(template)
erb = ERB.new(template)
hb = HashBinding.new(@guess.merge(@data))
template = erb.result(hb.getBinding)
end
private
def ask(databit)
if @data[databit]
return
end
if ! @questions
@questions = []
end
@questions << { databit => @@databits[databit] }
end
@@databits = {
# note that the name is already known when running in the buildservice
"name" => {
'type' => "text",
'label' => "Name of the package"
},
"sourcefile" => {
'type' => "url",
'label' => "Source file to download"
},
"generator" => {
'type' => "select",
'label' => "Generate build description",
'options' => [
# shall be requested from backend
{ "-" => { 'label' => "skip"} },
{ "qmake" => { 'label' => "qmake based code generator"} }
]
},
"summary" => {
'type' => "text",
'label' => "Short summary of the package"
},
"description" => {
'type' => "longtext",
'label' => "Describe your package"
},
"license" => {
'type' => "text",
'label' => "License of the package"
},
"group" => {
'type' => "text",
'label' => "Package group",
'legend' => "See http://en.opensuse.org/SUSE_Package_Conventions/RPM_Groups"
}
}
def self.guess_version(name, tarball)
if tarball =~ /^#{name}-(.*)\.tar\.(gz|bz2)$/i
return $1
elsif tarball =~ /.*-([0-9\.]*)\.tar\.(gz|bz2)$/
return $1
end
return nil
end
# hash that sets a dirty flag on write
class DirtyHash < Hash
attr_reader :dirty
def initialize(h = {})
replace(h)
end
def []=(key, value)
if self[key] != value
@dirty = true
super(key, value)
end
end
end
# convert a hash into a binding, turning keys into instance variables
class HashBinding
def initialize(hash)
hash.each do |key, value|
if key !~ /^[a-zA-Z][a-zA-Z0-9_]*$/
raise RuntimeError.new("Illegal key: #{key}")
end
instance_variable_set("@#{key}", value)
end
end
def getBinding
binding
end
end
end
|