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