bitbake/lib/bb/utils.py:
[vuplus_bitbake] / lib / bb / manifest.py
1 # ex:ts=4:sw=4:sts=4:et
2 # -*- tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*-
3 #
4 # Copyright (C) 2003, 2004  Chris Larson
5
6 # This program is free software; you can redistribute it and/or modify it under
7 # the terms of the GNU General Public License as published by the Free Software
8 # Foundation; either version 2 of the License, or (at your option) any later
9 # version.
10
11 # This program is distributed in the hope that it will be useful, but WITHOUT
12 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13 # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
14
15 # You should have received a copy of the GNU General Public License along with
16 # this program; if not, write to the Free Software Foundation, Inc., 59 Temple
17 # Place, Suite 330, Boston, MA 02111-1307 USA. 
18
19 import os, sys
20 import bb, bb.data
21
22 def getfields(line):
23     fields = {}
24     fieldmap = ( "pkg", "src", "dest", "type", "mode", "uid", "gid", "major", "minor", "start", "inc", "count" )
25     for f in xrange(len(fieldmap)):
26         fields[fieldmap[f]] = None
27
28     if not line:
29         return None
30
31     splitline = line.split()
32     if not len(splitline):
33         return None
34
35     try:
36         for f in xrange(len(fieldmap)):
37             if splitline[f] == '-':
38                 continue
39             fields[fieldmap[f]] = splitline[f]
40     except IndexError:
41         pass
42     return fields
43
44 def parse (mfile, d):
45     manifest = []
46     while 1:
47         line = mfile.readline()
48         if not line:
49             break
50         if line.startswith("#"):
51             continue
52         fields = getfields(line)
53         if not fields:
54             continue
55         manifest.append(fields)
56     return manifest
57
58 def emit (func, manifest, d):
59 #str = "%s () {\n" % func
60     str = ""
61     for line in manifest:
62         emittedline = emit_line(func, line, d)
63         if not emittedline:
64             continue
65         str += emittedline + "\n"
66 #       str += "}\n"
67     return str
68
69 def mangle (func, line, d):
70     import copy
71     newline = copy.copy(line)
72     src = bb.data.expand(newline["src"], d)
73
74     if src:
75         if not os.path.isabs(src):
76             src = "${WORKDIR}/" + src
77
78     dest = newline["dest"]
79     if not dest:
80         return
81
82     if dest.startswith("/"):
83         dest = dest[1:]
84
85     if func is "do_install":
86         dest = "${D}/" + dest
87
88     elif func is "do_populate":
89         dest = "${WORKDIR}/install/" + newline["pkg"] + "/" + dest
90
91     elif func is "do_stage":
92         varmap = {}
93         varmap["${bindir}"] = "${STAGING_DIR}/${HOST_SYS}/bin"
94         varmap["${libdir}"] = "${STAGING_DIR}/${HOST_SYS}/lib"
95         varmap["${includedir}"] = "${STAGING_DIR}/${HOST_SYS}/include"
96         varmap["${datadir}"] = "${STAGING_DATADIR}"
97
98         matched = 0
99         for key in varmap.keys():
100             if dest.startswith(key):
101                 dest = varmap[key] + "/" + dest[len(key):]
102                 matched = 1
103         if not matched:
104             newline = None
105             return
106     else:
107         newline = None
108         return
109
110     newline["src"] = src
111     newline["dest"] = dest
112     return newline
113
114 def emit_line (func, line, d):
115     import copy
116     newline = copy.deepcopy(line)
117     newline = mangle(func, newline, d)
118     if not newline:
119         return None
120
121     str = ""
122     type = newline["type"]
123     mode = newline["mode"]
124     src = newline["src"]
125     dest = newline["dest"]
126     if type is "d":
127         str = "install -d "
128         if mode:
129             str += "-m %s " % mode
130         str += dest
131     elif type is "f":
132         if not src:
133             return None
134         if dest.endswith("/"):
135             str = "install -d "
136             str += dest + "\n"
137             str += "install "
138         else:
139             str = "install -D "
140         if mode:
141             str += "-m %s " % mode
142         str += src + " " + dest
143     del newline
144     return str