summaryrefslogtreecommitdiffstats
path: root/scripts/jhbuild/rewrite.py
diff options
context:
space:
mode:
Diffstat (limited to 'scripts/jhbuild/rewrite.py')
-rwxr-xr-xscripts/jhbuild/rewrite.py222
1 files changed, 222 insertions, 0 deletions
diff --git a/scripts/jhbuild/rewrite.py b/scripts/jhbuild/rewrite.py
new file mode 100755
index 0000000000..7200a195c6
--- /dev/null
+++ b/scripts/jhbuild/rewrite.py
@@ -0,0 +1,222 @@
1#!/usr/bin/env python
2# Available modulesets:
3#
4# bootstrap.modules
5# freedesktop.modules
6# gcj.modules
7# gnome-2.10.modules
8# gnome-2.12.modules
9# gnome-2.14.modules
10# gnome-2.16.modules
11# gnutls.modules
12# gtk28.modules
13# gtk.modules
14# xorg-7.0.modules
15# xorg.modules
16
17moduleset = 'xorg.modules'
18
19
20
21import cElementTree as ElementTree
22# import lxml.etree as ElementTree
23import re, os, bb, bb.data
24
25class Handlers(object):
26 """
27 Class to act as a store for handlers of jhbuild xml elements, and as a
28 dispatcher of parsed Elements to those handlers.
29
30 These handlers exist to take an xml element from the jhbuild files and
31 either produce bitbake metadata in self.packages, or produce data which
32 will be used by other element handlers to do so.
33
34 Handlers(filename) -> new object to parse and process jhbuild file of
35 name 'filename'.
36 """
37
38 cvsrootpat = re.compile(r'''
39 \s* # Skip leading whitespace
40 :(?P<scheme>[^:]+): # scheme (i.e. pserver, ext)
41 ((?P<user>\S+?)@)? # username
42 (?P<host>\S+?): # non-greedy match of the remote host
43 (?P<path>\S+) # remote path
44 ''', re.VERBOSE)
45
46
47 def __init__(self, msfile):
48 self.msfile = msfile
49 self.msbasename = os.path.basename(msfile)
50 self.msdirname = os.path.dirname(msfile)
51
52 self.handled = {}
53
54 self.cvsroots = {}
55 self.repositories = {}
56 self.packages = []
57
58 def handle(self, element, parent):
59 import sys
60 """
61 XML Element dispatch function. Can be called both from outside the
62 Handlers object to initiate handling, and from within individual XML
63 element handlers to ensure that dependent elements have been handled.
64
65 Does not handle a given XML Element more than once, as it retains
66 information about the handling state of the Elements it encounters.
67 """
68
69 try:
70 state = self.handled[element]
71 except KeyError:
72 pass
73 except:
74 return
75
76 try:
77 self.__class__.__dict__[element.tag](self, element, parent)
78 self.handled[element] = True
79 except KeyError:
80 self.handled[element] = False
81 sys.__stderr__.write('Unhandled element: %s\n' % element.tag)
82 except Exception:
83 sys.__stderr__.write('Error handling %s: %s:\n %s\n' % (element.tag, sys.exc_type, sys.exc_value))
84 self.handled[element] = False
85
86 print('handle(%s, %s) -> %s' % (element, parent, self.handled[element]))
87 return self.handled[element]
88
89 def cvsroot(self, element, parent):
90 # Rip apart the cvsroot style location to build a cvs:// url for
91 # bitbake's usage in the cvsmodule handler.
92 # root=":pserver:anoncvs@cvs.freedesktop.org:/cvs/fontconfig"
93 print("cvsroot(%s, %s)" % (element, parent))
94
95 root = element.attrib.get('root')
96 rootmatch = re.match(Handlers.cvsrootpat, root)
97 name = element.attrib.get('name')
98 user = rootmatch.group('user') or ''
99 if user != '':
100 pw = element.attrib.get('password') or ''
101 if pw != '':
102 pw = ':' + pw + '@'
103 else:
104 user = user + '@'
105 print('user: %s' % user)
106 print('pw: %s' % pw)
107
108 host = rootmatch.group('host')
109 print('host: %s' % host)
110 path = rootmatch.group('path') or '/'
111 print('path: %s' % path)
112
113 root = "cvs://%s%s%s%s" % (user, pw, host, path)
114 print('root: %s' % root)
115 self.cvsroots[name] = root
116
117 def cvsmodule(self, element, parent):
118 rootlist = [root for root in list(parent) if root.attrib.get('name') == element.attrib.get('cvsroot')]
119 if len(rootlist) < 1:
120 raise Exception("Error: cvsmodule '%s' requires cvsroot '%s'." % (element.attrib.get('module'), element.attrib.get('cvsroot')))
121
122 cvsroot = rootlist[0]
123
124
125 def include(self, element, parent):
126 href = element.attrib.get('href')
127 fullhref = os.path.join(self.msdirname, href)
128 tree = ElementTree.ElementTree(file=fullhref)
129 elem = tree.getroot()
130
131 # Append the children of the newly included root element to the parent
132 # element, and manually handle() them, as the currently running
133 # iteration isn't going to hit them.
134 for child in elem:
135 self.handle(child, elem)
136 parent.append(elem)
137
138 def repository(self, element, parent):
139 # TODO:
140 # Convert the URL in the href attribute, if necessary, to the format
141 # which bitbake expects to see in SRC_URI.
142 name = element.attrib.get('name')
143 self.repositories[name] = element.attrib.get('href')
144
145
146 def moduleset(self, element, parent):
147 for child in element:
148 self.handle(child, element)
149
150 def packagename(self, name):
151 # mangle name into an appropriate bitbake package name
152 return name.replace('/', '-')
153
154 def metamodule(self, element, parent):
155 # grab the deps
156 dependlist = [child for child in element if child.tag == "dependencies"]
157 deps = [self.packagename(dep.attrib.get('package')) for dep in dependlist[0] if child.tag == "dep"]
158
159 # create the package
160 d = bb.data.init()
161 pn = self.packagename(element.attrib.get('id'))
162 bb.data.setVar('PN', pn, d)
163 bb.data.setVar('DEPENDS', ' '.join(deps), d)
164 bb.data.setVar('_handler', 'metamodule', d)
165 self.packages.append(d)
166
167 def autotools(self, element, parent):
168 deps = None
169 branch = None
170 for child in element:
171 if child.tag == 'dependencies':
172 deps = [self.packagename(dep.attrib.get('package')) for dep in child if dep.tag == "dep"]
173 elif child.tag == 'branch':
174 branch = child
175
176 # create the package
177 d = bb.data.init()
178 id = element.attrib.get('id')
179 if id is None:
180 raise Exception('Error: autotools element has no id attribute.')
181 pn = self.packagename(id)
182 bb.data.setVar('PN', pn, d)
183 if deps is not None:
184 bb.data.setVar('DEPENDS', ' '.join(deps), d)
185
186 if branch is not None:
187 # <branch repo="git.freedesktop.org" module="xorg/xserver"/>
188 repo = os.path.join(self.repositories[branch.attrib.get('repo')], branch.attrib.get('module'))
189 bb.data.setVar('SRC_URI', repo, d)
190
191 checkoutdir = branch.attrib.get('checkoutdir')
192 if checkoutdir is not None:
193 bb.data.setVar('S', os.path.join('${WORKDIR}', checkoutdir), d)
194
195 # build class
196 bb.data.setVar('INCLUDES', 'autotools', d)
197 bb.data.setVarFlag('INCLUDES', 'operator', '+=', d)
198 bb.data.setVar('_handler', 'autotools', d)
199 self.packages.append(d)
200
201class Emitter(object):
202 """
203 Class to take a Handlers object after processing and emit the
204 bitbake files from the metadata. It supports either emitting
205 the data as is, using templates based on package name, and using
206 templates based on the name of handler / xml element associated
207 with the package itself.
208 """
209
210def _test():
211 msfile = os.path.join(os.path.abspath(os.curdir), 'modulesets', moduleset)
212 tree = ElementTree.ElementTree(file=msfile)
213 elem = tree.getroot()
214
215 handlers = Handlers(msfile)
216 handlers.handle(elem, None)
217
218 for package in handlers.packages:
219 print(bb.data.getVar('PN', package))
220
221if __name__ == "__main__":
222 _test()