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