4 # Copyright (c) 2011, Novell Inc.
6 # This program is licensed under the BSD license, read LICENSE.BSD
7 # for further information
10 # pysolv a little software installer demoing the sat solver library/bindings
13 # - understands globs for package names / dependencies
14 # - understands .arch suffix
15 # - repository data caching
16 # - on demand loading of secondary repository data
17 # - checksum verification
19 # - installation of commandline packages
21 # things not yet ported:
24 # - fastestmirror implementation
26 # things available in the library but missing from pysolv:
27 # - vendor policy loading
28 # - soft locks file handling
29 # - multi version handling
41 from iniparse import INIConfig
42 from optparse import OptionParser
45 #gc.set_debug(gc.DEBUG_LEAK)
47 class repo_generic(dict):
48 def __init__(self, name, type, attribs = {}):
54 def calc_cookie_file(self, filename):
55 chksum = solv.Chksum(solv.REPOKEY_TYPE_SHA256)
57 chksum.add_stat(filename)
60 def calc_cookie_fp(self, fp):
61 chksum = solv.Chksum(solv.REPOKEY_TYPE_SHA256)
66 def calc_cookie_ext(self, f, cookie):
67 chksum = solv.Chksum(solv.REPOKEY_TYPE_SHA256)
70 chksum.add_fstat(f.fileno())
71 extcookie = chksum.raw()
73 # compatibility to c code
77 # compatibility to c code
78 if ord(extcookie[0]) == 0:
82 def cachepath(self, ext = None):
83 path = re.sub(r'^\.', '_', self.name)
85 path += "_" + ext + ".solvx"
88 return "/var/cache/solv/" + re.sub(r'[/]', '_', path)
91 self.handle = pool.add_repo(self.name)
92 self.handle.appdata = self
93 self.handle.priority = 99 - self['priority']
94 dorefresh = bool(int(self['autorefresh']))
97 st = os.stat(self.cachepath())
98 if self['metadata_expire'] == -1 or time.time() - st[ST_MTIME] < self['metadata_expire']:
103 if not dorefresh and self.usecachedrepo(None):
104 print("repo: '%s': cached" % self.name)
108 def load_ext(self, repodata):
111 def setfromurls(self, urls):
115 print("[using mirror %s]" % re.sub(r'^(.*?/...*?)/.*$', r'\1', url))
116 self['baseurl'] = url
118 def setfrommetalink(self, metalink):
119 f = self.download(metalink, False, None)
122 f = os.fdopen(f.dup(), 'r')
125 for l in f.readlines():
127 m = re.match(r'^<hash type="sha256">([0-9a-fA-F]{64})</hash>', l)
129 chksum = solv.Chksum(solv.REPOKEY_TYPE_SHA256, m.group(1))
130 m = re.match(r'^<url.*>(https?://.+)repodata/repomd.xml</url>', l)
132 urls.append(m.group(1))
134 chksum = None # in case the metalink is about a different file
136 self.setfromurls(urls)
139 def setfrommirrorlist(self, mirrorlist):
140 f = self.download(mirrorlist, False, None)
143 f = os.fdopen(f.dup(), 'r')
145 for l in f.readline():
147 if l[0:6] == 'http://' or l[0:7] == 'https://':
149 self.setfromurls(urls)
152 def download(self, file, uncompress, chksum, markincomplete=False):
154 if 'baseurl' not in self:
155 if 'metalink' in self:
156 if file != self['metalink']:
157 metalinkchksum = self.setfrommetalink(self['metalink'])
158 if file == 'repodata/repomd.xml' and metalinkchksum and not chksum:
159 chksum = metalinkchksum
162 elif 'mirrorlist' in self:
163 if file != self['mirrorlist']:
164 self.setfrommirrorlist(self['mirrorlist'])
168 if 'baseurl' not in self:
169 print("%s: no baseurl" % self.name)
171 url = re.sub(r'/$', '', self['baseurl']) + '/' + file
172 f = tempfile.TemporaryFile()
173 st = subprocess.call(['curl', '-f', '-s', '-L', url], stdout=f.fileno())
174 if os.lseek(f.fileno(), 0, os.SEEK_CUR) == 0 and (st == 0 or not chksum):
176 os.lseek(f.fileno(), 0, os.SEEK_SET)
178 print("%s: download error %d" % (file, st))
180 self['incomplete'] = True
183 fchksum = solv.Chksum(chksum.type)
185 print("%s: unknown checksum type" % file)
187 self['incomplete'] = True
189 fchksum.add_fd(f.fileno())
190 if fchksum != chksum:
191 print("%s: checksum mismatch" % file)
193 self['incomplete'] = True
196 return solv.xfopen_fd(file, f.fileno())
197 return solv.xfopen_fd(None, f.fileno())
199 def usecachedrepo(self, ext, mark=False):
201 repopath = self.cachepath(ext)
202 f = open(repopath, 'rb')
203 f.seek(-32, os.SEEK_END)
205 if len(fcookie) != 32:
208 cookie = self['cookie']
210 cookie = self['extcookie']
211 if cookie and fcookie != cookie:
213 if self.type != 'system' and not ext:
214 f.seek(-32 * 2, os.SEEK_END)
215 fextcookie = f.read(32)
216 if len(fextcookie) != 32:
219 f = solv.xfopen_fd('', f.fileno())
222 flags = solv.Repo.REPO_USE_LOADING|solv.Repo.REPO_EXTEND_SOLVABLES
224 flags |= solv.Repo.REPO_LOCALPOOL
225 if not self.handle.add_solv(f, flags):
227 if self.type != 'system' and not ext:
228 self['cookie'] = fcookie
229 self['extcookie'] = fextcookie
231 # no futimes in python?
233 os.utime(repopath, None)
240 def writecachedrepo(self, ext, info=None):
241 if 'incomplete' in self:
245 if not os.path.isdir("/var/cache/solv"):
246 os.mkdir("/var/cache/solv", 0o755)
247 (fd, tmpname) = tempfile.mkstemp(prefix='.newsolv-', dir='/var/cache/solv')
249 f = os.fdopen(fd, 'wb+')
250 f = solv.xfopen_fd(None, f.fileno())
255 else: # rewrite_repos case
256 self.handle.write_first_repodata(f)
258 if self.type != 'system' and not ext:
259 if 'extcookie' not in self:
260 self['extcookie'] = self.calc_cookie_ext(f, self['cookie'])
261 os.write(f.fileno(), self['extcookie'])
263 os.write(f.fileno(), self['cookie'])
265 os.write(f.fileno(), self['extcookie'])
267 if self.handle.iscontiguous():
268 # switch to saved repo to activate paging and save memory
269 nf = solv.xfopen(tmpname)
273 if not self.handle.add_solv(nf, solv.Repo.SOLV_ADD_NO_STUBS):
274 sys.exit("internal error, cannot reload solv file")
277 # need to extend to repo boundaries, as this is how
278 # info.write() has written the data
279 info.extend_to_repo()
280 flags = solv.Repo.REPO_EXTEND_SOLVABLES
282 flags |= solv.Repo.REPO_LOCALPOOL
283 info.add_solv(nf, flags)
284 os.rename(tmpname, self.cachepath(ext))
285 except (OSError, IOError):
289 def updateaddedprovides(self, addedprovides):
290 if 'incomplete' in self:
292 if not hasattr(self, 'handle'):
294 if self.handle.isempty():
296 # make sure there's just one real repodata with extensions
297 repodata = self.handle.first_repodata()
300 oldaddedprovides = repodata.lookup_idarray(solv.SOLVID_META, solv.REPOSITORY_ADDEDFILEPROVIDES)
301 if not set(addedprovides) <= set(oldaddedprovides):
302 for id in addedprovides:
303 repodata.add_idarray(solv.SOLVID_META, solv.REPOSITORY_ADDEDFILEPROVIDES, id)
304 repodata.internalize()
305 self.writecachedrepo(None, repodata)
307 def packagespath(self):
310 class repo_repomd(repo_generic):
311 def load(self, pool):
312 if super(repo_repomd, self).load(pool):
314 sys.stdout.write("rpmmd repo '%s': " % self.name)
316 f = self.download("repodata/repomd.xml", False, None, None)
318 print("no repomd.xml file, skipped")
319 self.handle.free(True)
322 self['cookie'] = self.calc_cookie_fp(f)
323 if self.usecachedrepo(None, True):
326 self.handle.add_repomdxml(f, 0)
328 (filename, filechksum) = self.find('primary')
330 f = self.download(filename, True, filechksum, True)
332 self.handle.add_rpmmd(f, None, 0)
333 if 'incomplete' in self:
334 return False # hopeless, need good primary
335 (filename, filechksum) = self.find('updateinfo')
337 f = self.download(filename, True, filechksum, True)
339 self.handle.add_updateinfoxml(f, 0)
341 self.writecachedrepo(None)
342 # must be called after writing the repo
343 self.handle.create_stubs()
346 def find(self, what):
347 di = self.handle.Dataiterator(solv.SOLVID_META, solv.REPOSITORY_REPOMD_TYPE, what, solv.Dataiterator.SEARCH_STRING)
348 di.prepend_keyname(solv.REPOSITORY_REPOMD)
351 filename = dp.lookup_str(solv.REPOSITORY_REPOMD_LOCATION)
352 chksum = dp.lookup_checksum(solv.REPOSITORY_REPOMD_CHECKSUM)
353 if filename and not chksum:
354 print("no %s file checksum!" % filename)
358 return (filename, chksum)
361 def add_ext(self, repodata, what, ext):
362 filename, chksum = self.find(what)
363 if not filename and what == 'deltainfo':
364 filename, chksum = self.find('prestodelta')
367 handle = repodata.new_handle()
368 repodata.set_poolstr(handle, solv.REPOSITORY_REPOMD_TYPE, what)
369 repodata.set_str(handle, solv.REPOSITORY_REPOMD_LOCATION, filename)
370 repodata.set_checksum(handle, solv.REPOSITORY_REPOMD_CHECKSUM, chksum)
372 repodata.add_idarray(handle, solv.REPOSITORY_KEYS, solv.REPOSITORY_DELTAINFO)
373 repodata.add_idarray(handle, solv.REPOSITORY_KEYS, solv.REPOKEY_TYPE_FLEXARRAY)
375 repodata.add_idarray(handle, solv.REPOSITORY_KEYS, solv.SOLVABLE_FILELIST)
376 repodata.add_idarray(handle, solv.REPOSITORY_KEYS, solv.REPOKEY_TYPE_DIRSTRARRAY)
377 repodata.add_flexarray(solv.SOLVID_META, solv.REPOSITORY_EXTERNAL, handle)
380 repodata = self.handle.add_repodata(0)
381 self.add_ext(repodata, 'deltainfo', 'DL')
382 self.add_ext(repodata, 'filelists', 'FL')
383 repodata.internalize()
385 def load_ext(self, repodata):
386 repomdtype = repodata.lookup_str(solv.SOLVID_META, solv.REPOSITORY_REPOMD_TYPE)
387 if repomdtype == 'filelists':
389 elif repomdtype == 'deltainfo':
393 sys.stdout.write("[%s:%s: " % (self.name, ext))
394 if self.usecachedrepo(ext):
395 sys.stdout.write("cached]\n")
398 sys.stdout.write("fetching]\n")
400 filename = repodata.lookup_str(solv.SOLVID_META, solv.REPOSITORY_REPOMD_LOCATION)
401 filechksum = repodata.lookup_checksum(solv.SOLVID_META, solv.REPOSITORY_REPOMD_CHECKSUM)
402 f = self.download(filename, True, filechksum)
406 self.handle.add_rpmmd(f, 'FL', solv.Repo.REPO_USE_LOADING|solv.Repo.REPO_EXTEND_SOLVABLES|solv.Repo.REPO_LOCALPOOL)
408 self.handle.add_deltainfoxml(f, solv.Repo.REPO_USE_LOADING)
409 self.writecachedrepo(ext, repodata)
412 class repo_susetags(repo_generic):
413 def load(self, pool):
414 if super(repo_susetags, self).load(pool):
416 sys.stdout.write("susetags repo '%s': " % self.name)
418 f = self.download("content", False, None, None)
420 print("no content file, skipped")
421 self.handle.free(True)
424 self['cookie'] = self.calc_cookie_fp(f)
425 if self.usecachedrepo(None, True):
428 self.handle.add_content(f, 0)
430 defvendorid = self.handle.meta.lookup_id(solv.SUSETAGS_DEFAULTVENDOR)
431 descrdir = self.handle.meta.lookup_str(solv.SUSETAGS_DESCRDIR)
433 descrdir = "suse/setup/descr"
434 (filename, filechksum) = self.find('packages.gz')
436 (filename, filechksum) = self.find('packages')
438 f = self.download(descrdir + '/' + filename, True, filechksum, True)
440 self.handle.add_susetags(f, defvendorid, None, solv.Repo.REPO_NO_INTERNALIZE|solv.Repo.SUSETAGS_RECORD_SHARES)
441 (filename, filechksum) = self.find('packages.en.gz')
443 (filename, filechksum) = self.find('packages.en')
445 f = self.download(descrdir + '/' + filename, True, filechksum, True)
447 self.handle.add_susetags(f, defvendorid, None, solv.Repo.REPO_NO_INTERNALIZE|solv.Repo.REPO_REUSE_REPODATA|solv.Repo.REPO_EXTEND_SOLVABLES)
448 self.handle.internalize()
450 self.writecachedrepo(None)
451 # must be called after writing the repo
452 self.handle.create_stubs()
455 def find(self, what):
456 di = self.handle.Dataiterator(solv.SOLVID_META, solv.SUSETAGS_FILE_NAME, what, solv.Dataiterator.SEARCH_STRING)
457 di.prepend_keyname(solv.SUSETAGS_FILE)
460 chksum = dp.lookup_checksum(solv.SUSETAGS_FILE_CHECKSUM)
461 return (what, chksum)
464 def add_ext(self, repodata, what, ext):
465 (filename, chksum) = self.find(what)
468 handle = repodata.new_handle()
469 repodata.set_str(handle, solv.SUSETAGS_FILE_NAME, filename)
471 repodata.set_checksum(handle, solv.SUSETAGS_FILE_CHECKSUM, chksum)
473 repodata.add_idarray(handle, solv.REPOSITORY_KEYS, solv.SOLVABLE_DISKUSAGE)
474 repodata.add_idarray(handle, solv.REPOSITORY_KEYS, solv.REPOKEY_TYPE_DIRNUMNUMARRAY)
476 repodata.add_idarray(handle, solv.REPOSITORY_KEYS, solv.SOLVABLE_FILELIST)
477 repodata.add_idarray(handle, solv.REPOSITORY_KEYS, solv.REPOKEY_TYPE_DIRSTRARRAY)
479 for langtag, langtagtype in [
480 (solv.SOLVABLE_SUMMARY, solv.REPOKEY_TYPE_STR),
481 (solv.SOLVABLE_DESCRIPTION, solv.REPOKEY_TYPE_STR),
482 (solv.SOLVABLE_EULA, solv.REPOKEY_TYPE_STR),
483 (solv.SOLVABLE_MESSAGEINS, solv.REPOKEY_TYPE_STR),
484 (solv.SOLVABLE_MESSAGEDEL, solv.REPOKEY_TYPE_STR),
485 (solv.SOLVABLE_CATEGORY, solv.REPOKEY_TYPE_ID)
487 repodata.add_idarray(handle, solv.REPOSITORY_KEYS, self.handle.pool.id2langid(langtag, ext, 1))
488 repodata.add_idarray(handle, solv.REPOSITORY_KEYS, langtagtype)
489 repodata.add_flexarray(solv.SOLVID_META, solv.REPOSITORY_EXTERNAL, handle)
492 repodata = self.handle.add_repodata(0)
493 di = self.handle.Dataiterator(solv.SOLVID_META, solv.SUSETAGS_FILE_NAME, None, 0)
494 di.prepend_keyname(solv.SUSETAGS_FILE)
499 if filename[0:9] != "packages.":
501 if len(filename) == 11 and filename != "packages.gz":
503 elif filename[11:12] == ".":
509 self.add_ext(repodata, filename, ext)
510 repodata.internalize()
512 def load_ext(self, repodata):
513 filename = repodata.lookup_str(solv.SOLVID_META, solv.SUSETAGS_FILE_NAME)
515 sys.stdout.write("[%s:%s: " % (self.name, ext))
516 if self.usecachedrepo(ext):
517 sys.stdout.write("cached]\n")
520 sys.stdout.write("fetching]\n")
522 defvendorid = self.handle.meta.lookup_id(solv.SUSETAGS_DEFAULTVENDOR)
523 descrdir = self.handle.meta.lookup_str(solv.SUSETAGS_DESCRDIR)
525 descrdir = "suse/setup/descr"
526 filechksum = repodata.lookup_checksum(solv.SOLVID_META, solv.SUSETAGS_FILE_CHECKSUM)
527 f = self.download(descrdir + '/' + filename, True, filechksum)
530 flags = solv.Repo.REPO_USE_LOADING|solv.Repo.REPO_EXTEND_SOLVABLES
532 flags |= solv.Repo.REPO_LOCALPOOL
533 self.handle.add_susetags(f, defvendorid, ext, flags)
534 self.writecachedrepo(ext, repodata)
537 def packagespath(self):
538 datadir = repo.handle.meta.lookup_str(solv.SUSETAGS_DATADIR)
543 class repo_unknown(repo_generic):
544 def load(self, pool):
545 print("unsupported repo '%s': skipped" % self.name)
548 class repo_system(repo_generic):
549 def load(self, pool):
550 self.handle = pool.add_repo(self.name)
551 self.handle.appdata = self
552 pool.installed = self.handle
553 sys.stdout.write("rpm database: ")
554 self['cookie'] = self.calc_cookie_file("/var/lib/rpm/Packages")
555 if self.usecachedrepo(None):
559 if hasattr(self.handle.__class__, 'add_products'):
560 self.handle.add_products("/etc/products.d", solv.Repo.REPO_NO_INTERNALIZE)
561 f = solv.xfopen(self.cachepath())
562 self.handle.add_rpmdb_reffp(f, solv.Repo.REPO_REUSE_REPODATA)
563 self.writecachedrepo(None)
566 class repo_cmdline(repo_generic):
567 def load(self, pool):
568 self.handle = pool.add_repo(self.name)
569 self.handle.appdata = self
572 def load_stub(repodata):
573 repo = repodata.repo.appdata
575 return repo.load_ext(repodata)
579 parser = OptionParser(usage="usage: solv.py [options] COMMAND")
580 parser.add_option('-r', '--repo', action="append", type="string", dest="repos", help="limit to specified repositories")
581 parser.add_option('--best', action="store_true", dest="best", help="force installation/update to best packages")
582 parser.add_option('--clean', action="store_true", dest="clean", help="delete no longer needed packages")
583 (options, args) = parser.parse_args()
585 parser.print_help(sys.stderr)
591 cmdabbrev = {'ls': 'list', 'in': 'install', 'rm': 'erase', 've': 'verify', 'se': 'search'}
596 'install': solv.Job.SOLVER_INSTALL,
597 'erase': solv.Job.SOLVER_ERASE,
598 'up': solv.Job.SOLVER_UPDATE,
599 'dup': solv.Job.SOLVER_DISTUPGRADE,
600 'verify': solv.Job.SOLVER_VERIFY,
605 # read all repo configs
608 if os.path.isdir("/etc/zypp/repos.d"):
609 reposdirs = [ "/etc/zypp/repos.d" ]
611 reposdirs = [ "/etc/yum/repos.d" ]
613 for reposdir in reposdirs:
614 if not os.path.isdir(reposdir):
616 for reponame in sorted(glob.glob('%s/*.repo' % reposdir)):
617 cfg = INIConfig(open(reponame))
619 repoattr = {'enabled': 0, 'priority': 99, 'autorefresh': 1, 'type': 'rpm-md', 'metadata_expire': 900}
621 repoattr[k] = cfg[alias][k]
622 if 'mirrorlist' in repoattr and 'metalink' not in repoattr:
623 if repoattr['mirrorlist'].find('/metalink'):
624 repoattr['metalink'] = repoattr['mirrorlist']
625 del repoattr['mirrorlist']
626 if repoattr['type'] == 'rpm-md':
627 repo = repo_repomd(alias, 'repomd', repoattr)
628 elif repoattr['type'] == 'yast2':
629 repo = repo_susetags(alias, 'susetags', repoattr)
631 repo = repo_unknown(alias, 'unknown', repoattr)
636 pool.set_loadcallback(load_stub)
638 # now load all enabled repos into the pool
639 sysrepo = repo_system('@System', 'system')
642 if int(repo['enabled']):
647 for reponame in options.repos:
648 mrepos = [ repo for repo in repos if repo.name == reponame ]
650 print("no repository matches '%s'" % reponame)
653 if hasattr(repo, 'handle'):
655 repofilter = pool.Selection()
656 repofilter.add(repo.handle.Selection(solv.Job.SOLVER_SETVENDOR))
659 pool.createwhatprovides()
660 sel = pool.Selection()
661 di = pool.Dataiterator(0, solv.SOLVABLE_NAME, args[0], solv.Dataiterator.SEARCH_SUBSTRING|solv.Dataiterator.SEARCH_NOCASE)
663 sel.add_raw(solv.Job.SOLVER_SOLVABLE, d.solvid)
665 sel.filter(repofilter)
666 for s in sel.solvables():
667 print(" - %s [%s]: %s" % (s, s.repo.name, s.lookup_str(solv.SOLVABLE_SUMMARY)))
670 if cmd not in cmdactionmap:
671 print("unknown command %s" % cmd)
675 if cmd == 'list' or cmd == 'info' or cmd == 'install':
677 if arg.endswith(".rpm") and os.access(arg, os.R_OK):
679 cmdlinerepo = repo_cmdline('@commandline', 'cmdline')
680 cmdlinerepo.load(pool)
681 cmdlinerepo['packages'] = {}
682 s = cmdlinerepo.handle.add_rpm(arg, solv.Repo.REPO_REUSE_REPODATA|solv.Repo.REPO_NO_INTERNALIZE)
686 cmdlinerepo['packages'][arg] = s
688 cmdlinerepo.handle.internalize()
690 addedprovides = pool.addfileprovides_queue()
692 sysrepo.updateaddedprovides(addedprovides)
694 repo.updateaddedprovides(addedprovides)
696 pool.createwhatprovides()
698 # convert arguments into jobs
701 if cmdlinerepo and arg in cmdlinerepo['packages']:
702 jobs.append(pool.Job(solv.Job.SOLVER_SOLVABLE, cmdlinerepo['packages'][arg].id))
704 flags = solv.Selection.SELECTION_NAME|solv.Selection.SELECTION_PROVIDES|solv.Selection.SELECTION_GLOB
705 flags |= solv.Selection.SELECTION_CANON|solv.Selection.SELECTION_DOTARCH|solv.Selection.SELECTION_REL
706 if len(arg) and arg[0] == '/':
707 flags |= solv.Selection.SELECTION_FILELIST
709 flags |= solv.Selection.SELECTION_INSTALLED_ONLY
710 sel = pool.select(arg, flags)
712 sel.filter(repofilter)
714 sel = pool.select(arg, flags | solv.Selection.SELECTION_NOCASE)
716 sel.filter(repofilter)
717 if not sel.isempty():
718 print("[ignoring case for '%s']" % arg)
720 print("nothing matches '%s'" % arg)
722 if sel.flags() & solv.Selection.SELECTION_FILELIST:
723 print("[using file list match for '%s']" % arg)
724 if sel.flags() & solv.Selection.SELECTION_PROVIDES:
725 print("[using capability match for '%s']" % arg)
726 jobs += sel.jobs(cmdactionmap[cmd])
728 if not jobs and (cmd == 'up' or cmd == 'dup' or cmd == 'verify' or repofilter):
729 sel = pool.Selection_all()
731 sel.filter(repofilter)
732 jobs += sel.jobs(cmdactionmap[cmd])
735 print("no package matched.")
738 if cmd == 'list' or cmd == 'info':
740 for s in job.solvables():
742 print("Name: %s" % s)
743 print("Repo: %s" % s.repo)
744 print("Summary: %s" % s.lookup_str(solv.SOLVABLE_SUMMARY))
745 str = s.lookup_str(solv.SOLVABLE_URL)
747 print("Url: %s" % str)
748 str = s.lookup_str(solv.SOLVABLE_LICENSE)
750 print("License: %s" % str)
751 print("Description:\n%s" % s.lookup_str(solv.SOLVABLE_DESCRIPTION))
754 print(" - %s [%s]" % (s, s.repo))
755 print(" %s" % s.lookup_str(solv.SOLVABLE_SUMMARY))
758 # up magic: use install instead of update if no installed package matches
760 if cmd == 'up' and job.isemptyupdate():
761 job.how ^= solv.Job.SOLVER_UPDATE ^ solv.Job.SOLVER_INSTALL
763 job.how |= solv.Job.SOLVER_FORCEBEST
765 job.how |= solv.Job.SOLVER_CLEANDEPS
767 #pool.set_debuglevel(2)
768 solver = pool.Solver()
769 solver.set_flag(solv.Solver.SOLVER_FLAG_SPLITPROVIDES, 1);
771 solver.set_flag(solv.Solver.SOLVER_FLAG_ALLOW_UNINSTALL, 1);
774 problems = solver.solve(jobs)
777 for problem in problems:
778 print("Problem %d/%d:" % (problem.id, len(problems)))
780 solutions = problem.solutions()
781 for solution in solutions:
782 print(" Solution %d:" % solution.id)
783 elements = solution.elements(True)
784 for element in elements:
785 print(" - %s" % element.str())
788 while not (sol == 's' or sol == 'q' or (sol.isdigit() and int(sol) >= 1 and int(sol) <= len(solutions))):
789 sys.stdout.write("Please choose a solution: ")
791 sol = sys.stdin.readline().strip()
793 continue # skip problem
796 solution = solutions[int(sol) - 1]
797 for element in solution.elements():
798 newjob = element.Job()
799 if element.type == solv.Solver.SOLVER_SOLUTION_JOB:
800 jobs[element.jobidx] = newjob
802 if newjob and newjob not in jobs:
805 # no problems, show transaction
806 trans = solver.transaction()
809 print("Nothing to do.")
812 print("Transaction summary:")
814 for cl in trans.classify(solv.Transaction.SOLVER_TRANSACTION_SHOW_OBSOLETES | solv.Transaction.SOLVER_TRANSACTION_OBSOLETE_IS_UPGRADE):
815 if cl.type == solv.Transaction.SOLVER_TRANSACTION_ERASE:
816 print("%d erased packages:" % cl.count)
817 elif cl.type == solv.Transaction.SOLVER_TRANSACTION_INSTALL:
818 print("%d installed packages:" % cl.count)
819 elif cl.type == solv.Transaction.SOLVER_TRANSACTION_REINSTALLED:
820 print("%d reinstalled packages:" % cl.count)
821 elif cl.type == solv.Transaction.SOLVER_TRANSACTION_DOWNGRADED:
822 print("%d downgraded packages:" % cl.count)
823 elif cl.type == solv.Transaction.SOLVER_TRANSACTION_CHANGED:
824 print("%d changed packages:" % cl.count)
825 elif cl.type == solv.Transaction.SOLVER_TRANSACTION_UPGRADED:
826 print("%d upgraded packages:" % cl.count)
827 elif cl.type == solv.Transaction.SOLVER_TRANSACTION_VENDORCHANGE:
828 print("%d vendor changes from '%s' to '%s':" % (cl.count, cl.fromstr, cl.tostr))
829 elif cl.type == solv.Transaction.SOLVER_TRANSACTION_ARCHCHANGE:
830 print("%d arch changes from '%s' to '%s':" % (cl.count, cl.fromstr, cl.tostr))
833 for p in cl.solvables():
834 if cl.type == solv.Transaction.SOLVER_TRANSACTION_UPGRADED or cl.type == solv.Transaction.SOLVER_TRANSACTION_DOWNGRADED:
835 op = trans.othersolvable(p)
836 print(" - %s -> %s" % (p, op))
840 print("install size change: %d K" % trans.calc_installsizechange())
844 sys.stdout.write("OK to continue (y/n)? ")
846 yn = sys.stdin.readline().strip()
848 if yn == 'n' or yn == 'q': sys.exit(1)
849 newpkgs = trans.newsolvables()
854 downloadsize += p.lookup_num(solv.SOLVABLE_DOWNLOADSIZE)
855 print("Downloading %d packages, %d K" % (len(newpkgs), downloadsize))
857 repo = p.repo.appdata
858 location, medianr = p.lookup_location()
861 if repo.type == 'commandline':
862 f = solv.xfopen(location)
864 sys.exit("\n%s: %s not found" % location)
867 if not sysrepo.handle.isempty() and os.access('/usr/bin/applydeltarpm', os.X_OK):
869 di = p.repo.Dataiterator(solv.SOLVID_META, solv.DELTA_PACKAGE_NAME, pname, solv.Dataiterator.SEARCH_STRING)
870 di.prepend_keyname(solv.REPOSITORY_DELTAINFO)
873 if dp.lookup_id(solv.DELTA_PACKAGE_EVR) != p.evrid or dp.lookup_id(solv.DELTA_PACKAGE_ARCH) != p.archid:
875 baseevrid = dp.lookup_id(solv.DELTA_BASE_EVR)
877 for installedp in pool.whatprovides(p.nameid):
878 if installedp.isinstalled() and installedp.nameid == p.nameid and installedp.archid == p.archid and installedp.evrid == baseevrid:
879 candidate = installedp
882 seq = dp.lookup_deltaseq()
883 st = subprocess.call(['/usr/bin/applydeltarpm', '-a', p.arch, '-c', '-s', seq])
886 chksum = dp.lookup_checksum(solv.DELTA_CHECKSUM)
889 dloc, dmedianr = dp.lookup_deltalocation()
890 dloc = repo.packagespath() + dloc
891 f = repo.download(dloc, False, chksum)
894 nf = tempfile.TemporaryFile()
895 nf = os.dup(nf.fileno()) # get rid of CLOEXEC
896 st = subprocess.call(['/usr/bin/applydeltarpm', '-a', p.arch, "/dev/fd/%d" % f.fileno(), "/dev/fd/%d" % nf])
900 os.lseek(nf, 0, os.SEEK_SET)
901 newpkgsfp[p.id] = solv.xfopen_fd("", nf)
904 if p.id in newpkgsfp:
905 sys.stdout.write("d")
909 chksum = p.lookup_checksum(solv.SOLVABLE_CHECKSUM)
910 location = repo.packagespath() + location
911 f = repo.download(location, False, chksum)
913 sys.exit("\n%s: %s not found in repository" % (repo.name, location))
915 sys.stdout.write(".")
918 print("Committing transaction:")
920 ts = rpm.TransactionSet('/')
921 ts.setVSFlags(rpm._RPMVSF_NOSIGNATURES)
923 for p in trans.steps():
924 type = trans.steptype(p, solv.Transaction.SOLVER_TRANSACTION_RPM_ONLY)
925 if type == solv.Transaction.SOLVER_TRANSACTION_ERASE:
926 rpmdbid = p.lookup_num(solv.RPM_RPMDBID)
927 erasenamehelper[p.name] = p
929 sys.exit("\ninternal error: installed package %s has no rpmdbid\n" % p)
931 elif type == solv.Transaction.SOLVER_TRANSACTION_INSTALL:
933 h = ts.hdrFromFdno(f.fileno())
934 os.lseek(f.fileno(), 0, os.SEEK_SET)
935 ts.addInstall(h, p, 'u')
936 elif type == solv.Transaction.SOLVER_TRANSACTION_MULTIINSTALL:
938 h = ts.hdrFromFdno(f.fileno())
939 os.lseek(f.fileno(), 0, os.SEEK_SET)
940 ts.addInstall(h, p, 'i')
941 checkproblems = ts.check()
946 def runCallback(reason, amount, total, p, d):
947 if reason == rpm.RPMCALLBACK_INST_OPEN_FILE:
948 return newpkgsfp[p.id].fileno()
949 if reason == rpm.RPMCALLBACK_INST_START:
950 print("install %s" % p)
951 if reason == rpm.RPMCALLBACK_UNINST_START:
952 # argh, p is just the name of the package
953 if p in erasenamehelper:
954 p = erasenamehelper[p]
955 print("erase %s" % p)
956 runproblems = ts.run(runCallback, '')