cache.py: Share the parser's mtime_cache, reducing the number of stat syscalls
[vuplus_bitbake] / lib / bb / parse / __init__.py
1 """
2 BitBake Parsers
3
4 File parsers for the BitBake build tools.
5
6 Copyright (C) 2003, 2004  Chris Larson
7 Copyright (C) 2003, 2004  Phil Blundell
8
9 This program is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free Software
11 Foundation; either version 2 of the License, or (at your option) any later
12 version.
13
14 This program is distributed in the hope that it will be useful, but WITHOUT
15 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License along with
19 this program; if not, write to the Free Software Foundation, Inc., 59 Temple
20 Place, Suite 330, Boston, MA 02111-1307 USA.
21
22 Based on functions from the base bb module, Copyright 2003 Holger Schurig
23 """
24
25 __all__ = [ 'ParseError', 'SkipPackage', 'cached_mtime', 'mark_dependency',
26             'supports', 'handle', 'init' ]
27 handlers = []
28
29 import bb, os
30
31 class ParseError(Exception):
32     """Exception raised when parsing fails"""
33
34 class SkipPackage(Exception):
35     """Exception raised to skip this package"""
36
37 __mtime_cache = {}
38 def cached_mtime(f):
39     if not __mtime_cache.has_key(f):
40         __mtime_cache[f] = os.stat(f)[8]
41     return __mtime_cache[f]
42
43 def cached_mtime_noerror(f):
44     if not __mtime_cache.has_key(f):
45         try:
46             __mtime_cache[f] = os.stat(f)[8]
47         except OSError:
48             return 0
49     return __mtime_cache[f]
50
51 def mark_dependency(d, f):
52     if f.startswith('./'):
53         f = "%s/%s" % (os.getcwd(), f[2:])
54     deps = bb.data.getVar('__depends', d) or []
55     deps.append( (f, cached_mtime(f)) )
56     bb.data.setVar('__depends', deps, d)
57
58 def supports(fn, data):
59     """Returns true if we have a handler for this file, false otherwise"""
60     for h in handlers:
61         if h['supports'](fn, data):
62             return 1
63     return 0
64
65 def handle(fn, data, include = 0):
66     """Call the handler that is appropriate for this file"""
67     for h in handlers:
68         if h['supports'](fn, data):
69             return h['handle'](fn, data, include)
70     raise ParseError("%s is not a BitBake file" % fn)
71
72 def init(fn, data):
73     for h in handlers:
74         if h['supports'](fn):
75             return h['init'](data)
76
77
78 from parse_py import __version__, ConfHandler, BBHandler