This script use the EnvBash object from this snipett : http://www.friendsnippets.com/snippet/9/
1 #!/usr/bin/ruby
2
3 # Copyright (C) 2006 enki <enki@crocobox.org>
4
5 # This program is free software; you can redistribute it
6 # and/or modify it under the terms of the GNU General
7 # Public License as published by the Free Software
8 # Foundation; either version 2 of the License, or (at
9 # your option) any later version.
10 #
11 # This program is distributed in the hope that it will be
12 # useful, but WITHOUT ANY WARRANTY; without even the
13 # implied warranty of MERCHANTABILITY or FITNESS FOR A
14 # PARTICULAR PURPOSE. See the GNU General Public License
15 # for more details.
16 #
17 # You should have received a copy of the GNU General
18 # Public License along with this program; if not, write
19 # to the Free Software Foundation, Inc., 59 Temple Place,
20 # Suite 330, Boston, MA 02111-1307 USA
21
22
23 class MultiRegexp < Regexp
24 def matches(str)
25 str.scan(self) do
26 yield Regexp.last_match
27 end
28 end
29 end
30
31
32 class EnvBash
33 attr_reader :bash_env
34
35 def initialize(source)
36 bash_script ="\
37 source #{source}
38 set
39 "
40 @bash_env = `#{bash_script}`
41 end
42
43 def vars
44 bvars=Hash.new
45 re=MultiRegexp.new('(\w*)=(.*)$', true)
46 re.matches(@bash_env) { |i|
47 if (i[1] != "")
48 if i[2] =~ /\(([^\)]*)\)/
49 ta=Array.new
50 re_a=MultiRegexp.new('\[\d*\]=\"([^"]*)\"', true)
51 re_a.matches($1) { |j|
52 ta.push(j[1])
53 }
54 bvars[i[1]]=ta
55 elsif
56 bvars[i[1]]=i[2]
57 end
58 end
59 }
60 return bvars
61 end
62 end
63
64 class PKGBUILD
65 attr_reader :properties, :vars
66
67 def initialize(file)
68 @properties=Hash.new
69
70 properties['name']=""
71 properties['version']=""
72 properties['rel']=""
73 properties['desc']=""
74 properties['depends']=""
75 properties['url']=""
76 properties['source']=""
77
78
79 # get all vars
80 vpkgs=EnvBash.new(file)
81 vars=vpkgs.vars
82
83 properties['name'] = vars['pkgname']
84 properties['version'] = vars['pkgver']
85 properties['rel'] = vars['pkgrel']
86 properties['desc'] = vars['pkgdesc']
87 properties['depends'] = vars['depends']
88
89 url=""
90 if vars['url'].instance_of?(Array)
91 vars['url'].each { |u| url +="#{u} " }
92 else
93 url = vars['url']
94 end
95
96 properties['url'] = url
97
98 source=""
99 if vars['source'].instance_of?(Array)
100 vars['source'].each { |s| source +="#{s} " }
101 else
102 source = vars['source']
103 end
104
105
106 properties['source'] = source
107 end
108
109
110 def name
111 return properties['name']
112 end
113
114 end
115
116
117 pkg = PKGBUILD.new("/var/abs/kernels/kernel26/PKGBUILD")
118 pkg.properties.each do |k,v|
119 puts "#{k}=#{v}"
120 end
This class allow you to parse variable in a bash script from ruby . It retourn an array of variables. bash array is also saved as ruby array.
1 #!/usr/bin/ruby
2 #
3 # how to use it :
4 # ovars=EnvBash.new(file)
5 # bash_vars=ovars.vars
6 #
7
8
9 class MultiRegexp < Regexp
10 def matches(str)
11 str.scan(self) do
12 yield Regexp.last_match
13 end
14 end
15 end
16
17
18 class EnvBash
19 attr_reader :bash_env
20
21 def initialize(source)
22 bash_script ="\
23 source #{source}
24 set
25 "
26 @bash_env = `#{bash_script}`
27 end
28
29 def vars
30 bvars=Hash.new
31 re=MultiRegexp.new('(\w*)=(.*)$', true)
32 re.matches(@bash_env) { |i|
33 if (i[1] != "")
34 if i[2] =~ /\(([^\)]*)\)/
35 ta=Array.new
36 re_a=MultiRegexp.new('\[\d*\]=\"([^"]*)\"', true)
37 re_a.matches($1) { |j|
38 ta.push(j[1])
39 }
40 bvars[i[1]]=ta
41 elsif
42 bvars[i[1]]=i[2]
43 end
44 end
45 }
46 return bvars
47 end
48 end
49
50 # test on a pkgbuild (archlinux)
51 bashvars=EnvBash.new("/var/abs/kernels/kernel26/PKGBUILD")
52
53 bvars=bashvars.vars
54 puts "#{bvars['pkgname']}-#{bvars['pkgver']}-#{bvars['pkgrel']}"
55
56 if bvars['source'].instance_of?(Array)
57 puts "sources : "
58 bvars['source'].each { |src|
59 puts src
60 }
61 end
Pages : 1