Replacing B.has_key(A) calls with A in B.
[bitbake.git] / bin / oe / parse / SRPMHandler.py
1 """class for handling .src.rpm files
2
3    Accesses the file and obtains its metadata"""
4
5 import re, oe, string, os, sys
6 import oe
7 import oe.fetch
8 from oe import debug, data, fetch, fatal
9 from oe.parse.ConfHandler import init
10
11 _srpm_vartranslate = {
12 "NAME": "PN",
13 "VERSION": "PV",
14 "RELEASE": "PR",
15 }
16
17 def supports(fn, d):
18         return fn[-8:] == ".src.rpm"
19
20 def handle(fn, d = {}, include = 0):
21         init(d)
22         oepath = ['.']
23         if not os.path.isabs(fn):
24                 f = None
25                 voepath = data.getVar("OEPATH", d)
26                 if voepath:
27                         oepath += voepath.split(":")
28                 for p in oepath:
29                         p = data.expand(p, d)
30                         if os.access(os.path.join(p, fn), os.R_OK):
31                                 f = open(os.path.join(p, fn), 'r')
32                 if f is None:
33                         raise IOError("file not found")
34         else:
35                 f = open(fn,'r')
36
37         srpm_vars = os.popen('rpm --querytags').read().split('\n')
38         for v in srpm_vars:
39                 if v in _srpm_vartranslate:
40                         var = _srpm_vartranslate[v]
41                 else:
42                         var = v
43                 querycmd = 'rpm -qp --qf \'%%{%s}\' %s 2>/dev/null' % (v, fn)
44                 value = os.popen(querycmd).read().strip()
45                 if value == "(none)":
46                         value = None
47                 if value:
48                         data.setVar(var, value, d)
49
50         data.setVar("SRPMFILE", fn, d)
51
52         inheritclasses = data.getVar("INHERIT", d)
53         if inheritclasses:
54                 i = inheritclasses.split()
55         else:
56                 i = []
57
58         if not "base_srpm" in i:
59                 i[0:0] = ["base_srpm"]
60
61         for c in i:
62                 oe.parse.handle('classes/%s.oeclass' % c, d)
63
64         set_automatic_vars(fn, d, include)
65         set_additional_vars(fn, d, include)
66         data.update_data(d)
67         return d
68
69 def set_automatic_vars(file, d, include):
70         """Deduce per-package environment variables"""
71
72         debug(2, "setting automatic vars")
73
74         data.setVar('CATEGORY', 'srpm', d)
75         data.setVar('P', '${PN}-${PV}', d)
76         data.setVar('PF', '${P}-${PR}', d)
77
78         for s in ['${TOPDIR}/${CATEGORY}/${PF}', 
79                   '${TOPDIR}/${CATEGORY}/${PN}-${PV}',
80                   '${TOPDIR}/${CATEGORY}/files',
81                   '${TOPDIR}/${CATEGORY}']:
82                 s = data.expand(s, d)
83                 if os.access(s, os.R_OK):
84                         data.setVar('FILESDIR', s, d)
85                         break
86
87         data.setVar('WORKDIR', '${TMPDIR}/${CATEGORY}/${PF}', d)
88         data.setVar('T', '${WORKDIR}/temp', d)
89         data.setVar('D', '${WORKDIR}/image', d)
90         if not data.getVar('S', d):
91                 data.setVar('S', '${WORKDIR}/${P}', d)
92         data.setVar('SLOT', '0', d)
93
94 def set_additional_vars(file, d, include):
95         """Deduce rest of variables, e.g. ${A} out of ${SRC_URI}"""
96
97         debug(2,"set_additional_vars")
98
99         src_uri = data.getVar('SRC_URI', d)
100         if not src_uri:
101                 return
102         src_uri = data.expand(src_uri, d)
103
104         a = data.getVar('A', d)
105         if a:
106                 a = data.expand(a, d).split()
107         else:
108                 a = []
109
110         from oe import fetch
111         try:
112                 fetch.init(src_uri.split())
113         except fetch.NoMethodError:
114                 pass
115
116         a += fetch.localpaths(d)
117         del fetch
118         data.setVar('A', string.join(a), d)
119
120
121 # Add us to the handlers list
122 from oe.parse import handlers
123 handlers.append({'supports': supports, 'handle': handle, 'init': init})
124 del handlers