]> TLD Linux GIT Repositories - tld-builder.git/blob - PLD_Builder/get_br.py
- from https://github.com/pld-linux/pld-builder.new
[tld-builder.git] / PLD_Builder / get_br.py
1 # vi: encoding=utf-8 ts=8 sts=4 sw=4 et
2
3 import re
4 import string
5 import xreadlines
6 from util import *
7
8
9 def get_build_requires(spec, bconds_with, bconds_without):
10     cond_rx = re.compile(r"%\{(\!\?|\?\!|\?)([a-zA-Z0-9_+]+)\s*:([^%\{\}]*)\}")
11
12     def expand_conds(l):
13         def expand_one(m):
14             if m.group(1) == "?":
15                 if macros.has_key(m.group(2)):
16                     return m.group(3)
17             else:
18                 if not macros.has_key(m.group(2)):
19                     return m.group(3)
20             return ""
21
22         for i in range(10):
23             l = cond_rx.sub(expand_one, l)
24             if len(l) > 1000: break
25
26         return l
27
28     macro_rx = re.compile(r"%\{([a-zA-Z0-9_+]+)\}")
29     def expand_macros(l):
30         def expand_one(m):
31             if macros.has_key(m.group(1)):
32                 return string.strip(macros[m.group(1)])
33             else:
34                 return m.group(0) # don't change
35
36         for i in range(10):
37             l = macro_rx.sub(expand_one, l)
38             if len(l) > 1000: break
39
40         return expand_conds(l)
41
42     simple_br_rx = re.compile(r"^BuildRequires\s*:\s*([^\s]+)", re.I)
43     bcond_rx = re.compile(r"^%bcond_(with|without)\s+([^\s]+)")
44     version_rx = re.compile(r"^Version\s*:\s*([^\s]+)", re.I)
45     release_rx = re.compile(r"^Release\s*:\s*([^\s]+)", re.I)
46     name_rx = re.compile(r"^Name\s*:\s*([^\s]+)", re.I)
47     define_rx = re.compile(r"^\%define\s+([a-zA-Z0-9_+]+)\s+(.*)", re.I)
48     any_br_rx = re.compile(r"BuildRequires", re.I)
49
50     macros = {}
51     for b in bconds_with:
52         macros["_with_%s" % b] = 1
53     for b in bconds_without:
54         macros["_without_%s" % b] = 1
55
56     macros["__perl"] = "/usr/bin/perl"
57     macros["_bindir"] = "/usr/bin"
58     macros["_sbindir"] = "/usr/sbin"
59     macros["kgcc_package"] = "gcc"
60
61     build_req = []
62
63     f = open(spec)
64     for l in xreadlines.xreadlines(f):
65         l = string.strip(l)
66         if l == "%changelog": break
67
68         # %bcond_with..
69         m = bcond_rx.search(l)
70         if m:
71             bcond = m.group(2)
72             if m.group(1) == "with":
73                 if macros.has_key("_with_%s" % bcond):
74                     macros["with_%s" % bcond] = 1
75             else:
76                 if not macros.has_key("_without_%s" % bcond):
77                     macros["with_%s" % bcond] = 1
78             continue
79
80         # name,version,release
81         m = version_rx.search(l)
82         if m: macros["version"] = m.group(1)
83         m = release_rx.search(l)
84         if m: macros["release"] = m.group(1)
85         m = name_rx.search(l)
86         if m: macros["name"] = m.group(1)
87
88         # %define
89         m = define_rx.search(l)
90         if m: macros[m.group(1)] = m.group(2)
91
92         # *BuildRequires*
93         if any_br_rx.search(l):
94             l = expand_macros(l)
95             m = simple_br_rx.search(l)
96             if m:
97                 build_req.append(m.group(1))
98             else:
99                 if l <> "" and l[0] <> '#':
100                     msg("spec error (%s): %s\n" % (spec, l))
101
102     for x in build_req:
103         print x