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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
|
#!/usr/bin/env python
# ex:ts=4:sw=4:sts=4:et
# -*- tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*-
"""
OpenEmbedded 'Build' implementation
Core code for function execution and task handling in the
OpenEmbedded (http://openembedded.org) build infrastructure.
Copyright: (c) 2003 Chris Larson
Based on functions from the base oe module, Copyright 2003 Holger Schurig
"""
from oe import debug, data, fetch, fatal, error, note, event, mkdirhier
import oe, os
# data holds flags and function name for a given task
_task_data = data.init()
# graph represents task interdependencies
_task_graph = oe.digraph()
# stack represents execution order, excepting dependencies
_task_stack = []
# events
class FuncFailed(Exception):
"""Executed function failed"""
class EventException(Exception):
"""Exception which is associated with an Event."""
def __init__(self, msg, event):
self.event = event
def getEvent(self):
return self._event
def setEvent(self, event):
self._event = event
event = property(getEvent, setEvent, None, "event property")
class TaskBase(event.Event):
"""Base class for task events"""
def __init__(self, t, d = {}):
self.task = t
self.data = d
def getTask(self):
return self._task
def setTask(self, task):
self._task = task
task = property(getTask, setTask, None, "task property")
def getData(self):
return self._data
def setData(self, data):
self._data = data
data = property(getData, setData, None, "data property")
class TaskStarted(TaskBase):
"""Task execution started"""
class TaskSucceeded(TaskBase):
"""Task execution completed"""
class TaskFailed(TaskBase):
"""Task execution failed"""
class InvalidTask(TaskBase):
"""Invalid Task"""
# functions
def init(data):
global _task_data, _task_graph, _task_stack
_task_data = data.init()
_task_graph = oe.digraph()
_task_stack = []
def exec_func(func, d, dirs = None):
"""Execute an OE 'function'"""
body = data.getVar(func, d)
if not body:
return
if not dirs:
dirs = (data.getVarFlag(func, 'dirs', d) or "").split()
for adir in dirs:
adir = data.expand(adir, d)
mkdirhier(adir)
if len(dirs) > 0:
adir = dirs[-1]
else:
adir = data.getVar('B', d, 1)
adir = data.expand(adir, d)
try:
prevdir = os.getcwd()
except OSError:
prevdir = data.expand('${TOPDIR}', d)
if adir and os.access(adir, os.F_OK):
os.chdir(adir)
if data.getVarFlag(func, "python", d):
exec_func_python(func, d)
else:
exec_func_shell(func, d)
os.chdir(prevdir)
def exec_func_python(func, d):
"""Execute a python OE 'function'"""
import re, os
tmp = "def " + func + "():\n%s" % data.getVar(func, d)
comp = compile(tmp + '\n' + func + '()', oe.data.getVar('FILE', d, 1) + ':' + func, "exec")
prevdir = os.getcwd()
g = {} # globals
g['oe'] = oe
g['os'] = os
g['d'] = d
exec comp in g
if os.path.exists(prevdir):
os.chdir(prevdir)
def exec_func_shell(func, d):
"""Execute a shell OE 'function' Returns true if execution was successful.
For this, it creates a bash shell script in the tmp dectory, writes the local
data into it and finally executes. The output of the shell will end in a log file and stdout.
Note on directory behavior. The 'dirs' varflag should contain a list
of the directories you need created prior to execution. The last
item in the list is where we will chdir/cd to.
"""
import sys
deps = data.getVarFlag(func, 'deps', d)
check = data.getVarFlag(func, 'check', d)
if check in globals():
if globals()[check](func, deps):
return
global logfile
t = data.getVar('T', d, 1)
if not t:
return 0
mkdirhier(t)
logfile = "%s/log.%s.%s" % (t, func, str(os.getpid()))
runfile = "%s/run.%s.%s" % (t, func, str(os.getpid()))
f = open(runfile, "w")
f.write("#!/bin/sh -e\n")
if data.getVar("OEDEBUG", d): f.write("set -x\n")
data.emit_env(f, d)
f.write("cd %s\n" % os.getcwd())
if func: f.write("%s\n" % func)
f.close()
os.chmod(runfile, 0775)
if not func:
error("Function not specified")
raise FuncFailed()
# open logs
si = file('/dev/null', 'r')
try:
if data.getVar("OEDEBUG", d):
so = os.popen("tee \"%s\"" % logfile, "w")
se = so
else:
so = file(logfile, 'w')
se = so
except OSError, e:
oe.error("opening log files: %s" % e)
pass
# dup the existing fds so we dont lose them
osi = [os.dup(sys.stdin.fileno()), sys.stdin.fileno()]
oso = [os.dup(sys.stdout.fileno()), sys.stdout.fileno()]
ose = [os.dup(sys.stderr.fileno()), sys.stderr.fileno()]
# replace those fds with our own
os.dup2(si.fileno(), osi[1])
os.dup2(so.fileno(), oso[1])
os.dup2(se.fileno(), ose[1])
# execute function
prevdir = os.getcwd()
if data.getVarFlag(func, "fakeroot", d):
maybe_fakeroot = oe.data.expand("${STAGING_BINDIR}/fakeroot ",d)
else:
maybe_fakeroot = ''
ret = os.system('%ssh -e %s' % (maybe_fakeroot, runfile))
os.chdir(prevdir)
# restore the backups
os.dup2(osi[0], osi[1])
os.dup2(oso[0], oso[1])
os.dup2(ose[0], ose[1])
# close our logs
si.close()
so.close()
se.close()
# close the backup fds
os.close(osi[0])
os.close(oso[0])
os.close(ose[0])
if ret==0:
if not data.getVar("OEDEBUG", d):
os.remove(runfile)
# os.remove(logfile)
return
else:
error("function %s failed" % func)
if data.getVar("OEINCLUDELOGS", d):
error("log data follows")
f = open(logfile, "r")
while True:
l = f.readline()
if l == '':
break
l = l.rstrip()
print '| %s' % l
f.close()
else:
error("see log in %s" % logfile)
raise FuncFailed()
_task_cache = []
def exec_task(task, d):
"""Execute an OE 'task'
The primary difference between executing a task versus executing
a function is that a task exists in the task digraph, and therefore
has dependencies amongst other tasks."""
# check if the task is in the graph..
task_graph = data.getVar('_task_graph', d)
if not task_graph:
task_graph = oe.digraph()
data.setVar('_task_graph', task_graph, d)
task_cache = data.getVar('_task_cache', d)
if not task_cache:
task_cache = []
data.setVar('_task_cache', task_cache, d)
if not task_graph.hasnode(task):
raise EventException("", InvalidTask(task, d))
# check whether this task needs executing..
if not data.getVarFlag(task, 'force', d):
if stamp_is_current(task, d):
return 1
# follow digraph path up, then execute our way back down
def execute(graph, item):
if data.getVarFlag(item, 'task', d):
if item in task_cache:
return 1
if task != item:
# deeper than toplevel, exec w/ deps
exec_task(item, d)
return 1
try:
debug(1, "Executing task %s" % item)
event.fire(TaskStarted(item, d))
exec_func(item, d)
event.fire(TaskSucceeded(item, d))
task_cache.append(item)
except FuncFailed, reason:
note( "Task failed: %s" % reason )
failedevent = TaskFailed(item, d)
event.fire(failedevent)
raise EventException(None, failedevent)
# execute
task_graph.walkdown(task, execute)
# make stamp, or cause event and raise exception
if not data.getVarFlag(task, 'nostamp', d):
mkstamp(task, d)
def stamp_is_current(task, d, checkdeps = 1):
"""Check status of a given task's stamp. returns 0 if it is not current and needs updating."""
task_graph = data.getVar('_task_graph', d)
if not task_graph:
task_graph = oe.digraph()
data.setVar('_task_graph', task_graph, d)
stamp = data.getVar('STAMP', d)
if not stamp:
return 0
stampfile = "%s.%s" % (data.expand(stamp, d), task)
if not os.access(stampfile, os.F_OK):
return 0
if checkdeps == 0:
return 1
import stat
tasktime = os.stat(stampfile)[stat.ST_MTIME]
_deps = []
def checkStamp(graph, task):
# check for existance
if data.getVarFlag(task, 'nostamp', d):
return 1
if not stamp_is_current(task, d, 0):
return 0
depfile = "%s.%s" % (data.expand(stamp, d), task)
deptime = os.stat(depfile)[stat.ST_MTIME]
if deptime > tasktime:
return 0
return 1
return task_graph.walkdown(task, checkStamp)
def md5_is_current(task):
"""Check if a md5 file for a given task is current"""
def mkstamp(task, d):
"""Creates/updates a stamp for a given task"""
mkdirhier(data.expand('${TMPDIR}/stamps', d));
stamp = data.getVar('STAMP', d)
if not stamp:
return
stamp = "%s.%s" % (data.expand(stamp, d), task)
open(stamp, "w+")
def add_task(task, deps, d):
task_graph = data.getVar('_task_graph', d)
if not task_graph:
task_graph = oe.digraph()
data.setVar('_task_graph', task_graph, d)
data.setVarFlag(task, 'task', 1, d)
task_graph.addnode(task, None)
for dep in deps:
if not task_graph.hasnode(dep):
task_graph.addnode(dep, None)
task_graph.addnode(task, dep)
def remove_task(task, kill, d):
"""Remove an OE 'task'.
If kill is 1, also remove tasks that depend on this task."""
task_graph = data.getVar('_task_graph', d)
if not task_graph:
task_graph = oe.digraph()
data.setVar('_task_graph', task_graph, d)
if not task_graph.hasnode(task):
return
data.delVarFlag(task, 'task', d)
ref = 1
if kill == 1:
ref = 2
task_graph.delnode(task, ref)
def task_exists(task, d):
task_graph = data.getVar('_task_graph', d)
if not task_graph:
task_graph = oe.digraph()
data.setVar('_task_graph', task_graph, d)
return task_graph.hasnode(task)
def get_task_data():
return _task_data
|