aboutsummaryrefslogtreecommitdiffstats
path: root/bin/oe/parse/SRPMHandler.py
blob: db1b9ce5e89eac6611cbf5883330a5c709fe6a53 (plain)
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
#!/usr/bin/env python
# ex:ts=4:sw=4:sts=4:et
# -*- tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*-
"""class for handling .src.rpm files

   Accesses the file and obtains its metadata"""

import re, oe, os, sys
import oe.fetch
from oe import debug, data, fetch, fatal
from oe.parse.ConfHandler import init

_srpm_vartranslate = {
"NAME": "PN",
"VERSION": "PV",
"RELEASE": "PR",
}

def supports(fn, d):
    return fn[-8:] == ".src.rpm"

def handle(fn, d = {}, include = 0):
    init(d)
    oepath = ['.']
    if not os.path.isabs(fn):
        f = None
        voepath = data.getVar("OEPATH", d)
        if voepath:
            oepath += voepath.split(":")
        for p in oepath:
            p = data.expand(p, d)
            if os.access(os.path.join(p, fn), os.R_OK):
                f = open(os.path.join(p, fn), 'r')
        if f is None:
            raise IOError("file not found")
    else:
        f = open(fn,'r')

    srpm_vars = os.popen('rpm --querytags').read().split('\n')
    for v in srpm_vars:
        if v in _srpm_vartranslate:
            var = _srpm_vartranslate[v]
        else:
            var = v
        querycmd = 'rpm -qp --qf \'%%{%s}\' %s 2>/dev/null' % (v, fn)
        value = os.popen(querycmd).read().strip()
        if value == "(none)":
            value = None
        if value:
            data.setVar(var, value, d)

    data.setVar("SRPMFILE", fn, d)

    inheritclasses = data.getVar("INHERIT", d)
    if inheritclasses:
        i = inheritclasses.split()
    else:
        i = []

    if not "base_srpm" in i:
        i[0:0] = ["base_srpm"]

    for c in i:
        oe.parse.handle('classes/%s.oeclass' % c, d)

    set_automatic_vars(fn, d, include)
    set_additional_vars(fn, d, include)
    data.update_data(d)
    return d

def set_automatic_vars(file, d, include):
    """Deduce per-package environment variables"""

    debug(2, "setting automatic vars")

    data.setVar('CATEGORY', 'srpm', d)
    data.setVar('P', '${PN}-${PV}', d)
    data.setVar('PF', '${P}-${PR}', d)

    for s in ['${TOPDIR}/${CATEGORY}/${PF}',
          '${TOPDIR}/${CATEGORY}/${PN}-${PV}',
          '${TOPDIR}/${CATEGORY}/files',
          '${TOPDIR}/${CATEGORY}']:
        s = data.expand(s, d)
        if os.access(s, os.R_OK):
            data.setVar('FILESDIR', s, d)
            break

    data.setVar('WORKDIR', '${TMPDIR}/${CATEGORY}/${PF}', d)
    data.setVar('T', '${WORKDIR}/temp', d)
    data.setVar('D', '${WORKDIR}/image', d)
    if not data.getVar('S', d):
        data.setVar('S', '${WORKDIR}/${P}', d)
    data.setVar('SLOT', '0', d)

def set_additional_vars(file, d, include):
    """Deduce rest of variables, e.g. ${A} out of ${SRC_URI}"""

    debug(2,"set_additional_vars")

    src_uri = data.getVar('SRC_URI', d)
    if not src_uri:
        return
    src_uri = data.expand(src_uri, d)

    a = data.getVar('A', d)
    if a:
        a = data.expand(a, d).split()
    else:
        a = []

    from oe import fetch
    try:
        fetch.init(src_uri.split())
    except fetch.NoMethodError:
        pass

    a += fetch.localpaths(d)
    del fetch
    data.setVar('A', ''.join(a), d)


# Add us to the handlers list
from oe.parse import handlers
handlers.append({'supports': supports, 'handle': handle, 'init': init})
del handlers