snippets / parse archlinux pkgbuilds in ruby

Language: Ruby - First posted by enki on 2007-09-14 09:50 (1 year)
Link to the snippet: http://www.friendsnippets.org/snippet/10/

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
In order to post a comment, you should have a friendsnippet account. Please sign-in.

0 comments

Sep '07
  • This script use the EnvBash object from this snipett : http://www.friendsnippets.com/snippet/9/

Common Tags



snippet History

Sep '07