]> TLD Linux GIT Repositories - tld-builder.git/blob - PLD_Builder/install.py
- from https://github.com/pld-linux/pld-builder.new
[tld-builder.git] / PLD_Builder / install.py
1 # vi: encoding=utf-8 ts=8 sts=4 sw=4 et
2
3 import re, os
4 import string
5 import StringIO
6
7 import chroot
8 import util
9 import log
10
11 hold = [
12     'dev',
13     'poldek',
14     'rpm-build',
15     'pdksh',
16     'coreutils'
17 ]
18
19 def close_killset(killset):
20     k = killset.keys()
21     if len(k) == 0:
22         return True
23     rx = re.compile(r'^.* marks (?P<name>[^\s]+?)-[^-]+-[^-]+\s.*$')
24     errors = ""
25     for p in k:
26         if p in hold:
27             del killset[p]
28             errors += "cannot remove %s because it's crucial\n" % p
29         else:
30             f = chroot.popen("poldek --noask --test --test --erase %s" % p, user = "root")
31             crucial = 0
32             e = []
33             for l in f.xreadlines():
34                 m = rx.search(l)
35                 if m:
36                     pkg = m.group('name')
37                     if pkg in hold:
38                         errors += "cannot remove %s because it's required " \
39                                   "by %s, that is crucial\n" % (p, pkg)
40                         crucial = 1
41                     e.append(pkg)
42             f.close()
43             if crucial:
44                 del killset[p]
45             else:
46                 for p in e:
47                     killset[p] = 2
48     return errors
49
50 def upgrade_from_batch(r, b):
51     f = chroot.popen("rpm --test -F %s 2>&1" % string.join(b.files), user = "root")
52     killset = {}
53     rx = re.compile(r' \(installed\) (?P<name>[^\s]+)-[^-]+-[^-]+$')
54     for l in f.xreadlines():
55         m = rx.search(l)
56         if m: killset[m.group('name')] = 1
57     f.close()
58     if len(killset) != 0:
59         err = close_killset(killset)
60         if err != "":
61             util.append_to(b.logfile, err)
62             log.notice("cannot upgrade rpms")
63             return False
64         k = string.join(killset.keys())
65         if True:
66             b.log_line("upgrade requires removal of %s" % k)
67             res = chroot.run("rpm -e %s" % k, logfile = b.logfile, user = "root")
68             if res != 0:
69                 b.log_line("package removal failed")
70                 return False
71             else:
72                 b.log_line("packages removed sucessfuly")
73         else:
74             b.log_line("upgrade would need removal of %s" % k)
75             return False
76     b.log_line("upgrading packages")
77     logbuf = StringIO.StringIO()
78     res = chroot.run("rpm -Fvh %s" % string.join(b.files), user = "root", logfile = b.logfile)
79     if res != 0:
80         b.log_line("package upgrade failed")
81         logbuf.close()
82         return False
83     logbuf.close()
84     return True
85
86 def uninstall(conflicting, b):
87     b.log_line("uninstalling conflicting packages")
88     err = close_killset(conflicting)
89     if err != "":
90         util.append_to(b.logfile, err)
91         b.log_line("error: conflicting packages uninstallation failed")
92         return False
93     else:
94         for k in conflicting.keys():
95             b.log_line("removing %s" % k)
96             res = chroot.run("poldek --noask --erase %s" % k, logfile = b.logfile, user = "root")
97             if res != 0:
98                 b.log_line("package %s removal failed" % k)
99     return True
100
101 def uninstall_self_conflict(b):
102     b.log_line("checking BuildConflict-ing packages")
103     packagename = b.spec[:-5]
104     tmpdir = os.environ.get('HOME') + "/rpm/BUILD/%s/tmp" % packagename
105     f = chroot.popen("set -e; TMPDIR=%(tmpdir)s rpmbuild -bp --nobuild --short-circuit --define 'prep exit 0' %(rpmdefs)s rpm/packages/%(package)s/%(spec)s 2>&1" % {
106         'tmpdir': tmpdir,
107         'rpmdefs' : b.rpmbuild_opts(),
108         'package' : packagename,
109         'spec': b.spec,
110     })
111     # java-sun >= 1.5 conflicts with soprano-2.1.67-1.src
112     # java-sun conflicts with soprano-2.1.67-1.src
113     rx = re.compile(r"\s+(?P<name>[\w-]+)\s+.*conflicts with [^\s]+-[^-]+-[^-]+\.src($| .*)")
114     conflicting = {}
115     for l in f.xreadlines():
116         m = rx.search(l)
117         if m:
118             b.log_line("rpmbuild: %s" % l.rstrip())
119             conflicting[m.group('name')] = 1
120     f.close()
121     if len(conflicting) and not uninstall(conflicting, b):
122         return False
123     b.log_line("no BuildConflicts found")
124     return True
125
126 def install_br(r, b):
127     def get_missing_br(r, b):
128         # ignore internal rpm dependencies, see lib/rpmns.c for list
129         ignore_br = re.compile(r'^\s*(rpmlib|cpuinfo|getconf|uname|soname|user|group|mounted|diskspace|digest|gnupg|macro|envvar|running|sanitycheck|vcheck|signature|verify|exists|executable|readable|writable)\(.*')
130
131         packagename = b.spec[:-5]
132         tmpdir = os.environ.get('HOME') + "/rpm/BUILD/%s/tmp" % packagename
133         chroot.run("install -m 700 -d %s" % tmpdir)
134         cmd = "set -e; TMPDIR=%(tmpdir)s rpmbuild --nobuild %(rpmdefs)s rpm/packages/%(package)s/%(spec)s 2>&1" % {
135             'tmpdir': tmpdir,
136             'rpmdefs' : b.rpmbuild_opts(),
137             'package' : packagename,
138             'spec': b.spec,
139         }
140         f = chroot.popen(cmd)
141         rx = re.compile(r"^\s*(?P<name>[^\s]+) .*is needed by")
142         needed = {}
143         b.log_line("checking BR")
144         for l in f.xreadlines():
145             b.log_line("rpm: %s" % l.rstrip())
146             m = rx.search(l)
147             if m and not ignore_br.match(l):
148                 needed[m.group('name')] = 1
149         f.close()
150         return needed
151
152     needed = get_missing_br(r, b);
153
154     if len(needed) == 0:
155         b.log_line("no BR needed")
156         return True
157
158     nbr = ""
159     for bre in needed.keys():
160         nbr = nbr + " " + re.escape(bre)
161     br = string.strip(nbr)
162     b.log_line("updating poldek cache...")
163     chroot.run("poldek --up --upa", user = "root", logfile = b.logfile)
164     # check conflicts in BRed packages
165     b.log_line("checking conflicting packages in BRed packages")
166     f = chroot.popen("poldek --test --test --noask --caplookup -Q -v --upgrade %s" % br, user = "root")
167     # phonon-devel-4.3.1-1.i686 conflicts with qt4-phonon-devel-4.5.0-6.i686
168     # jdbc-stdext >= 2.0 is required by installed java-struts-1.3.10-1.noarch
169     # jmx is needed by (installed) java-commons-modeler-2.0-1.noarch
170     rx = re.compile(r".*(conflicts with|is required by|is needed by)( installed| \(installed\)|) (?P<name>[^\s]+)-[^-]+-[^-]+($| .*)")
171     conflicting = {}
172     for l in f.xreadlines():
173         b.log_line("poldek: %s" % l.rstrip())
174         m = rx.search(l)
175         if m: conflicting[m.group('name')] = 1
176     f.close()
177     if len(conflicting) == 0:
178         b.log_line("no conflicts found")
179     else:
180         if not uninstall(conflicting, b):
181             return False
182
183     # recheck BuildRequires since above uninstallation could remove some required deps
184     needed = get_missing_br(r, b);
185
186     if len(needed) == 0:
187         b.log_line("no BR needed")
188         return True
189
190     nbr = ""
191     for bre in needed.keys():
192         nbr = nbr + " " + re.escape(bre)
193     br = string.strip(nbr)
194
195     b.log_line("installing BR: %s" % br)
196     res = chroot.run("poldek --noask --caplookup -Q -v --upgrade %s" % br,
197             user = "root",
198             logfile = b.logfile)
199     if res != 0:
200         b.log_line("error: BR installation failed")
201         return False
202     return True